Skip to content

Commit

Permalink
Merge pull request #90 from virtualidentityag/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
mebo4b authored Jun 30, 2022
2 parents 3f3ee0a + 60fb55e commit 3bd1c54
Show file tree
Hide file tree
Showing 8 changed files with 323 additions and 10 deletions.
132 changes: 129 additions & 3 deletions services/tenantservice.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,30 @@ paths:
$ref: '#/components/schemas/TenantDTO'
400:
description: BAD REQUEST - invalid/incomplete request or body object
401:
description: UNAUTHORIZED - no/invalid Keycloak token
500:
description: INTERNAL SERVER ERROR - server encountered unexpected condition
get:
tags:
- tenant-controller
summary: 'Get all tenants'
operationId: getAllTenants
responses:
200:
description: OK - successful operation
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/BasicTenantLicensingDTO'
204:
description: NO CONTENT - no content found
400:
description: BAD REQUEST - invalid/incomplete request or body object
401:
description: UNAUTHORIZED - no/invalid Keycloak token
500:
description: INTERNAL SERVER ERROR - server encountered unexpected condition
/tenant/{id}:
Expand All @@ -51,10 +75,12 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/TenantDTO'
404:
description: Not found
400:
description: BAD REQUEST - invalid/incomplete request or body object
401:
description: UNAUTHORIZED - no/invalid Keycloak token
404:
description: Not found
500:
description: INTERNAL SERVER ERROR - server encountered unexpected condition
put:
Expand Down Expand Up @@ -82,6 +108,8 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/TenantDTO'
401:
description: UNAUTHORIZED - no/invalid Keycloak token
409:
description: CONFLICT - unique constraint validation fails
500:
Expand Down Expand Up @@ -114,6 +142,55 @@ paths:
description: BAD REQUEST - invalid/incomplete request or body object
500:
description: INTERNAL SERVER ERROR - server encountered unexpected condition
/tenant/public/id/{tenantId}:
summary: 'Represents a publicly allowed tenant data'
description: This resource represents an individual tenant in a system.
get:
tags:
- tenant-controller
summary: 'Gets a tenant public information [Authorization: no-auth]'
operationId: getRestrictedTenantDataByTenantId
parameters:
- name: tenantId
in: path
description: Tenant ID
required: true
schema:
type: long
responses:
200:
description: Successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/RestrictedTenantDTO'
404:
description: Not found
400:
description: BAD REQUEST - invalid/incomplete request or body object
500:
description: INTERNAL SERVER ERROR - server encountered unexpected condition
/tenant/public/single:
summary: 'Represents a publicly allowed tenant data'
description: This resource represents the tenant in a single-tenant system.
get:
tags:
- tenant-controller
summary: 'Gets a tenant public information [Authorization: no-auth]'
operationId: getRestrictedSingleTenantData
responses:
200:
description: Successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/RestrictedTenantDTO'
404:
description: Not found
400:
description: BAD REQUEST - invalid/incomplete request or body object
500:
description: INTERNAL SERVER ERROR - server encountered unexpected condition
components:
schemas:
TenantDTO:
Expand All @@ -129,7 +206,7 @@ components:
name:
type: string
example: "Company name AG"
maxLength: 100
maxLength: 40
subdomain:
type: string
example: "companyname"
Expand All @@ -146,6 +223,33 @@ components:
content:
$ref:
'#/components/schemas/Content'
settings:
$ref:
'#/components/schemas/Settings'
BasicTenantLicensingDTO:
type: object
required:
- id
- name
- subdomain
properties:
id:
type: long
example: 12132
name:
type: string
example: "Company name AG"
maxLength: 40
subdomain:
type: string
example: "companyname"
maxLength: 100
createDate:
type: string
updateDate:
type: string
licensing:
$ref: '#/components/schemas/Licensing'
RestrictedTenantDTO:
type: object
required:
Expand All @@ -158,13 +262,20 @@ components:
name:
type: string
example: "Company name AG"
maxLength: 40
subdomain:
type: string
example: "subdomain"
maxLength: 100
theming:
$ref:
'#/components/schemas/Theming'
content:
$ref:
'#/components/schemas/Content'
settings:
$ref:
'#/components/schemas/Settings'
Licensing:
type: object
required:
Expand Down Expand Up @@ -199,3 +310,18 @@ components:
claim:
type: string
example: "Llorem ipsum..."
maxLength: 40
privacy:
type: string
example: "Llorem ipsum..."
termsAndConditions:
type: string
example: "Llorem ipsum..."
Settings:
type: object
required:
- impressum
properties:
topicsInRegistrationEnabled:
type: boolean
example: "false"
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.caritas.cob.agencyservice.api.admin.service.agency;

