Skip to content

Commit

Permalink
Enhancement to ween out indy schools
Browse files Browse the repository at this point in the history
  • Loading branch information
arcshiftsolutions committed Oct 5, 2023
1 parent 2401863 commit 208d53b
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 10 deletions.
6 changes: 3 additions & 3 deletions api/src/main/java/ca/bc/gov/educ/api/edx/rest/RestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,14 @@ public Map<String, School> getSchoolMap() {
return schoolMap;
}

public Map<String, List<UUID>> getDistrictSchoolsMap() {
Map<String, List<UUID>> districtSchoolsMap = new ConcurrentHashMap<>();
public Map<String, List<School>> getDistrictSchoolsMap() {
Map<String, List<School>> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ public class EdxUsersService {

private static final String EDX_USER_DISTRICT_ID="edxUserDistrictID";

private static final List<String> 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;
Expand Down Expand Up @@ -125,9 +127,21 @@ public List<EdxUserEntity> findEdxUsers(Optional<UUID> digitalId, Optional<UUID>
return this.getEdxUserRepository().findEdxUsers(digitalId, schoolID, firstName, lastName, districtID);
}

private List<UUID> getDistrictSchoolsWithoutIndependents(String districtID){
var districtSchoolIDsMap = restUtils.getDistrictSchoolsMap();
var fullSchooList = districtSchoolIDsMap.get(districtID);
var filteredSchoolList = new ArrayList<UUID>();

fullSchooList.forEach(school -> {
if(!INDEPENDENT_SCHOOL_CATEGORIES.contains(school.getSchoolCategoryCode())){
filteredSchoolList.add(UUID.fromString(school.getSchoolId()));
}
});
return filteredSchoolList;
}

public List<EdxSchool> findAllDistrictEdxUsers(String districtID) {
Map<String, List<UUID>> districtSchoolIDsMap = restUtils.getDistrictSchoolsMap();
List<EdxUserSchoolEntity> edxUserSchoolEntities = this.getEdxUserSchoolsRepository().findAllBySchoolIDIn(districtSchoolIDsMap.get(districtID));
List<EdxUserSchoolEntity> edxUserSchoolEntities = this.getEdxUserSchoolsRepository().findAllBySchoolIDIn(getDistrictSchoolsWithoutIndependents(districtID));
Map<String, EdxSchool> edxSchools = new HashMap<>();
edxUserSchoolEntities.stream().forEach(edxUserSchoolEntity -> {
String schoolID = edxUserSchoolEntity.getSchoolID().toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand Down Expand Up @@ -133,14 +134,21 @@ void testFindEdxUsers_GivenValidDigitalIdentityID_ShouldReturnOkStatusWithResult
void testFindAllEdxUsersByDistrict_GivenValidDistrictID_ShouldReturnOkStatusWithResult() throws Exception {
List<UUID> 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<UUID> 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<String, List<UUID>>();
var districtSchoolsMap = new HashMap<String, List<School>>();
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)
Expand Down Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -105,9 +106,49 @@ void findAllEdxUsersByDistrictID() {
schoolEntity.getEdxUserSchoolRoleEntities().add(roleEntity);
edxUserSchoolRepository.save(schoolEntity);

var districtSchoolsMap = new HashMap<String, List<UUID>>();
List<UUID> schoolIDList1 = new ArrayList<>();
schoolIDList1.add(schoolEntity.getSchoolID());
School school1 = new School();
school1.setSchoolCategoryCode("PUBLIC");
school1.setSchoolId(schoolIDList1.get(0).toString());

var districtSchoolsMap = new HashMap<String, List<School>>();
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<String, List<School>>();
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());
Expand Down

0 comments on commit 208d53b

Please sign in to comment.