DomXmlMessageValidator extends AbstractMessageValidator<XmlMessageValidationContext> { protected void validateNamespaces(Map<String, String> expectedNamespaces, Message receivedMessage) { if (CollectionUtils.isEmpty(expectedNamespaces)) { return; } if (receivedMessage.getPayload() == null || !StringUtils.hasText(receivedMessage.getPayload(String.class))) { throw new ValidationException("Unable to validate message namespaces - receive message payload was empty"); } log.debug("Start XML namespace validation"); Document received = XMLUtils.parseMessagePayload(receivedMessage.getPayload(String.class)); Map<String, String> foundNamespaces = NamespaceContextBuilder.lookupNamespaces(receivedMessage.getPayload(String.class)); if (foundNamespaces.size() != expectedNamespaces.size()) { throw new ValidationException("Number of namespace declarations not equal for node " + XMLUtils.getNodesPathName(received.getFirstChild()) + " found " + foundNamespaces.size() + " expected " + expectedNamespaces.size()); } for (Entry<String, String> entry : expectedNamespaces.entrySet()) { String namespace = entry.getKey(); String url = entry.getValue(); if (foundNamespaces.containsKey(namespace)) { if (!foundNamespaces.get(namespace).equals(url)) { throw new ValidationException("Namespace '" + namespace + "' values not equal: found '" + foundNamespaces.get(namespace) + "' expected '" + url + "' in reference node " + XMLUtils.getNodesPathName(received.getFirstChild())); } else { if (log.isDebugEnabled()) { log.debug("Validating namespace " + namespace + " value as expected " + url + " - value OK"); } } } else { throw new ValidationException("Missing namespace " + namespace + "(" + url + ") in node " + XMLUtils.getNodesPathName(received.getFirstChild())); } } log.info("XML namespace validation successful: All values OK"); }  @Override void validateMessage(Message receivedMessage, Message controlMessage,
                                TestContext context, XmlMessageValidationContext validationContext); @Override boolean supportsMessageType(String messageType, Message message); void addSchemaRepository(XsdSchemaRepository schemaRepository);  }
