For writing a test for a certain feature I have to cast a String containing []
into a JsonNode.
The problem is when mapping it to JsonNode it seems to be adding extra quotes to it.
What I expect "[]", but what I get ""[]"", which leads to a failing test. When I debug the code in normal operation, I do get only "[]" when testing the code with Postman instead of the not working ""[]"" which I only get during the tests.
This is what my DTO looks like in Spring Boot
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
public class PostLabelDTO {
private final String templateId;
private final String labels;
@JsonCreator
public PostLabelDTO(
final @JsonProperty("templateId") String templateId,
final @JsonProperty("labels") JsonNode labels
) {
this.templateId = templateId;
this.labels = labels.toString();
}
public String getId() {
return templateId;
}
public String getData() {
return labels;
}
}
My test has to fake this object with the properties to pass to the method which is gonna get tested.
My test looks like this:
@Test
public void getEmptyDocumentException() throws InvalidBarcodeException, EmptyStringException, InvalidBarcodeGeometryException, EmptyFieldException, TemplateNotFoundException, InvalidBarcodeStrategyException {
//defining an ID for the templateId JsonProperty
final String templateId = "fj2j931j2ijd1";
//this is the "labels" JsonNode that gets sent in through the Post request
//i checked 10 times how the value comes into the DTO and it was always "[]" (empty labels (document) object, for which I wrote this test for)
final String jsonString = "[]";
ObjectMapper mapper = new ObjectMapper();
mapper.enable(JsonParser.Feature.ALLOW_MISSING_VALUES);
JsonNode labels = mapper.valueToTree(jsonString);
//when I do this, the "[]" which is normally passed into the PostLabelDTO, becomes ""[]"", so there are extra quotes added
PostLabelDTO dto = new PostLabelDTO(templateId, labels);
final Document document = new Document(dto, templateRepository);
Exception resultingException = null;
try {
document.getPDF();
} catch (Exception e) {
e.printStackTrace();
assertThat(resultingException).isExactlyInstanceOf(EmptyDocumentException.class);
}
}
So basically I tried to put the above Json into a new instance of PostLabelDTO
as the labels
JsonNode object for testing purposes, but it doesn't work.
This is the request with which it works through postman (it works as in, it throws the right exception)
{
"templateId":"5b1140608134691d1804e74e",
"labels":[]
}
So basically I tried to put the above Json into a new instance of PostLabelDTO
as the labels
JsonNode object for testing purposes, but it doesn't work.
This is a working request (which returns a PDF with labels with a label on each page)
{
"templateId": "5b1140608134691d1804e74e",
"labels": [{
"data": {
"Ivolgnr": "Volgnr",
"Ilkw-nr": "Ilkw-nr",
"bedrijf": "Hornbach",
"wagenNr": "13513542626",
"barcode": {
"waarde": "9780471117094"
},
"leverdatumVan": "x",
"leverdatumNaar": "x",
"orderList": [{
"order": [{
"articlenumber": "29-840-4512"
},
{
"description": "Mooie grote plant"
},
{
"ordernumber": "3584479012860361"
},
{
"amount": "20"
},
{
"sellprice": "€5,00"
},
{
"deliverydate": "01-09-2018"
}
]
},
{
"order": [{
"articlenumber": "29-840-4512"
},
{
"description": "Mooie grote plant"
},
{
"ordernumber": "3584479012860361"
},
{
"amount": "20"
},
{
"sellprice": "€5,00"
},
{
"deliverydate": "01-09-2018"
}
]
},
{
"order": [{
"articlenumber": "29-840-4512"
},
{
"description": "Mooie grote plant"
},
{
"ordernumber": "3584479012860361"
},
{
"amount": "20"
},
{
"sellprice": "€5,00"
},
{
"deliverydate": "01-09-2018"
}
]
},
{
"order": [{
"articlenumber": "29-840-4512"
},
{
"description": "Mooie grote plant"
},
{
"ordernumber": "3584479012860361"
},
{
"amount": "20"
},
{
"sellprice": "€5,00"
},
{
"deliverydate": "01-09-2018"
}
]
},
{
"order": [{
"articlenumber": "29-840-4512"
},
{
"description": "Mooie grote plant"
},
{
"ordernumber": "3584479012860361"
},
{
"amount": "20"
},
{
"sellprice": "€5,00"
},
{
"deliverydate": "01-09-2018"
}
]
}
]
}
}, {
"data": {
"Ivolgnr": "22324rff",
"Ilkw-nr": "246426246",
"bedrijf": "bedrijfffff",
"wagenNr": "wagennrrrrrrr",
"barcode": {
"waarde": "9780471117094"
},
"leverdatumVan": "x",
"leverdatumNaar": "x",
"orderList": [{
"order": [{
"articlenumber": "a"
},
{
"description": "b"
},
{
"ordernumber": "c"
},
{
"amount": "d"
},
{
"sellprice": "e"
},
{
"deliverydate": "f"
}
]
}]
}
}]
}
ATTENTION The schema of a label (called data for each label in this request) can always vary depending on which template is used to be filled. So there is no possibility to make a Label object containing all properties as these always vary (depends on the HTML of the Template that is to be filled with this request. My service does a "search&replace" based on the tag property-names.
I already tried this: How to parse a JSON string into JsonNode in Jackson?
But I can't seem to add an empty array to the JsonNode object as its supposed to.
Can someone please help me?
Regards,
Ali