В настоящее время я пишу тесты, которые должны прочитать значение из XML для получения информации, а затем создать запрос. Эти тесты в джуните. Я пишу тесты для заказа ошибок, поэтому каждый XML будет иметь ошибку, и тест покажет правильный код ошибки.
Я хочу найти способ отделить XML от теста. Для каждого теста я пишу, что не хочу создавать новый XML. Есть ли способ поместить XML для каждого теста в отдельную папку?
В настоящее время я строю XML ниже, и это делается для каждого теста. Это кажется повторяющимся, и должен быть лучший способ сделать это.
private <T> T readValueFromXml(ResponseEntity<String> response, String xpathExpression,
Function<String, T> parser)
throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document xmlDocument = builder.parse(new ByteArrayInputStream(response.getBody().getBytes()));
XPath xPath = XPathFactory.newInstance().newXPath();
NodeList nodeList = (NodeList) xPath.compile(xpathExpression).evaluate(xmlDocument,
XPathConstants.NODESET);
if (nodeList.getLength() != 1) {
LOG.warn("Found {} nodes for {}", nodeList.getLength(), xpathExpression);
}
return parser.apply(nodeList.item(0).getTextContent());
}
private RequestEntity<String> buildRequest(URI orderUri, String body) {
Signature submitSignature = auth.generateExpectedSignature(orderUri,
Collections.singletonMap("h1", "bla"),
secret, keyId, "hmac-sha256");
RequestEntity<String> request = RequestEntity.post(orderUri)
.header(AUTHORIZATION, submitSignature.toString())
.header(CONTENT_TYPE, MediaType.TEXT_XML_VALUE)
.header(ACCEPT, MediaType.TEXT_XML_VALUE)
.header("h1", "bla")
.body(body);
LOG.debug("{} {}", request.getMethod(), request.getUrl());
LOG.debug("headers: {}", request.getHeaders());
return request;
}
private URI orderUri(String orderToken) {
String uri = SBUX_DPI_SERVICE_URI + "order/" + orderToken;
return URI.create(uri);
}
private URI totalsUri(String orderToken) {
String uri = SBUX_DPI_SERVICE_URI + "totals/" + orderToken;
return URI.create(uri);
}
/**
* Request containing SH AMERICANO w/ Nonfat milk and Extra Caramel Syrup
*/
private String priceOrderXml(String orderToken) {
return "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\n"
+ "<priceOrder>\n"
+ " <storeNumber>" + STORE_NUMBER + "</storeNumber>\n"
+ " <dateTime>2020-01-21T16:00:25Z</dateTime>\n"
+ " <orderToken>" + orderToken + "</orderToken>\n"
+ " <subChannelType>mobile</subChannelType>\n"
+ " <user>\n"
+ " <currentTierNumber>1</currentTierNumber>\n"
+ " <displayName>Jim Jones</displayName>\n"
+ " <partnerNumber>PARTNER1</partnerNumber>\n"
+ " </user>\n"
+ " <cart>\n"
+ " <items>\n"
+ " <item>\n"
+ " <qty>1</qty>\n"
+ " <sku>\n"
+ " <code>93</code>\n"
+ " </sku>\n"
+ " <childItems>\n"
+ " <childItem>\n"
+ " <qty>2</qty>\n"
+ " <sku>\n"
+ " <code>159778</code>\n"
+ " </sku>\n"
+ " </childItem>\n"
+ " <childItem>\n"
+ " <qty>1</qty>\n"
+ " <sku>\n"
+ " <code>109840</code>\n"
+ " </sku>\n"
+ " </childItem>\n"
+ " </childItems>"
+ " </item>\n"
+ " </items>\n"
+ " <promotions/>\n"
+ " <delivery/>\n"
+ " <preparation>ConsumeInStore</preparation>\n"
+ " </cart>\n"
+ "</priceOrder>";
}
private String submitOrderXml(String orderToken) {
return "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\n"
+ "<submitOrder>\n"
+ " <storeNumber>" + STORE_NUMBER + "</storeNumber>\n"
+ " <dateTime>2020-01-21T16:00:25Z</dateTime>\n"
+ " <orderToken>" + orderToken + "</orderToken>\n"
+ " <subChannelType>mobile</subChannelType>\n"
+ " <orderPrintType>1</orderPrintType>\n"
+ " <user>\n"
+ " <currentTierNumber>1</currentTierNumber>\n"
+ " <displayName>Jim Jones</displayName>\n"
+ " <partnerNumber>PARTNER1</partnerNumber>\n"
+ " </user>\n"
+ " <cart>\n"
+ " <items>\n"
+ " <item>\n"
+ " <qty>1</qty>\n"
+ " <sku>\n"
+ " <code>93</code>\n"
+ " </sku>\n"
+ " <childItems>\n"
+ " <childItem>\n"
+ " <qty>1</qty>\n"
+ " <sku>\n"
+ " <code>159778</code>\n"
+ " </sku>\n"
+ " </childItem>\n"
+ " <childItem>\n"
+ " <qty>1</qty>\n"
+ " <sku>\n"
+ " <code>109840</code>\n"
+ " </sku>\n"
+ " </childItem>\n"
+ " </childItems>"
+ " </item>\n"
+ " </items>\n"
+ " <promotions/>\n"
+ " <delivery/>\n"
+ " <preparation>ConsumeInStore</preparation>\n"
+ " </cart>\n"
+ " <payment>\n"
+ " <tender>\n"
+ " <type>SVC</type>\n"
+ " <cardNumber>1111222233334444</cardNumber>\n"
+ " <amountToCharge>1990</amountToCharge>\n"
+ " </tender>\n"
+ " </payment>\n"
+ "</submitOrder>";
}
}