Skip to content

Commit

Permalink
Set default value to ORIGIN
Browse files Browse the repository at this point in the history
Co-authored-by: pierpaolo.didato@emeal.nttdata.com
  • Loading branch information
andrea-putzu authored Oct 18, 2024
1 parent 4c5fb1e commit 4f6a9f8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Collectors;

@Slf4j
@Service
Expand All @@ -30,8 +28,6 @@ class InstitutionServiceImpl implements InstitutionService {
private final OpenDataRestClient openDataRestClient;
private final IndexWriterService<Institution> institutionIndexWriterService;



@Autowired
InstitutionServiceImpl(IndexSearchService<Institution> indexSearchService, OpenDataRestClient openDataRestClient, IndexWriterService<Institution> institutionIndexWriterService) {
this.openDataRestClient = openDataRestClient;
Expand All @@ -40,7 +36,6 @@ class InstitutionServiceImpl implements InstitutionService {
this.indexSearchService = indexSearchService;
}


@Override
public QueryResult<Institution> search(Optional<String> searchText, int page, int limit) {
log.trace("search start");
Expand All @@ -59,13 +54,11 @@ public QueryResult<Institution> search(Optional<String> searchText, String categ
final QueryResult<Institution> queryResult = searchText.map(text -> indexSearchService.fullTextSearch(Field.DESCRIPTION, searchText.orElseThrow(), Field.CATEGORY, categories, page, limit))
.orElseGet(() -> indexSearchService.findAll(page, limit, Entity.INSTITUTION.toString()));


log.debug("search result = {}", queryResult);
log.trace("search end");
return queryResult;
}


@Override
public Institution findById(String id, Optional<Origin> origin, List<String> categories) {
log.trace("findById start");
Expand All @@ -75,15 +68,13 @@ public Institution findById(String id, Optional<Origin> origin, List<String> cat
} else {
final Supplier<List<Institution>> institutionsSupplier = () -> indexSearchService.findById(Field.ID, id);
List<Institution> institutions;
if (origin.isPresent()) {
Origin orig = origin.get();
institutions = institutionsSupplier.get().stream()
.filter(institution -> institution.getOrigin().equals(orig) &&
(categories.isEmpty() || categories.contains(institution.getCategory())))
.collect(Collectors.toList());
} else {
institutions = !categories.isEmpty() ? new ArrayList<>() : institutionsSupplier.get();
}

Origin orig = origin.get();
institutions = institutionsSupplier.get().stream()
.filter(institution -> institution.getOrigin().equals(orig) &&
(categories.isEmpty() || categories.contains(institution.getCategory())))
.toList();

if (institutions.isEmpty()) {
throw new ResourceNotFoundException();
} else if (institutions.size() > 1) {
Expand Down Expand Up @@ -125,6 +116,4 @@ private List<Institution> getInstitutions() {
log.trace("getInstitutions end");
return institutions;
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Optional;

import static it.pagopa.selfcare.commons.utils.TestUtils.mockInstance;
import static it.pagopa.selfcare.party.registry_proxy.connector.model.Origin.IPA;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.*;
Expand Down Expand Up @@ -159,7 +160,7 @@ void findById_ResourceNotFound2() {
// given
final String id = "pippo";
final List<String> categories = List.of("cat1", "cat2");
final Optional<Origin> origin = Optional.of(Origin.IPA);
final Optional<Origin> origin = Optional.of(IPA);
final DummyInstitution institution = mockInstance(new DummyInstitution());
institution.setOrigin(Origin.MOCK);
institution.setCategory("cat1");
Expand All @@ -179,7 +180,7 @@ void findById_ResourceNotFound3() {
// given
final String id = "pippo";
final List<String> categories = List.of("cat1", "cat2");
final Optional<Origin> origin = Optional.of(Origin.IPA);
final Optional<Origin> origin = Optional.of(IPA);
final DummyInstitution institution = mockInstance(new DummyInstitution());
institution.setOrigin(origin.get());
institution.setCategory("cat3");
Expand All @@ -199,7 +200,7 @@ void findById_ResourceNotFound4() {
// given
final String id = "pippo";
final List<String> categories = Collections.emptyList();
final Optional<Origin> origin = Optional.of(Origin.IPA);
final Optional<Origin> origin = Optional.of(IPA);
final DummyInstitution institution = mockInstance(new DummyInstitution());
institution.setOrigin(Origin.MOCK);
institution.setCategory("cat3");
Expand All @@ -219,7 +220,7 @@ void findById_emptyCategories() {
// given
final String id = "pippo";
final List<String> categories = Collections.emptyList();
final Optional<Origin> origin = Optional.of(Origin.IPA);
final Optional<Origin> origin = Optional.of(IPA);
final DummyInstitution institution = mockInstance(new DummyInstitution());
institution.setOrigin(origin.get());
institution.setCategory("cat3");
Expand All @@ -241,8 +242,9 @@ void findById_TooManyResourceFound() {
final String id = "pippo";

final List<String> categoriesMatcher = Collections.emptyList();
final Optional<Origin> origin = Optional.empty();
final Optional<Origin> origin = Optional.of(IPA);
final DummyInstitution institution = mockInstance(new DummyInstitution());
institution.setOrigin(IPA);
when(indexSearchService.findById(any(), anyString()))
.thenReturn(List.of(institution, institution));
// when
Expand All @@ -260,7 +262,7 @@ void findById_filtered_found() {
// given
final String id = "pippo";
final List<String> categories = List.of("cat1", "cat2");
final Optional<Origin> origin = Optional.of(Origin.IPA);
final Optional<Origin> origin = Optional.of(IPA);
final DummyInstitution institution = mockInstance(new DummyInstitution());
institution.setOrigin(origin.get());
institution.setCategory("cat1");
Expand All @@ -282,10 +284,11 @@ void findById_TooManyResourceFound2() {

final List<String> categoriesMatcher = Collections.emptyList();
final DummyInstitution institution = mockInstance(new DummyInstitution());
institution.setOrigin(IPA);
when(indexSearchService.findById(any(), anyString()))
.thenReturn(List.of(institution, institution));
// when
final Executable executable = () -> institutionService.findById(id, Optional.empty(), categoriesMatcher);
final Executable executable = () -> institutionService.findById(id, Optional.of(IPA), categoriesMatcher);
// then
assertThrows(TooManyResourceFoundException.class, executable);
verify(indexSearchService, times(1))
Expand All @@ -301,7 +304,7 @@ void findById_ResourceNotFound5() {
when(indexSearchService.findById(any(), anyString()))
.thenReturn(new ArrayList<>());
// when
final Executable executable = () -> institutionService.findById(id, Optional.empty(), categories);
final Executable executable = () -> institutionService.findById(id, Optional.of(IPA), categories);
// then
assertThrows(ResourceNotFoundException.class, executable);
verify(indexSearchService, times(1))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ public InstitutionResource findInstitution(@ApiParam("${swagger.api.institution.
if (categories.isPresent()) {
categoriesList = Arrays.stream(categories.get().split(",")).collect(Collectors.toList());
}
final InstitutionResource institutionResource = InstitutionMapper.toResource(institutionService.findById(id, origin, categoriesList));
final InstitutionResource institutionResource = InstitutionMapper.toResource(institutionService.findById(id,
origin.isEmpty() ? Optional.of(Origin.IPA) : origin, categoriesList));

log.debug("findInstitution result = {}", institutionResource);
log.trace("findInstitution end");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ void findInstitution_defaultInputParams() throws Exception {
.andExpect(jsonPath("$.istatCode", notNullValue()));
// then
verify(institutionServiceMock, times(1))
.findById(id, Optional.empty(), categoriesMatcher);
.findById(id, Optional.of(Origin.IPA), categoriesMatcher);
verifyNoMoreInteractions(institutionServiceMock);
}
}

0 comments on commit 4f6a9f8

Please sign in to comment.