Skip to content

Commit b5dad3b

Browse files
committed
add examples tests
1 parent 1acf8e6 commit b5dad3b

8 files changed

Lines changed: 257 additions & 30 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.mastercard.app.petstore.examples;
2+
3+
import com.mastercard.app.petstore.services.AdoptionsService;
4+
import com.mastercard.app.petstore.services.CatService;
5+
import com.mastercard.app.petstore.utils.MockDataBuilders;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
import org.mockito.InjectMocks;
9+
import org.mockito.Mock;
10+
import org.mockito.MockedStatic;
11+
import org.mockito.MockitoAnnotations;
12+
import org.openapitools.client.ApiException;
13+
import org.openapitools.client.model.Adoption;
14+
import org.openapitools.client.model.Cat;
15+
import org.openapitools.client.model.NewAdoption;
16+
import org.openapitools.client.model.NewCat;
17+
18+
import java.util.UUID;
19+
20+
import static org.mockito.Mockito.any;
21+
import static org.mockito.Mockito.mock;
22+
import static org.mockito.Mockito.mockStatic;
23+
import static org.mockito.Mockito.verify;
24+
import static org.mockito.Mockito.when;
25+
26+
class AdoptionFlowExampleTest {
27+
28+
@InjectMocks
29+
private AdoptionFlowExample adoptionFlowExample;
30+
31+
@Mock
32+
private AdoptionsService adoptionsService;
33+
34+
@Mock
35+
private CatService catService;
36+
37+
@BeforeEach
38+
void setup() {
39+
MockitoAnnotations.openMocks(this);
40+
}
41+
42+
@Test
43+
void testAdoptionUseCase() throws ApiException {
44+
UUID catId = UUID.randomUUID();
45+
UUID adoptionId = UUID.randomUUID();
46+
47+
NewCat newCat = mock(NewCat.class);
48+
Cat cat = mock(Cat.class);
49+
when(cat.getId()).thenReturn(catId);
50+
51+
NewAdoption newAdoption = mock(NewAdoption.class);
52+
Adoption adoption = mock(Adoption.class);
53+
when(adoption.getId()).thenReturn(adoptionId);
54+
55+
when(catService.addCat(any(NewCat.class))).thenReturn(cat);
56+
when(adoptionsService.adoptPet(any(NewAdoption.class))).thenReturn("/adoptions/" + adoptionId);
57+
when(adoptionsService.getAdoption(adoptionId.toString())).thenReturn(adoption);
58+
59+
try (MockedStatic<MockDataBuilders> mockBuilders = mockStatic(MockDataBuilders.class)) {
60+
mockBuilders.when(MockDataBuilders::buildNewCat).thenReturn(newCat);
61+
mockBuilders.when(() -> MockDataBuilders.buildNewAdoptionObject(catId)).thenReturn(newAdoption);
62+
63+
adoptionFlowExample.adoptionUseCase();
64+
65+
verify(catService).addCat(newCat);
66+
verify(adoptionsService).adoptPet(newAdoption);
67+
verify(adoptionsService).getAdoption(adoptionId.toString());
68+
verify(adoptionsService).updateAdoption("0", adoption);
69+
verify(adoptionsService).deleteAdoption(adoptionId.toString());
70+
}
71+
}
72+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.mastercard.app.petstore.examples;
2+
3+
import com.mastercard.app.petstore.services.EmployeeService;
4+
import com.mastercard.app.petstore.utils.MockDataBuilders;
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
import org.mockito.*;
8+
import org.openapitools.client.ApiException;
9+
import org.openapitools.client.model.*;
10+
11+
import static org.mockito.Mockito.*;
12+
13+
class EmployeeFlowExampleTest {
14+
15+
@InjectMocks
16+
private EmployeeFlowExample employeeFlowExample;
17+
18+
@Mock
19+
private EmployeeService employeeService;
20+
21+
@BeforeEach
22+
void setup() {
23+
MockitoAnnotations.openMocks(this);
24+
}
25+
26+
@Test
27+
void testEmployeeUseCase() throws ApiException {
28+
NewEmployee newEmployee = new NewEmployee().ssn("123-45-6789");
29+
Employee employee = new Employee().ssn("123-45-6789");
30+
EmployeeWrapper employeeWrapper = new EmployeeWrapper().username("jdoe");
31+
32+
NewEmployeeData newEmployeeData = new NewEmployeeData().addNewEmployeesItem(newEmployee);
33+
34+
EmployeeListData employeeData = new EmployeeListData().addEmployeesItem(employee);
35+
36+
when(employeeService.createEmployee(any(NewEmployeeData.class))).thenReturn(employeeData);
37+
when(employeeService.searchEmployee(any(EmployeeSearch.class))).thenReturn(employeeWrapper);
38+
39+
try (MockedStatic<MockDataBuilders> mockBuilders = mockStatic(MockDataBuilders.class)) {
40+
mockBuilders.when(MockDataBuilders::buildNewEmployee).thenReturn(newEmployee);
41+
42+
employeeFlowExample.employeeUseCase();
43+
44+
verify(employeeService).createEmployee(newEmployeeData);
45+
verify(employeeService).searchEmployee(argThat(search -> "123-45-6789".equals(search.getSsn())));
46+
verify(employeeService).deleteEmployee("jdoe");
47+
}
48+
}
49+
50+
@Test
51+
void testEmployeeUseCaseThrowsApiException() throws ApiException {
52+
NewEmployee newEmployee = new NewEmployee().ssn("123-45-6789");
53+
54+
when(employeeService.createEmployee(any(NewEmployeeData.class))).thenThrow(new ApiException("API error"));
55+
56+
try (MockedStatic<MockDataBuilders> mockBuilders = mockStatic(MockDataBuilders.class)) {
57+
mockBuilders.when(MockDataBuilders::buildNewEmployee).thenReturn(newEmployee);
58+
59+
org.junit.jupiter.api.Assertions.assertThrows(ApiException.class, () -> {
60+
employeeFlowExample.employeeUseCase();
61+
});
62+
}
63+
}
64+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.mastercard.app.petstore.examples;
2+
3+
import com.mastercard.app.petstore.services.CatService;
4+
import com.mastercard.app.petstore.services.PetService;
5+
import com.mastercard.app.petstore.utils.MockDataBuilders;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
import org.mockito.InjectMocks;
9+
import org.mockito.Mock;
10+
import org.mockito.MockedStatic;
11+
import org.mockito.MockitoAnnotations;
12+
import org.openapitools.client.ApiException;
13+
import org.openapitools.client.model.Cat;
14+
import org.openapitools.client.model.NewCat;
15+
import org.openapitools.client.model.PetStatus;
16+
17+
import java.util.Date;
18+
import java.util.UUID;
19+
20+
import static org.mockito.Mockito.any;
21+
import static org.mockito.Mockito.argThat;
22+
import static org.mockito.Mockito.eq;
23+
import static org.mockito.Mockito.mockStatic;
24+
import static org.mockito.Mockito.verify;
25+
import static org.mockito.Mockito.when;
26+
27+
public class PetFlowExampleTest {
28+
29+
@InjectMocks
30+
private PetFlowExample petFlowExample;
31+
32+
@Mock
33+
private CatService catService;
34+
35+
@Mock
36+
private PetService petService;
37+
38+
@BeforeEach
39+
public void setup() {
40+
MockitoAnnotations.openMocks(this);
41+
}
42+
43+
@Test
44+
public void testPetUseCaseFlow() throws ApiException {
45+
NewCat newCat = new NewCat();
46+
UUID catId = UUID.randomUUID();
47+
Cat cat = new Cat(catId, new Date(), new Date()).name("Whiskers");
48+
PetStatus status = new PetStatus().value("RESERVED");
49+
50+
when(catService.addCat(any(NewCat.class))).thenReturn(cat);
51+
when(catService.getCat(catId.toString())).thenReturn(cat);
52+
53+
try (MockedStatic<MockDataBuilders> mockBuilders = mockStatic(MockDataBuilders.class)) {
54+
mockBuilders.when(MockDataBuilders::buildNewCat).thenReturn(newCat);
55+
56+
petFlowExample.petUseCaseFlow();
57+
58+
verify(catService).addCat(newCat);
59+
verify(catService).getCat(catId.toString());
60+
verify(catService).updateCat(argThat(updatedCat -> "Catso".equals(updatedCat.getName())), eq("0"));
61+
verify(petService).updatePetStatus(eq(catId), eq(status), eq("1"));
62+
verify(petService).removePet(catId);
63+
}
64+
}
65+
66+
@Test
67+
public void testPetUseCaseFlowThrowsApiException() throws ApiException {
68+
NewCat newCat = new NewCat();
69+
when(catService.addCat(any(NewCat.class))).thenThrow(new ApiException("API error"));
70+
71+
try (MockedStatic<MockDataBuilders> mockBuilders = mockStatic(MockDataBuilders.class)) {
72+
mockBuilders.when(MockDataBuilders::buildNewCat).thenReturn(newCat);
73+
74+
org.junit.jupiter.api.Assertions.assertThrows(ApiException.class, () -> petFlowExample.petUseCaseFlow());
75+
}
76+
}
77+
}

src/test/java/com/mastercard/app/petstore/services/AdoptionsServiceTest.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.mastercard.app.petstore.services;
22

33
import com.mastercard.app.petstore.utils.MockDataBuilders;
4+
import org.junit.jupiter.api.Assertions;
45
import org.junit.jupiter.api.BeforeEach;
56
import org.junit.jupiter.api.Test;
67
import org.mockito.InjectMocks;
@@ -28,7 +29,7 @@
2829
import static org.mockito.Mockito.verify;
2930
import static org.mockito.Mockito.when;
3031

31-
public class AdoptionsServiceTest {
32+
class AdoptionsServiceTest {
3233

3334
private AdoptionsApi adoptionsApiFle;
3435

@@ -38,14 +39,14 @@ public class AdoptionsServiceTest {
3839
AdoptionsService adoptionsService;
3940

4041
@BeforeEach
41-
public void setUp() {
42+
void setUp() {
4243
adoptionsApiFle = mock(AdoptionsApi.class);
4344
adoptionsApiFullBody = mock(AdoptionsApi.class);
4445
adoptionsService = new AdoptionsService(adoptionsApiFle, adoptionsApiFullBody);
4546
}
4647

4748
@Test
48-
public void adoptPet_shouldReturnALocation() throws ApiException {
49+
void adoptPet_shouldReturnALocation() throws ApiException {
4950
Map<String, List<String>> headers = Collections.singletonMap("location", Collections.singletonList("/adoptions/b1546cc7-a979-4c13-8817-40a8a0770868"));
5051
ApiResponse<Void> responseWithHeader = new ApiResponse<>(200, headers);
5152
when(adoptionsApiFle.adoptPetWithHttpInfo(any())).thenReturn(responseWithHeader);
@@ -56,28 +57,30 @@ public void adoptPet_shouldReturnALocation() throws ApiException {
5657
}
5758

5859
@Test
59-
public void getAdoption_shouldReturnAnAdoption() throws ApiException {
60+
void getAdoption_shouldReturnAnAdoption() throws ApiException {
6061
Adoption adoption = MockDataBuilders.buildAdoptionObject();
6162
when(adoptionsApiFle.getAdoption(any())).thenReturn(adoption);
6263

64+
Assertions.assertNotNull(adoption.getId());
6365
Adoption returnedAdoption = adoptionsService.getAdoption(adoption.getId().toString());
6466

6567
assertEquals(adoption.getId(), returnedAdoption.getId());
6668
}
6769

6870
@Test
69-
public void searchAdoption_shouldReturnAnAdoptionSearch() throws ApiException {
71+
void searchAdoption_shouldReturnAnAdoptionSearch() throws ApiException {
7072
AdoptionSearch adoptionSearch = MockDataBuilders.buildAdoptionSearch();
7173

7274
when(adoptionsApiFle.searchAdoptedPets(adoptionSearch.getFromDate(), adoptionSearch.getToDate(), adoptionSearch.getPetCategory(), adoptionSearch.getPetIdentifier())).thenReturn(adoptionSearch);
7375

76+
Assertions.assertNotNull(adoptionSearch.getPetIdentifier());
7477
AdoptionSearch returnedAdoptionSearch = adoptionsService.searchAdoption(adoptionSearch.getFromDate(), adoptionSearch.getToDate(), adoptionSearch.getPetCategory(), adoptionSearch.getPetIdentifier().toString());
7578

7679
assertEquals(adoptionSearch.getSearchResults().size(), returnedAdoptionSearch.getSearchResults().size());
7780
}
7881

7982
@Test
80-
public void updateAdoption_shouldUpdateAnAdoption() throws ApiException {
83+
void updateAdoption_shouldUpdateAnAdoption() throws ApiException {
8184
Adoption adoption = MockDataBuilders.buildAdoptionObject();
8285
String etag = "33a64df551425f";
8386

@@ -97,7 +100,7 @@ public void updateAdoption_shouldUpdateAnAdoption() throws ApiException {
97100
}
98101

99102
@Test
100-
public void deleteAdoption_shouldDeleteAnAdoption() throws ApiException {
103+
void deleteAdoption_shouldDeleteAnAdoption() throws ApiException {
101104
UUID id = UUID.randomUUID();
102105
doNothing().when(adoptionsApiFle).deleteAdoption(id);
103106

@@ -107,7 +110,7 @@ public void deleteAdoption_shouldDeleteAnAdoption() throws ApiException {
107110
}
108111

109112
@Test
110-
public void adoptionPayment_shouldReturnAnAdoptionPayment() throws ApiException {
113+
void adoptionPayment_shouldReturnAnAdoptionPayment() throws ApiException {
111114
Payment payment = MockDataBuilders.buildPayment();
112115
PaymentDetails paymentDetails = MockDataBuilders.buildPaymentDetailsFromPayment(payment);
113116
UUID id = UUID.randomUUID();

src/test/java/com/mastercard/app/petstore/services/CatServiceTest.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.mastercard.app.petstore.services;
22

33
import com.mastercard.app.petstore.utils.MockDataBuilders;
4+
import org.junit.jupiter.api.Assertions;
45
import org.junit.jupiter.api.BeforeEach;
56
import org.junit.jupiter.api.Test;
67
import org.mockito.InjectMocks;
@@ -12,21 +13,21 @@
1213
import static org.junit.jupiter.api.Assertions.assertEquals;
1314
import static org.mockito.Mockito.*;
1415

15-
public class CatServiceTest {
16+
class CatServiceTest {
1617

1718
private CatsApi catsApi;
1819

1920
@InjectMocks
2021
CatService catService;
2122

2223
@BeforeEach
23-
public void setUp() {
24+
void setUp() {
2425
catsApi = mock(CatsApi.class);
2526
catService = new CatService(catsApi);
2627
}
2728

2829
@Test
29-
public void addCat_shouldReturnACat() throws ApiException {
30+
void addCat_shouldReturnACat() throws ApiException {
3031
NewCat newCat = MockDataBuilders.buildNewCat();
3132
Cat expectedCat = MockDataBuilders.buildCat();
3233

@@ -38,17 +39,18 @@ public void addCat_shouldReturnACat() throws ApiException {
3839
}
3940

4041
@Test
41-
public void getCat_shouldReturnACat() throws ApiException {
42+
void getCat_shouldReturnACat() throws ApiException {
4243
Cat cat = MockDataBuilders.buildCat();
4344
when(catsApi.getCat(any())).thenReturn(cat);
4445

46+
Assertions.assertNotNull(cat.getId());
4547
Cat returnedCat = catService.getCat(cat.getId().toString());
4648

4749
assertEquals(returnedCat.getId(), cat.getId());
4850
}
4951

5052
@Test
51-
public void updateCat_shouldUpdateACat() throws ApiException {
53+
void updateCat_shouldUpdateACat() throws ApiException {
5254
Cat cat = MockDataBuilders.buildCat();
5355
String etag = "33a64df551425f";
5456

0 commit comments

Comments
 (0)