Skip to content

Commit

Permalink
Fix #174: all territories are sorted in alphabetical order
Browse files Browse the repository at this point in the history
  • Loading branch information
fjlopez committed Apr 6, 2024
1 parent 1fcc351 commit 63d849e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,36 @@ public Page<Territory> 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));
}

private static Supplier<PageImpl<Territory>> 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<Territory> extractPageFromTerritories(List<Territory> 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<Territory> getApplicationTerritories(User user, Integer appId) {
return userConfigurationRepository.findByUser(user).stream()
.filter(uc -> isAppPartOfUserConfiguration(appId, uc))
Expand All @@ -65,13 +81,12 @@ private List<Territory> 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<Territory> getTerritories(String username, Pageable pageable) {
return userRepository.findByUsername(username)
Expand All @@ -88,6 +103,7 @@ private List<Territory> getTerritories(User user) {
return userConfigurationRepository.findByUser(user).stream()
.map(UserConfiguration::getTerritory)
.distinct()
.sorted(Comparator.comparing(Territory::getName))
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")));
}


Expand All @@ -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
Expand Down

0 comments on commit 63d849e

Please sign in to comment.