Skip to content

Commit

Permalink
Merge pull request #116 from Onlineberatung/develop
Browse files Browse the repository at this point in the history
[pull] develop from Onlineberatung:develop
  • Loading branch information
tkuzynow authored Jan 10, 2024
2 parents 14d4865 + 3b544d9 commit 6a1e923
Show file tree
Hide file tree
Showing 10 changed files with 288 additions and 52 deletions.
3 changes: 3 additions & 0 deletions api/agencyadminservice.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,9 @@ components:
items:
type: string
enum: [ RELATIVE_COUNSELLING, SELF_COUNSELLING, PARENTAL_COUNSELLING ]
dataProtection:
type: object
$ref: '#/components/schemas/DataProtectionDTO'

DemographicsDTO:
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.google.common.base.Joiner;
import de.caritas.cob.agencyservice.api.admin.service.agency.AgencyAdminFullResponseDTOBuilder;
import de.caritas.cob.agencyservice.api.admin.service.agency.AgencyTopicEnrichmentService;
import de.caritas.cob.agencyservice.api.admin.service.agency.DataProtectionConverter;
import de.caritas.cob.agencyservice.api.admin.service.agency.DemographicsConverter;
import de.caritas.cob.agencyservice.api.admin.validation.DeleteAgencyValidator;
import de.caritas.cob.agencyservice.api.exception.httpresponses.ConflictException;
Expand All @@ -26,7 +27,6 @@
import java.time.ZoneOffset;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -55,6 +55,9 @@ public class AgencyAdminService {
@Autowired(required = false)
private DemographicsConverter demographicsConverter;

@Autowired
private DataProtectionConverter dataProtectionConverter;

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

Expand Down Expand Up @@ -207,6 +210,10 @@ private Agency mergeAgencies(Agency agency, UpdateAgencyDTO updateAgencyDTO) {
.counsellingRelations(agency.getCounsellingRelations())
.deleteDate(agency.getDeleteDate());

if (dataProtectionConverter != null) {
dataProtectionConverter.convertToEntity(updateAgencyDTO.getDataProtection(), agencyBuilder);
}

if (nonNull(updateAgencyDTO.getConsultingType())) {
agencyBuilder.consultingTypeId(updateAgencyDTO.getConsultingType());
} else {
Expand Down Expand Up @@ -235,6 +242,8 @@ private Agency mergeAgencies(Agency agency, UpdateAgencyDTO updateAgencyDTO) {
}




/**
* Changes the type of the agency.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package de.caritas.cob.agencyservice.api.admin.service.agency;

import de.caritas.cob.agencyservice.api.model.DataProtectionDTO;
import de.caritas.cob.agencyservice.api.repository.agency.Agency.AgencyBuilder;
import de.caritas.cob.agencyservice.api.repository.agency.DataProtectionResponsibleEntity;
import de.caritas.cob.agencyservice.api.util.JsonConverter;
import org.springframework.stereotype.Component;

@Component
public class DataProtectionConverter {

public void convertToEntity(DataProtectionDTO dataProtectionDTO, AgencyBuilder agencyBuilder) {
if (dataProtectionDTO != null) {
agencyBuilder.dataProtectionResponsibleEntity(convertEnums(dataProtectionDTO));
agencyBuilder.dataProtectionOfficerContactData(
JsonConverter.convertToJson(dataProtectionDTO.getDataProtectionOfficerContact()));
agencyBuilder.dataProtectionAgencyResponsibleContactData(JsonConverter.convertToJson(
dataProtectionDTO.getAgencyDataProtectionResponsibleContact()));
agencyBuilder.dataProtectionAlternativeContactData(JsonConverter.convertToJson(
dataProtectionDTO.getAlternativeDataProtectionRepresentativeContact()));
} else {
nullifyDataProtectionAttributes(agencyBuilder);
}
}

private static void nullifyDataProtectionAttributes(AgencyBuilder agencyBuilder) {
agencyBuilder.dataProtectionResponsibleEntity(null);
agencyBuilder.dataProtectionOfficerContactData(null);
agencyBuilder.dataProtectionAgencyResponsibleContactData(null);
agencyBuilder.dataProtectionAlternativeContactData(null);
}

private static DataProtectionResponsibleEntity convertEnums(
DataProtectionDTO dataProtectionDTO) {
var dataProtectionEntity = dataProtectionDTO.getDataProtectionResponsibleEntity();
if (dataProtectionEntity == null) {
return null;
}
switch (dataProtectionEntity) {
case AGENCY_RESPONSIBLE:
return DataProtectionResponsibleEntity.AGENCY_RESPONSIBLE;
case DATA_PROTECTION_OFFICER:
return DataProtectionResponsibleEntity.DATA_PROTECTION_OFFICER;
case ALTERNATIVE_REPRESENTATIVE:
return DataProtectionResponsibleEntity.ALTERNATIVE_REPRESENTATIVE;
default:
throw new IllegalArgumentException(
"DataProtectionResponsibleEntity not supported: " + dataProtectionEntity);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import de.caritas.cob.agencyservice.api.model.DataProtectionDTO;
import de.caritas.cob.agencyservice.api.model.DataProtectionDTO.DataProtectionResponsibleEntityEnum;
import de.caritas.cob.agencyservice.api.repository.agency.Agency;
import de.caritas.cob.agencyservice.api.repository.agency.DataProtectionResponsibleEntity;
import de.caritas.cob.agencyservice.api.util.JsonConverter;
import java.util.function.Supplier;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;

Expand All @@ -13,60 +15,45 @@ public class DataProtectionDTOBuilder {

private final @NonNull Agency agency;


public DataProtectionDTO fromAgency() {
if (agency.getDataProtectionResponsibleEntity() == null) {
return null;
}

switch (this.agency.getDataProtectionResponsibleEntity()) {
case AGENCY_RESPONSIBLE:
return getAgencyResponsibleDataProtectionDTO();
case ALTERNATIVE_REPRESENTATIVE:
return getAlternativeRepresentative();
case DATA_PROTECTION_OFFICER:
return getDataProtectionOfficerDTO();
default:
return null;
}
return new DataProtectionDTO()
.dataProtectionResponsibleEntity(convertEnums(agency.getDataProtectionResponsibleEntity()))
.agencyDataProtectionResponsibleContact(
convertNullSafe(agency::getDataProtectionAgencyResponsibleContactData))
.alternativeDataProtectionRepresentativeContact(
convertNullSafe(agency::getDataProtectionAlternativeContactData))
.dataProtectionOfficerContact(
convertNullSafe(agency::getDataProtectionOfficerContactData));
}

private DataProtectionDTO getDataProtectionOfficerDTO() {
String dataProtectionOfficerContactData = this.agency.getDataProtectionOfficerContactData();
if (dataProtectionOfficerContactData == null) {
private DataProtectionContactDTO convertNullSafe(Supplier<String> dataProtectionDataSupplier) {
if (dataProtectionDataSupplier.get() == null) {
return null;
} else {
return JsonConverter.convertFromJson(dataProtectionDataSupplier.get());
}
DataProtectionContactDTO dataProtectionContactDTO = JsonConverter.convertFromJson(
dataProtectionOfficerContactData);
return new DataProtectionDTO()
.dataProtectionResponsibleEntity(
DataProtectionResponsibleEntityEnum.DATA_PROTECTION_OFFICER)
.dataProtectionOfficerContact(dataProtectionContactDTO);
}

private DataProtectionDTO getAlternativeRepresentative() {
String dataProtectionAlternativeContactData = this.agency.getDataProtectionAlternativeContactData();
if (dataProtectionAlternativeContactData == null) {
return null;
}
DataProtectionContactDTO dataProtectionContactDTO = JsonConverter.convertFromJson(
dataProtectionAlternativeContactData);

return new DataProtectionDTO()
.dataProtectionResponsibleEntity(
DataProtectionResponsibleEntityEnum.ALTERNATIVE_REPRESENTATIVE)
.alternativeDataProtectionRepresentativeContact(dataProtectionContactDTO);
}
private static DataProtectionResponsibleEntityEnum convertEnums(
DataProtectionResponsibleEntity dataProtectionEntity) {

private DataProtectionDTO getAgencyResponsibleDataProtectionDTO() {
String dataProtectionAgencyResponsibleContactData = this.agency.getDataProtectionAgencyResponsibleContactData();
if (dataProtectionAgencyResponsibleContactData == null) {
return null;
switch (dataProtectionEntity) {
case AGENCY_RESPONSIBLE:
return DataProtectionDTO.DataProtectionResponsibleEntityEnum.AGENCY_RESPONSIBLE;
case DATA_PROTECTION_OFFICER:
return DataProtectionDTO.DataProtectionResponsibleEntityEnum.DATA_PROTECTION_OFFICER;
case ALTERNATIVE_REPRESENTATIVE:
return DataProtectionDTO.DataProtectionResponsibleEntityEnum.ALTERNATIVE_REPRESENTATIVE;
default:
throw new IllegalArgumentException(
"DataProtectionResponsibleEntity not supported: " + dataProtectionEntity);
}
DataProtectionContactDTO dataProtectionContactDTO = JsonConverter.convertFromJson(
dataProtectionAgencyResponsibleContactData);
return new DataProtectionDTO()
.dataProtectionResponsibleEntity(DataProtectionResponsibleEntityEnum.AGENCY_RESPONSIBLE)
.agencyDataProtectionResponsibleContact(dataProtectionContactDTO);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import de.caritas.cob.agencyservice.tenantservice.generated.web.model.RestrictedTenantDTO;

Expand Down Expand Up @@ -66,16 +67,16 @@ protected Map<DataProtectionPlaceHolderType, String> renderDataProtectionPlaceho
var renderedDataProtectionOfficerContact = renderDataProtectionOfficerContactFromTemplate(
agency, restrictedTenantDataByTenantId.getContent().getDataProtectionContactTemplate());

if (renderedDataProtectionOfficerContact != null) {
result.put(DataProtectionPlaceHolderType.DATA_PROTECTION_OFFICER,
renderedDataProtectionOfficerContact);
}

result.put(DataProtectionPlaceHolderType.DATA_PROTECTION_OFFICER,
renderedDataProtectionOfficerContact != null ? renderedDataProtectionOfficerContact : StringUtils.EMPTY);

var renderedDataProtectionResponsible = renderDataProtectionResponsibleFromTemplate(
agency, restrictedTenantDataByTenantId.getContent().getDataProtectionContactTemplate());
if (renderedDataProtectionResponsible != null) {
result.put(DataProtectionPlaceHolderType.DATA_PROTECTION_RESPONSIBLE,
renderedDataProtectionResponsible);
}

result.put(DataProtectionPlaceHolderType.DATA_PROTECTION_RESPONSIBLE,
renderedDataProtectionResponsible != null ? renderedDataProtectionResponsible : StringUtils.EMPTY);

}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ private JsonConverter() {
}

public static String convertToJson(Object object) {
if (object == null) {
return null;
}
return serializeToJsonString(object);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import de.caritas.cob.agencyservice.api.admin.service.agency.AgencyTopicEnrichmentService;
import de.caritas.cob.agencyservice.api.admin.service.agency.DataProtectionConverter;
import de.caritas.cob.agencyservice.api.admin.service.agency.DemographicsConverter;
import de.caritas.cob.agencyservice.api.admin.validation.DeleteAgencyValidator;
import de.caritas.cob.agencyservice.api.exception.httpresponses.ConflictException;
Expand Down Expand Up @@ -74,6 +75,9 @@ class AgencyAdminServiceTest {
@Mock
DemographicsConverter demographicsConverter;

@Mock
DataProtectionConverter dataProtectionConverter;

@Mock
AppointmentService appointmentService;

Expand Down Expand Up @@ -107,7 +111,7 @@ void createAgency_Should_CreateAgencyAndAddDefaultCounsellingRelations() {
var agency = this.easyRandom.nextObject(Agency.class);
agency.setCounsellingRelations(null);
agency.setDataProtectionOfficerContactData(null);
agency.setDataProtectionResponsibleEntity(null);
clearDataProtection(agency);
var agencyDTO = this.easyRandom.nextObject(AgencyDTO.class);
agencyDTO.setCounsellingRelations(null);
agencyDTO.setConsultingType(1);
Expand All @@ -121,6 +125,7 @@ void createAgency_Should_CreateAgencyAndAddDefaultCounsellingRelations() {
@Test
void updateAgency_Should_SaveAgencyMandatoryChanges_WhenAgencyIsFound() {
var agency = this.easyRandom.nextObject(Agency.class);
clearDataProtection(agency);
DataProtectionContactDTO dataProtectionContactDTO = this.easyRandom.nextObject(DataProtectionContactDTO.class);
agency.setDataProtectionOfficerContactData(JsonConverter.convertToJson(dataProtectionContactDTO));
agency.setDataProtectionAlternativeContactData(null);
Expand All @@ -139,12 +144,21 @@ void updateAgency_Should_SaveAgencyMandatoryChanges_WhenAgencyIsFound() {
assertEquals(agency.getConsultingTypeId(), passedConsultingTypeId);
}

private void clearDataProtection(Agency agency) {
agency.setDataProtectionResponsibleEntity(null);
agency.setDataProtectionAgencyResponsibleContactData(null);
agency.setDataProtectionAlternativeContactData(null);
agency.setDataProtectionOfficerContactData(null);
}

@Test
void updateAgency_Should_SaveOptionalAgencyChanges_WhenAgencyIsFound() {
var agency = easyRandom.nextObject(Agency.class);
agency.setCounsellingRelations(AgencyAdminResponseDTO.CounsellingRelationsEnum.PARENTAL_COUNSELLING.getValue());
agency.setDataProtectionResponsibleEntity(DataProtectionResponsibleEntity.ALTERNATIVE_REPRESENTATIVE);
agency.setDataProtectionAlternativeContactData(JsonConverter.convertToJson(new DataProtectionContactDTO()));
agency.setDataProtectionOfficerContactData(null);
agency.setDataProtectionAgencyResponsibleContactData(null);
when(agencyRepository.findById(AGENCY_ID)).thenReturn(Optional.of(agency));
when(agencyRepository.save(any())).thenReturn(agency);

Expand All @@ -164,7 +178,7 @@ void updateAgency_Should_SaveAgencyChanges_WhenAgencyIsFoundAndTopicFeatureEnabl
// given
ReflectionTestUtils.setField(agencyAdminService, "featureTopicsEnabled", true);
var agency = this.easyRandom.nextObject(Agency.class);
agency.setDataProtectionResponsibleEntity(null);
clearDataProtection(agency);
agency.setCounsellingRelations(null);
when(agencyRepository.findById(AGENCY_ID)).thenReturn(Optional.of(agency));
when(agencyRepository.save(any())).thenReturn(agency);
Expand All @@ -185,6 +199,7 @@ void updateAgency_Should_SaveAgencyChanges_WhenAgencyIsFoundAndDemographicsFeatu
// given
ReflectionTestUtils.setField(agencyAdminService, "featureDemographicsEnabled", true);
var agency = this.easyRandom.nextObject(Agency.class);
clearDataProtection(agency);
agency.setDataProtectionAgencyResponsibleContactData(null);
agency.setDataProtectionResponsibleEntity(DataProtectionResponsibleEntity.AGENCY_RESPONSIBLE);
agency.setCounsellingRelations(AgencyAdminResponseDTO.CounsellingRelationsEnum.PARENTAL_COUNSELLING.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public void init() {
this.agency.setDataProtectionResponsibleEntity(DataProtectionResponsibleEntity.AGENCY_RESPONSIBLE);
this.agency.setDataProtectionAgencyResponsibleContactData(JsonConverter.convertToJson(new DataProtectionContactDTO()));
this.agency.setDataProtectionOfficerContactData(JsonConverter.convertToJson(new DataProtectionContactDTO()));
this.agency.setDataProtectionAlternativeContactData(JsonConverter.convertToJson(new DataProtectionContactDTO()));
this.agency.setTenantId(TENANT_ID);
this.agencyAdminFullResponseDTOBuilder = new AgencyAdminFullResponseDTOBuilder(agency);
this.agency.setCounsellingRelations(AgencyDTO.CounsellingRelationsEnum.PARENTAL_COUNSELLING.getValue() + "," + AgencyDTO.CounsellingRelationsEnum.RELATIVE_COUNSELLING.getValue());
Expand Down
Loading

0 comments on commit 6a1e923

Please sign in to comment.