@Test public void xmlPathCanExtractNodeFromSoap() throws Exception { // Given String soap = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" + "<env:Envelope \n" + " xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" \n" + " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" \n" + " xmlns:env=\"http://schemas.xmlsoap.org/soap/envelope/\" \n" + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" + " <env:Header/>\n" + "\n" + "<env:Body>\n" + " <n1:importProjectResponse \n" + " xmlns:n1=\"n1\" \n" + " xmlns:n2=\"n2\" \n" + " xsi:type=\"n2:ArrayOfProjectImportResultCode\">\n" + " <n2:ProjectImportResultCode>\n" + " <n2:code>1</n2:code>\n" + " <n2:message>Project 'test1' import was successful.</n2:message>\n" + " </n2:ProjectImportResultCode>\n" + " </n1:importProjectResponse>\n" + "</env:Body></env:Envelope>"; // When XmlPath xmlPath = new XmlPath(soap); Node node = xmlPath.getNode("Envelope"); // Then assertThat( node.<String>getPath("Body.importProjectResponse.ProjectImportResultCode.code"), equalTo("1")); }
@Test public void firstCategoryAttributeFromJava() throws Exception { Node node = with(XML).get("shopping.category[0]"); assertThat(node.getAttribute("@type"), equalTo("groceries")); assertThat(node.getAttribute("type"), equalTo("groceries")); assertThat((String) node.get("@type"), equalTo("groceries")); }
@Test public void gettingChildrenFromJava() throws Exception { Node category = with(XML).get("shopping.category[0]"); final NodeChildren categoryChildren = category.children(); assertThat(categoryChildren.size(), equalTo(2)); for (Node item : categoryChildren.nodeIterable()) { assertThat(item.children().size(), equalTo(2)); final Node name = item.get("name"); final Node price = item.get("price"); assertThat(name.value(), anyOf(equalTo("Chocolate"), equalTo("Coffee"))); assertThat(price.value(), anyOf(equalTo("10"), equalTo("20"))); } }
@Test public void itemsWithPriceBetweenTenAndTwenty() throws Exception { final List<Node> itemsBetweenTenAndTwenty = with(XML) .get( "shopping.category.item.findAll { item -> def price = item.price.toFloat(); price >= 10 && price <= 20 }"); assertThat(itemsBetweenTenAndTwenty.size(), equalTo(3)); final Node category1 = itemsBetweenTenAndTwenty.get(0); final NodeChildren categoryChildren = category1.children(); assertThat(categoryChildren, hasItems("Chocolate", "10")); for (Node item : categoryChildren.nodeIterable()) { assertThat(item.name(), anyOf(equalTo("name"), equalTo("price"))); } }
@Test public void getEntireObjectGraph() throws Exception { final Node node = with(XML).get(); assertThat(node.name(), is("shopping")); }
@Test public void convertsRootObjectGraphToJavaObjects() throws Exception { Node objects = with(XML).get("shopping"); assertThat( objects.toString(), equalTo("Chocolate10Coffee20Paper5Pens15.5Kathryn's Birthday200")); }