@Test public void testExpectDefaultNamespace() { Message message = new DefaultMessage("<root xmlns='http: + "<element attributeA='attribute-value' attributeB='attribute-value'>" + "<sub-element attribute='A'>text-value</sub-element>" + "</element>" + "</root>"); Map<String, String> expectedNamespaces = new HashMap<String, String>(); expectedNamespaces.put("", "http: DomXmlMessageValidator validator = new DomXmlMessageValidator(); validator.validateNamespaces(expectedNamespaces, message); }
@Test public void testExpectNamespace() { Message message = new DefaultMessage("<ns1:root xmlns:ns1='http: + "<ns1:element attributeA='attribute-value' attributeB='attribute-value'>" + "<ns1:sub-element attribute='A'>text-value</ns1:sub-element>" + "</ns1:element>" + "</ns1:root>"); Map<String, String> expectedNamespaces = new HashMap<String, String>(); expectedNamespaces.put("ns1", "http: DomXmlMessageValidator validator = new DomXmlMessageValidator(); validator.validateNamespaces(expectedNamespaces, message); }
@Test public void testExpectMixedNamespaces() { Message message = new DefaultMessage("<root xmlns='http: + "<element attributeA='attribute-value' attributeB='attribute-value'>" + "<sub-element attribute='A'>text-value</sub-element>" + "</element>" + "</root>"); Map<String, String> expectedNamespaces = new HashMap<String, String>(); expectedNamespaces.put("", "http: expectedNamespaces.put("ns1", "http: DomXmlMessageValidator validator = new DomXmlMessageValidator(); validator.validateNamespaces(expectedNamespaces, message); }
@Test public void testExpectMultipleNamespaces() { Message message = new DefaultMessage("<root xmlns='http: + "<element attributeA='attribute-value' attributeB='attribute-value'>" + "<sub-element attribute='A'>text-value</sub-element>" + "</element>" + "</root>"); Map<String, String> expectedNamespaces = new HashMap<String, String>(); expectedNamespaces.put("", "http: expectedNamespaces.put("ns1", "http: expectedNamespaces.put("ns2", "http: DomXmlMessageValidator validator = new DomXmlMessageValidator(); validator.validateNamespaces(expectedNamespaces, message); }
@Test(expectedExceptions = {ValidationException.class}) public void testExpectDefaultNamespaceError() { Message message = new DefaultMessage("<root xmlns='http: + "<element attributeA='attribute-value' attributeB='attribute-value'>" + "<sub-element attribute='A'>text-value</sub-element>" + "</element>" + "</root>"); Map<String, String> expectedNamespaces = new HashMap<String, String>(); expectedNamespaces.put("", "http: DomXmlMessageValidator validator = new DomXmlMessageValidator(); validator.validateNamespaces(expectedNamespaces, message); }
@Test(expectedExceptions = {ValidationException.class}) public void testExpectNamespaceError() { Message message = new DefaultMessage("<ns1:root xmlns:ns1='http: + "<ns1:element attributeA='attribute-value' attributeB='attribute-value'>" + "<ns1:sub-element attribute='A'>text-value</ns1:sub-element>" + "</ns1:element>" + "</ns1:root>"); Map<String, String> expectedNamespaces = new HashMap<String, String>(); expectedNamespaces.put("ns1", "http: DomXmlMessageValidator validator = new DomXmlMessageValidator(); validator.validateNamespaces(expectedNamespaces, message); }
@Test(expectedExceptions = {ValidationException.class}) public void testExpectMixedNamespacesError() { Message message = new DefaultMessage("<root xmlns='http: + "<element attributeA='attribute-value' attributeB='attribute-value'>" + "<sub-element attribute='A'>text-value</sub-element>" + "</element>" + "</root>"); Map<String, String> expectedNamespaces = new HashMap<String, String>(); expectedNamespaces.put("", "http: expectedNamespaces.put("ns1", "http: DomXmlMessageValidator validator = new DomXmlMessageValidator(); validator.validateNamespaces(expectedNamespaces, message); }
@Test(expectedExceptions = {ValidationException.class}) public void testExpectMultipleNamespacesError() { Message message = new DefaultMessage("<root xmlns='http: + "<element attributeA='attribute-value' attributeB='attribute-value'>" + "<sub-element attribute='A'>text-value</sub-element>" + "</element>" + "</root>"); Map<String, String> expectedNamespaces = new HashMap<String, String>(); expectedNamespaces.put("", "http: expectedNamespaces.put("ns1", "http: expectedNamespaces.put("ns2", "http: DomXmlMessageValidator validator = new DomXmlMessageValidator(); validator.validateNamespaces(expectedNamespaces, message); }
@Test(expectedExceptions = {ValidationException.class}) public void testExpectWrongNamespacePrefix() { Message message = new DefaultMessage("<root xmlns='http: + "<element attributeA='attribute-value' attributeB='attribute-value'>" + "<sub-element attribute='A'>text-value</sub-element>" + "</element>" + "</root>"); Map<String, String> expectedNamespaces = new HashMap<String, String>(); expectedNamespaces.put("", "http: expectedNamespaces.put("nswrong", "http: expectedNamespaces.put("ns2", "http: DomXmlMessageValidator validator = new DomXmlMessageValidator(); validator.validateNamespaces(expectedNamespaces, message); }
@Test(expectedExceptions = {ValidationException.class}) public void testExpectDefaultNamespaceButNamespace() { Message message = new DefaultMessage("<ns0:root xmlns:ns0='http: + "<ns0:element attributeA='attribute-value' attributeB='attribute-value'>" + "<ns0:sub-element attribute='A'>text-value</ns0:sub-element>" + "</ns0:element>" + "</ns0:root>"); Map<String, String> expectedNamespaces = new HashMap<String, String>(); expectedNamespaces.put("", "http: expectedNamespaces.put("ns1", "http: expectedNamespaces.put("ns2", "http: DomXmlMessageValidator validator = new DomXmlMessageValidator(); validator.validateNamespaces(expectedNamespaces, message); }
@Test(expectedExceptions = {ValidationException.class}) public void testExpectNamespaceButDefaultNamespace() { Message message = new DefaultMessage("<root xmlns='http: + "<element attributeA='attribute-value' attributeB='attribute-value'>" + "<sub-element attribute='A'>text-value</sub-element>" + "</element>" + "</root>"); Map<String, String> expectedNamespaces = new HashMap<String, String>(); expectedNamespaces.put("ns0", "http: expectedNamespaces.put("ns1", "http: expectedNamespaces.put("ns2", "http: DomXmlMessageValidator validator = new DomXmlMessageValidator(); validator.validateNamespaces(expectedNamespaces, message); }
@Test(expectedExceptions = {ValidationException.class}) public void testExpectAdditionalNamespace() { Message message = new DefaultMessage("<root xmlns='http: + "<element attributeA='attribute-value' attributeB='attribute-value'>" + "<sub-element attribute='A'>text-value</sub-element>" + "</element>" + "</root>"); Map<String, String> expectedNamespaces = new HashMap<String, String>(); expectedNamespaces.put("", "http: expectedNamespaces.put("ns1", "http: expectedNamespaces.put("ns2", "http: expectedNamespaces.put("ns4", "http: DomXmlMessageValidator validator = new DomXmlMessageValidator(); validator.validateNamespaces(expectedNamespaces, message); }
@Test(expectedExceptions = {ValidationException.class}) public void testExpectNamespaceButNamespaceMissing() { Message message = new DefaultMessage("<root xmlns='http: + "<element attributeA='attribute-value' attributeB='attribute-value'>" + "<sub-element attribute='A'>text-value</sub-element>" + "</element>" + "</root>"); Map<String, String> expectedNamespaces = new HashMap<String, String>(); expectedNamespaces.put("", "http: expectedNamespaces.put("ns1", "http: expectedNamespaces.put("ns2", "http: DomXmlMessageValidator validator = new DomXmlMessageValidator(); validator.validateNamespaces(expectedNamespaces, message); }