diff --git a/common/src/main/java/org/sitmun/authorization/service/ProfileService.java b/common/src/main/java/org/sitmun/authorization/service/ProfileService.java index 3904f4ad..6438006b 100644 --- a/common/src/main/java/org/sitmun/authorization/service/ProfileService.java +++ b/common/src/main/java/org/sitmun/authorization/service/ProfileService.java @@ -42,7 +42,10 @@ public Page getApplicationTerritories(String username, Integer appId, return userRepository.findByUsername(username) .filter(user -> Boolean.FALSE.equals(user.getBlocked())) .map(user -> getApplicationTerritories(user, appId)) - .map(territories -> extractPageFromTerritories(territories, pageable)) + .map(territories -> { + territories.sort(Comparator.comparing(Territory::getName)); + return extractPageFromTerritories(territories, pageable); + }) .orElseGet(emptyPageSupplier(pageable)); } @@ -50,12 +53,25 @@ private static Supplier> emptyPageSupplier(Pageable pageable return () -> new PageImpl<>(Collections.emptyList(), pageable, 0); } + /** + * Extract a page from a list of territories sorted by name. + * @param territories the list of territories sorted by name + * @param pageable the pagination information + * @return a page of territories + */ private static PageImpl extractPageFromTerritories(List territories, Pageable pageable) { final int start = Math.min((int) pageable.getOffset(), territories.size()); final int end = Math.min((start + pageable.getPageSize()), territories.size()); return new PageImpl<>(territories.subList(start, end), pageable, territories.size()); } + /** + * Get the list of territories for the user in a given application. + * + * @param user the user + * @param appId the identifier of the application + * @return a list of territories sorted by name + */ private List getApplicationTerritories(User user, Integer appId) { return userConfigurationRepository.findByUser(user).stream() .filter(uc -> isAppPartOfUserConfiguration(appId, uc)) @@ -65,13 +81,12 @@ private List getApplicationTerritories(User user, Integer appId) { .collect(Collectors.toList()); } - /** * Get the list of territories for the user. * * @param username the username * @param pageable pagination information - * @return a page of territories + * @return a page of territories sorted by name */ public Page getTerritories(String username, Pageable pageable) { return userRepository.findByUsername(username) @@ -88,6 +103,7 @@ private List getTerritories(User user) { return userConfigurationRepository.findByUser(user).stream() .map(UserConfiguration::getTerritory) .distinct() + .sorted(Comparator.comparing(Territory::getName)) .collect(Collectors.toList()); } diff --git a/deploy/heroku-dev-lite/src/main/resources/config/liquibase/changelog/04_initial_data_dev/STM_TERRITORY.csv b/deploy/heroku-dev-lite/src/main/resources/config/liquibase/changelog/04_initial_data_dev/STM_TERRITORY.csv index e5e661cc..371900cf 100644 --- a/deploy/heroku-dev-lite/src/main/resources/config/liquibase/changelog/04_initial_data_dev/STM_TERRITORY.csv +++ b/deploy/heroku-dev-lite/src/main/resources/config/liquibase/changelog/04_initial_data_dev/STM_TERRITORY.csv @@ -1,4 +1,4 @@ TER_ID,TER_CODTER,TER_NAME,TER_ADMNAME,TER_ADDRESS,TER_EMAIL,TER_SCOPE,TER_LOGO,TER_EXTENT,TER_BLOCKED,TER_TYPID,TER_NOTE,TER_CREATED,TER_GTYPID,TER_CENTER,TER_ZOOM -1,60001,Provincia,Provincia,"",NULL,T,http://sitmun.diba.cat/sitmun2/img/logo/lbuit.png,363487 4561229 481617 4686464,0,8,NULL,"2011-04-29T11:41:55.000000",NULL,422552 4623846,8 -2,60002,Municipio,Municipio,"",NULL,M,http://sitmun.diba.cat/sitmun2/img/logo/l08075.png,448046 4603029 458244 4609234,0,6,NULL,"2011-04-29T11:41:55.000000",NULL,453145 4606132,10 -3,70003,Otro,Otro,"",NULL,R,NULL,430250 4612070 469609 4638298,0,5,NULL,"2011-04-29T11:41:55.000000",0,449929 4625184,10 +1,60001,Provincia A,Provincia A,"",NULL,T,http://sitmun.diba.cat/sitmun2/img/logo/lbuit.png,363487 4561229 481617 4686464,0,8,NULL,"2011-04-29T11:41:55.000000",NULL,422552 4623846,8 +2,60002,Municipio B,Municipio B,"",NULL,M,http://sitmun.diba.cat/sitmun2/img/logo/l08075.png,448046 4603029 458244 4609234,0,6,NULL,"2011-04-29T11:41:55.000000",NULL,453145 4606132,10 +3,70003,Otro C,Otro C,"",NULL,R,NULL,430250 4612070 469609 4638298,0,5,NULL,"2011-04-29T11:41:55.000000",0,449929 4625184,10 diff --git a/deploy/heroku-dev-lite/src/test/java/org/sitmun/authorization/controller/ClientConfigurationTerritoryControllerTest.java b/deploy/heroku-dev-lite/src/test/java/org/sitmun/authorization/controller/ClientConfigurationTerritoryControllerTest.java index 913a694b..363245bd 100644 --- a/deploy/heroku-dev-lite/src/test/java/org/sitmun/authorization/controller/ClientConfigurationTerritoryControllerTest.java +++ b/deploy/heroku-dev-lite/src/test/java/org/sitmun/authorization/controller/ClientConfigurationTerritoryControllerTest.java @@ -33,7 +33,7 @@ void readPublicUser() throws Exception { mvc.perform(get(URIConstants.CONFIG_CLIENT_TERRITORY_URI)) .andExpect(status().isOk()) .andExpect(jsonPath("$.content", hasSize(3))) - .andExpect(jsonPath("$.content[*].name", hasItem("Provincia"))); + .andExpect(jsonPath("$.content[*].name", hasItems("Municipio B", "Otro C", "Provincia A"))); } @Test @@ -47,7 +47,7 @@ void readOtherUser() throws Exception { .andExpect(jsonPath("$.size", is(pageSize))) .andExpect(jsonPath("$.number", is(0))) .andExpect(jsonPath("$.totalPages", is(1))) - .andExpect(jsonPath("$.content[*].name", hasItem("Provincia"))); + .andExpect(jsonPath("$.content[*].name", hasItem("Provincia A"))); } @@ -62,7 +62,7 @@ void readOtherUserWithPagination() throws Exception { .andExpect(jsonPath("$.size", is(1))) .andExpect(jsonPath("$.number", is(1))) .andExpect(jsonPath("$.totalPages", is(3))) - .andExpect(jsonPath("$.content[*].name", hasItem("Municipio"))); + .andExpect(jsonPath("$.content[*].name", hasItem("Otro C"))); } @Test