diff --git a/api/agencyservice.yaml b/api/agencyservice.yaml index 35ab4208..02e740bb 100644 --- a/api/agencyservice.yaml +++ b/api/agencyservice.yaml @@ -75,6 +75,68 @@ paths: description: FORBIDDEN - no/invalid CSRF token 500: description: INTERNAL SERVER ERROR - server encountered unexpected condition + /agencies/by-tenant: + get: + tags: + - agency-controller + summary: 'Returns a list of agencies from a specific tenant (retrieved from the security context). + The security context is retrieved from the bearer token, so it is not passed as a request parameter. + If no agency is found No Content is being returned. [Authorization: none]' + operationId: getTenantAgencies + parameters: + - name: postcode + in: query + required: true + description: The postcode the user entered + schema: + type: string + minLength: 5 + maxLength: 5 + example: "56789" + - name: topicId + in: query + required: true + description: The main topic of the registration form + schema: + type: integer + format: int32 + example: 7 + responses: + 200: + description: OK - successfull operation + content: + 'application/json': + schema: + type: array + items: + $ref: '#/components/schemas/FullAgencyResponseDTO' + 400: + description: BAD REQUEST - invalid/incomplete request or body object + 403: + description: FORBIDDEN - no/invalid CSRF token + 500: + description: INTERNAL SERVER ERROR - server encountered unexpected condition + /agencies/topics: + get: + tags: + - agency-controller + summary: 'Returns a list of all the topics assigned to all the agencies of a specific tenant' + operationId: getAgenciesTopics + responses: + 200: + description: OK - successfull operation + content: + 'application/json': + schema: + type: array + items: + $ref: '#/components/schemas/AgencyTopicsDTO' + 400: + description: BAD REQUEST - invalid/incomplete request or body object + 403: + description: FORBIDDEN - no/invalid CSRF token + 500: + description: INTERNAL SERVER ERROR - server encountered unexpected condition /agencies/{agencyIds}: get: tags: @@ -198,6 +260,17 @@ components: type: boolean example: "false" + AgencyTopicsDTO: + type: object + properties: + id: + type: integer + format: int64 + example: 684 + name: + type: string + example: "Adoption and fostering a child" + DemographicsDTO: type: object properties: diff --git a/src/main/java/de/caritas/cob/agencyservice/api/authorization/Authority.java b/src/main/java/de/caritas/cob/agencyservice/api/authorization/Authority.java index fb11c718..b349d1b7 100644 --- a/src/main/java/de/caritas/cob/agencyservice/api/authorization/Authority.java +++ b/src/main/java/de/caritas/cob/agencyservice/api/authorization/Authority.java @@ -17,7 +17,9 @@ public enum Authority { TENANT_ADMIN("tenant-admin", AuthorityValue.TENANT_ADMIN), RESTRICTED_AGENCY_ADMIN("restricted-agency-admin", AuthorityValue.RESTRICTED_AGENCY_ADMIN, AuthorityValue.SEARCH_AGENCIES), - RESTRICTED_CONSULTANT_ADMIN("restricted-consultant-admin", AuthorityValue.SEARCH_AGENCIES); + RESTRICTED_CONSULTANT_ADMIN("restricted-consultant-admin", AuthorityValue.SEARCH_AGENCIES), + + ADVICE_SEEKER("user", AuthorityValue.SEARCH_AGENCIES_WITHIN_TENANT); private final String roleName; private final List authorities; @@ -55,6 +57,7 @@ private AuthorityValue() {} public static final String SEARCH_AGENCIES = PREFIX + "SEARCH_AGENCIES"; public static final String TENANT_ADMIN = PREFIX + "TENANT_ADMIN"; public static final String RESTRICTED_AGENCY_ADMIN = PREFIX + "RESTRICTED_AGENCY_ADMIN"; + public static final String SEARCH_AGENCIES_WITHIN_TENANT = PREFIX + "SEARCH_AGENCIES_WITHIN_TENANT"; } diff --git a/src/main/java/de/caritas/cob/agencyservice/api/controller/AgencyController.java b/src/main/java/de/caritas/cob/agencyservice/api/controller/AgencyController.java index bb50d200..f15089ab 100644 --- a/src/main/java/de/caritas/cob/agencyservice/api/controller/AgencyController.java +++ b/src/main/java/de/caritas/cob/agencyservice/api/controller/AgencyController.java @@ -3,8 +3,10 @@ import static java.util.Optional.ofNullable; import de.caritas.cob.agencyservice.api.model.AgencyResponseDTO; +import de.caritas.cob.agencyservice.api.model.AgencyTopicsDTO; import de.caritas.cob.agencyservice.api.model.FullAgencyResponseDTO; import de.caritas.cob.agencyservice.api.service.AgencyService; +import de.caritas.cob.agencyservice.api.service.TopicEnrichmentService; import de.caritas.cob.agencyservice.generated.api.controller.AgenciesApi; import io.swagger.annotations.Api; import java.util.List; @@ -27,6 +29,7 @@ public class AgencyController implements AgenciesApi { private final @NonNull AgencyService agencyService; + private final @NonNull TopicEnrichmentService topicEnrichmentService; /** * Gets a randomly sorted list of AgencyResponseDTOs (from database) and returns the list and a @@ -54,6 +57,17 @@ public ResponseEntity> getAgencies( : new ResponseEntity<>(HttpStatus.NO_CONTENT); } + @Override + public ResponseEntity> getTenantAgencies(String postcode, + Integer topicId) { + + var agencies = agencyService.getAgencies(postcode, topicId); + + return !CollectionUtils.isEmpty(agencies) + ? new ResponseEntity<>(agencies, HttpStatus.OK) + : new ResponseEntity<>(HttpStatus.NO_CONTENT); + } + /** * Returns information of the provided agencies. * @@ -84,4 +98,19 @@ public ResponseEntity> getAgenciesByConsultingType( return new ResponseEntity<>(agencies, HttpStatus.OK); } + + /** + * Returns all the topics from all the agencies of a specific tenant + * + * @return lest of topics + */ + @Override + public ResponseEntity> getAgenciesTopics() { + + var topics = this.agencyService.getAgenciesTopics(); + var enrichedTopics = topicEnrichmentService.enrichTopicIdsWithTopicData(topics); + + return enrichedTopics.isEmpty() ? new ResponseEntity<>(HttpStatus.NO_CONTENT) + : new ResponseEntity<>(enrichedTopics, HttpStatus.OK); + } } diff --git a/src/main/java/de/caritas/cob/agencyservice/api/repository/agency/AgencyRepository.java b/src/main/java/de/caritas/cob/agencyservice/api/repository/agency/AgencyRepository.java index dce30e15..31d11de5 100644 --- a/src/main/java/de/caritas/cob/agencyservice/api/repository/agency/AgencyRepository.java +++ b/src/main/java/de/caritas/cob/agencyservice/api/repository/agency/AgencyRepository.java @@ -46,9 +46,14 @@ public interface AgencyRepository extends JpaRepository { + "AND ((:gender IS NULL) OR (a.genders LIKE CONCAT('%,',:gender,'%') OR a.genders LIKE CONCAT(:gender,'%'))) " + "AND a.delete_date IS NULL "; + String SELECT_ALL_AGENCIES_TOPICS = "SELECT distinct(at.topic_id) FROM agency_topic at " + + "INNER JOIN agency a ON a.id = at.agency_id "; + String GROUP_BY_ORDER_BY = "GROUP BY a.id " + "ORDER BY a.postcode DESC"; + String ORDER_BY_TOPIC = "ORDER BY at.topic_id"; + /** * Returns a list of {@link Agency}s that are assigned to the given post code. * @@ -80,6 +85,12 @@ List searchWithTopic(@Param(value = "postcode") String postCode, @Param(value = "counselling_relation") String counsellingRelation, Long tenantId); + @Query( + value = SELECT_ALL_AGENCIES_TOPICS + + ORDER_BY_TOPIC, + nativeQuery = true) + List findAllAgenciesTopics(Long tenantId); + Optional findByIdAndDeleteDateNull(Long agencyId); List findByIdIn(List agencyIds); diff --git a/src/main/java/de/caritas/cob/agencyservice/api/repository/agency/AgencyTenantAwareRepository.java b/src/main/java/de/caritas/cob/agencyservice/api/repository/agency/AgencyTenantAwareRepository.java index ba353ec7..20c0c4e2 100644 --- a/src/main/java/de/caritas/cob/agencyservice/api/repository/agency/AgencyTenantAwareRepository.java +++ b/src/main/java/de/caritas/cob/agencyservice/api/repository/agency/AgencyTenantAwareRepository.java @@ -6,7 +6,6 @@ import org.springframework.context.annotation.Primary; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; -import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.query.Param; /** @@ -51,6 +50,13 @@ List searchWithTopic(@Param(value = "postcode") String postCode, @Param(value = "counselling_relation") String counsellingRelation, Long tenantId); + @Query( + value = SELECT_ALL_AGENCIES_TOPICS + + AND_A_TENANT_ID_FILTER + + ORDER_BY_TOPIC, + nativeQuery = true) + List findAllAgenciesTopics(Long tenantId); + @Query("select a from Agency as a where a.id = :agencyId ") Optional findById(Long agencyId); diff --git a/src/main/java/de/caritas/cob/agencyservice/api/service/AgencyService.java b/src/main/java/de/caritas/cob/agencyservice/api/service/AgencyService.java index b4394ac3..193083ea 100644 --- a/src/main/java/de/caritas/cob/agencyservice/api/service/AgencyService.java +++ b/src/main/java/de/caritas/cob/agencyservice/api/service/AgencyService.java @@ -69,6 +69,8 @@ public class AgencyService { @Value("${feature.multitenancy.with.single.domain.enabled}") private boolean multitenancyWithSingleDomain; + private static final String DB_POSTCODES_ERROR = "Database error while getting postcodes"; + /** * Returns a list of {@link AgencyResponseDTO} which match the provided agencyIds. * @@ -81,7 +83,6 @@ public List getAgencies(List agencyIds) { .toList(); } - /** * Returns a list of {@link AgencyResponseDTO} which match the provided consulting type. * @@ -102,7 +103,6 @@ public List getAgencies(int consultingTypeId) { } } - public List getAgencies(Optional postCode, int consultingTypeId, Optional topicId) { return getAgencies(postCode, consultingTypeId, topicId, Optional.empty(), Optional.empty(), Optional.empty()); @@ -128,7 +128,6 @@ public List getAgencies(Optional postCode, return Collections.emptyList(); } - var agencies = findAgencies(postCode, getConsultingTypeIdForSearch(consultingTypeId), topicId, age, gender, counsellingRelation); Collections.shuffle(agencies); @@ -145,6 +144,18 @@ public List getAgencies(Optional postCode, return mutableResponseDTO; } + public List getAgencies(String postCode, Integer topicId) { + + var agencies = findAgenciesForCurrentTenant(postCode, topicId); + return agencies.stream() + .map(this::convertToFullAgencyResponseDTO) + .toList(); + } + + public List getAgenciesTopics() { + return agencyRepository.findAllAgenciesTopics(TenantContext.getCurrentTenant()); + } + private Optional getConsultingTypeIdForSearch(int consultingTypeId) { return multitenancyWithSingleDomain ? Optional.empty() : Optional.of(consultingTypeId); } @@ -153,14 +164,8 @@ private List findAgencies(Optional postCode, Optional c Optional optionalTopicId, Optional age, Optional gender, Optional counsellingRelation) { - AgencySearch agencySearch = AgencySearch.builder() - .postCode(postCode) - .consultingTypeId(consultingTypeId) - .topicId(optionalTopicId) - .age(age) - .gender(gender) - .counsellingRelation(counsellingRelation) - .build(); + AgencySearch agencySearch = buildAgencySearch(postCode, + consultingTypeId, optionalTopicId, age, gender, counsellingRelation); if (demographicsFeatureEnabled) { assertAgeAndGenderAreProvided(age, gender); @@ -185,10 +190,32 @@ private List findAgencies(AgencySearch agencySearch) { TenantContext.getCurrentTenant()); } catch (DataAccessException ex) { throw new InternalServerErrorException(LogService::logDatabaseError, - "Database error while getting postcodes"); + DB_POSTCODES_ERROR); } } + private List findAgenciesForCurrentTenant(String postCode, Integer topicId) { + + AgencySearch agencySearch = buildAgencySearch(Optional.of(postCode), + Optional.empty(), Optional.of(topicId), Optional.empty(), + Optional.empty(), Optional.empty()); + + return findAgenciesWithTopicForCurrentTenant(agencySearch); + } + + private static AgencySearch buildAgencySearch(Optional postCode, + Optional consultingTypeId, Optional optionalTopicId, Optional age, + Optional gender, Optional counsellingRelation) { + return AgencySearch.builder() + .postCode(postCode) + .consultingTypeId(consultingTypeId) + .topicId(optionalTopicId) + .age(age) + .gender(gender) + .counsellingRelation(counsellingRelation) + .build(); + } + private void assertTopicIdIsProvided(Optional topicId) { if (!topicId.isPresent()) { throw new BadRequestException("Topic id not provided in the search"); @@ -249,7 +276,23 @@ private List findAgenciesWithTopic(AgencySearch agencySearch) { } catch (DataAccessException ex) { throw new InternalServerErrorException(LogService::logDatabaseError, - "Database error while getting postcodes"); + DB_POSTCODES_ERROR); + } + } + + private List findAgenciesWithTopicForCurrentTenant(AgencySearch agencySearch) { + try { + return agencyRepository + .searchWithTopic(agencySearch.getPostCode().orElse(null), agencySearch.getPostCode().orElse("").length(), + agencySearch.getConsultingTypeId().orElse(null), + agencySearch.getTopicId().orElseThrow(), + agencySearch.getAge().orElse(null), agencySearch.getGender().orElse(null), + agencySearch.getCounsellingRelation().orElse(null), + TenantContext.getCurrentTenant()); + + } catch (DataAccessException ex) { + throw new InternalServerErrorException(LogService::logDatabaseError, + DB_POSTCODES_ERROR); } } diff --git a/src/main/java/de/caritas/cob/agencyservice/api/service/TopicEnrichmentService.java b/src/main/java/de/caritas/cob/agencyservice/api/service/TopicEnrichmentService.java new file mode 100644 index 00000000..685a6634 --- /dev/null +++ b/src/main/java/de/caritas/cob/agencyservice/api/service/TopicEnrichmentService.java @@ -0,0 +1,68 @@ +package de.caritas.cob.agencyservice.api.service; + +import de.caritas.cob.agencyservice.api.model.AgencyTopicsDTO; +import de.caritas.cob.agencyservice.topicservice.generated.web.model.TopicDTO; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.stream.Collectors; +import lombok.NonNull; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +@ConditionalOnExpression("${feature.topics.enabled:true}") +@Slf4j +public class TopicEnrichmentService { + + private final @NonNull TopicService topicService; + + public List enrichTopicIdsWithTopicData(List topicIds) { + log.debug("Enriching topic ids with topics short titles"); + var availableTopics = topicService.getAllTopics(); + log.debug("Enriching topic ids list with size: {} ", topicIds.size()); + logAvailableTopicsList(availableTopics); + return enrichTopicIds(availableTopics, topicIds); + } + + private static void logAvailableTopicsList(List availableTopics) { + if (availableTopics == null) { + log.debug("Available topics list is null "); + } else if (availableTopics.isEmpty()) { + log.debug("Available topics list is empty"); + } else { + log.debug("Available topics list has size: {} ", availableTopics.size()); + } + } + + private List enrichTopicIds(List availableTopics, List topicIds) { + + if (availableTopics == null) { + return Collections.emptyList(); + } + // Create a map of availableTopics to quickly access TopicDTO by id + Map topicMap = availableTopics.stream() + .collect(Collectors.toMap(TopicDTO::getId, TopicDTO::getName)); + + // Filter and map the topicIds to AgencyTopicsDTOs, using the topicMap for fast lookup + return topicIds.stream() + .map(id -> { + Long topicId = Long.valueOf(id); + String topicName = topicMap.get(topicId); + + // Only create an AgencyTopicsDTO if the topicName exists + if (topicName != null) { + return new AgencyTopicsDTO() + .id(topicId) + .name(topicName); + } + return null; + }) + .filter(Objects::nonNull) + .toList(); + } +} diff --git a/src/main/java/de/caritas/cob/agencyservice/config/SecurityConfig.java b/src/main/java/de/caritas/cob/agencyservice/config/SecurityConfig.java index c5b0e17e..49294f8c 100644 --- a/src/main/java/de/caritas/cob/agencyservice/config/SecurityConfig.java +++ b/src/main/java/de/caritas/cob/agencyservice/config/SecurityConfig.java @@ -79,16 +79,17 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { httpSecurity.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) .and().authorizeRequests() - .requestMatchers("/agencies/**").permitAll() .requestMatchers(WHITE_LIST).permitAll() .requestMatchers("/agencies").permitAll() .requestMatchers(HttpMethod.GET, "/agencyadmin/agencies") .hasAuthority(AuthorityValue.SEARCH_AGENCIES) + .requestMatchers("/agencies/by-tenant").hasAuthority(AuthorityValue.SEARCH_AGENCIES_WITHIN_TENANT) .requestMatchers("/agencyadmin/agencies/tenant/*") .access("hasAuthority('" + AuthorityValue.AGENCY_ADMIN + "') and hasAuthority('" + AuthorityValue.TENANT_ADMIN + "')") .requestMatchers("/agencyadmin", "/agencyadmin/", "/agencyadmin/**") .hasAnyAuthority(AuthorityValue.AGENCY_ADMIN, AuthorityValue.RESTRICTED_AGENCY_ADMIN) + .requestMatchers("/agencies/**").permitAll() .anyRequest().denyAll(); diff --git a/src/test/java/de/caritas/cob/agencyservice/api/admin/service/agency/AgencyAdminSearchServiceIT.java b/src/test/java/de/caritas/cob/agencyservice/api/admin/service/agency/AgencyAdminSearchServiceIT.java index 7eb068cf..e2400078 100644 --- a/src/test/java/de/caritas/cob/agencyservice/api/admin/service/agency/AgencyAdminSearchServiceIT.java +++ b/src/test/java/de/caritas/cob/agencyservice/api/admin/service/agency/AgencyAdminSearchServiceIT.java @@ -5,6 +5,7 @@ import com.google.common.collect.Lists; import de.caritas.cob.agencyservice.AgencyServiceApplication; +import de.caritas.cob.agencyservice.api.service.TopicEnrichmentService; import de.caritas.cob.agencyservice.api.util.AuthenticatedUser; import de.caritas.cob.agencyservice.api.model.Sort; import de.caritas.cob.agencyservice.api.model.Sort.FieldEnum; @@ -38,6 +39,9 @@ class AgencyAdminSearchServiceIT { @Autowired private AgencyAdminSearchService agencyAdminSearchService; + @MockBean + private TopicEnrichmentService topicEnrichmentService; + @MockBean private AuthenticatedUser authenticatedUser; @@ -160,4 +164,4 @@ private AdminAgencyResponseDTO getAdminAgencies(Long... agencyIds) { } -} \ No newline at end of file +} diff --git a/src/test/java/de/caritas/cob/agencyservice/api/admin/service/agency/AgencyAdminSearchTenantSupportServiceTest.java b/src/test/java/de/caritas/cob/agencyservice/api/admin/service/agency/AgencyAdminSearchTenantSupportServiceTest.java index 280ab0d8..c3cee8d5 100644 --- a/src/test/java/de/caritas/cob/agencyservice/api/admin/service/agency/AgencyAdminSearchTenantSupportServiceTest.java +++ b/src/test/java/de/caritas/cob/agencyservice/api/admin/service/agency/AgencyAdminSearchTenantSupportServiceTest.java @@ -2,6 +2,7 @@ import com.google.common.collect.Lists; import de.caritas.cob.agencyservice.AgencyServiceApplication; +import de.caritas.cob.agencyservice.api.service.TopicEnrichmentService; import de.caritas.cob.agencyservice.api.util.AuthenticatedUser; import de.caritas.cob.agencyservice.api.model.Sort; import de.caritas.cob.agencyservice.api.service.securityheader.SecurityHeaderSupplier; @@ -34,6 +35,9 @@ class AgencyAdminSearchTenantSupportServiceTest { @Autowired private AgencyAdminSearchTenantSupportService agencyAdminSearchTenantSupportService; + @MockBean + private TopicEnrichmentService topicEnrichmentService; + @MockBean private AuthenticatedUser authenticatedUser; diff --git a/src/test/java/de/caritas/cob/agencyservice/api/admin/service/agency/AgencyTopicEnrichmentServiceTest.java b/src/test/java/de/caritas/cob/agencyservice/api/admin/service/agency/AgencyTopicEnrichmentServiceTest.java index 0118c575..d3ba0733 100644 --- a/src/test/java/de/caritas/cob/agencyservice/api/admin/service/agency/AgencyTopicEnrichmentServiceTest.java +++ b/src/test/java/de/caritas/cob/agencyservice/api/admin/service/agency/AgencyTopicEnrichmentServiceTest.java @@ -112,4 +112,4 @@ private Agency newAgencyWithTopics(ArrayList agencyTopics) { agency.setAgencyTopics(agencyTopics); return agency; } -} \ No newline at end of file +} diff --git a/src/test/java/de/caritas/cob/agencyservice/api/controller/ActuatorControllerIT.java b/src/test/java/de/caritas/cob/agencyservice/api/controller/ActuatorControllerIT.java index 0d643308..6484b365 100644 --- a/src/test/java/de/caritas/cob/agencyservice/api/controller/ActuatorControllerIT.java +++ b/src/test/java/de/caritas/cob/agencyservice/api/controller/ActuatorControllerIT.java @@ -1,11 +1,13 @@ package de.caritas.cob.agencyservice.api.controller; import de.caritas.cob.agencyservice.AgencyServiceApplication; +import de.caritas.cob.agencyservice.api.service.TopicEnrichmentService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.test.context.TestPropertySource; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; @@ -25,6 +27,9 @@ class ActuatorControllerIT { @Autowired private WebApplicationContext context; + @MockBean + private TopicEnrichmentService topicEnrichmentService; + private MockMvc mockMvc; @BeforeEach diff --git a/src/test/java/de/caritas/cob/agencyservice/api/controller/AgencyAdminControllerIT.java b/src/test/java/de/caritas/cob/agencyservice/api/controller/AgencyAdminControllerIT.java index d4f0591d..74b91bf9 100644 --- a/src/test/java/de/caritas/cob/agencyservice/api/controller/AgencyAdminControllerIT.java +++ b/src/test/java/de/caritas/cob/agencyservice/api/controller/AgencyAdminControllerIT.java @@ -3,7 +3,6 @@ import static javax.ws.rs.core.MediaType.APPLICATION_JSON; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; -import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.Mockito.anyInt; import static org.mockito.Mockito.when; import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; @@ -18,6 +17,7 @@ import de.caritas.cob.agencyservice.api.model.DataProtectionContactDTO; import de.caritas.cob.agencyservice.api.model.DataProtectionDTO; import de.caritas.cob.agencyservice.api.service.TenantService; +import de.caritas.cob.agencyservice.api.service.TopicEnrichmentService; import de.caritas.cob.agencyservice.api.util.AuthenticatedUser; import de.caritas.cob.agencyservice.api.manager.consultingtype.ConsultingTypeManager; import de.caritas.cob.agencyservice.api.model.AgencyDTO; @@ -40,7 +40,6 @@ import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.context.WebApplicationContext; @@ -60,6 +59,9 @@ class AgencyAdminControllerIT { @MockBean private ConsultingTypeManager consultingTypeManager; + @MockBean + private TopicEnrichmentService topicEnrichmentService; + @Autowired private WebApplicationContext context; diff --git a/src/test/java/de/caritas/cob/agencyservice/api/controller/AgencyAdminControllerWithDemographicsIT.java b/src/test/java/de/caritas/cob/agencyservice/api/controller/AgencyAdminControllerWithDemographicsIT.java index 489b4f90..d6c3bb86 100644 --- a/src/test/java/de/caritas/cob/agencyservice/api/controller/AgencyAdminControllerWithDemographicsIT.java +++ b/src/test/java/de/caritas/cob/agencyservice/api/controller/AgencyAdminControllerWithDemographicsIT.java @@ -13,6 +13,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import de.caritas.cob.agencyservice.api.service.TenantService; +import de.caritas.cob.agencyservice.api.service.TopicEnrichmentService; import de.caritas.cob.agencyservice.api.util.AuthenticatedUser; import de.caritas.cob.agencyservice.api.manager.consultingtype.ConsultingTypeManager; import de.caritas.cob.agencyservice.api.model.AgencyDTO; @@ -54,6 +55,9 @@ class AgencyAdminControllerWithDemographicsIT { @MockBean private ConsultingTypeManager consultingTypeManager; + @MockBean + private TopicEnrichmentService topicEnrichmentService; + @Autowired private WebApplicationContext context; diff --git a/src/test/java/de/caritas/cob/agencyservice/api/controller/AgencyControllerIT.java b/src/test/java/de/caritas/cob/agencyservice/api/controller/AgencyControllerIT.java index f6ae2b9d..6f0dfd4c 100644 --- a/src/test/java/de/caritas/cob/agencyservice/api/controller/AgencyControllerIT.java +++ b/src/test/java/de/caritas/cob/agencyservice/api/controller/AgencyControllerIT.java @@ -1,251 +1,124 @@ package de.caritas.cob.agencyservice.api.controller; -import static de.caritas.cob.agencyservice.testHelper.PathConstants.PATH_GET_AGENCIES_WITH_IDS; -import static de.caritas.cob.agencyservice.testHelper.PathConstants.PATH_GET_LIST_OF_AGENCIES; -import static de.caritas.cob.agencyservice.testHelper.TestConstants.AGENCY_ID; +import static de.caritas.cob.agencyservice.testHelper.PathConstants.PATH_GET_LIST_OF_AGENCIES_BY_TENANT; import static de.caritas.cob.agencyservice.testHelper.TestConstants.AGENCY_RESPONSE_DTO; -import static de.caritas.cob.agencyservice.testHelper.TestConstants.AGENCY_RESPONSE_DTO_LIST; import static de.caritas.cob.agencyservice.testHelper.TestConstants.FULL_AGENCY_RESPONSE_DTO; -import static de.caritas.cob.agencyservice.testHelper.TestConstants.INVALID_AGENCY_ID; -import static de.caritas.cob.agencyservice.testHelper.TestConstants.INVALID_CONSULTING_TYPE_QUERY; import static de.caritas.cob.agencyservice.testHelper.TestConstants.INVALID_POSTCODE_QUERY; -import static de.caritas.cob.agencyservice.testHelper.TestConstants.VALID_AGE_QUERY; -import static de.caritas.cob.agencyservice.testHelper.TestConstants.VALID_CONSULTING_TYPE_QUERY; import static de.caritas.cob.agencyservice.testHelper.TestConstants.VALID_POSTCODE_QUERY; -import static org.apache.commons.lang3.exception.ExceptionUtils.getStackTrace; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyInt; +import static de.caritas.cob.agencyservice.testHelper.TestConstants.VALID_TOPIC_ID_QUERY; import static org.mockito.Mockito.atLeastOnce; -import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -import de.caritas.cob.agencyservice.api.authorization.RoleAuthorizationAuthorityMapper; -import de.caritas.cob.agencyservice.api.exception.httpresponses.InternalServerErrorException; +import de.caritas.cob.agencyservice.api.authorization.Authority.AuthorityValue; import de.caritas.cob.agencyservice.api.model.FullAgencyResponseDTO; import de.caritas.cob.agencyservice.api.service.AgencyService; -import de.caritas.cob.agencyservice.api.service.LogService; -import de.caritas.cob.agencyservice.config.security.AuthorisationService; -import de.caritas.cob.agencyservice.config.security.JwtAuthConverter; -import de.caritas.cob.agencyservice.config.security.JwtAuthConverterProperties; +import de.caritas.cob.agencyservice.api.service.TopicEnrichmentService; import java.util.ArrayList; -import java.util.Collections; import java.util.List; -import java.util.Optional; import org.junit.jupiter.api.Test; -import org.mockito.Mock; import org.mockito.Mockito; -import org.slf4j.Logger; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; -import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.hateoas.client.LinkDiscoverers; import org.springframework.http.MediaType; +import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.web.servlet.MockMvc; -@WebMvcTest(AgencyController.class) -@AutoConfigureMockMvc(addFilters = false) +@SpringBootTest(properties = "spring.liquibase.enabled=false") +@AutoConfigureMockMvc +@AutoConfigureTestDatabase class AgencyControllerIT { - static final String PATH_GET_AGENCIES_BY_CONSULTINGTYPE = "/agencies/consultingtype/1"; - @Autowired private MockMvc mvc; @MockBean - private AgencyService agencyService; - - @MockBean - private LinkDiscoverers linkDiscoverers; - - @MockBean - private RoleAuthorizationAuthorityMapper roleAuthorizationAuthorityMapper; - - @MockBean - private JwtAuthConverter jwtAuthConverter; - - @MockBean - private AuthorisationService authorisationService; + private TopicEnrichmentService topicEnrichmentService; @MockBean - private JwtAuthConverterProperties jwtAuthConverterProperties; - - @Mock - private Logger logger; + private AgencyService agencyService; @Test - void getAgencies_Should_ReturnNoContent_When_ServiceReturnsEmptyList() throws Exception { + @WithMockUser(authorities = {AuthorityValue.SEARCH_AGENCIES_WITHIN_TENANT}) + void getTenantAgencies_Should_ReturnNoContent_When_ServiceReturnsEmptyList() throws Exception { - when(agencyService.getAgencies(Mockito.any(Optional.class), Mockito.anyInt(), Mockito.any(Optional.class), Mockito.any(Optional.class), Mockito.any(Optional.class), Mockito.any(Optional.class))) + when(agencyService.getAgencies(Mockito.anyString(), Mockito.anyInt())) .thenReturn(null); mvc.perform( - get(PATH_GET_LIST_OF_AGENCIES + "?" + VALID_POSTCODE_QUERY + "&" - + VALID_CONSULTING_TYPE_QUERY + "&" + VALID_AGE_QUERY) - .accept(MediaType.APPLICATION_JSON)) + get(PATH_GET_LIST_OF_AGENCIES_BY_TENANT + "?" + VALID_POSTCODE_QUERY + "&" + + VALID_TOPIC_ID_QUERY) + .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isNoContent()); } @Test - void getAgencies_Should_ReturnBadRequest_When_PostcodeParamIsInvalid() throws Exception { + @WithMockUser(authorities = {AuthorityValue.SEARCH_AGENCIES_WITHIN_TENANT}) + void getTenantAgencies_Should_ReturnBadRequest_When_PostcodeParamIsInvalid() throws Exception { mvc.perform( - get(PATH_GET_LIST_OF_AGENCIES + "?" + INVALID_POSTCODE_QUERY + "&" - + VALID_CONSULTING_TYPE_QUERY) - .accept(MediaType.APPLICATION_JSON)) + get(PATH_GET_LIST_OF_AGENCIES_BY_TENANT + "?" + INVALID_POSTCODE_QUERY + "&" + + VALID_TOPIC_ID_QUERY) + .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isBadRequest()); } @Test - void getAgencies_Should_ReturnBadRequest_When_ConsultingTypeParamIsInvalid() + @WithMockUser(authorities = {AuthorityValue.SEARCH_AGENCIES_WITHIN_TENANT}) + void getTenantAgencies_Should_ReturnBadRequest_When_topicIdParamIsNotProvided() throws Exception { - mvc.perform( - get(PATH_GET_LIST_OF_AGENCIES + "?" + VALID_POSTCODE_QUERY + "&" - + INVALID_CONSULTING_TYPE_QUERY) - .accept(MediaType.APPLICATION_JSON)) - .andExpect(status().isBadRequest()); - } - - @Test - void getAgencies_Should_ReturnRespondWith2XXResponseCode_When_PostcodeParamIsNotProvided() - throws Exception { - - mvc.perform(get(PATH_GET_LIST_OF_AGENCIES + "?" + VALID_CONSULTING_TYPE_QUERY) - .accept(MediaType.APPLICATION_JSON)).andExpect(status().isNoContent()); + mvc.perform(get(PATH_GET_LIST_OF_AGENCIES_BY_TENANT + "?" + VALID_POSTCODE_QUERY) + .accept(MediaType.APPLICATION_JSON)).andExpect(status().isBadRequest()); } @Test - void getAgencies_Should_ReturnBadRequest_When_ConsultingTypeParamIsNotProvided() + @WithMockUser(authorities = {AuthorityValue.SEARCH_AGENCIES_WITHIN_TENANT}) + void getTenantAgencies_Should_ReturnBadRequest_When_PostCodeParamIsNotProvided() throws Exception { - mvc.perform(get(PATH_GET_LIST_OF_AGENCIES + "?" + VALID_POSTCODE_QUERY) + mvc.perform(get(PATH_GET_LIST_OF_AGENCIES_BY_TENANT + "?" + VALID_TOPIC_ID_QUERY) .accept(MediaType.APPLICATION_JSON)).andExpect(status().isBadRequest()); } @Test - void getAgencies_Should_ReturnListAndOk_When_ServiceReturnsList() throws Exception { + @WithMockUser(authorities = {AuthorityValue.SEARCH_AGENCIES_WITHIN_TENANT}) + void getTenantAgencies_Should_ReturnListAndOk_When_ServiceReturnsList() throws Exception { List agencies = new ArrayList<>(); agencies.add(FULL_AGENCY_RESPONSE_DTO); - when(agencyService.getAgencies(Mockito.any(Optional.class), Mockito.anyInt(), Mockito.any(Optional.class), Mockito.any(Optional.class), Mockito.any(Optional.class), Mockito.any(Optional.class))) + when(agencyService.getAgencies(Mockito.anyString(), Mockito.anyInt())) .thenReturn(agencies); mvc.perform( - get(PATH_GET_LIST_OF_AGENCIES + "?" + VALID_POSTCODE_QUERY + "&" - + VALID_CONSULTING_TYPE_QUERY) - .accept(MediaType.APPLICATION_JSON)) + get(PATH_GET_LIST_OF_AGENCIES_BY_TENANT + "?" + VALID_POSTCODE_QUERY + "&" + + VALID_TOPIC_ID_QUERY) + .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(jsonPath("[0].name").value(AGENCY_RESPONSE_DTO.getName())); - verify(agencyService, atLeastOnce()).getAgencies(Mockito.any(Optional.class), - Mockito.anyInt(), Mockito.any(Optional.class), Mockito.any(Optional.class), Mockito.any(Optional.class), Mockito.any(Optional.class)); + verify(agencyService, atLeastOnce()).getAgencies(Mockito.anyString(), Mockito.anyInt()); } @Test - void getAgencies_With_Ids_Should_ReturnNoContent_When_ServiceReturnsNoAgency() - throws Exception { + void getTenantAgencies_Should_ReturnUnauthorized_When_UserHasNoAuthority() throws Exception { - when(agencyService.getAgencies(Mockito.anyList())).thenReturn(Collections.emptyList()); - - mvc.perform(get(PATH_GET_AGENCIES_WITH_IDS + AGENCY_ID).accept(MediaType.APPLICATION_JSON)) - .andExpect(status().isNotFound()); + mvc.perform(get(PATH_GET_LIST_OF_AGENCIES_BY_TENANT + "?" + VALID_TOPIC_ID_QUERY) + .accept(MediaType.APPLICATION_JSON)).andExpect(status().isUnauthorized()); } @Test - void getAgencies_With_Ids_Should_ReturnBadRequest_When_IdInvalid() throws Exception { + @WithMockUser(authorities = {AuthorityValue.SEARCH_AGENCIES}) + void getTenantAgencies_Should_ReturnForbidden_When_UserHasWrongAuthority() throws Exception { - mvc.perform( - get(PATH_GET_AGENCIES_WITH_IDS + INVALID_AGENCY_ID).contentType(MediaType.APPLICATION_JSON) - .accept(MediaType.APPLICATION_JSON)).andExpect(status().isBadRequest()); + mvc.perform(get(PATH_GET_LIST_OF_AGENCIES_BY_TENANT + "?" + VALID_TOPIC_ID_QUERY) + .accept(MediaType.APPLICATION_JSON)).andExpect(status().isForbidden()); } - - @Test - void getAgencies_With_Ids_Should_ReturnAgencyAndOk_When_ServiceReturnsAgency() - throws Exception { - - when(agencyService.getAgencies(Mockito.anyList())).thenReturn(AGENCY_RESPONSE_DTO_LIST); - - mvc.perform(get(PATH_GET_AGENCIES_WITH_IDS + AGENCY_ID + "," + AGENCY_ID) - .accept(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()) - .andExpect(jsonPath("[0].name").value(AGENCY_RESPONSE_DTO.getName())); - - verify(agencyService, atLeastOnce()).getAgencies(Mockito.anyList()); - } - - @Test - void getListOfAgencies_Should_ReturnServerErrorAndLogDatabaseError_OnAgencyServiceThrowsServerErrorException() - throws Exception { - - InternalServerErrorException dbEx = new InternalServerErrorException( - LogService::logDatabaseError, "message"); - when(agencyService.getAgencies(any(), anyInt(), Mockito.any(Optional.class), Mockito.any(Optional.class), Mockito.any(Optional.class), Mockito.any(Optional.class))).thenThrow(dbEx); - - mvc.perform(get(PATH_GET_LIST_OF_AGENCIES + "?" + VALID_POSTCODE_QUERY + "&" - + VALID_CONSULTING_TYPE_QUERY) - .accept(MediaType.APPLICATION_JSON)) - .andExpect(status().isInternalServerError()); - } - - @Test - void getAgencies_With_Ids_Should_ReturnServerErrorAndLogDatabaseError_OnAgencyServiceThrowsServerErrorException() - throws Exception { - - InternalServerErrorException dbEx = new InternalServerErrorException( - LogService::logDatabaseError, "message"); - when(agencyService.getAgencies(any())).thenThrow(dbEx); - - mvc.perform(get(PATH_GET_AGENCIES_WITH_IDS + AGENCY_ID + "," + AGENCY_ID) - .accept(MediaType.APPLICATION_JSON)) - .andExpect(status().isInternalServerError()); - } - - @Test - void getListOfAgencies_Should_ReturnServerErrorAndLogNumberFormatError_OnAgencyServiceThrowsServerErrorException() - throws Exception { - - InternalServerErrorException nfEx = new InternalServerErrorException( - LogService::logNumberFormatException, "message"); - - when(agencyService.getAgencies(any(), anyInt(), Mockito.any(Optional.class), Mockito.any(Optional.class), Mockito.any(Optional.class), Mockito.any(Optional.class))).thenThrow(nfEx); - - mvc.perform(get(PATH_GET_LIST_OF_AGENCIES + "?" + VALID_POSTCODE_QUERY + "&" - + VALID_CONSULTING_TYPE_QUERY) - .accept(MediaType.APPLICATION_JSON)) - .andExpect(status().isInternalServerError()); - - } - - @Test - void getAgencies_Should_ReturnBadRequest_When_ConsultingTypeIsNull() throws Exception { - mvc.perform( - get(PATH_GET_LIST_OF_AGENCIES + "?" + VALID_POSTCODE_QUERY + "&" - + "consultingType=").accept(MediaType.APPLICATION_JSON)) - .andExpect(status().isBadRequest()); - } - - @Test - void getAgenciesByConsultingType_Should_ReturnBadRequest_When_consultingTypeIsInvalid() - throws Exception { - mvc.perform( - get(PATH_GET_AGENCIES_BY_CONSULTINGTYPE.replace("1", "invalid")) - .accept(MediaType.APPLICATION_JSON)) - .andExpect(status().isBadRequest()); - } - - @Test - void getAgenciesByConsultingType_Should_ReturnOk_When_consultingTypeIsValid() - throws Exception { - mvc.perform( - get(PATH_GET_AGENCIES_BY_CONSULTINGTYPE) - .accept(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()); - } - } diff --git a/src/test/java/de/caritas/cob/agencyservice/api/controller/AgencyControllerTest.java b/src/test/java/de/caritas/cob/agencyservice/api/controller/AgencyControllerTest.java new file mode 100644 index 00000000..c2185b21 --- /dev/null +++ b/src/test/java/de/caritas/cob/agencyservice/api/controller/AgencyControllerTest.java @@ -0,0 +1,345 @@ +package de.caritas.cob.agencyservice.api.controller; + +import static de.caritas.cob.agencyservice.testHelper.PathConstants.PATH_GET_AGENCIES_WITH_IDS; +import static de.caritas.cob.agencyservice.testHelper.PathConstants.PATH_GET_LIST_OF_AGENCIES; +import static de.caritas.cob.agencyservice.testHelper.PathConstants.PATH_GET_LIST_OF_AGENCIES_BY_TENANT; +import static de.caritas.cob.agencyservice.testHelper.PathConstants.PATH_GET_LIST_OF_AGENCIES_TOPICS; +import static de.caritas.cob.agencyservice.testHelper.TestConstants.AGENCY_ID; +import static de.caritas.cob.agencyservice.testHelper.TestConstants.AGENCY_RESPONSE_DTO; +import static de.caritas.cob.agencyservice.testHelper.TestConstants.AGENCY_RESPONSE_DTO_LIST; +import static de.caritas.cob.agencyservice.testHelper.TestConstants.AGENCY_TOPICS_DTO; +import static de.caritas.cob.agencyservice.testHelper.TestConstants.FULL_AGENCY_RESPONSE_DTO; +import static de.caritas.cob.agencyservice.testHelper.TestConstants.INVALID_AGENCY_ID; +import static de.caritas.cob.agencyservice.testHelper.TestConstants.INVALID_CONSULTING_TYPE_QUERY; +import static de.caritas.cob.agencyservice.testHelper.TestConstants.INVALID_POSTCODE_QUERY; +import static de.caritas.cob.agencyservice.testHelper.TestConstants.VALID_AGE_QUERY; +import static de.caritas.cob.agencyservice.testHelper.TestConstants.VALID_CONSULTING_TYPE_QUERY; +import static de.caritas.cob.agencyservice.testHelper.TestConstants.VALID_POSTCODE_QUERY; +import static de.caritas.cob.agencyservice.testHelper.TestConstants.VALID_TOPIC_ID_QUERY; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.anyList; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.atLeastOnce; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import de.caritas.cob.agencyservice.api.authorization.RoleAuthorizationAuthorityMapper; +import de.caritas.cob.agencyservice.api.exception.httpresponses.InternalServerErrorException; +import de.caritas.cob.agencyservice.api.model.AgencyTopicsDTO; +import de.caritas.cob.agencyservice.api.model.FullAgencyResponseDTO; +import de.caritas.cob.agencyservice.api.service.AgencyService; +import de.caritas.cob.agencyservice.api.service.LogService; +import de.caritas.cob.agencyservice.api.service.TopicEnrichmentService; +import de.caritas.cob.agencyservice.config.security.AuthorisationService; +import de.caritas.cob.agencyservice.config.security.JwtAuthConverter; +import de.caritas.cob.agencyservice.config.security.JwtAuthConverterProperties; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Optional; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.slf4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.hateoas.client.LinkDiscoverers; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +@WebMvcTest(AgencyController.class) +@AutoConfigureMockMvc(addFilters = false) +class AgencyControllerTest { + + static final String PATH_GET_AGENCIES_BY_CONSULTINGTYPE = "/agencies/consultingtype/1"; + + @Autowired + private MockMvc mvc; + + @MockBean + private TopicEnrichmentService topicEnrichmentService; + + @MockBean + private AgencyService agencyService; + + @MockBean + private LinkDiscoverers linkDiscoverers; + + @MockBean + private RoleAuthorizationAuthorityMapper roleAuthorizationAuthorityMapper; + + @MockBean + private JwtAuthConverter jwtAuthConverter; + + @MockBean + private AuthorisationService authorisationService; + + @MockBean + private JwtAuthConverterProperties jwtAuthConverterProperties; + + @Mock + private Logger logger; + + @Test + void getAgencies_Should_ReturnNoContent_When_ServiceReturnsEmptyList() throws Exception { + + when(agencyService.getAgencies(any(Optional.class), anyInt(), any(Optional.class), any(Optional.class), any(Optional.class), any(Optional.class))) + .thenReturn(null); + + mvc.perform( + get(PATH_GET_LIST_OF_AGENCIES + "?" + VALID_POSTCODE_QUERY + "&" + + VALID_CONSULTING_TYPE_QUERY + "&" + VALID_AGE_QUERY) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isNoContent()); + } + + @Test + void getAgencies_Should_ReturnBadRequest_When_PostcodeParamIsInvalid() throws Exception { + + mvc.perform( + get(PATH_GET_LIST_OF_AGENCIES + "?" + INVALID_POSTCODE_QUERY + "&" + + VALID_CONSULTING_TYPE_QUERY) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isBadRequest()); + } + + @Test + void getAgencies_Should_ReturnBadRequest_When_ConsultingTypeParamIsInvalid() + throws Exception { + + mvc.perform( + get(PATH_GET_LIST_OF_AGENCIES + "?" + VALID_POSTCODE_QUERY + "&" + + INVALID_CONSULTING_TYPE_QUERY) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isBadRequest()); + } + + @Test + void getAgencies_Should_ReturnRespondWith2XXResponseCode_When_PostcodeParamIsNotProvided() + throws Exception { + + mvc.perform(get(PATH_GET_LIST_OF_AGENCIES + "?" + VALID_CONSULTING_TYPE_QUERY) + .accept(MediaType.APPLICATION_JSON)).andExpect(status().isNoContent()); + } + + @Test + void getAgencies_Should_ReturnBadRequest_When_ConsultingTypeParamIsNotProvided() + throws Exception { + + mvc.perform(get(PATH_GET_LIST_OF_AGENCIES + "?" + VALID_POSTCODE_QUERY) + .accept(MediaType.APPLICATION_JSON)).andExpect(status().isBadRequest()); + } + + @Test + void getAgencies_Should_ReturnListAndOk_When_ServiceReturnsList() throws Exception { + + List agencies = new ArrayList<>(); + agencies.add(FULL_AGENCY_RESPONSE_DTO); + + when(agencyService.getAgencies(any(Optional.class), anyInt(), any(Optional.class), any(Optional.class), any(Optional.class), any(Optional.class))) + .thenReturn(agencies); + + mvc.perform( + get(PATH_GET_LIST_OF_AGENCIES + "?" + VALID_POSTCODE_QUERY + "&" + + VALID_CONSULTING_TYPE_QUERY) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("[0].name").value(AGENCY_RESPONSE_DTO.getName())); + + verify(agencyService, atLeastOnce()).getAgencies(any(Optional.class), + anyInt(), any(Optional.class), any(Optional.class), any(Optional.class), any(Optional.class)); + } + + @Test + void getAgencies_With_Ids_Should_ReturnNoContent_When_ServiceReturnsNoAgency() + throws Exception { + + when(agencyService.getAgencies(anyList())).thenReturn(Collections.emptyList()); + + mvc.perform(get(PATH_GET_AGENCIES_WITH_IDS + AGENCY_ID).accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isNotFound()); + } + + @Test + void getAgencies_With_Ids_Should_ReturnBadRequest_When_IdInvalid() throws Exception { + + mvc.perform( + get(PATH_GET_AGENCIES_WITH_IDS + INVALID_AGENCY_ID).contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON)).andExpect(status().isBadRequest()); + } + + @Test + void getAgencies_With_Ids_Should_ReturnAgencyAndOk_When_ServiceReturnsAgency() + throws Exception { + + when(agencyService.getAgencies(anyList())).thenReturn(AGENCY_RESPONSE_DTO_LIST); + + mvc.perform(get(PATH_GET_AGENCIES_WITH_IDS + AGENCY_ID + "," + AGENCY_ID) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("[0].name").value(AGENCY_RESPONSE_DTO.getName())); + + verify(agencyService, atLeastOnce()).getAgencies(anyList()); + } + + @Test + void getListOfAgencies_Should_ReturnServerErrorAndLogDatabaseError_When_AgencyServiceThrowsServerErrorException() + throws Exception { + + InternalServerErrorException dbEx = new InternalServerErrorException( + LogService::logDatabaseError, "message"); + when(agencyService.getAgencies(any(), anyInt(), any(Optional.class), any(Optional.class), any(Optional.class), any(Optional.class))).thenThrow(dbEx); + + mvc.perform(get(PATH_GET_LIST_OF_AGENCIES + "?" + VALID_POSTCODE_QUERY + "&" + + VALID_CONSULTING_TYPE_QUERY) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isInternalServerError()); + } + + @Test + void getAgencies_With_Ids_Should_ReturnServerErrorAndLogDatabaseError_OnAgencyServiceThrowsServerErrorException() + throws Exception { + + InternalServerErrorException dbEx = new InternalServerErrorException( + LogService::logDatabaseError, "message"); + when(agencyService.getAgencies(any())).thenThrow(dbEx); + + mvc.perform(get(PATH_GET_AGENCIES_WITH_IDS + AGENCY_ID + "," + AGENCY_ID) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isInternalServerError()); + } + + @Test + void getListOfAgencies_Should_ReturnServerErrorAndLogNumberFormatError_OnAgencyServiceThrowsServerErrorException() + throws Exception { + + InternalServerErrorException nfEx = new InternalServerErrorException( + LogService::logNumberFormatException, "message"); + + when(agencyService.getAgencies(any(), anyInt(), any(Optional.class), any(Optional.class), any(Optional.class), any(Optional.class))).thenThrow(nfEx); + + mvc.perform(get(PATH_GET_LIST_OF_AGENCIES + "?" + VALID_POSTCODE_QUERY + "&" + + VALID_CONSULTING_TYPE_QUERY) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isInternalServerError()); + + } + + @Test + void getAgencies_Should_ReturnBadRequest_When_ConsultingTypeIsNull() throws Exception { + mvc.perform( + get(PATH_GET_LIST_OF_AGENCIES + "?" + VALID_POSTCODE_QUERY + "&" + + "consultingType=").accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isBadRequest()); + } + + @Test + void getAgenciesByConsultingType_Should_ReturnBadRequest_When_consultingTypeIsInvalid() + throws Exception { + mvc.perform( + get(PATH_GET_AGENCIES_BY_CONSULTINGTYPE.replace("1", "invalid")) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isBadRequest()); + } + + @Test + void getAgenciesByConsultingType_Should_ReturnOk_When_consultingTypeIsValid() + throws Exception { + mvc.perform( + get(PATH_GET_AGENCIES_BY_CONSULTINGTYPE) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()); + } + + @Test + void getAgenciesTopics_Should_ReturnNoContent_When_ServiceReturnsEmptyList() throws Exception { + + when(agencyService.getAgenciesTopics()) + .thenReturn(null); + + mvc.perform( + get(PATH_GET_LIST_OF_AGENCIES_TOPICS) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isNoContent()); + } + + @Test + void getAgenciesTopics_Should_ReturnListAndOk_When_ServiceReturnsList() throws Exception { + + List agenciesTopics = new ArrayList<>(); + agenciesTopics.add(AGENCY_TOPICS_DTO); + + when(topicEnrichmentService.enrichTopicIdsWithTopicData(anyList())) + .thenReturn(agenciesTopics); + + mvc.perform( + get(PATH_GET_LIST_OF_AGENCIES_TOPICS)) + .andExpect(status().isOk()); + + verify(topicEnrichmentService, atLeastOnce()).enrichTopicIdsWithTopicData(anyList()); + } + + @Test + void getTenantAgencies_Should_ReturnNoContent_When_ServiceReturnsEmptyList() throws Exception { + + when(agencyService.getAgencies(anyString(), anyInt())) + .thenReturn(null); + + mvc.perform( + get(PATH_GET_LIST_OF_AGENCIES_BY_TENANT + "?" + VALID_POSTCODE_QUERY + "&" + + VALID_TOPIC_ID_QUERY) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isNoContent()); + } + + @Test + void getTenantAgencies_Should_ReturnBadRequest_When_PostcodeParamIsInvalid() throws Exception { + + mvc.perform( + get(PATH_GET_LIST_OF_AGENCIES_BY_TENANT + "?" + INVALID_POSTCODE_QUERY + "&" + + VALID_TOPIC_ID_QUERY) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isBadRequest()); + } + + @Test + void getTenantAgencies_Should_ReturnBadRequest_When_topicIdParamIsNotProvided() + throws Exception { + + mvc.perform(get(PATH_GET_LIST_OF_AGENCIES_BY_TENANT + "?" + VALID_POSTCODE_QUERY) + .accept(MediaType.APPLICATION_JSON)).andExpect(status().isBadRequest()); + } + + @Test + void getTenantAgencies_Should_ReturnBadRequest_When_PostCodeParamIsNotProvided() + throws Exception { + + mvc.perform(get(PATH_GET_LIST_OF_AGENCIES_BY_TENANT + "?" + VALID_TOPIC_ID_QUERY) + .accept(MediaType.APPLICATION_JSON)).andExpect(status().isBadRequest()); + } + + @Test + void getTenantAgencies_Should_ReturnListAndOk_When_ServiceReturnsList() throws Exception { + + List agencies = new ArrayList<>(); + agencies.add(FULL_AGENCY_RESPONSE_DTO); + + when(agencyService.getAgencies(anyString(), anyInt())) + .thenReturn(agencies); + + mvc.perform( + get(PATH_GET_LIST_OF_AGENCIES_BY_TENANT + "?" + VALID_POSTCODE_QUERY + "&" + + VALID_TOPIC_ID_QUERY) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("[0].name").value(AGENCY_RESPONSE_DTO.getName())); + + verify(agencyService, atLeastOnce()).getAgencies(anyString(), anyInt()); + } + +} diff --git a/src/test/java/de/caritas/cob/agencyservice/api/controller/AgencyControllerWithDemographicsIT.java b/src/test/java/de/caritas/cob/agencyservice/api/controller/AgencyControllerWithDemographicsIT.java index 5d09e747..51da6be5 100644 --- a/src/test/java/de/caritas/cob/agencyservice/api/controller/AgencyControllerWithDemographicsIT.java +++ b/src/test/java/de/caritas/cob/agencyservice/api/controller/AgencyControllerWithDemographicsIT.java @@ -15,6 +15,7 @@ import de.caritas.cob.agencyservice.api.exception.MissingConsultingTypeException; import de.caritas.cob.agencyservice.api.manager.consultingtype.ConsultingTypeManager; +import de.caritas.cob.agencyservice.api.service.TopicEnrichmentService; import de.caritas.cob.agencyservice.api.tenant.TenantContext; import jakarta.transaction.Transactional; @@ -28,7 +29,6 @@ import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.TestPropertySource; import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; @@ -54,6 +54,9 @@ public void setup() { @MockBean private ConsultingTypeManager consultingTypeManager; + @MockBean + private TopicEnrichmentService topicEnrichmentService; + @Autowired private WebApplicationContext context; diff --git a/src/test/java/de/caritas/cob/agencyservice/api/controller/AgencyControllerWithSingleDomainMultitenancyIT.java b/src/test/java/de/caritas/cob/agencyservice/api/controller/AgencyControllerWithSingleDomainMultitenancyIT.java index 517f5cce..be2d2b24 100644 --- a/src/test/java/de/caritas/cob/agencyservice/api/controller/AgencyControllerWithSingleDomainMultitenancyIT.java +++ b/src/test/java/de/caritas/cob/agencyservice/api/controller/AgencyControllerWithSingleDomainMultitenancyIT.java @@ -2,8 +2,6 @@ import static de.caritas.cob.agencyservice.testHelper.PathConstants.PATH_GET_AGENCIES_WITH_IDS; import static de.caritas.cob.agencyservice.testHelper.PathConstants.PATH_GET_LIST_OF_AGENCIES; -import static de.caritas.cob.agencyservice.testHelper.TestConstants.VALID_AGE_QUERY; -import static de.caritas.cob.agencyservice.testHelper.TestConstants.VALID_GENDER_QUERY; import static org.hamcrest.Matchers.hasSize; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.Mockito.when; @@ -14,6 +12,7 @@ import de.caritas.cob.agencyservice.api.exception.MissingConsultingTypeException; import de.caritas.cob.agencyservice.api.manager.consultingtype.ConsultingTypeManager; +import de.caritas.cob.agencyservice.api.service.TopicEnrichmentService; import de.caritas.cob.agencyservice.api.tenant.TenantContext; import de.caritas.cob.agencyservice.applicationsettingsservice.generated.ApiClient; import de.caritas.cob.agencyservice.applicationsettingsservice.generated.web.model.ApplicationSettingsDTO; @@ -32,7 +31,6 @@ import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.TestPropertySource; import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.transaction.annotation.Transactional; @@ -63,6 +61,9 @@ public void setup() { @MockBean private ConsultingTypeManager consultingTypeManager; + @MockBean + private TopicEnrichmentService topicEnrichmentService; + @MockBean private ApplicationSettingsApiControllerFactory applicationSettingsApiControllerFactory; @MockBean diff --git a/src/test/java/de/caritas/cob/agencyservice/api/service/AgencyServiceIT.java b/src/test/java/de/caritas/cob/agencyservice/api/service/AgencyServiceIT.java index dafcdb23..7da11a80 100644 --- a/src/test/java/de/caritas/cob/agencyservice/api/service/AgencyServiceIT.java +++ b/src/test/java/de/caritas/cob/agencyservice/api/service/AgencyServiceIT.java @@ -36,4 +36,13 @@ public void getAgenciesByConsultingType_Should_returnResults_When_ConsultingType super.getAgenciesByConsultingType_Should_returnResults_When_ConsultingTypeIsValid(); } + @Test + public void getAgenciesTopics_Should_ReturnResults_When_topics_exist() { + super.getAgenciesTopics_Should_ReturnResults_When_topics_exist(); + } + + @Test + public void getAgencies_Should_returnMatchingAgencies_When_postcodeAndTopicIdIsGiven() { + super.getAgencies_Should_returnMatchingAgencies_When_postcodeAndTopicIdIsGiven(); + } } diff --git a/src/test/java/de/caritas/cob/agencyservice/api/service/AgencyServiceITBase.java b/src/test/java/de/caritas/cob/agencyservice/api/service/AgencyServiceITBase.java index f51fdb95..0f39ddeb 100644 --- a/src/test/java/de/caritas/cob/agencyservice/api/service/AgencyServiceITBase.java +++ b/src/test/java/de/caritas/cob/agencyservice/api/service/AgencyServiceITBase.java @@ -32,6 +32,8 @@ public class AgencyServiceITBase { private AgencyRepository agencyRepository; @MockBean private ConsultingTypeManager consultingTypeManager; + @MockBean + private TopicEnrichmentService topicEnrichmentService; public void getAgencies_Should_returnMatchingAgencies_When_postcodeAndConsultingTypeIsGiven() throws MissingConsultingTypeException { @@ -64,4 +66,22 @@ public void getAgenciesByConsultingType_Should_returnResults_When_ConsultingType assertThat(agencies, hasSize(greaterThan(0))); } + public void getAgenciesTopics_Should_ReturnResults_When_topics_exist() { + List topicIds = this.agencyService.getAgenciesTopics(); + + assertThat(topicIds, hasSize(greaterThan(0))); + } + + public void getAgencies_Should_returnMatchingAgencies_When_postcodeAndTopicIdIsGiven() { + + String postCode = "45501"; + Integer topicId = 1; + + List resultAgencies = agencyService.getAgencies(postCode, topicId); + + assertThat(resultAgencies, hasSize(1)); + FullAgencyResponseDTO resultAgency = resultAgencies.get(0); + assertThat(resultAgency.getId(), is(14352L)); + } + } diff --git a/src/test/java/de/caritas/cob/agencyservice/api/service/AgencyServiceTest.java b/src/test/java/de/caritas/cob/agencyservice/api/service/AgencyServiceTest.java index e4aa639b..11882d69 100644 --- a/src/test/java/de/caritas/cob/agencyservice/api/service/AgencyServiceTest.java +++ b/src/test/java/de/caritas/cob/agencyservice/api/service/AgencyServiceTest.java @@ -3,6 +3,7 @@ import static de.caritas.cob.agencyservice.testHelper.TestConstants.AGENCY_ID; import static de.caritas.cob.agencyservice.testHelper.TestConstants.AGENCY_IDS_LIST; import static de.caritas.cob.agencyservice.testHelper.TestConstants.AGENCY_LIST; +import static de.caritas.cob.agencyservice.testHelper.TestConstants.AGENCY_LIST_WITH_TOPICS; import static de.caritas.cob.agencyservice.testHelper.TestConstants.AGENCY_OFFLINE; import static de.caritas.cob.agencyservice.testHelper.TestConstants.AGENCY_ONLINE_U25; import static de.caritas.cob.agencyservice.testHelper.TestConstants.AGENCY_RESPONSE_DTO; @@ -15,6 +16,7 @@ import static de.caritas.cob.agencyservice.testHelper.TestConstants.EMPTY_AGENCY_LIST; import static de.caritas.cob.agencyservice.testHelper.TestConstants.FIELD_AGENCY_ID; import static de.caritas.cob.agencyservice.testHelper.TestConstants.POSTCODE; +import static de.caritas.cob.agencyservice.testHelper.TestConstants.TOPIC_ID_LIST; import static de.caritas.cob.agencyservice.testHelper.TestConstants.VALID_POSTCODE; import static de.caritas.cob.agencyservice.testHelper.TestConstants.VALID_POSTCODE_LENGTH; import static org.assertj.core.api.Assertions.assertThat; @@ -46,8 +48,10 @@ import de.caritas.cob.agencyservice.consultingtypeservice.generated.web.model.RegistrationDTO; import de.caritas.cob.agencyservice.tenantservice.generated.web.model.RestrictedTenantDTO; import de.caritas.cob.agencyservice.tenantservice.generated.web.model.Settings; +import io.swagger.models.auth.In; import java.util.ArrayList; import java.util.Collections; +import java.util.List; import java.util.Optional; import javax.swing.text.html.Option; import org.hamcrest.collection.IsEmptyCollection; @@ -333,4 +337,45 @@ public void getAgencies_Should_searchByTopicId_When_TopicIdProvidedAndFeatureEna verify(agencyRepository).searchWithTopic("12123", 5, 1, 2, AGE, GENDER, COUNSELLING_RELATION, TENANT_ID); } + + @Test + public void getAgenciesTopics_Should_ReturnListOfTopicIds_When_topicsExist() { + + when(agencyRepository.findAllAgenciesTopics(TenantContext.getCurrentTenant())) + .thenReturn(TOPIC_ID_LIST); + + Integer result = agencyService.getAgenciesTopics().get(0); + + assertEquals(TOPIC_ID_LIST.get(0), result); + } + + @Test + public void getAgenciesTopics_Should_ReturnEmptyListOfTopicIds_When_topicsDontExist() { + + when(agencyRepository.findAllAgenciesTopics(TenantContext.getCurrentTenant())) + .thenReturn(new ArrayList<>()); + + List result = agencyService.getAgenciesTopics(); + + assertThat(result).isEmpty(); + } + + @Test + public void getAgencies_WithOnlyPostCodeAndTopicId_Should_ReturnEmptyList_When_NoAgencyFound() { + + assertThat(agencyService.getAgencies("99999", 1), + IsEmptyCollection.empty()); + } + + @Test + public void getAgencies_WithOnlyPostCodeAndTopicId_Should_ReturnResultList_When_AgencyFound() { + + when(agencyRepository.searchWithTopic("99999", 5, null, + 1, null, null, null, null)) + .thenReturn(AGENCY_LIST_WITH_TOPICS); + + var result = agencyService.getAgencies("99999", 1); + + assertThat(result).isNotEmpty(); + } } diff --git a/src/test/java/de/caritas/cob/agencyservice/api/service/CentralDataProtectionTemplateServiceTest.java b/src/test/java/de/caritas/cob/agencyservice/api/service/CentralDataProtectionTemplateServiceTest.java index b1ce330c..7bf4a70a 100644 --- a/src/test/java/de/caritas/cob/agencyservice/api/service/CentralDataProtectionTemplateServiceTest.java +++ b/src/test/java/de/caritas/cob/agencyservice/api/service/CentralDataProtectionTemplateServiceTest.java @@ -38,6 +38,9 @@ class CentralDataProtectionTemplateServiceTest { @Autowired CentralDataProtectionTemplateService centralDataProtectionTemplateService; + @MockBean + private TopicEnrichmentService topicEnrichmentService; + @MockBean TenantService tenantService; @@ -313,4 +316,4 @@ private AgencyContextDTO getAgencyContext() { } -} \ No newline at end of file +} diff --git a/src/test/java/de/caritas/cob/agencyservice/api/service/TopicEnrichmentServiceTest.java b/src/test/java/de/caritas/cob/agencyservice/api/service/TopicEnrichmentServiceTest.java new file mode 100644 index 00000000..e981ae98 --- /dev/null +++ b/src/test/java/de/caritas/cob/agencyservice/api/service/TopicEnrichmentServiceTest.java @@ -0,0 +1,87 @@ +package de.caritas.cob.agencyservice.api.service; + +import static com.google.common.collect.Lists.newArrayList; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; + +import de.caritas.cob.agencyservice.api.model.AgencyTopicsDTO; +import java.util.List; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import de.caritas.cob.agencyservice.topicservice.generated.web.model.TopicDTO; + +@ExtendWith(MockitoExtension.class) +class TopicEnrichmentServiceTest { + + @InjectMocks + TopicEnrichmentService topicEnrichmentService; + + @Mock + TopicService topicService; + + @Test + void enrichTopicIdsWithTopicData_Should_EnrichAgencyWithTopicDataFromTopicService() { + // given + when(topicService.getAllTopics()).thenReturn( + newArrayList(new TopicDTO().id(1L).name("first topic").description("desc"), + new TopicDTO().id(2L).name("second topic").description("desc"))); + List topicIds = newArrayList(1, 2); + + // when + var result = topicEnrichmentService.enrichTopicIdsWithTopicData(topicIds); + + // then + assertThat(result).isNotEmpty(); + assertThat(result) + .extracting(AgencyTopicsDTO::getName) + .contains("first topic", "second topic"); + } + + + @Test + void enrichTopicIdsWithTopicData_Should_ReturnEmptyListIfNoTopicsAreDefined() { + // given + when(topicService.getAllTopics()).thenReturn( + newArrayList()); + List topicIds = newArrayList(1, 2); + + // when + var result = topicEnrichmentService.enrichTopicIdsWithTopicData(topicIds); + + // then + assertThat(result).isEmpty(); + } + + + @Test + void enrichTopicIdsWithTopicData_Should_ReturnEmptyListIfTopicsListIsNull() { + // given + when(topicService.getAllTopics()).thenReturn( + null); + List topicIds = newArrayList(1, 2); + + // when + var result = topicEnrichmentService.enrichTopicIdsWithTopicData(topicIds); + + // then + assertThat(result).isEmpty(); + } + + @Test + void enrichTopicIdsWithTopicData_Should_ReturnEmptyListIfTopicIdDoNotMatch() { + // given + when(topicService.getAllTopics()).thenReturn( + newArrayList(new TopicDTO().id(3L).name("third topic").description("desc"), + new TopicDTO().id(4L).name("fourth topic").description("desc"))); + List topicIds = newArrayList(1, 2); + + // when + var result = topicEnrichmentService.enrichTopicIdsWithTopicData(topicIds); + + // then + assertThat(result).isEmpty(); + } +} diff --git a/src/test/java/de/caritas/cob/agencyservice/testHelper/PathConstants.java b/src/test/java/de/caritas/cob/agencyservice/testHelper/PathConstants.java index d12b5836..49fe25ab 100644 --- a/src/test/java/de/caritas/cob/agencyservice/testHelper/PathConstants.java +++ b/src/test/java/de/caritas/cob/agencyservice/testHelper/PathConstants.java @@ -13,6 +13,8 @@ public class PathConstants { public static final String PAGE_PARAM = "page"; public static final String PER_PAGE_PARAM = "perPage"; public static final String PATH_GET_LIST_OF_AGENCIES = "/agencies"; + public static final String PATH_GET_LIST_OF_AGENCIES_BY_TENANT = "/agencies/by-tenant"; + public static final String PATH_GET_LIST_OF_AGENCIES_TOPICS = "/agencies/topics"; public static final String PATH_GET_AGENCIES_WITH_IDS = "/agencies/"; public static final String GET_AGENCY_PATH = ROOT_PATH + "/agencies"; } diff --git a/src/test/java/de/caritas/cob/agencyservice/testHelper/TestConstants.java b/src/test/java/de/caritas/cob/agencyservice/testHelper/TestConstants.java index 6854ceca..ccc757aa 100644 --- a/src/test/java/de/caritas/cob/agencyservice/testHelper/TestConstants.java +++ b/src/test/java/de/caritas/cob/agencyservice/testHelper/TestConstants.java @@ -3,8 +3,10 @@ import de.caritas.cob.agencyservice.api.manager.consultingtype.registration.Registration; import de.caritas.cob.agencyservice.api.manager.consultingtype.whiteSpot.WhiteSpot; import de.caritas.cob.agencyservice.api.model.AgencyResponseDTO; +import de.caritas.cob.agencyservice.api.model.AgencyTopicsDTO; import de.caritas.cob.agencyservice.api.model.FullAgencyResponseDTO; import de.caritas.cob.agencyservice.api.repository.agency.Agency; +import de.caritas.cob.agencyservice.api.repository.agencytopic.AgencyTopic; import de.caritas.cob.agencyservice.consultingtypeservice.generated.web.model.ExtendedConsultingTypeResponseDTO; import java.util.ArrayList; import java.util.Collections; @@ -148,6 +150,7 @@ public class TestConstants { public static final String VALID_POSTCODE_6 = "33445"; public static final String AGENCY_CITY = "Test city"; public static final String VALID_POSTCODE_QUERY = "postcode=88488"; + public static final String VALID_TOPIC_ID_QUERY = "topicId=1"; public static final int VALID_POSTCODE_LENGTH = 5; public static final Long AGENCY_ID = 98L; public static final String AGENCY_NAME = "Test agency"; @@ -164,6 +167,28 @@ public class TestConstants { .isExternal(false) .build(); + public static final Long TOPIC_ID = 1L; + + public static final AgencyTopic AGENCY_TOPIC = AgencyTopic.builder() + .topicId(TOPIC_ID) + .agency(AGENCY_SUCHT) + .build(); + + public static final Agency AGENCY_SEARCH_WITH_TOPICS = Agency.builder() + .id(AGENCY_ID) + .name(AGENCY_NAME) + .description(AGENCY_DESCRIPTION) + .postCode(POSTCODE) + .city("Test city") + .teamAgency(false) + .consultingTypeId(CONSULTING_TYPE_SUCHT) + .offline(false) + .isExternal(false) + .agencyTopics(Collections.singletonList(AGENCY_TOPIC)) + .build(); + + public static final Integer TOPIC_SUCHT = 1; + public static final Agency AGENCY_KREUZBUND = new Agency(AGENCY_ID, AGENCY_NAME, AGENCY_DESCRIPTION, POSTCODE, "Test city", false, CONSULTING_TYPE_KREUZBUND, false, null, false, null, null, null, @@ -185,6 +210,8 @@ public class TestConstants { new FullAgencyResponseDTO().id(AGENCY_ID).name(AGENCY_NAME).postcode(POSTCODE) .city(AGENCY_CITY).description(AGENCY_DESCRIPTION).teamAgency(false).offline(false) .consultingType(CONSULTING_TYPE_SUCHT).url(null).external(false); + public static final AgencyTopicsDTO AGENCY_TOPICS_DTO = + new AgencyTopicsDTO().id(AGENCY_ID).name(AGENCY_NAME); public static final List FULL_AGENCY_RESPONSE_DTO_LIST = Collections.singletonList( FULL_AGENCY_RESPONSE_DTO); public static final int MIN_POSTCODE_SIZE_3 = 3; @@ -200,6 +227,9 @@ public class TestConstants { public static final List EMPTY_AGENCY_LIST = new ArrayList<>(); public static final List AGENCY_LIST = Collections.singletonList(AGENCY_SUCHT); + public static final List AGENCY_LIST_WITH_TOPICS = Collections.singletonList( + AGENCY_SEARCH_WITH_TOPICS); + public static final List TOPIC_ID_LIST = Collections.singletonList(TOPIC_SUCHT); public static final List AGENCY_IDS_LIST = Collections.singletonList(AGENCY_ID); public static final String VALID_CONSULTING_TYPE_QUERY = "consultingType=0";