fixing tests after merging master

This commit is contained in:
Michael Hoennig 2019-04-18 18:11:56 +02:00
parent 16cf845015
commit 6ab67995ff
2 changed files with 18 additions and 20 deletions

View File

@ -2,7 +2,6 @@ package org.hostsharing.hsadminng.service.dto;
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.TreeNode; import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer; import com.fasterxml.jackson.databind.JsonDeserializer;
@ -57,10 +56,6 @@ public class CustomerDTO implements Serializable {
@AccessFor(init = Role.ADMIN, update = Role.ADMIN, read = Role.CONTRACTUAL_CONTACT) @AccessFor(init = Role.ADMIN, update = Role.ADMIN, read = Role.CONTRACTUAL_CONTACT)
private String contractualAddress; private String contractualAddress;
@Size(max = 80)
@AccessFor(init = Role.ADMIN, update = Role.CONTRACTUAL_CONTACT, read = Role.ANY_CUSTOMER_CONTACT)
private String contractualSalutation;
@Size(max = 80) @Size(max = 80)
@AccessFor(init = Role.ADMIN, update = {Role.CONTRACTUAL_CONTACT, Role.FINANCIAL_CONTACT}, read = Role.CONTRACTUAL_CONTACT) @AccessFor(init = Role.ADMIN, update = {Role.CONTRACTUAL_CONTACT, Role.FINANCIAL_CONTACT}, read = Role.CONTRACTUAL_CONTACT)
private String billingSalutation; private String billingSalutation;
@ -182,10 +177,10 @@ public class CustomerDTO implements Serializable {
} }
@JsonComponent @JsonComponent
public static class CustomerJsonSerializer extends JsonSerializer<CustomerDTO> { public static class JsonSerializerWithAccessFilter extends JsonSerializer<Object> {
@Override @Override
public void serialize(CustomerDTO dto, JsonGenerator jsonGenerator, public void serialize(Object dto, JsonGenerator jsonGenerator,
SerializerProvider serializerProvider) throws IOException { SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeStartObject(); jsonGenerator.writeStartObject();
@ -196,7 +191,7 @@ public class CustomerDTO implements Serializable {
jsonGenerator.writeEndObject(); jsonGenerator.writeEndObject();
} }
private void toJSon(CustomerDTO dto, JsonGenerator jsonGenerator, Field prop) throws IOException { private void toJSon(Object dto, JsonGenerator jsonGenerator, Field prop) throws IOException {
if (getLoginUserRole().isAllowedToRead(prop)) { if (getLoginUserRole().isAllowedToRead(prop)) {
final String fieldName = prop.getName(); final String fieldName = prop.getName();
if (Integer.class.isAssignableFrom(prop.getType()) || int.class.isAssignableFrom(prop.getType())) { if (Integer.class.isAssignableFrom(prop.getType()) || int.class.isAssignableFrom(prop.getType())) {
@ -211,7 +206,7 @@ public class CustomerDTO implements Serializable {
} }
} }
private Object get(CustomerDTO dto, Field field) { private Object get(Object dto, Field field) {
try { try {
field.setAccessible(true); field.setAccessible(true);
return field.get(dto); return field.get(dto);
@ -239,20 +234,20 @@ public class CustomerDTO implements Serializable {
@Override @Override
public CustomerDTO deserialize(JsonParser jsonParser, public CustomerDTO deserialize(JsonParser jsonParser,
DeserializationContext deserializationContext) throws IOException, DeserializationContext deserializationContext) throws IOException {
JsonProcessingException {
TreeNode treeNode = jsonParser.getCodec().readTree(jsonParser); TreeNode treeNode = jsonParser.getCodec().readTree(jsonParser);
CustomerDTO dto = new CustomerDTO(); CustomerDTO dto = new CustomerDTO();
dto.setId(((IntNode) treeNode.get("id")).asLong()); dto.setId(((IntNode) treeNode.get("id")).asLong());
dto.setNumber(((IntNode) treeNode.get("number")).asInt()); dto.setReference(((IntNode) treeNode.get("reference")).asInt());
dto.setPrefix(((TextNode) treeNode.get("prefix")).asText()); dto.setPrefix(((TextNode) treeNode.get("prefix")).asText());
dto.setName(((TextNode) treeNode.get("name")).asText()); dto.setName(((TextNode) treeNode.get("name")).asText());
dto.setContractualAddress(((TextNode) treeNode.get("contractualAddress")).asText()); dto.setContractualAddress(((TextNode) treeNode.get("contractualAddress")).asText());
dto.setContractualSalutation(((TextNode) treeNode.get("contractualSalutation")).asText()); dto.setContractualSalutation(((TextNode) treeNode.get("contractualSalutation")).asText());
dto.setBillingAddress(((TextNode) treeNode.get("billingAddress")).asText()); dto.setBillingAddress(((TextNode) treeNode.get("billingAddress")).asText());
dto.setBillingSalutation(((TextNode) treeNode.get("billingSalutation")).asText()); dto.setBillingSalutation(((TextNode) treeNode.get("billingSalutation")).asText());
dto.setRemark(((TextNode) treeNode.get("remark")).asText());
return dto; return dto;
} }

View File

@ -41,6 +41,7 @@ public class CustomerDTOUnitTest {
given.setContractualSalutation(null); given.setContractualSalutation(null);
given.setBillingAddress(null); given.setBillingAddress(null);
given.setBillingSalutation(null); given.setBillingSalutation(null);
given.setRemark(null);
assertEquals(createExpectedJSon(given), actual); assertEquals(createExpectedJSon(given), actual);
} }
@ -55,13 +56,13 @@ public class CustomerDTOUnitTest {
String actual = objectMapper.writeValueAsString(given); String actual = objectMapper.writeValueAsString(given);
// then // then
assertEquals(createExpectedJSon(given), actual); assertThat(actual).isEqualTo(createExpectedJSon(given));
} }
@Test @Test
public void testDeserializeAsContractualCustomerContact() throws IOException { public void testDeserializeAsContractualCustomerContact() throws IOException {
// given // given
String json = "{\"id\":1234,\"number\":10001,\"prefix\":\"abc\",\"name\":\"Mein Name\",\"contractualAddress\":\"Eine Adresse\",\"contractualSalutation\":\"Hallo\",\"billingAddress\":\"Noch eine Adresse\",\"billingSalutation\":\"Moin\"}"; String json = "{\"id\":1234,\"reference\":10001,\"prefix\":\"abc\",\"name\":\"Mein Name\",\"contractualAddress\":\"Eine Adresse\",\"contractualSalutation\":\"Hallo\",\"billingAddress\":\"Noch eine Adresse\",\"billingSalutation\":\"Moin\",\"remark\":\"Eine Bemerkung\"}";
givenLoginUserWithRole("CONTRACTUAL_CONTACT"); givenLoginUserWithRole("CONTRACTUAL_CONTACT");
// when // when
@ -70,26 +71,28 @@ public class CustomerDTOUnitTest {
// then // then
CustomerDTO expected = new CustomerDTO(); CustomerDTO expected = new CustomerDTO();
expected.setId(1234L); expected.setId(1234L);
expected.setNumber(10001); expected.setReference(10001);
expected.setPrefix("abc"); expected.setPrefix("abc");
expected.setName("Mein Name"); expected.setName("Mein Name");
expected.setContractualAddress(null); // not allowed expected.setContractualAddress(null); // not allowed
expected.setContractualSalutation("Hallo"); expected.setContractualSalutation("Hallo");
expected.setBillingAddress("Noch eine Adresse"); expected.setBillingAddress("Noch eine Adresse");
expected.setBillingSalutation("Moin"); expected.setBillingSalutation("Moin");
expected.setRemark("Eine Bemerkung");
assertEquals(actual, expected); assertEquals(actual, expected);
} }
private String createExpectedJSon(CustomerDTO dto) { private String createExpectedJSon(CustomerDTO dto) {
String json = // the fields in alphanumeric order: String json = // the fields in alphanumeric order:
toJSonFieldDefinitionIfPresent("id", dto.getId()) + toJSonFieldDefinitionIfPresent("id", dto.getId()) +
toJSonFieldDefinitionIfPresent("number", dto.getNumber()) + toJSonFieldDefinitionIfPresent("reference", dto.getReference()) +
toJSonFieldDefinitionIfPresent("prefix", dto.getPrefix()) + toJSonFieldDefinitionIfPresent("prefix", dto.getPrefix()) +
toJSonFieldDefinitionIfPresent("name", dto.getName()) + toJSonFieldDefinitionIfPresent("name", dto.getName()) +
toJSonFieldDefinitionIfPresent("contractualAddress", dto.getContractualAddress()) +
toJSonFieldDefinitionIfPresent("contractualSalutation", dto.getContractualSalutation()) + toJSonFieldDefinitionIfPresent("contractualSalutation", dto.getContractualSalutation()) +
toJSonFieldDefinitionIfPresent("contractualAddress", dto.getContractualAddress()) +
toJSonFieldDefinitionIfPresent("billingSalutation", dto.getBillingSalutation()) +
toJSonFieldDefinitionIfPresent("billingAddress", dto.getBillingAddress()) + toJSonFieldDefinitionIfPresent("billingAddress", dto.getBillingAddress()) +
toJSonFieldDefinitionIfPresent("billingSalutation", dto.getBillingSalutation()); toJSonFieldDefinitionIfPresent("remark", dto.getRemark()) ;
return "{" + json.substring(0, json.length() - 1) + "}"; return "{" + json.substring(0, json.length() - 1) + "}";
} }
@ -108,14 +111,14 @@ public class CustomerDTOUnitTest {
private CustomerDTO createSomeCustomerDTO() { private CustomerDTO createSomeCustomerDTO() {
CustomerDTO given = new CustomerDTO(); CustomerDTO given = new CustomerDTO();
given.setId(1234L); given.setId(1234L);
given.setNumber(10001); given.setReference(10001);
given.setPrefix("abc"); given.setPrefix("abc");
given.setName("Mein Name"); given.setName("Mein Name");
given.setContractualAddress("Eine Adresse"); given.setContractualAddress("Eine Adresse");
given.setContractualSalutation("Hallo"); given.setContractualSalutation("Hallo");
given.setBillingAddress("Noch eine Adresse"); given.setBillingAddress("Noch eine Adresse");
given.setBillingSalutation("Moin"); given.setBillingSalutation("Moin");
givenLoginUserWithRole("admin"); given.setRemark("Eine Bemerkung");
return given; return given;
} }