diff --git a/.github/workflows/sonar.yml b/.github/workflows/sonar.yml index 0df083f..1c5c4d5 100644 --- a/.github/workflows/sonar.yml +++ b/.github/workflows/sonar.yml @@ -45,4 +45,4 @@ jobs: -Dsonar.projectKey=Mastercard_petstore-application-java -Dsonar.organization=mastercard -Dsonar.host.url=https://sonarcloud.io -Dsonar.login=$SONAR_TOKEN -Dsonar.cpd.exclusions=**/OkHttp*.java - -Dsonar.exclusions=**/*.xml -Dgpg.signature.skip=true + -Dsonar.exclusions=**/*.xml,**/configuration/**,**/PetstoreApplication.java,**/utils/** -Dgpg.signature.skip=true diff --git a/src/main/java/com/mastercard/app/petstore/utils/MockDataBuilders.java b/src/main/java/com/mastercard/app/petstore/utils/MockDataBuilders.java index 276fc07..97bc2c9 100644 --- a/src/main/java/com/mastercard/app/petstore/utils/MockDataBuilders.java +++ b/src/main/java/com/mastercard/app/petstore/utils/MockDataBuilders.java @@ -124,7 +124,7 @@ public static NewEmployee buildNewEmployee(){ public static Employee buildEmployee(){ String firstName = generateRandomName(); - Employee employee = new Employee(); + Employee employee = new Employee(UUID.randomUUID(), new Date(), new Date()); employee.setFirstName(firstName); employee.setLastName(generateRandomName()); employee.setPhoneNumber("+6573437115596"); @@ -133,6 +133,22 @@ public static Employee buildEmployee(){ return employee; } + public static EmployeeWrapper buildEmployeeWrapper(){ + String firstName = generateRandomName(); + return new EmployeeWrapper() + .firstName(generateRandomName()) + .lastName(generateRandomName()) + .phoneNumber("+6573437115596") + .ssn(generateRandomSsn()) + .username(firstName + "123"); + } + + public static EmployeeSearch buildEmployeeSearch(){ + EmployeeSearch employeeSearch = new EmployeeSearch(); + employeeSearch.setSsn(generateRandomSsn()); + return employeeSearch; + } + public static Payment buildPayment(){ Payment payment = new Payment(); payment.setAmount(new BigDecimal(50)); diff --git a/src/test/java/com/mastercard/app/petstore/AdoptionsServiceTest.java b/src/test/java/com/mastercard/app/petstore/AdoptionsServiceTest.java deleted file mode 100644 index bf092eb..0000000 --- a/src/test/java/com/mastercard/app/petstore/AdoptionsServiceTest.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.mastercard.app.petstore; - -import com.mastercard.app.petstore.services.AdoptionsService; -import com.mastercard.app.petstore.utils.MockDataBuilders; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockito.InjectMocks; -import org.openapitools.client.ApiException; -import org.openapitools.client.api.AdoptionsApi; -import org.openapitools.client.model.Adoption; -import org.openapitools.client.model.AdoptionSearch; -import org.openapitools.client.model.Payment; -import org.openapitools.client.model.PaymentDetails; - -import java.util.UUID; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.Mockito.*; - -public class AdoptionsServiceTest { - - private AdoptionsApi adoptionsApiFle; - - private AdoptionsApi adoptionsApiFullBody; - - @InjectMocks - AdoptionsService adoptionsService; - - @BeforeEach - public void setUp() { - adoptionsApiFle = mock(AdoptionsApi.class); - adoptionsApiFullBody = mock(AdoptionsApi.class); - adoptionsService = new AdoptionsService(adoptionsApiFle, adoptionsApiFullBody); - } - - @Test - public void getAdoption_shouldReturnAnAdoption() throws ApiException { - Adoption adoption = MockDataBuilders.buildAdoptionObject(); - when(adoptionsApiFle.getAdoption(any())).thenReturn(adoption); - - Adoption returnedAdoption = adoptionsService.getAdoption(adoption.getId().toString()); - - assertEquals(adoption.getId(), returnedAdoption.getId()); - } - - @Test - public void searchAdoption_shouldReturnAnAdoptionSearch() throws ApiException { - AdoptionSearch adoptionSearch = MockDataBuilders.buildAdoptionSearch(); - - when(adoptionsApiFle.searchAdoptedPets(adoptionSearch.getFromDate(), adoptionSearch.getToDate(), adoptionSearch.getPetCategory(), adoptionSearch.getPetIdentifier())).thenReturn(adoptionSearch); - - AdoptionSearch returnedAdoptionSearch = adoptionsService.searchAdoption(adoptionSearch.getFromDate(), adoptionSearch.getToDate(), adoptionSearch.getPetCategory(), adoptionSearch.getPetIdentifier().toString()); - - assertEquals(adoptionSearch.getSearchResults().size(), returnedAdoptionSearch.getSearchResults().size()); - } - - @Test - public void adoptionPayment_shouldReturnAnAdoptionPayment() throws ApiException { - Payment payment = MockDataBuilders.buildPayment(); - PaymentDetails paymentDetails = MockDataBuilders.buildPaymentDetailsFromPayment(payment); - UUID id = UUID.randomUUID(); - - when(adoptionsApiFullBody.adoptionPayment(id, payment)).thenReturn(paymentDetails); - - PaymentDetails returnedPaymentDetails = adoptionsService.adoptionPayment(id.toString(), payment); - - assertEquals(payment.getAmount(), returnedPaymentDetails.getAmount()); - } - - -} diff --git a/src/test/java/com/mastercard/app/petstore/examples/AdoptionFlowExampleTest.java b/src/test/java/com/mastercard/app/petstore/examples/AdoptionFlowExampleTest.java new file mode 100644 index 0000000..4485ba9 --- /dev/null +++ b/src/test/java/com/mastercard/app/petstore/examples/AdoptionFlowExampleTest.java @@ -0,0 +1,72 @@ +package com.mastercard.app.petstore.examples; + +import com.mastercard.app.petstore.services.AdoptionsService; +import com.mastercard.app.petstore.services.CatService; +import com.mastercard.app.petstore.utils.MockDataBuilders; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.MockitoAnnotations; +import org.openapitools.client.ApiException; +import org.openapitools.client.model.Adoption; +import org.openapitools.client.model.Cat; +import org.openapitools.client.model.NewAdoption; +import org.openapitools.client.model.NewCat; + +import java.util.UUID; + +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +class AdoptionFlowExampleTest { + + @InjectMocks + private AdoptionFlowExample adoptionFlowExample; + + @Mock + private AdoptionsService adoptionsService; + + @Mock + private CatService catService; + + @BeforeEach + void setup() { + MockitoAnnotations.openMocks(this); + } + + @Test + void testAdoptionUseCase() throws ApiException { + UUID catId = UUID.randomUUID(); + UUID adoptionId = UUID.randomUUID(); + + NewCat newCat = mock(NewCat.class); + Cat cat = mock(Cat.class); + when(cat.getId()).thenReturn(catId); + + NewAdoption newAdoption = mock(NewAdoption.class); + Adoption adoption = mock(Adoption.class); + when(adoption.getId()).thenReturn(adoptionId); + + when(catService.addCat(any(NewCat.class))).thenReturn(cat); + when(adoptionsService.adoptPet(any(NewAdoption.class))).thenReturn("/adoptions/" + adoptionId); + when(adoptionsService.getAdoption(adoptionId.toString())).thenReturn(adoption); + + try (MockedStatic mockBuilders = mockStatic(MockDataBuilders.class)) { + mockBuilders.when(MockDataBuilders::buildNewCat).thenReturn(newCat); + mockBuilders.when(() -> MockDataBuilders.buildNewAdoptionObject(catId)).thenReturn(newAdoption); + + adoptionFlowExample.adoptionUseCase(); + + verify(catService).addCat(newCat); + verify(adoptionsService).adoptPet(newAdoption); + verify(adoptionsService).getAdoption(adoptionId.toString()); + verify(adoptionsService).updateAdoption("0", adoption); + verify(adoptionsService).deleteAdoption(adoptionId.toString()); + } + } +} \ No newline at end of file diff --git a/src/test/java/com/mastercard/app/petstore/examples/EmployeeFlowExampleTest.java b/src/test/java/com/mastercard/app/petstore/examples/EmployeeFlowExampleTest.java new file mode 100644 index 0000000..7f6297c --- /dev/null +++ b/src/test/java/com/mastercard/app/petstore/examples/EmployeeFlowExampleTest.java @@ -0,0 +1,64 @@ +package com.mastercard.app.petstore.examples; + +import com.mastercard.app.petstore.services.EmployeeService; +import com.mastercard.app.petstore.utils.MockDataBuilders; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.*; +import org.openapitools.client.ApiException; +import org.openapitools.client.model.*; + +import static org.mockito.Mockito.*; + +class EmployeeFlowExampleTest { + + @InjectMocks + private EmployeeFlowExample employeeFlowExample; + + @Mock + private EmployeeService employeeService; + + @BeforeEach + void setup() { + MockitoAnnotations.openMocks(this); + } + + @Test + void testEmployeeUseCase() throws ApiException { + NewEmployee newEmployee = new NewEmployee().ssn("123-45-6789"); + Employee employee = new Employee().ssn("123-45-6789"); + EmployeeWrapper employeeWrapper = new EmployeeWrapper().username("jdoe"); + + NewEmployeeData newEmployeeData = new NewEmployeeData().addNewEmployeesItem(newEmployee); + + EmployeeListData employeeData = new EmployeeListData().addEmployeesItem(employee); + + when(employeeService.createEmployee(any(NewEmployeeData.class))).thenReturn(employeeData); + when(employeeService.searchEmployee(any(EmployeeSearch.class))).thenReturn(employeeWrapper); + + try (MockedStatic mockBuilders = mockStatic(MockDataBuilders.class)) { + mockBuilders.when(MockDataBuilders::buildNewEmployee).thenReturn(newEmployee); + + employeeFlowExample.employeeUseCase(); + + verify(employeeService).createEmployee(newEmployeeData); + verify(employeeService).searchEmployee(argThat(search -> "123-45-6789".equals(search.getSsn()))); + verify(employeeService).deleteEmployee("jdoe"); + } + } + + @Test + void testEmployeeUseCaseThrowsApiException() throws ApiException { + NewEmployee newEmployee = new NewEmployee().ssn("123-45-6789"); + + when(employeeService.createEmployee(any(NewEmployeeData.class))).thenThrow(new ApiException("API error")); + + try (MockedStatic mockBuilders = mockStatic(MockDataBuilders.class)) { + mockBuilders.when(MockDataBuilders::buildNewEmployee).thenReturn(newEmployee); + + org.junit.jupiter.api.Assertions.assertThrows(ApiException.class, () -> { + employeeFlowExample.employeeUseCase(); + }); + } + } +} diff --git a/src/test/java/com/mastercard/app/petstore/examples/PetFlowExampleTest.java b/src/test/java/com/mastercard/app/petstore/examples/PetFlowExampleTest.java new file mode 100644 index 0000000..9f321cd --- /dev/null +++ b/src/test/java/com/mastercard/app/petstore/examples/PetFlowExampleTest.java @@ -0,0 +1,77 @@ +package com.mastercard.app.petstore.examples; + +import com.mastercard.app.petstore.services.CatService; +import com.mastercard.app.petstore.services.PetService; +import com.mastercard.app.petstore.utils.MockDataBuilders; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.MockitoAnnotations; +import org.openapitools.client.ApiException; +import org.openapitools.client.model.Cat; +import org.openapitools.client.model.NewCat; +import org.openapitools.client.model.PetStatus; + +import java.util.Date; +import java.util.UUID; + +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.argThat; +import static org.mockito.Mockito.eq; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +class PetFlowExampleTest { + + @InjectMocks + private PetFlowExample petFlowExample; + + @Mock + private CatService catService; + + @Mock + private PetService petService; + + @BeforeEach + void setup() { + MockitoAnnotations.openMocks(this); + } + + @Test + void testPetUseCaseFlow() throws ApiException { + NewCat newCat = new NewCat(); + UUID catId = UUID.randomUUID(); + Cat cat = new Cat(catId, new Date(), new Date()).name("Whiskers"); + PetStatus status = new PetStatus().value("RESERVED"); + + when(catService.addCat(any(NewCat.class))).thenReturn(cat); + when(catService.getCat(catId.toString())).thenReturn(cat); + + try (MockedStatic mockBuilders = mockStatic(MockDataBuilders.class)) { + mockBuilders.when(MockDataBuilders::buildNewCat).thenReturn(newCat); + + petFlowExample.petUseCaseFlow(); + + verify(catService).addCat(newCat); + verify(catService).getCat(catId.toString()); + verify(catService).updateCat(argThat(updatedCat -> "Catso".equals(updatedCat.getName())), eq("0")); + verify(petService).updatePetStatus(eq(catId), eq(status), eq("1")); + verify(petService).removePet(catId); + } + } + + @Test + void testPetUseCaseFlowThrowsApiException() throws ApiException { + NewCat newCat = new NewCat(); + when(catService.addCat(any(NewCat.class))).thenThrow(new ApiException("API error")); + + try (MockedStatic mockBuilders = mockStatic(MockDataBuilders.class)) { + mockBuilders.when(MockDataBuilders::buildNewCat).thenReturn(newCat); + + org.junit.jupiter.api.Assertions.assertThrows(ApiException.class, () -> petFlowExample.petUseCaseFlow()); + } + } +} diff --git a/src/test/java/com/mastercard/app/petstore/services/AdoptionsServiceTest.java b/src/test/java/com/mastercard/app/petstore/services/AdoptionsServiceTest.java new file mode 100644 index 0000000..0fc6c94 --- /dev/null +++ b/src/test/java/com/mastercard/app/petstore/services/AdoptionsServiceTest.java @@ -0,0 +1,126 @@ +package com.mastercard.app.petstore.services; + +import com.mastercard.app.petstore.utils.MockDataBuilders; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.openapitools.client.ApiException; +import org.openapitools.client.ApiResponse; +import org.openapitools.client.api.AdoptionsApi; +import org.openapitools.client.model.Adoption; +import org.openapitools.client.model.AdoptionSearch; +import org.openapitools.client.model.AdoptionWrapper; +import org.openapitools.client.model.Payment; +import org.openapitools.client.model.PaymentDetails; + +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.argThat; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +class AdoptionsServiceTest { + + private AdoptionsApi adoptionsApiFle; + + private AdoptionsApi adoptionsApiFullBody; + + @InjectMocks + AdoptionsService adoptionsService; + + @BeforeEach + void setUp() { + adoptionsApiFle = mock(AdoptionsApi.class); + adoptionsApiFullBody = mock(AdoptionsApi.class); + adoptionsService = new AdoptionsService(adoptionsApiFle, adoptionsApiFullBody); + } + + @Test + void adoptPet_shouldReturnALocation() throws ApiException { + Map> headers = Collections.singletonMap("location", Collections.singletonList("/adoptions/b1546cc7-a979-4c13-8817-40a8a0770868")); + ApiResponse responseWithHeader = new ApiResponse<>(200, headers); + when(adoptionsApiFle.adoptPetWithHttpInfo(any())).thenReturn(responseWithHeader); + + String location = adoptionsService.adoptPet(MockDataBuilders.buildNewAdoptionObject()); + + assertEquals("/adoptions/b1546cc7-a979-4c13-8817-40a8a0770868", location); + } + + @Test + void getAdoption_shouldReturnAnAdoption() throws ApiException { + Adoption adoption = MockDataBuilders.buildAdoptionObject(); + when(adoptionsApiFle.getAdoption(any())).thenReturn(adoption); + + Assertions.assertNotNull(adoption.getId()); + Adoption returnedAdoption = adoptionsService.getAdoption(adoption.getId().toString()); + + assertEquals(adoption.getId(), returnedAdoption.getId()); + } + + @Test + void searchAdoption_shouldReturnAnAdoptionSearch() throws ApiException { + AdoptionSearch adoptionSearch = MockDataBuilders.buildAdoptionSearch(); + + when(adoptionsApiFle.searchAdoptedPets(adoptionSearch.getFromDate(), adoptionSearch.getToDate(), adoptionSearch.getPetCategory(), adoptionSearch.getPetIdentifier())).thenReturn(adoptionSearch); + + Assertions.assertNotNull(adoptionSearch.getPetIdentifier()); + AdoptionSearch returnedAdoptionSearch = adoptionsService.searchAdoption(adoptionSearch.getFromDate(), adoptionSearch.getToDate(), adoptionSearch.getPetCategory(), adoptionSearch.getPetIdentifier().toString()); + + assertEquals(adoptionSearch.getSearchResults().size(), returnedAdoptionSearch.getSearchResults().size()); + } + + @Test + void updateAdoption_shouldUpdateAnAdoption() throws ApiException { + Adoption adoption = MockDataBuilders.buildAdoptionObject(); + String etag = "33a64df551425f"; + + when(adoptionsApiFle.updateAdoption( + eq(adoption.getId()), + eq(etag), + argThat(wrapper -> adoption.equals(wrapper.getAdoption()))) + ).thenReturn(new AdoptionWrapper()); + + adoptionsService.updateAdoption(etag, adoption); + + verify(adoptionsApiFle, times(1)).updateAdoption( + eq(adoption.getId()), + eq(etag), + argThat(wrapper -> adoption.equals(wrapper.getAdoption())) + ); + } + + @Test + void deleteAdoption_shouldDeleteAnAdoption() throws ApiException { + UUID id = UUID.randomUUID(); + doNothing().when(adoptionsApiFle).deleteAdoption(id); + + adoptionsService.deleteAdoption(id.toString()); + + verify(adoptionsApiFle, times(1)).deleteAdoption(id); + } + + @Test + void adoptionPayment_shouldReturnAnAdoptionPayment() throws ApiException { + Payment payment = MockDataBuilders.buildPayment(); + PaymentDetails paymentDetails = MockDataBuilders.buildPaymentDetailsFromPayment(payment); + UUID id = UUID.randomUUID(); + + when(adoptionsApiFullBody.adoptionPayment(id, payment)).thenReturn(paymentDetails); + + PaymentDetails returnedPaymentDetails = adoptionsService.adoptionPayment(id.toString(), payment); + + assertEquals(payment.getAmount(), returnedPaymentDetails.getAmount()); + } + + +} diff --git a/src/test/java/com/mastercard/app/petstore/CatServiceTest.java b/src/test/java/com/mastercard/app/petstore/services/CatServiceTest.java similarity index 80% rename from src/test/java/com/mastercard/app/petstore/CatServiceTest.java rename to src/test/java/com/mastercard/app/petstore/services/CatServiceTest.java index 5608ab8..7dbaa2e 100644 --- a/src/test/java/com/mastercard/app/petstore/CatServiceTest.java +++ b/src/test/java/com/mastercard/app/petstore/services/CatServiceTest.java @@ -1,7 +1,7 @@ -package com.mastercard.app.petstore; +package com.mastercard.app.petstore.services; -import com.mastercard.app.petstore.services.CatService; import com.mastercard.app.petstore.utils.MockDataBuilders; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; @@ -13,7 +13,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.*; -public class CatServiceTest { +class CatServiceTest { private CatsApi catsApi; @@ -21,13 +21,13 @@ public class CatServiceTest { CatService catService; @BeforeEach - public void setUp() { + void setUp() { catsApi = mock(CatsApi.class); catService = new CatService(catsApi); } @Test - public void addCat_shouldReturnACat() throws ApiException { + void addCat_shouldReturnACat() throws ApiException { NewCat newCat = MockDataBuilders.buildNewCat(); Cat expectedCat = MockDataBuilders.buildCat(); @@ -39,17 +39,18 @@ public void addCat_shouldReturnACat() throws ApiException { } @Test - public void getCat_shouldReturnACat() throws ApiException { + void getCat_shouldReturnACat() throws ApiException { Cat cat = MockDataBuilders.buildCat(); when(catsApi.getCat(any())).thenReturn(cat); + Assertions.assertNotNull(cat.getId()); Cat returnedCat = catService.getCat(cat.getId().toString()); assertEquals(returnedCat.getId(), cat.getId()); } @Test - public void updateCat_shouldUpdateACat() throws ApiException { + void updateCat_shouldUpdateACat() throws ApiException { Cat cat = MockDataBuilders.buildCat(); String etag = "33a64df551425f"; diff --git a/src/test/java/com/mastercard/app/petstore/DogServiceTest.java b/src/test/java/com/mastercard/app/petstore/services/DogServiceTest.java similarity index 80% rename from src/test/java/com/mastercard/app/petstore/DogServiceTest.java rename to src/test/java/com/mastercard/app/petstore/services/DogServiceTest.java index 82a6e44..e06fe0b 100644 --- a/src/test/java/com/mastercard/app/petstore/DogServiceTest.java +++ b/src/test/java/com/mastercard/app/petstore/services/DogServiceTest.java @@ -1,7 +1,7 @@ -package com.mastercard.app.petstore; +package com.mastercard.app.petstore.services; -import com.mastercard.app.petstore.services.DogService; import com.mastercard.app.petstore.utils.MockDataBuilders; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; @@ -13,7 +13,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.*; -public class DogServiceTest { +class DogServiceTest { private DogsApi dogsApi; @@ -21,13 +21,13 @@ public class DogServiceTest { DogService dogService; @BeforeEach - public void setUp() { + void setUp() { dogsApi = mock(DogsApi.class); dogService = new DogService(dogsApi); } @Test - public void addDog_shouldReturnADog() throws ApiException { + void addDog_shouldReturnADog() throws ApiException { NewDog newDog = MockDataBuilders.buildNewDog(); Dog expectedDog = MockDataBuilders.buildDog(); @@ -39,17 +39,18 @@ public void addDog_shouldReturnADog() throws ApiException { } @Test - public void getDog_shouldReturnADog() throws ApiException { + void getDog_shouldReturnADog() throws ApiException { Dog dog = MockDataBuilders.buildDog(); when(dogsApi.getDog(any())).thenReturn(dog); + Assertions.assertNotNull(dog.getId()); Dog returnedDog = dogService.getDog(dog.getId().toString()); assertEquals(returnedDog.getId(), dog.getId()); } @Test - public void updateDog_shouldUpdateADog() throws ApiException { + void updateDog_shouldUpdateADog() throws ApiException { Dog dog = MockDataBuilders.buildDog(); String etag = "33a64df551425f"; diff --git a/src/test/java/com/mastercard/app/petstore/EmployeeServiceTest.java b/src/test/java/com/mastercard/app/petstore/services/EmployeeServiceTest.java similarity index 51% rename from src/test/java/com/mastercard/app/petstore/EmployeeServiceTest.java rename to src/test/java/com/mastercard/app/petstore/services/EmployeeServiceTest.java index 8db27d4..6d2aa08 100644 --- a/src/test/java/com/mastercard/app/petstore/EmployeeServiceTest.java +++ b/src/test/java/com/mastercard/app/petstore/services/EmployeeServiceTest.java @@ -1,7 +1,7 @@ -package com.mastercard.app.petstore; +package com.mastercard.app.petstore.services; -import com.mastercard.app.petstore.services.EmployeeService; import com.mastercard.app.petstore.utils.MockDataBuilders; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; @@ -10,13 +10,21 @@ import org.openapitools.client.model.Employee; import org.openapitools.client.model.EmployeeData; import org.openapitools.client.model.EmployeeListData; +import org.openapitools.client.model.EmployeeSearch; +import org.openapitools.client.model.EmployeeWrapper; import org.openapitools.client.model.NewEmployee; import org.openapitools.client.model.NewEmployeeData; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.Mockito.*; +import static org.mockito.ArgumentMatchers.argThat; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; -public class EmployeeServiceTest { +class EmployeeServiceTest { private EmployeesApi employeesApi; private EmployeesApi employeesApiEncryptedForBody; @@ -25,14 +33,14 @@ public class EmployeeServiceTest { EmployeeService employeeService; @BeforeEach - public void setUp() { + void setUp() { employeesApi = mock(EmployeesApi.class); employeesApiEncryptedForBody = mock(EmployeesApi.class); employeeService = new EmployeeService(employeesApi, employeesApiEncryptedForBody); } @Test - public void createEmployee_shouldCreateAnEmployee() throws ApiException { + void createEmployee_shouldCreateAnEmployee() throws ApiException { NewEmployee newEmployee = MockDataBuilders.buildNewEmployee(); NewEmployeeData newEmployeeData = new NewEmployeeData().addNewEmployeesItem(newEmployee); Employee employee = MockDataBuilders.buildEmployee(); @@ -42,11 +50,25 @@ public void createEmployee_shouldCreateAnEmployee() throws ApiException { EmployeeListData returnEemployeeData = employeeService.createEmployee(newEmployeeData); + Assertions.assertNotNull(employeeData.getEmployees()); + Assertions.assertNotNull(returnEemployeeData.getEmployees()); assertEquals(employeeData.getEmployees().size(), returnEemployeeData.getEmployees().size()); } @Test - public void returnEmployee_shouldReturnAnEmployee() throws ApiException { + void searchEmployee_shouldReturnAnEmployee() throws ApiException { + EmployeeSearch employeeSearch = MockDataBuilders.buildEmployeeSearch(); + EmployeeWrapper employee = MockDataBuilders.buildEmployeeWrapper(); + + when(employeesApiEncryptedForBody.searchEmployee(employeeSearch)).thenReturn(employee); + + EmployeeWrapper response = employeeService.searchEmployee(employeeSearch); + + assertEquals(employee, response); + } + + @Test + void getEmployee_shouldReturnAnEmployee() throws ApiException { Employee employee = MockDataBuilders.buildEmployee(); EmployeeData employeeData = new EmployeeData(); employeeData.setEmployee(employee); @@ -54,11 +76,31 @@ public void returnEmployee_shouldReturnAnEmployee() throws ApiException { EmployeeData returntedEmployeeData = employeeService.getEmployee(employee.getUsername()); + Assertions.assertNotNull(returntedEmployeeData.getEmployee()); assertEquals(returntedEmployeeData.getEmployee().getUsername(), employee.getUsername()); } @Test - public void deleteEmployee_shouldJustRun() throws ApiException { + void updateEmployee_shouldUpdateAnEmployee() throws ApiException { + String etag = "33a64df551425f"; + Employee employee = MockDataBuilders.buildEmployee(); + + Assertions.assertNotNull(employee.getId()); + doNothing().when(employeesApiEncryptedForBody).updateEmployee( + eq(employee.getId().toString()), + eq(etag), + argThat(data -> employee.equals(data.getEmployee()))); + + employeeService.updateEmployee(etag, employee); + + verify(employeesApiEncryptedForBody, times(1)).updateEmployee( + eq(employee.getId().toString()), + eq(etag), + argThat(data -> employee.equals(data.getEmployee()))); + } + + @Test + void deleteEmployee_shouldJustRun() throws ApiException { String id = "Bob123"; doNothing().when(employeesApi).removeEmployee(id); diff --git a/src/test/java/com/mastercard/app/petstore/PetServiceTest.java b/src/test/java/com/mastercard/app/petstore/services/PetServiceTest.java similarity index 83% rename from src/test/java/com/mastercard/app/petstore/PetServiceTest.java rename to src/test/java/com/mastercard/app/petstore/services/PetServiceTest.java index 7640ec9..185171f 100644 --- a/src/test/java/com/mastercard/app/petstore/PetServiceTest.java +++ b/src/test/java/com/mastercard/app/petstore/services/PetServiceTest.java @@ -1,8 +1,8 @@ -package com.mastercard.app.petstore; +package com.mastercard.app.petstore.services; -import com.mastercard.app.petstore.services.PetService; import com.mastercard.app.petstore.utils.MockDataBuilders; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.BeforeEach; import org.mockito.InjectMocks; @@ -18,7 +18,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.*; -public class PetServiceTest { +class PetServiceTest { private PetsApi petsApi; @@ -26,13 +26,13 @@ public class PetServiceTest { PetService petService; @BeforeEach - public void setUp() { + void setUp() { petsApi = mock(PetsApi.class); petService = new PetService(petsApi); } @Test - public void updatePet_shouldUpdateAPet() throws ApiException { + void updatePet_shouldUpdateAPet() throws ApiException { Pet pet = MockDataBuilders.buildPet(); PetStatus petStatus = MockDataBuilders.buildPetStatus(); String etag = "33a64df551425f"; @@ -45,7 +45,7 @@ public void updatePet_shouldUpdateAPet() throws ApiException { } @Test - public void searchForPets_shouldReturnAListofPets() throws ApiException { + void searchForPets_shouldReturnAListofPets() throws ApiException { String status = "RESERVED"; List petCollection = Collections.singletonList(MockDataBuilders.buildPet()); PetList pets = new PetList(); @@ -59,11 +59,12 @@ public void searchForPets_shouldReturnAListofPets() throws ApiException { PetList returnedPets = petService.searchForPets(status); + Assertions.assertNotNull(returnedPets.getItems()); assertEquals(petCollection.size(), returnedPets.getItems().size()); } @Test - public void removePet_shouldRemoveAPet() throws ApiException { + void removePet_shouldRemoveAPet() throws ApiException { Pet pet = MockDataBuilders.buildPet(); doNothing().when(petsApi).deletePet(pet.getId());