Skip to content
Snippets Groups Projects
Commit aae291eb authored by OZGCloud's avatar OZGCloud
Browse files

OZG-6636 adjust mapper; cleanup

parent 6806a63e
Branches
Tags 0.4.0
No related merge requests found
package de.ozgcloud.eingang.formcycle; package de.ozgcloud.eingang.formcycle;
import java.util.Collections;
import java.util.List;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component; import org.mapstruct.CollectionMappingStrategy;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.ReportingPolicy;
import de.ozgcloud.eingang.common.formdata.ServiceKonto; import de.ozgcloud.eingang.common.formdata.ServiceKonto;
import de.ozgcloud.eingang.common.formdata.ServiceKonto.PostfachAddress; import de.ozgcloud.eingang.common.formdata.ServiceKonto.PostfachAddress;
import de.ozgcloud.eingang.common.formdata.StringBasedIdentifier; import de.ozgcloud.eingang.common.formdata.StringBasedIdentifier;
@Component @Mapper(collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED, unmappedTargetPolicy = ReportingPolicy.WARN)
class FormCycleServiceKontoMapper { interface FormCycleServiceKontoMapper {
public ServiceKonto fromGrpc(FormCycleServiceKonto serviceKonto) { @Mapping(target = "postfachAddress", ignore = true)
return ServiceKonto.builder() @Mapping(target = "postfachAddresses", expression = "java(fromGrpcPostfachAddresses(serviceKonto))")
.type(serviceKonto.getType()) ServiceKonto fromGrpc(FormCycleServiceKonto serviceKonto);
.postfachAddress(buildPostfachAddress(serviceKonto))
.trustLevel(StringUtils.trimToNull(serviceKonto.getTrustLevel()))
.build();
}
PostfachAddress buildPostfachAddress(FormCycleServiceKonto serviceKonto) { default String fromString(String str) {
if (!serviceKonto.hasAddress()) { return StringUtils.trimToNull(str);
return null;
} }
var address = serviceKonto.getAddress();
return PostfachAddress.builder().identifier(buildPostfachId(address.getIdentifier())).version(address.getVersion()).build(); default List<PostfachAddress> fromGrpcPostfachAddresses(FormCycleServiceKonto serviceKonto) {
return serviceKonto.hasAddress() ? List.of(fromGrpc(serviceKonto.getAddress())) : Collections.emptyList();
} }
private StringBasedIdentifier buildPostfachId(String identifier) { @Mapping(target = "type", ignore = true)
@Mapping(target = "identifier", expression = "java(buildIdentifier(postfachAddress.getIdentifier()))")
PostfachAddress fromGrpc(FormCyclePostfachAddress postfachAddress);
default StringBasedIdentifier buildIdentifier(String identifier) {
return StringBasedIdentifier.builder().postfachId(identifier).build(); return StringBasedIdentifier.builder().postfachId(identifier).build();
} }
} }
\ No newline at end of file
...@@ -6,7 +6,7 @@ import org.apache.commons.lang3.StringUtils; ...@@ -6,7 +6,7 @@ import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Spy; import org.mapstruct.factory.Mappers;
import de.ozgcloud.eingang.common.formdata.ServiceKonto.PostfachAddress; import de.ozgcloud.eingang.common.formdata.ServiceKonto.PostfachAddress;
import de.ozgcloud.eingang.common.formdata.ServiceKontoTestFactory; import de.ozgcloud.eingang.common.formdata.ServiceKontoTestFactory;
...@@ -14,12 +14,11 @@ import de.ozgcloud.eingang.common.formdata.StringBasedIdentifier; ...@@ -14,12 +14,11 @@ import de.ozgcloud.eingang.common.formdata.StringBasedIdentifier;
class FormCycleServiceKontoMapperTest { class FormCycleServiceKontoMapperTest {
@Spy private final FormCycleServiceKontoMapper mapper = Mappers.getMapper(FormCycleServiceKontoMapper.class);
private FormCycleServiceKontoMapper mapper;
@DisplayName("From grpc") @DisplayName("From grpc serviceKonto")
@Nested @Nested
class TestFromGrpc { class TestFromGrpcServiceKonto {
@DisplayName("trustLevel") @DisplayName("trustLevel")
@Nested @Nested
...@@ -42,28 +41,28 @@ class FormCycleServiceKontoMapperTest { ...@@ -42,28 +41,28 @@ class FormCycleServiceKontoMapperTest {
} }
} }
@DisplayName("build postfacha address") @Test
void shouldNotPostfachAddressIfNotExists() {
var serviceKonto = mapper.fromGrpc(FormCycleServiceKontoTestFactory.createBuilder().clearAddress().build());
assertThat(serviceKonto.getPostfachAddresses()).isEmpty();
}
@DisplayName("postfachAddress")
@Nested @Nested
class TestBuildPostfachAddress { class TestPostfachAddress {
@Test @Test
void shouldMapPostkorbId() { void shouldMapPostkorbId() {
var postfachAddress = mapper.buildPostfachAddress(FormCycleServiceKontoTestFactory.create()); var postfachAddress = mapPostfachAddressFromGrpc();
assertThat(((StringBasedIdentifier) postfachAddress.getIdentifier()).getPostfachId()) assertThat(((StringBasedIdentifier) postfachAddress.getIdentifier()).getPostfachId())
.isEqualTo(FormCyclePostfachAddressTestFactory.POSTKORB_ID); .isEqualTo(FormCyclePostfachAddressTestFactory.POSTKORB_ID);
} }
@Test @Test
void shouldNotMapEmptyPostkorbId() { void shouldMapIdentifier() {
var postfachAddress = mapper.buildPostfachAddress(FormCycleServiceKonto.newBuilder().build()); var postfachAddress = mapPostfachAddressFromGrpc();
assertThat(postfachAddress).isNull();
}
@Test
void shouldMapPostfachIdentifier() {
var postfachAddress = mapper.buildPostfachAddress(FormCycleServiceKontoTestFactory.create());
assertThat(getIdentifier(postfachAddress)).isEqualTo(FormCyclePostfachAddressTestFactory.POSTKORB_ID); assertThat(getIdentifier(postfachAddress)).isEqualTo(FormCyclePostfachAddressTestFactory.POSTKORB_ID);
} }
...@@ -73,11 +72,15 @@ class FormCycleServiceKontoMapperTest { ...@@ -73,11 +72,15 @@ class FormCycleServiceKontoMapperTest {
} }
@Test @Test
void shouldMapPostfachAddressVersion() { void shouldMapVersion() {
var postfachAddress = mapper.buildPostfachAddress(FormCycleServiceKontoTestFactory.create()); var postfachAddress = mapPostfachAddressFromGrpc();
assertThat(postfachAddress.getVersion()).isEqualTo(FormCyclePostfachAddressTestFactory.VERSION); assertThat(postfachAddress.getVersion()).isEqualTo(FormCyclePostfachAddressTestFactory.VERSION);
} }
private PostfachAddress mapPostfachAddressFromGrpc() {
return mapper.fromGrpc(FormCycleServiceKontoTestFactory.create()).getPostfachAddresses().get(0);
}
} }
} }
} }
\ No newline at end of file
...@@ -346,9 +346,4 @@ class FormDataControllerTest { ...@@ -346,9 +346,4 @@ class FormDataControllerTest {
return FormData.builder().header(FormHeader.builder().build()).build(); return FormData.builder().header(FormHeader.builder().build()).build();
} }
} }
// private String getPostfachIdFormData(FormData formData) {
// return ((StringBasedIdentifier) formData.getHeader().getServiceKonto().getPostfachAddresses()
// .get(0).getIdentifier()).getPostfachId();
// }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment