fixing tests after merging master
This commit is contained in:
parent
16cf845015
commit
6ab67995ff
@ -2,7 +2,6 @@ package org.hostsharing.hsadminng.service.dto;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.TreeNode;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
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)
|
||||
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)
|
||||
@AccessFor(init = Role.ADMIN, update = {Role.CONTRACTUAL_CONTACT, Role.FINANCIAL_CONTACT}, read = Role.CONTRACTUAL_CONTACT)
|
||||
private String billingSalutation;
|
||||
@ -182,10 +177,10 @@ public class CustomerDTO implements Serializable {
|
||||
}
|
||||
|
||||
@JsonComponent
|
||||
public static class CustomerJsonSerializer extends JsonSerializer<CustomerDTO> {
|
||||
public static class JsonSerializerWithAccessFilter extends JsonSerializer<Object> {
|
||||
|
||||
@Override
|
||||
public void serialize(CustomerDTO dto, JsonGenerator jsonGenerator,
|
||||
public void serialize(Object dto, JsonGenerator jsonGenerator,
|
||||
SerializerProvider serializerProvider) throws IOException {
|
||||
|
||||
jsonGenerator.writeStartObject();
|
||||
@ -196,7 +191,7 @@ public class CustomerDTO implements Serializable {
|
||||
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)) {
|
||||
final String fieldName = prop.getName();
|
||||
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 {
|
||||
field.setAccessible(true);
|
||||
return field.get(dto);
|
||||
@ -239,20 +234,20 @@ public class CustomerDTO implements Serializable {
|
||||
|
||||
@Override
|
||||
public CustomerDTO deserialize(JsonParser jsonParser,
|
||||
DeserializationContext deserializationContext) throws IOException,
|
||||
JsonProcessingException {
|
||||
DeserializationContext deserializationContext) throws IOException {
|
||||
|
||||
TreeNode treeNode = jsonParser.getCodec().readTree(jsonParser);
|
||||
|
||||
CustomerDTO dto = new CustomerDTO();
|
||||
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.setName(((TextNode) treeNode.get("name")).asText());
|
||||
dto.setContractualAddress(((TextNode) treeNode.get("contractualAddress")).asText());
|
||||
dto.setContractualSalutation(((TextNode) treeNode.get("contractualSalutation")).asText());
|
||||
dto.setBillingAddress(((TextNode) treeNode.get("billingAddress")).asText());
|
||||
dto.setBillingSalutation(((TextNode) treeNode.get("billingSalutation")).asText());
|
||||
dto.setRemark(((TextNode) treeNode.get("remark")).asText());
|
||||
|
||||
return dto;
|
||||
}
|
||||
|
@ -41,6 +41,7 @@ public class CustomerDTOUnitTest {
|
||||
given.setContractualSalutation(null);
|
||||
given.setBillingAddress(null);
|
||||
given.setBillingSalutation(null);
|
||||
given.setRemark(null);
|
||||
assertEquals(createExpectedJSon(given), actual);
|
||||
}
|
||||
|
||||
@ -55,13 +56,13 @@ public class CustomerDTOUnitTest {
|
||||
String actual = objectMapper.writeValueAsString(given);
|
||||
|
||||
// then
|
||||
assertEquals(createExpectedJSon(given), actual);
|
||||
assertThat(actual).isEqualTo(createExpectedJSon(given));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializeAsContractualCustomerContact() throws IOException {
|
||||
// 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");
|
||||
|
||||
// when
|
||||
@ -70,26 +71,28 @@ public class CustomerDTOUnitTest {
|
||||
// then
|
||||
CustomerDTO expected = new CustomerDTO();
|
||||
expected.setId(1234L);
|
||||
expected.setNumber(10001);
|
||||
expected.setReference(10001);
|
||||
expected.setPrefix("abc");
|
||||
expected.setName("Mein Name");
|
||||
expected.setContractualAddress(null); // not allowed
|
||||
expected.setContractualSalutation("Hallo");
|
||||
expected.setBillingAddress("Noch eine Adresse");
|
||||
expected.setBillingSalutation("Moin");
|
||||
expected.setRemark("Eine Bemerkung");
|
||||
assertEquals(actual, expected);
|
||||
}
|
||||
|
||||
private String createExpectedJSon(CustomerDTO dto) {
|
||||
String json = // the fields in alphanumeric order:
|
||||
toJSonFieldDefinitionIfPresent("id", dto.getId()) +
|
||||
toJSonFieldDefinitionIfPresent("number", dto.getNumber()) +
|
||||
toJSonFieldDefinitionIfPresent("reference", dto.getReference()) +
|
||||
toJSonFieldDefinitionIfPresent("prefix", dto.getPrefix()) +
|
||||
toJSonFieldDefinitionIfPresent("name", dto.getName()) +
|
||||
toJSonFieldDefinitionIfPresent("contractualAddress", dto.getContractualAddress()) +
|
||||
toJSonFieldDefinitionIfPresent("contractualSalutation", dto.getContractualSalutation()) +
|
||||
toJSonFieldDefinitionIfPresent("contractualAddress", dto.getContractualAddress()) +
|
||||
toJSonFieldDefinitionIfPresent("billingSalutation", dto.getBillingSalutation()) +
|
||||
toJSonFieldDefinitionIfPresent("billingAddress", dto.getBillingAddress()) +
|
||||
toJSonFieldDefinitionIfPresent("billingSalutation", dto.getBillingSalutation());
|
||||
toJSonFieldDefinitionIfPresent("remark", dto.getRemark()) ;
|
||||
return "{" + json.substring(0, json.length() - 1) + "}";
|
||||
}
|
||||
|
||||
@ -108,14 +111,14 @@ public class CustomerDTOUnitTest {
|
||||
private CustomerDTO createSomeCustomerDTO() {
|
||||
CustomerDTO given = new CustomerDTO();
|
||||
given.setId(1234L);
|
||||
given.setNumber(10001);
|
||||
given.setReference(10001);
|
||||
given.setPrefix("abc");
|
||||
given.setName("Mein Name");
|
||||
given.setContractualAddress("Eine Adresse");
|
||||
given.setContractualSalutation("Hallo");
|
||||
given.setBillingAddress("Noch eine Adresse");
|
||||
given.setBillingSalutation("Moin");
|
||||
givenLoginUserWithRole("admin");
|
||||
given.setRemark("Eine Bemerkung");
|
||||
return given;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user