import com.google.common.collect.Maps;
import de.caritas.cob.agencyservice.api.model.TopicDTO;
import de.caritas.cob.agencyservice.api.repository.agency.Agency;
import de.caritas.cob.agencyservice.api.repository.agencytopic.AgencyTopic;
Expand All @@ -26,7 +27,7 @@ public Agency enrichAgencyWithTopics(Agency agency) {
var availableTopics = getAvailableTopicsMap();
var agencyTopics = agency.getAgencyTopics();
log.debug("Enriching agency with {} with information about the topics", agency.getId());
log.debug("Available topics list has size: {} ", agencyTopics.size());
log.debug("Available topics list has size: {} ", availableTopics.size());
for (AgencyTopic agencyTopic : agencyTopics) {
enrichSingleAgencyTopic(availableTopics, agencyTopic);
}
Expand All @@ -47,12 +48,22 @@ private void enrichSingleAgencyTopic(Map<Long, TopicDTO> availableTopics,

private Map<Long, TopicDTO> getAvailableTopicsMap() {
var allTopics = topicService.getAllTopics();
return isEmptyOrNull(allTopics) ? Maps.newHashMap() : getAvailableTopicsMap(allTopics);
}

private Map<Long, TopicDTO> getAvailableTopicsMap(
List<de.caritas.cob.agencyservice.topicservice.generated.web.model.TopicDTO> allTopics) {
return allTopics.stream()
.collect(Collectors.toMap(
de.caritas.cob.agencyservice.topicservice.generated.web.model.TopicDTO::getId,
this::convertToAgencyServiceTopicViewDTO));
}

private boolean isEmptyOrNull(
List<de.caritas.cob.agencyservice.topicservice.generated.web.model.TopicDTO> allTopics) {
return allTopics == null || allTopics.isEmpty();
}

private TopicDTO convertToAgencyServiceTopicViewDTO(
de.caritas.cob.agencyservice.topicservice.generated.web.model.TopicDTO source) {
return new TopicDTO().id(source.getId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class AgencyController implements AgenciesApi {
*
* @param postcode the postcode for regarding agencies
* @param consultingType the type used to filter the agencies
* @param topicId the (optional) main topicId to filter the agencies
* @return the List of agencies with information
*/
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
import de.caritas.cob.agencyservice.api.model.FullAgencyResponseDTO;
import de.caritas.cob.agencyservice.api.repository.agency.Agency;
import de.caritas.cob.agencyservice.api.repository.agency.AgencyRepository;
import de.caritas.cob.agencyservice.consultingtypeservice.generated.web.model.ExtendedConsultingTypeResponseDTO;
import de.caritas.cob.agencyservice.api.tenant.TenantContext;
import de.caritas.cob.agencyservice.consultingtypeservice.generated.web.model.ExtendedConsultingTypeResponseDTO;
import de.caritas.cob.agencyservice.tenantservice.generated.web.model.RestrictedTenantDTO;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Collections;
Expand All @@ -36,10 +37,14 @@ public class AgencyService {

private final @NonNull ConsultingTypeManager consultingTypeManager;
private final @NonNull AgencyRepository agencyRepository;
private final @NonNull TenantService tenantService;

@Value("${feature.topics.enabled}")
private boolean topicsFeatureEnabled;

@Value("${multitenancy.enabled}")
private boolean multitenancy;

/**
* Returns a list of {@link AgencyResponseDTO} which match the provided agencyIds.
*
Expand Down Expand Up @@ -125,8 +130,21 @@ private boolean isTopicFeatureEnabledAndActivatedInRegistration() {
}

private boolean isTopicFeatureActivatedInRegistration() {
// TODO get this flag from the tenant service, implement within VIC-797
return true;
RestrictedTenantDTO restrictedTenantDTO = getRestrictedTenantData();
if (nonNull(restrictedTenantDTO) && nonNull(restrictedTenantDTO.getSettings())) {
return Boolean.TRUE.equals(
restrictedTenantDTO.getSettings().getTopicsInRegistrationEnabled());
}
return false;
}

private RestrictedTenantDTO getRestrictedTenantData() {
if (multitenancy) {
Long tenantId = TenantContext.getCurrentTenant();
return tenantService.getRestrictedTenantDataByTenantId(tenantId);
} else {
return tenantService.getRestrictedTenantDataForSingleTenant();
}
}

private void verifyConsultingTypeExists(int consultingTypeId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import de.caritas.cob.agencyservice.tenantservice.generated.web.model.RestrictedTenantDTO;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

Expand All @@ -14,10 +15,21 @@ public class TenantService {

private final @NonNull TenantControllerApi tenantControllerApi;

@Value("${multitenancy.enabled}")
private boolean multitenancy;

@Cacheable(cacheNames = CacheManagerConfig.TENANT_CACHE, key = "#subdomain")
public RestrictedTenantDTO getRestrictedTenantDataBySubdomain(String subdomain) {
return tenantControllerApi.getRestrictedTenantDataBySubdomainWithHttpInfo(subdomain).getBody();
}

@Cacheable(cacheNames = CacheManagerConfig.TENANT_CACHE, key = "#tenantId")
public RestrictedTenantDTO getRestrictedTenantDataByTenantId(Long tenantId) {
return tenantControllerApi.getRestrictedTenantDataByTenantId(tenantId);
}

@Cacheable(cacheNames = CacheManagerConfig.TENANT_CACHE)
public RestrictedTenantDTO getRestrictedTenantDataForSingleTenant() {
return tenantControllerApi.getRestrictedSingleTenantData();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,44 @@ void enrichAgencyWithTopics_Should_EnrichAgencyWithTopicDataFromTopicService() {
.contains("first topic", "second topic");
}


@Test
void enrichAgencyWithTopics_Should_NotEnrichIfNoTopicsAreDefined() {
// given
when(topicService.getAllTopics()).thenReturn(
newArrayList());
var agencyTopics = newArrayList(
createAgencyTopic(new Agency(), 1L), createAgencyTopic(new Agency(), 2L));

// when
var agency = newAgencyWithTopics(agencyTopics);
agencyTopicEnrichmentService.enrichAgencyWithTopics(agency);

// then
assertThat(agency.getAgencyTopics())
.extracting(agencyTopic -> agencyTopic.getTopicData().getName())
.containsOnlyNulls();
}


@Test
void enrichAgencyWithTopics_Should_NotEnrichIfTopicsListIsNull() {
// given
when(topicService.getAllTopics()).thenReturn(
null);
var agencyTopics = newArrayList(
createAgencyTopic(new Agency(), 1L), createAgencyTopic(new Agency(), 2L));

// when
var agency = newAgencyWithTopics(agencyTopics);
agencyTopicEnrichmentService.enrichAgencyWithTopics(agency);

// then
assertThat(agency.getAgencyTopics())
.extracting(agencyTopic -> agencyTopic.getTopicData().getName())
.containsOnlyNulls();
}

@Test
void enrichAgencyWithTopics_Should_NotEnrichWithTopicDataIfTopicIdDoNotMatch() {
// given
Expand Down
Loading

0 comments on commit 3bd1c54

Please sign in to comment.