From e6662b1607f1836d28d7645c770c567fe6ab77cc Mon Sep 17 00:00:00 2001 From: Benjamin Mwalimu Mulyungi Date: Sat, 16 Oct 2021 02:02:10 +0300 Subject: [PATCH] :bug: Fix a bug with with null plan effective end date --- .../web/rest/OrganizationResource.java | 7 +- .../web/rest/OrganizationResourceTest.java | 94 ++++++++++++++++--- 2 files changed, 85 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/opensrp/web/rest/OrganizationResource.java b/src/main/java/org/opensrp/web/rest/OrganizationResource.java index de1942989..96bd4350b 100644 --- a/src/main/java/org/opensrp/web/rest/OrganizationResource.java +++ b/src/main/java/org/opensrp/web/rest/OrganizationResource.java @@ -2,6 +2,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; + import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.ImmutablePair; import org.apache.logging.log4j.LogManager; @@ -246,9 +247,9 @@ public UserAssignmentBean getUserAssignedLocationsAndPlans(Authentication authen Arrays.asList(UserController.JURISDICTION, UserController.STATUS, UserController.EFFECTIVE_PERIOD), false) .stream() - .filter(plan -> PlanStatus.ACTIVE.equals(plan.getStatus()) - && (plan.getEffectivePeriod().getEnd().isAfterNow() - || plan.getEffectivePeriod().getEnd().isEqualNow())) + .filter(plan -> PlanStatus.ACTIVE.equals(plan.getStatus()) && (plan.getEffectivePeriod() != null + && plan.getEffectivePeriod().getEnd() != null && (plan.getEffectivePeriod().getEnd().isAfterNow() + || plan.getEffectivePeriod().getEnd().isEqualNow()))) .collect(Collectors.toUnmodifiableSet()); Set planLocationIds = validActivePlans.stream() diff --git a/src/test/java/org/opensrp/web/rest/OrganizationResourceTest.java b/src/test/java/org/opensrp/web/rest/OrganizationResourceTest.java index 34ccbac8d..646242659 100644 --- a/src/test/java/org/opensrp/web/rest/OrganizationResourceTest.java +++ b/src/test/java/org/opensrp/web/rest/OrganizationResourceTest.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; + import org.apache.commons.lang3.tuple.ImmutablePair; import org.joda.time.DateTime; import org.junit.Before; @@ -72,7 +73,9 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import static org.mockito.ArgumentMatchers.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.atLeast; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doThrow; @@ -432,21 +435,84 @@ public void testgetUserAssignedLocationsAndPlansWithoutPlansShouldReturnUserAssi assertFalse(userAssignment.getPlans().contains(assignment.getPlanId())); } verifyNoInteractions(planService); - + } - + + @Test + public void testGetUserAssignedLocationsAndPlansWithPlansWithoutEffectiveEndDateShouldReturnActivePlansOnly() + throws Exception { + Pair authenticatedUser = TestData.getAuthentication(token, keycloakPrincipal, securityContext); + List ids = Arrays.asList(123l, 124l); + + when(practitionerService.getOrganizationsByUserId(authenticatedUser.getFirst().getBaseEntityId())) + .thenReturn(new ImmutablePair<>(getPractioner(), ids)); + List assignments = getOrganizationLocationsAssigned(true); + when(organizationService.findAssignedLocationsAndPlans(ids)).thenReturn(assignments); + + List planIds = assignments.stream().map(a -> a.getPlanId()).collect(Collectors.toList()); + List plans = getPlans(assignments, false); + when(planService.getPlansByIdsReturnOptionalFields(any(), any(), eq(false))).thenReturn(plans); + + PhysicalLocation location = LocationResourceTest.createStructure(); + location.getProperties().setName("Vilage123"); + PhysicalLocation location2 = new PhysicalLocation(); + location2.setId(UUID.randomUUID().toString()); + location2.setProperties(new LocationProperty()); + location2.getProperties().setName("OA1"); + location2.getProperties().setParentId(location.getId()); + + PhysicalLocation location3 = new PhysicalLocation(); + location3.setId(UUID.randomUUID().toString()); + location3.setProperties(new LocationProperty()); + location3.getProperties().setName("Oa3"); + location3.getProperties().setParentId(location.getId()); + when(locationService.findLocationByIdsWithChildren(eq(false), any(), eq(Integer.MAX_VALUE))) + .thenReturn(Arrays.asList(location, location2, location3)); + + Authentication authentication = authenticatedUser.getSecond(); + MvcResult result = mockMvc + .perform(get(BASE_URL + "/user-assignment") + .with(SecurityMockMvcRequestPostProcessors.authentication(authentication))) + .andExpect(status().isOk()).andReturn(); + + UserAssignmentBean userAssignment = objectMapper.readValue(result.getResponse().getContentAsString(), + UserAssignmentBean.class); + + assertEquals(new HashSet<>(ids), userAssignment.getOrganizationIds()); + assertEquals(2, userAssignment.getJurisdictions().size()); + + Set activePlans = plans.stream() + .filter(p -> PlanStatus.ACTIVE.equals(p.getStatus()) + && (p.getEffectivePeriod().getEnd().isAfterNow() + || p.getEffectivePeriod().getEnd().isEqualNow())) + .map(p -> p.getIdentifier()) + .collect(Collectors.toSet()); + + assertEquals(activePlans.size(), userAssignment.getPlans().size()); + + assertTrue(activePlans.size() < planIds.size()); + assertTrue(userAssignment.getPlans().containsAll(activePlans)); + Set inActivePlans = new HashSet<>(planIds); + inActivePlans.removeAll(activePlans); + + verify(planService).getPlansByIdsReturnOptionalFields(ArgumentMatchers.argThat(arg -> arg.containsAll(planIds)), + eq(Arrays.asList(UserController.JURISDICTION, UserController.STATUS, UserController.EFFECTIVE_PERIOD)), + eq(false)); + + } + @Test public void testGetUserAssignedLocationsAndPlansWithPlansShouldReturnActivePlansOnly() throws Exception { Pair authenticatedUser = TestData.getAuthentication(token, keycloakPrincipal, securityContext); List ids = Arrays.asList(123l, 124l); - + when(practitionerService.getOrganizationsByUserId(authenticatedUser.getFirst().getBaseEntityId())) - .thenReturn(new ImmutablePair<>(getPractioner(), ids)); + .thenReturn(new ImmutablePair<>(getPractioner(), ids)); List assignments = getOrganizationLocationsAssigned(true); when(organizationService.findAssignedLocationsAndPlans(ids)).thenReturn(assignments); - + List planIds = assignments.stream().map(a -> a.getPlanId()).collect(Collectors.toList()); - List plans = getPlans(assignments); + List plans = getPlans(assignments, true); when(planService.getPlansByIdsReturnOptionalFields(any(), any(), eq(false))).thenReturn(plans); PhysicalLocation location = LocationResourceTest.createStructure(); @@ -537,24 +603,26 @@ private List getOrganizationLocationsAssigned(boolean include } return organizationAssigmentBeans; - + } - + private Practitioner getPractioner() { Practitioner practitioner = new Practitioner(); practitioner.setIdentifier("ID-123"); practitioner.setActive(Boolean.TRUE); return practitioner; - + } - - private List getPlans(List assignedLocations) { + + private List getPlans(List assignedLocations, boolean addEffectiveEndDate) { Random random = new Random(); return assignedLocations.stream().map(al -> { PlanDefinition plan = new PlanDefinition(); plan.setIdentifier(al.getPlanId()); plan.setJurisdiction(Collections.singletonList(new Jurisdiction(al.getJurisdictionId()))); - plan.setEffectivePeriod(new Period(DateTime.now(), DateTime.now().plusDays(2))); + plan.setEffectivePeriod(addEffectiveEndDate ? + new Period(DateTime.now(), DateTime.now().plusDays(2)) : + new Period(DateTime.now(), null)); plan.setStatus(random.nextBoolean() ? PlanStatus.ACTIVE : PlanStatus.COMPLETED); return plan; }).collect(Collectors.toList());