|
19 | 19 | package org.apache.axis2.validation; |
20 | 20 |
|
21 | 21 | import static org.assertj.core.api.Assertions.assertThat; |
| 22 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
22 | 23 |
|
| 24 | +import java.io.ByteArrayInputStream; |
| 25 | +import java.nio.charset.StandardCharsets; |
| 26 | + |
| 27 | +import javax.xml.transform.stream.StreamSource; |
| 28 | + |
| 29 | +import org.apache.axiom.om.OMAbstractFactory; |
| 30 | +import org.apache.axiom.om.OMElement; |
| 31 | +import org.apache.axiom.om.OMNamespace; |
| 32 | +import org.apache.axiom.soap.SOAPBody; |
| 33 | +import org.apache.axiom.soap.SOAPEnvelope; |
| 34 | +import org.apache.axiom.soap.SOAPFactory; |
| 35 | +import org.apache.axis2.AxisFault; |
| 36 | +import org.apache.axis2.context.ConfigurationContext; |
| 37 | +import org.apache.axis2.context.MessageContext; |
| 38 | +import org.apache.axis2.description.AxisService; |
| 39 | +import org.apache.axis2.engine.AxisConfiguration; |
| 40 | +import org.apache.ws.commons.schema.XmlSchema; |
| 41 | +import org.apache.ws.commons.schema.XmlSchemaCollection; |
23 | 42 | import org.junit.jupiter.api.Test; |
24 | 43 | import org.xml.sax.SAXException; |
25 | 44 |
|
@@ -48,4 +67,68 @@ public void testAppendRefHintHandlesNullMessage() { |
48 | 67 | String hint = SchemaValidationHandler.appendRefHint(ex); |
49 | 68 | assertThat(hint).isEmpty(); |
50 | 69 | } |
| 70 | + |
| 71 | + /** |
| 72 | + * Integration test: invoke the handler with a SOAP message containing an |
| 73 | + * attribute not declared in the schema, triggering cvc-complex-type.3.2.2, |
| 74 | + * and verify the AxisFault message includes the diagnostic ref= hint. |
| 75 | + */ |
| 76 | + @Test |
| 77 | + public void testInvokeProducesRefHintForUnexpectedAttribute() throws Exception { |
| 78 | + // Schema that defines <note> with child elements only — no attributes allowed |
| 79 | + String xsd = |
| 80 | + "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'" |
| 81 | + + " targetNamespace='http://example.com/test'" |
| 82 | + + " xmlns:tns='http://example.com/test'" |
| 83 | + + " elementFormDefault='qualified'>" |
| 84 | + + " <xs:element name='note'>" |
| 85 | + + " <xs:complexType>" |
| 86 | + + " <xs:sequence>" |
| 87 | + + " <xs:element name='to' type='xs:string'/>" |
| 88 | + + " </xs:sequence>" |
| 89 | + + " </xs:complexType>" |
| 90 | + + " </xs:element>" |
| 91 | + + "</xs:schema>"; |
| 92 | + |
| 93 | + XmlSchemaCollection schemaCol = new XmlSchemaCollection(); |
| 94 | + XmlSchema schema = schemaCol.read(new StreamSource( |
| 95 | + new ByteArrayInputStream(xsd.getBytes(StandardCharsets.UTF_8)))); |
| 96 | + |
| 97 | + // AxisService with the schema |
| 98 | + AxisService service = new AxisService("TestService"); |
| 99 | + service.addSchema(schema); |
| 100 | + |
| 101 | + // MessageContext |
| 102 | + ConfigurationContext configCtx = new ConfigurationContext(new AxisConfiguration()); |
| 103 | + MessageContext msgCtx = configCtx.createMessageContext(); |
| 104 | + msgCtx.setAxisService(service); |
| 105 | + |
| 106 | + // SOAP envelope whose body element has an attribute the schema doesn't allow |
| 107 | + SOAPFactory sf = OMAbstractFactory.getSOAP11Factory(); |
| 108 | + SOAPEnvelope envelope = sf.createSOAPEnvelope(); |
| 109 | + SOAPBody body = sf.createSOAPBody(envelope); |
| 110 | + |
| 111 | + OMNamespace tns = sf.createOMNamespace("http://example.com/test", "tns"); |
| 112 | + OMElement note = sf.createOMElement("note", tns); |
| 113 | + |
| 114 | + // Add the required child so the only error is the unexpected attribute |
| 115 | + OMElement to = sf.createOMElement("to", tns); |
| 116 | + to.setText("Alice"); |
| 117 | + note.addChild(to); |
| 118 | + |
| 119 | + // Add an attribute that the schema does not declare — triggers cvc-complex-type.3.2.2 |
| 120 | + OMNamespace xmimeNs = sf.createOMNamespace("http://www.w3.org/2005/05/xmlmime", "xmime"); |
| 121 | + note.addAttribute("contentType", "text/xml", xmimeNs); |
| 122 | + |
| 123 | + body.addChild(note); |
| 124 | + msgCtx.setEnvelope(envelope); |
| 125 | + |
| 126 | + // Invoke the handler and assert the AxisFault contains the hint |
| 127 | + SchemaValidationHandler handler = new SchemaValidationHandler(); |
| 128 | + assertThatThrownBy(() -> handler.invoke(msgCtx)) |
| 129 | + .isInstanceOf(AxisFault.class) |
| 130 | + .hasMessageContaining("cvc-complex-type.3.2.2") |
| 131 | + .hasMessageContaining("xs:attribute ref=") |
| 132 | + .hasMessageContaining("not imported or could not be resolved"); |
| 133 | + } |
51 | 134 | } |
0 commit comments