From 208d53b89dd081c4d20f37e7d68fcaef35254787 Mon Sep 17 00:00:00 2001 From: Marco Villeneuve Date: Thu, 5 Oct 2023 14:16:51 -0700 Subject: [PATCH] Enhancement to ween out indy schools --- .../bc/gov/educ/api/edx/rest/RestUtils.java | 6 +-- .../api/edx/service/v1/EdxUsersService.java | 18 +++++++- .../controller/EdxUsersControllerTest.java | 15 +++++-- .../api/edx/service/EdxUsersServiceTests.java | 45 ++++++++++++++++++- 4 files changed, 74 insertions(+), 10 deletions(-) diff --git a/api/src/main/java/ca/bc/gov/educ/api/edx/rest/RestUtils.java b/api/src/main/java/ca/bc/gov/educ/api/edx/rest/RestUtils.java index 499b1266..4061ec83 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/edx/rest/RestUtils.java +++ b/api/src/main/java/ca/bc/gov/educ/api/edx/rest/RestUtils.java @@ -126,14 +126,14 @@ public Map getSchoolMap() { return schoolMap; } - public Map> getDistrictSchoolsMap() { - Map> districtSchoolsMap = new ConcurrentHashMap<>(); + public Map> getDistrictSchoolsMap() { + Map> districtSchoolsMap = new ConcurrentHashMap<>(); for (val school : this.getSchools()) { if(!districtSchoolsMap.containsKey(school.getDistrictId())){ districtSchoolsMap.put(school.getDistrictId(),new ArrayList<>()); } - districtSchoolsMap.get(school.getDistrictId()).add(UUID.fromString(school.getSchoolId())); + districtSchoolsMap.get(school.getDistrictId()).add(school); } return districtSchoolsMap; diff --git a/api/src/main/java/ca/bc/gov/educ/api/edx/service/v1/EdxUsersService.java b/api/src/main/java/ca/bc/gov/educ/api/edx/service/v1/EdxUsersService.java index e789e766..ac1df749 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/edx/service/v1/EdxUsersService.java +++ b/api/src/main/java/ca/bc/gov/educ/api/edx/service/v1/EdxUsersService.java @@ -79,6 +79,8 @@ public class EdxUsersService { private static final String EDX_USER_DISTRICT_ID="edxUserDistrictID"; + private static final List INDEPENDENT_SCHOOL_CATEGORIES = Arrays.asList("INDEPEND", "INDP_FNS"); + @Autowired public EdxUsersService(final MinistryOwnershipTeamRepository ministryOwnershipTeamRepository, final EdxUserSchoolRepository edxUserSchoolsRepository, final EdxUserRepository edxUserRepository, EdxUserDistrictRoleRepository edxUserDistrictRoleRepository, EdxUserDistrictRepository edxUserDistrictRepository, EdxUserSchoolRoleRepository edxUserSchoolRoleRepository, EdxRoleRepository edxRoleRepository, EdxActivationCodeRepository edxActivationCodeRepository, EdxActivationRoleRepository edxActivationRoleRepository, RestUtils restUtils, ApplicationProperties props) { this.ministryOwnershipTeamRepository = ministryOwnershipTeamRepository; @@ -125,9 +127,21 @@ public List findEdxUsers(Optional digitalId, Optional return this.getEdxUserRepository().findEdxUsers(digitalId, schoolID, firstName, lastName, districtID); } + private List getDistrictSchoolsWithoutIndependents(String districtID){ + var districtSchoolIDsMap = restUtils.getDistrictSchoolsMap(); + var fullSchooList = districtSchoolIDsMap.get(districtID); + var filteredSchoolList = new ArrayList(); + + fullSchooList.forEach(school -> { + if(!INDEPENDENT_SCHOOL_CATEGORIES.contains(school.getSchoolCategoryCode())){ + filteredSchoolList.add(UUID.fromString(school.getSchoolId())); + } + }); + return filteredSchoolList; + } + public List findAllDistrictEdxUsers(String districtID) { - Map> districtSchoolIDsMap = restUtils.getDistrictSchoolsMap(); - List edxUserSchoolEntities = this.getEdxUserSchoolsRepository().findAllBySchoolIDIn(districtSchoolIDsMap.get(districtID)); + List edxUserSchoolEntities = this.getEdxUserSchoolsRepository().findAllBySchoolIDIn(getDistrictSchoolsWithoutIndependents(districtID)); Map edxSchools = new HashMap<>(); edxUserSchoolEntities.stream().forEach(edxUserSchoolEntity -> { String schoolID = edxUserSchoolEntity.getSchoolID().toString(); diff --git a/api/src/test/java/ca/bc/gov/educ/api/edx/controller/EdxUsersControllerTest.java b/api/src/test/java/ca/bc/gov/educ/api/edx/controller/EdxUsersControllerTest.java index 263fd2db..1331753b 100644 --- a/api/src/test/java/ca/bc/gov/educ/api/edx/controller/EdxUsersControllerTest.java +++ b/api/src/test/java/ca/bc/gov/educ/api/edx/controller/EdxUsersControllerTest.java @@ -23,6 +23,7 @@ import org.springframework.test.web.servlet.ResultActions; import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import java.util.*; @@ -133,14 +134,21 @@ void testFindEdxUsers_GivenValidDigitalIdentityID_ShouldReturnOkStatusWithResult void testFindAllEdxUsersByDistrict_GivenValidDistrictID_ShouldReturnOkStatusWithResult() throws Exception { List schoolIDList1 = new ArrayList<>(); schoolIDList1.add(UUID.randomUUID()); + School school1 = new School(); + school1.setSchoolCategoryCode("PUBLIC"); + school1.setSchoolId(schoolIDList1.get(0).toString()); + this.createUserEntityWithMultipleSchools(this.edxUserRepository, this.edxPermissionRepository, this.edxRoleRepository, this.edxUserSchoolRepository, this.edxUserDistrictRepository, schoolIDList1); List schoolIDList2 = new ArrayList<>(); schoolIDList2.add(UUID.randomUUID()); + School school2 = new School(); + school2.setSchoolCategoryCode("PUBLIC"); + school2.setSchoolId(schoolIDList2.get(0).toString()); this.createUserEntityWithMultipleSchools(this.edxUserRepository, this.edxPermissionRepository, this.edxRoleRepository, this.edxUserSchoolRepository, this.edxUserDistrictRepository, schoolIDList2); - var districtSchoolsMap = new HashMap>(); + var districtSchoolsMap = new HashMap>(); var districtID = UUID.randomUUID(); - districtSchoolsMap.put(districtID.toString(), Arrays.asList(schoolIDList1.get(0),schoolIDList2.get(0))); + districtSchoolsMap.put(districtID.toString(), Arrays.asList(school1,school2)); Mockito.when(this.restUtils.getDistrictSchoolsMap()).thenReturn(districtSchoolsMap); this.mockMvc.perform(get(URL.BASE_URL_USERS + "/districtSchools/" + districtID) @@ -2028,8 +2036,9 @@ void testCreateEdxUserDistrict_GivenValidData_ShouldCreateEntity_AndReturnResult val edxUsr = objectMapper.readValue(resultActions.andReturn().getResponse().getContentAsByteArray(), EdxUser.class); + DateTimeFormatter dtf = DateTimeFormatter.ISO_LOCAL_DATE_TIME; EdxUserDistrict edxUserDistrict = createEdxUserDistrict(edxUsr); - edxUserDistrict.setExpiryDate(LocalDateTime.now().plusDays(5).truncatedTo(ChronoUnit.SECONDS).toString()); + edxUserDistrict.setExpiryDate(LocalDateTime.now().plusDays(5).truncatedTo(ChronoUnit.SECONDS).format(dtf).toString()); String jsonEdxUserDistrict = getJsonString(edxUserDistrict); val resultActions1 = this.mockMvc.perform(post(URL.BASE_URL_USERS + "/{id}" + "/district", edxUsr.getEdxUserID()) diff --git a/api/src/test/java/ca/bc/gov/educ/api/edx/service/EdxUsersServiceTests.java b/api/src/test/java/ca/bc/gov/educ/api/edx/service/EdxUsersServiceTests.java index 435ef2c4..d059e19f 100644 --- a/api/src/test/java/ca/bc/gov/educ/api/edx/service/EdxUsersServiceTests.java +++ b/api/src/test/java/ca/bc/gov/educ/api/edx/service/EdxUsersServiceTests.java @@ -11,6 +11,7 @@ import ca.bc.gov.educ.api.edx.rest.RestUtils; import ca.bc.gov.educ.api.edx.service.v1.EdxUsersService; import ca.bc.gov.educ.api.edx.struct.v1.EdxPrimaryActivationCode; +import ca.bc.gov.educ.api.edx.struct.v1.School; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; import org.mockito.Mockito; @@ -105,9 +106,49 @@ void findAllEdxUsersByDistrictID() { schoolEntity.getEdxUserSchoolRoleEntities().add(roleEntity); edxUserSchoolRepository.save(schoolEntity); - var districtSchoolsMap = new HashMap>(); + List schoolIDList1 = new ArrayList<>(); + schoolIDList1.add(schoolEntity.getSchoolID()); + School school1 = new School(); + school1.setSchoolCategoryCode("PUBLIC"); + school1.setSchoolId(schoolIDList1.get(0).toString()); + + var districtSchoolsMap = new HashMap>(); + var districtID = UUID.randomUUID(); + districtSchoolsMap.put(districtID.toString(), Arrays.asList(school1)); + Mockito.when(this.restUtils.getDistrictSchoolsMap()).thenReturn(districtSchoolsMap); + + var edxSchools = this.service.findAllDistrictEdxUsers(districtID.toString()); + assertThat(edxSchools).isNotNull().hasSize(1); + } + + @Test + void findAllEdxUsersByDistrictIDNoIndependents() { + var entity = this.createUserEntity(this.edxUserRepository, this.edxPermissionRepository, this.edxRoleRepository, this.edxUserSchoolRepository, this.edxUserDistrictRepository); + var schoolEntity = getEdxUserSchoolEntity(entity); + schoolEntity.setSchoolID(UUID.randomUUID()); + EdxUserSchoolRoleEntity roleEntity = new EdxUserSchoolRoleEntity(); + roleEntity.setEdxUserSchoolEntity(schoolEntity); + roleEntity.setCreateUser("ABC"); + roleEntity.setEdxRoleCode("EDX_SCHOOL_ADMIN"); + roleEntity.setCreateDate(LocalDateTime.now()); + schoolEntity.getEdxUserSchoolRoleEntities().add(roleEntity); + edxUserSchoolRepository.save(schoolEntity); + + School school1 = new School(); + school1.setSchoolCategoryCode("PUBLIC"); + school1.setSchoolId(schoolEntity.getSchoolID().toString()); + + School school2 = new School(); + school2.setSchoolCategoryCode("INDP_FNS"); + school2.setSchoolId(UUID.randomUUID().toString()); + + School school3 = new School(); + school3.setSchoolCategoryCode("INDEPEND"); + school3.setSchoolId(UUID.randomUUID().toString()); + + var districtSchoolsMap = new HashMap>(); var districtID = UUID.randomUUID(); - districtSchoolsMap.put(districtID.toString(), Arrays.asList(schoolEntity.getSchoolID())); + districtSchoolsMap.put(districtID.toString(), Arrays.asList(school1, school2, school3)); Mockito.when(this.restUtils.getDistrictSchoolsMap()).thenReturn(districtSchoolsMap); var edxSchools = this.service.findAllDistrictEdxUsers(districtID.toString());