From b20dd52614f72c9db1a9b348ee8ec5bdf0d3b1fc Mon Sep 17 00:00:00 2001 From: tkuzynow Date: Thu, 30 Nov 2023 17:36:14 +0100 Subject: [PATCH 1/6] fix: null handling for rendered template --- .../CentralDataProtectionTemplateService.java | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/main/java/de/caritas/cob/agencyservice/api/service/CentralDataProtectionTemplateService.java b/src/main/java/de/caritas/cob/agencyservice/api/service/CentralDataProtectionTemplateService.java index f050c374..c688d067 100644 --- a/src/main/java/de/caritas/cob/agencyservice/api/service/CentralDataProtectionTemplateService.java +++ b/src/main/java/de/caritas/cob/agencyservice/api/service/CentralDataProtectionTemplateService.java @@ -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; @@ -66,16 +67,16 @@ protected Map 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; } From 7d46d8a02ba178033482b17799b098865ffd5bff Mon Sep 17 00:00:00 2001 From: tkuzynow Date: Thu, 21 Dec 2023 15:18:54 +0100 Subject: [PATCH 2/6] feat: add possibility to persist data protection information --- api/agencyadminservice.yaml | 3 + .../api/admin/service/AgencyAdminService.java | 9 +- .../agency/DataProtectionConverter.java | 50 ++++++++ .../agencyservice/api/util/JsonConverter.java | 3 + .../agency/DataProtectionConverterTest.java | 112 ++++++++++++++++++ 5 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 src/main/java/de/caritas/cob/agencyservice/api/admin/service/agency/DataProtectionConverter.java create mode 100644 src/test/java/de/caritas/cob/agencyservice/api/admin/service/agency/DataProtectionConverterTest.java diff --git a/api/agencyadminservice.yaml b/api/agencyadminservice.yaml index 45abe22d..237dbf1c 100644 --- a/api/agencyadminservice.yaml +++ b/api/agencyadminservice.yaml @@ -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 diff --git a/src/main/java/de/caritas/cob/agencyservice/api/admin/service/AgencyAdminService.java b/src/main/java/de/caritas/cob/agencyservice/api/admin/service/AgencyAdminService.java index 598ebba9..d793df01 100644 --- a/src/main/java/de/caritas/cob/agencyservice/api/admin/service/AgencyAdminService.java +++ b/src/main/java/de/caritas/cob/agencyservice/api/admin/service/AgencyAdminService.java @@ -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; @@ -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; @@ -55,6 +55,9 @@ public class AgencyAdminService { @Autowired(required = false) private DemographicsConverter demographicsConverter; + @Autowired(required = false) + private DataProtectionConverter dataProtectionConverter; + @Value("${feature.topics.enabled}") private boolean featureTopicsEnabled; @@ -207,6 +210,8 @@ private Agency mergeAgencies(Agency agency, UpdateAgencyDTO updateAgencyDTO) { .counsellingRelations(agency.getCounsellingRelations()) .deleteDate(agency.getDeleteDate()); + dataProtectionConverter.convertToEntity(updateAgencyDTO.getDataProtection(), agencyBuilder); + if (nonNull(updateAgencyDTO.getConsultingType())) { agencyBuilder.consultingTypeId(updateAgencyDTO.getConsultingType()); } else { @@ -235,6 +240,8 @@ private Agency mergeAgencies(Agency agency, UpdateAgencyDTO updateAgencyDTO) { } + + /** * Changes the type of the agency. * diff --git a/src/main/java/de/caritas/cob/agencyservice/api/admin/service/agency/DataProtectionConverter.java b/src/main/java/de/caritas/cob/agencyservice/api/admin/service/agency/DataProtectionConverter.java new file mode 100644 index 00000000..3f564bec --- /dev/null +++ b/src/main/java/de/caritas/cob/agencyservice/api/admin/service/agency/DataProtectionConverter.java @@ -0,0 +1,50 @@ +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; + } + return null; + } + +} diff --git a/src/main/java/de/caritas/cob/agencyservice/api/util/JsonConverter.java b/src/main/java/de/caritas/cob/agencyservice/api/util/JsonConverter.java index a44eab8b..5d1c9686 100644 --- a/src/main/java/de/caritas/cob/agencyservice/api/util/JsonConverter.java +++ b/src/main/java/de/caritas/cob/agencyservice/api/util/JsonConverter.java @@ -14,6 +14,9 @@ private JsonConverter() { } public static String convertToJson(Object object) { + if (object == null) { + return null; + } return serializeToJsonString(object); } diff --git a/src/test/java/de/caritas/cob/agencyservice/api/admin/service/agency/DataProtectionConverterTest.java b/src/test/java/de/caritas/cob/agencyservice/api/admin/service/agency/DataProtectionConverterTest.java new file mode 100644 index 00000000..aa91c3e0 --- /dev/null +++ b/src/test/java/de/caritas/cob/agencyservice/api/admin/service/agency/DataProtectionConverterTest.java @@ -0,0 +1,112 @@ +package de.caritas.cob.agencyservice.api.admin.service.agency; + +import de.caritas.cob.agencyservice.api.model.DataProtectionContactDTO; +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.Agency.AgencyBuilder; +import de.caritas.cob.agencyservice.api.repository.agency.DataProtectionResponsibleEntity; +import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; + +class DataProtectionConverterTest { + + @Test + void convertToEntity_Should_Convert_When_AllParamsAreNull() { + // given + DataProtectionDTO dataProtectionDTO = new DataProtectionDTO(); + DataProtectionConverter dataProtectionConverter = new DataProtectionConverter(); + AgencyBuilder builder = Agency.builder().name("test").consultingTypeId(1); + + // when + dataProtectionConverter.convertToEntity(dataProtectionDTO, builder); + Agency agency = builder.build(); + + // then + assertThat(agency.getDataProtectionResponsibleEntity()).isNull(); + assertThat(agency.getDataProtectionAlternativeContactData()).isNull(); + assertThat(agency.getDataProtectionOfficerContactData()).isNull(); + assertThat(agency.getDataProtectionAgencyResponsibleContactData()).isNull(); + } + + @Test + void convertToEntity_Should_Convert_When_DataProtectionDTOIsNull() { + // given + DataProtectionConverter dataProtectionConverter = new DataProtectionConverter(); + AgencyBuilder builder = Agency.builder().name("test").consultingTypeId(1); + + // when + dataProtectionConverter.convertToEntity(null, builder); + Agency agency = builder.build(); + + // then + assertThat(agency.getDataProtectionResponsibleEntity()).isNull(); + assertThat(agency.getDataProtectionAlternativeContactData()).isNull(); + assertThat(agency.getDataProtectionOfficerContactData()).isNull(); + assertThat(agency.getDataProtectionAgencyResponsibleContactData()).isNull(); + } + + @Test + void convertToEntity_Should_Convert_When_AgencyResponsible_IsNotNull() { + // given + DataProtectionConverter dataProtectionConverter = new DataProtectionConverter(); + AgencyBuilder builder = Agency.builder().name("test").consultingTypeId(1); + var dataProtectionDTO = new DataProtectionDTO().dataProtectionResponsibleEntity(DataProtectionDTO.DataProtectionResponsibleEntityEnum.AGENCY_RESPONSIBLE) + .agencyDataProtectionResponsibleContact(new DataProtectionContactDTO().nameAndLegalForm("agency data responsible")); + + // when + dataProtectionConverter.convertToEntity(dataProtectionDTO, builder); + Agency agency = builder.build(); + + // then + assertThat(agency.getDataProtectionResponsibleEntity()).isEqualTo( + DataProtectionResponsibleEntity.AGENCY_RESPONSIBLE); + assertThat(agency.getDataProtectionAlternativeContactData()).isNull(); + assertThat(agency.getDataProtectionOfficerContactData()).isNull(); + assertThat(agency.getDataProtectionAgencyResponsibleContactData()).isNotNull(); + } + + @Test + void convertToEntity_Should_Convert_When_DataProtectionOfficer_IsNotNull() { + // given + DataProtectionConverter dataProtectionConverter = new DataProtectionConverter(); + AgencyBuilder builder = Agency.builder().name("test").consultingTypeId(1); + var dataProtectionDTO = new DataProtectionDTO().dataProtectionResponsibleEntity( + DataProtectionResponsibleEntityEnum.DATA_PROTECTION_OFFICER) + .dataProtectionOfficerContact(new DataProtectionContactDTO().nameAndLegalForm("data protection officer data")); + + // when + dataProtectionConverter.convertToEntity(dataProtectionDTO, builder); + Agency agency = builder.build(); + + // then + assertThat(agency.getDataProtectionResponsibleEntity()).isEqualTo( + DataProtectionResponsibleEntity.DATA_PROTECTION_OFFICER); + assertThat(agency.getDataProtectionAlternativeContactData()).isNull(); + assertThat(agency.getDataProtectionOfficerContactData()).isNotNull(); + assertThat(agency.getDataProtectionAgencyResponsibleContactData()).isNull(); + } + + @Test + void convertToEntity_Should_Convert_When_AlternativeRepresentative_IsNotNull() { + // given + DataProtectionConverter dataProtectionConverter = new DataProtectionConverter(); + AgencyBuilder builder = Agency.builder().name("test").consultingTypeId(1); + var dataProtectionDTO = new DataProtectionDTO().dataProtectionResponsibleEntity( + DataProtectionResponsibleEntityEnum.ALTERNATIVE_REPRESENTATIVE) + .alternativeDataProtectionRepresentativeContact(new DataProtectionContactDTO().nameAndLegalForm("alternative representative data")); + + // when + dataProtectionConverter.convertToEntity(dataProtectionDTO, builder); + Agency agency = builder.build(); + + // then + assertThat(agency.getDataProtectionResponsibleEntity()).isEqualTo( + DataProtectionResponsibleEntity.ALTERNATIVE_REPRESENTATIVE); + assertThat(agency.getDataProtectionAlternativeContactData()).isNotNull(); + assertThat(agency.getDataProtectionOfficerContactData()).isNull(); + assertThat(agency.getDataProtectionAgencyResponsibleContactData()).isNull(); + } + + +} From ba591752269b0f52cb4e7617d1a347dab03f45c7 Mon Sep 17 00:00:00 2001 From: tkuzynow Date: Thu, 21 Dec 2023 15:21:03 +0100 Subject: [PATCH 3/6] fix: checkstyle --- .../api/admin/service/agency/DataProtectionConverter.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/caritas/cob/agencyservice/api/admin/service/agency/DataProtectionConverter.java b/src/main/java/de/caritas/cob/agencyservice/api/admin/service/agency/DataProtectionConverter.java index 3f564bec..fe5d7b29 100644 --- a/src/main/java/de/caritas/cob/agencyservice/api/admin/service/agency/DataProtectionConverter.java +++ b/src/main/java/de/caritas/cob/agencyservice/api/admin/service/agency/DataProtectionConverter.java @@ -43,8 +43,10 @@ private static DataProtectionResponsibleEntity convertEnums( return DataProtectionResponsibleEntity.DATA_PROTECTION_OFFICER; case ALTERNATIVE_REPRESENTATIVE: return DataProtectionResponsibleEntity.ALTERNATIVE_REPRESENTATIVE; + default: + throw new IllegalArgumentException( + "DataProtectionResponsibleEntity not supported: " + dataProtectionEntity); } - return null; } } From a7f2b14e8440bf1f16fb0c9679c3a98e310bf44c Mon Sep 17 00:00:00 2001 From: tkuzynow Date: Thu, 21 Dec 2023 15:27:58 +0100 Subject: [PATCH 4/6] fix: tests --- .../agencyservice/api/admin/service/AgencyAdminService.java | 6 ++++-- .../api/admin/service/AgencyAdminServiceTest.java | 4 ++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/caritas/cob/agencyservice/api/admin/service/AgencyAdminService.java b/src/main/java/de/caritas/cob/agencyservice/api/admin/service/AgencyAdminService.java index d793df01..07b9438c 100644 --- a/src/main/java/de/caritas/cob/agencyservice/api/admin/service/AgencyAdminService.java +++ b/src/main/java/de/caritas/cob/agencyservice/api/admin/service/AgencyAdminService.java @@ -55,7 +55,7 @@ public class AgencyAdminService { @Autowired(required = false) private DemographicsConverter demographicsConverter; - @Autowired(required = false) + @Autowired private DataProtectionConverter dataProtectionConverter; @Value("${feature.topics.enabled}") @@ -210,7 +210,9 @@ private Agency mergeAgencies(Agency agency, UpdateAgencyDTO updateAgencyDTO) { .counsellingRelations(agency.getCounsellingRelations()) .deleteDate(agency.getDeleteDate()); - dataProtectionConverter.convertToEntity(updateAgencyDTO.getDataProtection(), agencyBuilder); + if (dataProtectionConverter != null) { + dataProtectionConverter.convertToEntity(updateAgencyDTO.getDataProtection(), agencyBuilder); + } if (nonNull(updateAgencyDTO.getConsultingType())) { agencyBuilder.consultingTypeId(updateAgencyDTO.getConsultingType()); diff --git a/src/test/java/de/caritas/cob/agencyservice/api/admin/service/AgencyAdminServiceTest.java b/src/test/java/de/caritas/cob/agencyservice/api/admin/service/AgencyAdminServiceTest.java index bf18540d..982f483d 100644 --- a/src/test/java/de/caritas/cob/agencyservice/api/admin/service/AgencyAdminServiceTest.java +++ b/src/test/java/de/caritas/cob/agencyservice/api/admin/service/AgencyAdminServiceTest.java @@ -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; @@ -74,6 +75,9 @@ class AgencyAdminServiceTest { @Mock DemographicsConverter demographicsConverter; + @Mock + DataProtectionConverter dataProtectionConverter; + @Mock AppointmentService appointmentService; From f58920a9578532576287ffaee22a7f0bf625d9c2 Mon Sep 17 00:00:00 2001 From: tkuzynow Date: Fri, 22 Dec 2023 13:15:32 +0100 Subject: [PATCH 5/6] fix: converter --- .../agency/DataProtectionDTOBuilder.java | 69 ++++++++----------- .../controller/AgencyAdminControllerIT.java | 53 ++++++++++++++ 2 files changed, 81 insertions(+), 41 deletions(-) diff --git a/src/main/java/de/caritas/cob/agencyservice/api/admin/service/agency/DataProtectionDTOBuilder.java b/src/main/java/de/caritas/cob/agencyservice/api/admin/service/agency/DataProtectionDTOBuilder.java index ad911450..88683bea 100644 --- a/src/main/java/de/caritas/cob/agencyservice/api/admin/service/agency/DataProtectionDTOBuilder.java +++ b/src/main/java/de/caritas/cob/agencyservice/api/admin/service/agency/DataProtectionDTOBuilder.java @@ -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; @@ -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 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); } + + } 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 4b1fb43a..f5005e2b 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 @@ -14,6 +14,8 @@ import com.google.common.collect.Lists; import de.caritas.cob.agencyservice.api.admin.service.UserAdminService; +import de.caritas.cob.agencyservice.api.model.DataProtectionContactDTO; +import de.caritas.cob.agencyservice.api.model.DataProtectionDTO; 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; @@ -34,6 +36,7 @@ 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; @@ -242,6 +245,56 @@ void updateAgency_Should_returnStatusOk_When_calledWithEmptyDescription() throws assertNull(savedAgency.getDescription()); } + @Test + @WithMockUser(authorities = "AUTHORIZATION_AGENCY_ADMIN") + void updateAgency_Should_persistDataProtectionAttributes_When_payloadContainsDataProtection() throws Exception { + var response = new ExtendedConsultingTypeResponseDTO(); + when(consultingTypeManager.getConsultingTypeSettings(anyInt())).thenReturn(response); + + var agencyDTO = new UpdateAgencyDTO() + .name("Test update name") + .description(null) + .offline(true) + .external(false) + .dataProtection(new DataProtectionDTO() + .dataProtectionResponsibleEntity(DataProtectionDTO.DataProtectionResponsibleEntityEnum.AGENCY_RESPONSIBLE) + .agencyDataProtectionResponsibleContact(new DataProtectionContactDTO().nameAndLegalForm("agency responsible contact") + .city("Freiburg").postcode("99999").phoneNumber("123-123-123").email("agency@onlineberatung.net") + ) + .dataProtectionOfficerContact(new DataProtectionContactDTO().nameAndLegalForm("data protection contact").city("Munich") + .postcode("00001").phoneNumber("321-321-321").email("dataprotection@onlineberatung.net") + )); + + mockMvc.perform(put(PathConstants.UPDATE_DELETE_AGENCY_PATH) + .contentType(APPLICATION_JSON) + .content(JsonConverter.convertToJson(agencyDTO))) + .andExpect(status().isOk()) + .andExpect(jsonPath("_embedded.id").value(1)) + .andExpect(jsonPath("_embedded.name").value("Test update name")) + .andExpect(jsonPath("_embedded.description").isEmpty()) + .andExpect(jsonPath("_embedded.teamAgency").value("false")) + .andExpect(jsonPath("_embedded.external").value("false")) + .andExpect(jsonPath("_embedded.offline").exists()) + .andExpect(jsonPath("_embedded.topics").exists()) + .andExpect(jsonPath("_embedded.createDate").exists()) + .andExpect(jsonPath("_embedded.updateDate").exists()) + .andExpect(jsonPath("_embedded.deleteDate").exists()) + .andExpect(jsonPath("_embedded.dataProtection.dataProtectionResponsibleEntity").value("AGENCY_RESPONSIBLE")) + .andExpect(jsonPath("_embedded.dataProtection.agencyDataProtectionResponsibleContact.nameAndLegalForm").value("agency responsible contact")) + .andExpect(jsonPath("_embedded.dataProtection.agencyDataProtectionResponsibleContact.city").value("Freiburg")) + .andExpect(jsonPath("_embedded.dataProtection.agencyDataProtectionResponsibleContact.postcode").value("99999")) + .andExpect(jsonPath("_embedded.dataProtection.agencyDataProtectionResponsibleContact.phoneNumber").value("123-123-123")) + .andExpect(jsonPath("_embedded.dataProtection.agencyDataProtectionResponsibleContact.email").value("agency@onlineberatung.net")) + .andExpect(jsonPath("_embedded.dataProtection.dataProtectionOfficerContact.nameAndLegalForm").value("data protection contact")) + .andExpect(jsonPath("_embedded.dataProtection.dataProtectionOfficerContact.city").value("Munich")) + .andExpect(jsonPath("_embedded.dataProtection.dataProtectionOfficerContact.postcode").value("00001")) + .andExpect(jsonPath("_embedded.dataProtection.dataProtectionOfficerContact.phoneNumber").value("321-321-321")) + .andExpect(jsonPath("_embedded.dataProtection.dataProtectionOfficerContact.email").value("dataprotection@onlineberatung.net")); + + var savedAgency = agencyRepository.findById(1L).orElseThrow(); + assertNull(savedAgency.getDescription()); + } + @Test @WithMockUser(authorities = {"AUTHORIZATION_RESTRICTED_AGENCY_ADMIN"}) void updateAgency_Should_returnStatusOk_When_calledWithRestrictedAgencyAdminThatHasPermissionForGivenAgency() From b777e1f21af9a4ff7a2c4e6b8369301794745b1e Mon Sep 17 00:00:00 2001 From: tkuzynow Date: Fri, 22 Dec 2023 13:23:00 +0100 Subject: [PATCH 6/6] fix: tests --- .../api/admin/service/AgencyAdminServiceTest.java | 15 +++++++++++++-- .../AgencyAdminFullResponseDTOBuilderTest.java | 1 + 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/test/java/de/caritas/cob/agencyservice/api/admin/service/AgencyAdminServiceTest.java b/src/test/java/de/caritas/cob/agencyservice/api/admin/service/AgencyAdminServiceTest.java index 982f483d..edf30c75 100644 --- a/src/test/java/de/caritas/cob/agencyservice/api/admin/service/AgencyAdminServiceTest.java +++ b/src/test/java/de/caritas/cob/agencyservice/api/admin/service/AgencyAdminServiceTest.java @@ -111,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); @@ -125,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); @@ -143,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); @@ -168,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); @@ -189,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()); diff --git a/src/test/java/de/caritas/cob/agencyservice/api/admin/service/agency/AgencyAdminFullResponseDTOBuilderTest.java b/src/test/java/de/caritas/cob/agencyservice/api/admin/service/agency/AgencyAdminFullResponseDTOBuilderTest.java index 539eef6d..7780435c 100644 --- a/src/test/java/de/caritas/cob/agencyservice/api/admin/service/agency/AgencyAdminFullResponseDTOBuilderTest.java +++ b/src/test/java/de/caritas/cob/agencyservice/api/admin/service/agency/AgencyAdminFullResponseDTOBuilderTest.java @@ -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());