|
16 | 16 | import org.junit.runner.RunWith;
|
17 | 17 | import org.mockito.Mock;
|
18 | 18 | import org.mockito.junit.MockitoJUnitRunner;
|
| 19 | +import org.springframework.data.domain.Page; |
| 20 | +import org.springframework.data.domain.PageImpl; |
| 21 | +import org.springframework.data.domain.PageRequest; |
| 22 | +import org.springframework.data.domain.Pageable; |
19 | 23 |
|
20 |
| -import java.util.Arrays; |
21 |
| -import java.util.List; |
22 |
| -import java.util.Optional; |
| 24 | +import java.util.*; |
23 | 25 |
|
24 |
| -import static dev.sunbirdrc.claim.contants.AttributeNames.ATTESTOR_INFO; |
25 |
| -import static dev.sunbirdrc.claim.contants.AttributeNames.NOTES; |
| 26 | +import static dev.sunbirdrc.claim.contants.AttributeNames.*; |
26 | 27 | import static dev.sunbirdrc.claim.model.ClaimStatus.CLOSED;
|
27 | 28 | import static dev.sunbirdrc.claim.model.ClaimStatus.OPEN;
|
28 | 29 | import static org.junit.Assert.assertEquals;
|
@@ -52,15 +53,62 @@ public void shouldReturnOnlyAuthorizedClaims() {
|
52 | 53 | Claim claim2 = getClaim("2");
|
53 | 54 | Claim claim3 = getClaim("3");
|
54 | 55 | List<Claim> allClaimsForEntity = Arrays.asList(claim1, claim2, claim3);
|
| 56 | + Pageable pageable = PageRequest.of(0, 3); |
55 | 57 | String entity = "Teacher";
|
56 | 58 | JsonNode dummyNode = new ObjectMapper().nullNode();
|
57 | 59 | when(claimRepository.findByAttestorEntity(entity)).thenReturn(allClaimsForEntity);
|
58 | 60 | when(claimsAuthorizer.isAuthorizedAttestor(claim1, dummyNode)).thenReturn(true);
|
59 | 61 | when(claimsAuthorizer.isAuthorizedAttestor(claim2, dummyNode)).thenReturn(false);
|
60 | 62 | when(claimsAuthorizer.isAuthorizedAttestor(claim3, dummyNode)).thenReturn(true);
|
| 63 | + Map<String, Object> actualClaims = new HashMap<>(); |
| 64 | + actualClaims.put(CONTENT, Arrays.asList(claim1, claim3)); |
| 65 | + actualClaims.put(TOTAL_PAGES, 1); |
| 66 | + actualClaims.put(TOTAL_ELEMENTS, 2); |
| 67 | + assertEquals(claimService.findClaimsForAttestor(entity, dummyNode, pageable), actualClaims); |
| 68 | + } |
61 | 69 |
|
62 |
| - List<Claim> actualClaims = Arrays.asList(claim1, claim3); |
63 |
| - assertEquals(claimService.findClaimsForAttestor(entity, dummyNode), actualClaims); |
| 70 | + @Test |
| 71 | + public void shouldReturnAppropriateClaimsInPaginationFormat() { |
| 72 | + Claim claim1 = getClaim("1"); |
| 73 | + Claim claim2 = getClaim("2"); |
| 74 | + Claim claim3 = getClaim("3"); |
| 75 | + Claim claim4 = getClaim("4"); |
| 76 | + List<Claim> allClaimsForEntity = Arrays.asList(claim1, claim2, claim3, claim4); |
| 77 | + Pageable pageable = PageRequest.of(1, 2); |
| 78 | + String entity = "Teacher"; |
| 79 | + JsonNode dummyNode = new ObjectMapper().nullNode(); |
| 80 | + when(claimRepository.findByAttestorEntity(entity)).thenReturn(allClaimsForEntity); |
| 81 | + when(claimsAuthorizer.isAuthorizedAttestor(claim1, dummyNode)).thenReturn(true); |
| 82 | + when(claimsAuthorizer.isAuthorizedAttestor(claim2, dummyNode)).thenReturn(true); |
| 83 | + when(claimsAuthorizer.isAuthorizedAttestor(claim3, dummyNode)).thenReturn(true); |
| 84 | + when(claimsAuthorizer.isAuthorizedAttestor(claim4, dummyNode)).thenReturn(true); |
| 85 | + Map<String, Object> actualClaims = new HashMap<>(); |
| 86 | + actualClaims.put(CONTENT, Arrays.asList(claim3, claim4)); |
| 87 | + actualClaims.put(TOTAL_PAGES, 2); |
| 88 | + actualClaims.put(TOTAL_ELEMENTS, 4); |
| 89 | + assertEquals(claimService.findClaimsForAttestor(entity, dummyNode, pageable), actualClaims); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void shouldReturnEmptyClaimsIfOffsetGreaterThanClaimSize() { |
| 94 | + Claim claim1 = getClaim("1"); |
| 95 | + Claim claim2 = getClaim("2"); |
| 96 | + Claim claim3 = getClaim("3"); |
| 97 | + Claim claim4 = getClaim("4"); |
| 98 | + List<Claim> allClaimsForEntity = Arrays.asList(claim1, claim2, claim3, claim4); |
| 99 | + Pageable pageable = PageRequest.of(2, 2); |
| 100 | + String entity = "Teacher"; |
| 101 | + JsonNode dummyNode = new ObjectMapper().nullNode(); |
| 102 | + when(claimRepository.findByAttestorEntity(entity)).thenReturn(allClaimsForEntity); |
| 103 | + when(claimsAuthorizer.isAuthorizedAttestor(claim1, dummyNode)).thenReturn(true); |
| 104 | + when(claimsAuthorizer.isAuthorizedAttestor(claim2, dummyNode)).thenReturn(true); |
| 105 | + when(claimsAuthorizer.isAuthorizedAttestor(claim3, dummyNode)).thenReturn(true); |
| 106 | + when(claimsAuthorizer.isAuthorizedAttestor(claim4, dummyNode)).thenReturn(true); |
| 107 | + Map<String, Object> actualClaims = new HashMap<>(); |
| 108 | + actualClaims.put(CONTENT, new ArrayList<>()); |
| 109 | + actualClaims.put(TOTAL_PAGES, 2); |
| 110 | + actualClaims.put(TOTAL_ELEMENTS, 4); |
| 111 | + assertEquals(claimService.findClaimsForAttestor(entity, dummyNode, pageable), actualClaims); |
64 | 112 | }
|
65 | 113 |
|
66 | 114 | @Test(expected = ResourceNotFoundException.class)
|
|
0 commit comments