Skip to content

Commit

Permalink
Completed most of service tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CihatAltiparmak committed Jan 2, 2024
1 parent 70d646b commit a6b3aa9
Show file tree
Hide file tree
Showing 5 changed files with 425 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.production.ehayvanbackendapi.DTO.request;

import com.production.ehayvanbackendapi.DTO.MedicationDTO;

public class CreateOrUpdateMedicationDTO {
private String medicationName;
private Integer medTypeID;
Expand All @@ -13,6 +15,13 @@ public CreateOrUpdateMedicationDTO(String medicationName, Integer medTypeID, Cre
this.petID = petID;
}

public CreateOrUpdateMedicationDTO(MedicationDTO medicationDTO) {
this.medicationName = medicationDTO.getMedicationName();
this.medTypeID = medicationDTO.getMedTypeID();
this.scheduleID = new CreateOrUpdateScheduleDTO(medicationDTO.getScheduleID());
this.petID = medicationDTO.getPetID();
}

public String getMedicationName() {
return medicationName;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.production.ehayvanbackendapi.ServiceTests;

import com.production.ehayvanbackendapi.DTO.MedTypeDTO;
import com.production.ehayvanbackendapi.DTO.PetDTO;
import com.production.ehayvanbackendapi.DTO.request.CreateOrUpdatePetDTO;
import com.production.ehayvanbackendapi.Entities.*;
import com.production.ehayvanbackendapi.Mappers.MedTypeMapper;
import com.production.ehayvanbackendapi.Mappers.PetMapper;
import com.production.ehayvanbackendapi.Repositories.MedTypeRepository;
import com.production.ehayvanbackendapi.Repositories.PetRepository;
import com.production.ehayvanbackendapi.Services.MedTypeService;
import com.production.ehayvanbackendapi.Services.PetService;
import com.production.ehayvanbackendapi.TestUtils.DataSeed;
import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest
@AutoConfigureMockMvc
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class MedTypeServiceTest {
@SpyBean
@Autowired
private MedTypeService testMedTypeService;

@SpyBean
@Autowired
private MedTypeRepository testMedTypeRepository;

@Autowired
DataSeed dataSeed;

private MedType testMedType;

@BeforeAll
public void setUp() {
dataSeed.loadSeedToDatabase();
}

@BeforeEach
@Transactional
public void onEachTestStart() {
testMedType = new MedType();
testMedType.setMedTypeID(11);
testMedType.setMedications(List.of());
testMedType.setMedType("Nigde Buyuksehir Belediye Spor");
}

@AfterEach
public void onEachTestEnd() {
testMedType = null;
}

@Test
public void testServiceGetByIdWhichNoExists() {
int testMedTypeId = 0;
MedTypeDTO returnedMedTypeDTO = testMedTypeService.getMedTypeById(testMedTypeId);
assertThat(returnedMedTypeDTO).isNull();
}

@Test
@Transactional
public void testServiceGetByIdWhichInDatabase() {
MedType returnedMedType = testMedTypeRepository.save(testMedType);
MedTypeDTO returnedMedTypeDTO = testMedTypeService.getMedTypeById(returnedMedType.getMedTypeID());

assertThat(returnedMedTypeDTO).isNotNull();
assertThat(returnedMedTypeDTO.getMedTypeID()).isEqualTo(returnedMedType.getMedTypeID());
assertThat(returnedMedTypeDTO.getMedTypeName()).isEqualTo(returnedMedType.getMedType());

testMedTypeRepository.deleteById(returnedMedType.getMedTypeID());
}

@Test
@Transactional
public void testServiceDeleteMedType() {
// firstly save new MedType
MedType returnedMedType = testMedTypeRepository.save(testMedType);
Optional<MedType> searchedMedType = testMedTypeRepository.findById(returnedMedType.getMedTypeID());
assertThat(searchedMedType.isPresent()).isEqualTo(true);

MedTypeDTO deletedMedType = testMedTypeService.deleteMedType(returnedMedType.getMedTypeID());

assertThat(deletedMedType.getMedTypeID()).isEqualTo(returnedMedType.getMedTypeID());

searchedMedType = testMedTypeRepository.findById(returnedMedType.getMedTypeID());
assertThat(searchedMedType.isPresent()).isEqualTo(false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package com.production.ehayvanbackendapi.ServiceTests;

import com.production.ehayvanbackendapi.DTO.MedicationDTO;
import com.production.ehayvanbackendapi.DTO.request.CreateOrUpdateMedicationDTO;
import com.production.ehayvanbackendapi.Entities.*;
import com.production.ehayvanbackendapi.Mappers.MedicationMapper;
import com.production.ehayvanbackendapi.Repositories.MedicationRepository;
import com.production.ehayvanbackendapi.Services.MedicationService;
import com.production.ehayvanbackendapi.TestUtils.DataSeed;
import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.Month;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest
@AutoConfigureMockMvc
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class MedicationServiceTest {
@SpyBean
@Autowired
private MedicationService testMedicationService;

@SpyBean
@Autowired
private MedicationRepository testMedicationRepository;

@Autowired
DataSeed dataSeed;

private Medication testMedication;

private CreateOrUpdateMedicationDTO testCreateOrUpdateMedicationDTO;

@Autowired
private MedicationMapper testMedicationMapper;

@BeforeAll
public void setUp() {
dataSeed.loadSeedToDatabase();
}

@BeforeEach
@Transactional
public void onEachTestStart() {
testMedication = new Medication();
testMedication.setMedicationName("Akyazi Sifasi");
testMedication.setPetID(new Pet());
testMedication.setScheduleID(new Schedule());
testMedication.getScheduleID().setScheduleID(1);
testMedication.getScheduleID().setBeginningDate(LocalDate.of(1915, Month.MARCH, 15));
testMedication.getScheduleID().setDoseFrequency(15);
testMedication.getScheduleID().setDoseCount(1773);
testMedication.setMedTypeID(new MedType());
testMedication.getMedTypeID().setMedTypeID(1);
testMedication.setMedicationID(1);

testCreateOrUpdateMedicationDTO = new CreateOrUpdateMedicationDTO(testMedicationMapper.convertToDto(testMedication));
}

@AfterEach
public void onEachTestEnd() {
testCreateOrUpdateMedicationDTO = null;
testMedication = null;
}

@Test
public void testServiceGetByIdWhichNoExists() {
int testMedicationId = 0;
MedicationDTO returnedMedicationDTO = testMedicationService.getMedicationById(testMedicationId);
assertThat(returnedMedicationDTO).isNull();
}

@Test
@Transactional
public void testServiceGetByIdWhichInDatabase() {
Medication returnedMedication = testMedicationRepository.save(testMedication);
MedicationDTO returnedMedicationDTO = testMedicationService.getMedicationById(returnedMedication.getMedicationID());

assertThat(returnedMedicationDTO).isNotNull();
assertThat(returnedMedicationDTO.getMedicationID()).isEqualTo(returnedMedication.getMedicationID());
assertThat(returnedMedicationDTO.getMedicationName()).isEqualTo(returnedMedication.getMedicationName());

testMedicationRepository.deleteById(returnedMedication.getMedicationID());
}

@Test
@Transactional
public void testServicePostMedication() {
MedicationDTO returnedMedicationDTO = testMedicationService.postMedication(testCreateOrUpdateMedicationDTO);

Optional<Medication> searchedMedicationOptional = testMedicationRepository.findById(returnedMedicationDTO.getMedicationID());
assertThat(searchedMedicationOptional.isPresent()).isEqualTo(true);
assertThat(searchedMedicationOptional.orElseThrow().getMedicationID()).isEqualTo(returnedMedicationDTO.getMedicationID());

testMedicationRepository.deleteById(searchedMedicationOptional.orElseThrow().getMedicationID());
}

@Test
@Transactional
public void testServiceUpdateMedication() {
// firstly save new Medication
Medication returnedMedication = testMedicationRepository.save(testMedication);

// convert to DTO and change email address of new created DTO
testCreateOrUpdateMedicationDTO = new CreateOrUpdateMedicationDTO(testMedicationMapper.convertToDto(testMedication));
testCreateOrUpdateMedicationDTO.setMedicationName("Anaconda");
testCreateOrUpdateMedicationDTO.getScheduleID().setBeginningDate(LocalDate.of(3000, Month.DECEMBER, 12));

// update corresponding MedicationOwner
MedicationDTO returnedMedicationDTO = testMedicationService.updateMedication(returnedMedication.getMedicationID(), testCreateOrUpdateMedicationDTO);

// check if it is same as email that updated data and data we want to update
Optional<Medication> searchedMedicationOptional = testMedicationRepository.findById(returnedMedication.getMedicationID());
assertThat(searchedMedicationOptional.isPresent()).isEqualTo(true);
assertThat(searchedMedicationOptional.orElseThrow().getMedicationName()).isEqualTo(testCreateOrUpdateMedicationDTO.getMedicationName());

// delete added data for testing
testMedicationRepository.deleteById(searchedMedicationOptional.orElseThrow().getMedicationID());
}

@Test
@Transactional
public void testServiceDeleteMedication() {
// firstly save new Medication
Medication returnedMedication = testMedicationRepository.save(testMedication);
Optional<Medication> searchedMedication = testMedicationRepository.findById(returnedMedication.getMedicationID());
assertThat(searchedMedication.isPresent()).isEqualTo(true);

MedicationDTO deletedMedication = testMedicationService.deleteMedication(returnedMedication.getMedicationID());

assertThat(deletedMedication.getMedicationID()).isEqualTo(returnedMedication.getMedicationID());

searchedMedication = testMedicationRepository.findById(returnedMedication.getMedicationID());
assertThat(searchedMedication.isPresent()).isEqualTo(false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.production.ehayvanbackendapi.ServiceTests;

import com.production.ehayvanbackendapi.DTO.MedTypeDTO;
import com.production.ehayvanbackendapi.DTO.PetDTO;
import com.production.ehayvanbackendapi.DTO.PetTypeDTO;
import com.production.ehayvanbackendapi.DTO.request.CreateOrUpdatePetDTO;
import com.production.ehayvanbackendapi.Entities.*;
import com.production.ehayvanbackendapi.Mappers.MedTypeMapper;
import com.production.ehayvanbackendapi.Mappers.PetMapper;
import com.production.ehayvanbackendapi.Repositories.MedTypeRepository;
import com.production.ehayvanbackendapi.Repositories.PetRepository;
import com.production.ehayvanbackendapi.Repositories.PetTypeRepository;
import com.production.ehayvanbackendapi.Services.MedTypeService;
import com.production.ehayvanbackendapi.Services.PetService;
import com.production.ehayvanbackendapi.Services.PetTypeService;
import com.production.ehayvanbackendapi.TestUtils.DataSeed;
import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest
@AutoConfigureMockMvc
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class PetTypeServiceTest {
@SpyBean
@Autowired
private PetTypeService testPetTypeService;

@SpyBean
@Autowired
private PetTypeRepository testPetTypeRepository;

@Autowired
DataSeed dataSeed;

private PetType testPetType;

@BeforeAll
public void setUp() {
dataSeed.loadSeedToDatabase();
}

@BeforeEach
@Transactional
public void onEachTestStart() {
testPetType = new PetType();
testPetType.setPetTypeID(17);
testPetType.setPets(List.of());
testPetType.setType("micky mouse");
}

@AfterEach
public void onEachTestEnd() {
testPetType = null;
}

@Test
public void testServiceGetByIdWhichNoExists() {
int testPetTypeId = 0;
PetTypeDTO returnedPetTypeDTO = testPetTypeService.getPetTypeById(testPetTypeId);
assertThat(returnedPetTypeDTO).isNull();
}

@Test
@Transactional
public void testServiceGetByIdWhichInDatabase() {
PetType returnedPetType = testPetTypeRepository.save(testPetType);
PetTypeDTO returnedPetTypeDTO = testPetTypeService.getPetTypeById(returnedPetType.getPetTypeID());

assertThat(returnedPetTypeDTO).isNotNull();
assertThat(returnedPetTypeDTO.getPetTypeID()).isEqualTo(returnedPetType.getPetTypeID());
assertThat(returnedPetTypeDTO.getType()).isEqualTo(returnedPetType.getType());

testPetTypeRepository.deleteById(returnedPetType.getPetTypeID());
}
}
Loading

0 comments on commit a6b3aa9

Please sign in to comment.