-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
ValidationClientServerTest.java
363 lines (311 loc) · 15 KB
/
ValidationClientServerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.cxf.systest.schema_validation;
import java.io.Serializable;
import java.io.StringReader;
import java.net.URL;
import java.util.List;
import javax.xml.namespace.QName;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service.Mode;
import javax.xml.ws.WebServiceException;
import org.apache.cxf.annotations.SchemaValidation.SchemaValidationType;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.feature.LoggingFeature;
import org.apache.cxf.message.Message;
import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
import org.apache.schema_validation.DoSomethingFault;
import org.apache.schema_validation.SchemaValidation;
import org.apache.schema_validation.SchemaValidationService;
import org.apache.schema_validation.types.ComplexStruct;
import org.apache.schema_validation.types.OccuringStruct;
import org.apache.schema_validation.types.SomeRequest;
import org.apache.schema_validation.types.SomeResponse;
import org.junit.BeforeClass;
import org.junit.Test;
public class ValidationClientServerTest extends AbstractBusClientServerTestBase {
public static final String PORT = ValidationServer.PORT;
private final QName serviceName = new QName("http://apache.org/schema_validation",
"SchemaValidationService");
private final QName portName = new QName("http://apache.org/schema_validation", "SoapPort");
@BeforeClass
public static void startservers() throws Exception {
assertTrue("server did not launch correctly", launchServer(ValidationServer.class, true));
}
@Test
public void testSchemaValidationProviderPayload() throws Exception {
doProviderTest("PProvider");
}
@Test
public void testSchemaValidationProviderMessage() throws Exception {
doProviderTest("MProvider");
}
private void doProviderTest(String postfix) throws Exception {
SchemaValidation validation = createService(Boolean.FALSE, postfix);
SomeRequest req = new SomeRequest();
req.setId("9999999999");
try {
validation.doSomething(req);
fail("Should have faulted");
} catch (DoSomethingFault e) {
assertEquals("1234", e.getFaultInfo().getErrorCode());
}
req.setId("8888888888");
try {
validation.doSomething(req);
fail("Should have faulted");
} catch (DoSomethingFault e) {
fail("Should not have happened");
} catch (WebServiceException e) {
String expected = "Value '1' is not facet-valid";
assertTrue(e.getMessage().indexOf(expected) != -1);
}
((java.io.Closeable)validation).close();
}
@Test
public void testSchemaValidationServer() throws Exception {
SchemaValidation validation = createService(Boolean.FALSE, "SoapPortValidate");
runSchemaValidationTest(validation);
((java.io.Closeable)validation).close();
}
@Test
public void testSchemaValidationServerForMethod() throws Exception {
SchemaValidation validation = createService(Boolean.FALSE, "SoapPortMethodValidate");
ComplexStruct complexStruct = new ComplexStruct();
complexStruct.setElem1("one");
complexStruct.setElem3(3);
try {
validation.setComplexStruct(complexStruct);
fail("Set ComplexStruct should have thrown ProtocolException");
} catch (WebServiceException e) {
String expected = "'{\"http://apache.org/schema_validation/types\":elem2}' is expected.";
assertTrue(e.getMessage(), e.getMessage().indexOf(expected) != -1);
}
SchemaValidation novlidation = createService(Boolean.FALSE, "SoapPort");
try {
novlidation.setComplexStruct(complexStruct);
} catch (WebServiceException e) {
fail("Exception is not expected :" + e);
}
}
@Test
public void testSchemaValidationClient() throws Exception {
SchemaValidation validation = createService(Boolean.TRUE, "SoapPort");
runSchemaValidationTest(validation);
((java.io.Closeable)validation).close();
}
private void runSchemaValidationTest(SchemaValidation validation) {
ComplexStruct complexStruct = new ComplexStruct();
complexStruct.setElem1("one");
// Don't initialize a member of the structure.
// Client side validation should throw an exception.
// complexStruct.setElem2("two");
complexStruct.setElem3(3);
try {
/*boolean result =*/
validation.setComplexStruct(complexStruct);
fail("Set ComplexStruct should have thrown ProtocolException");
} catch (WebServiceException e) {
String expected = "'{\"http://apache.org/schema_validation/types\":elem2}' is expected.";
assertTrue(e.getMessage(), e.getMessage().indexOf(expected) != -1);
}
OccuringStruct occuringStruct = new OccuringStruct();
// Populate the list in the wrong order.
// Client side validation should throw an exception.
List<Serializable> floatIntStringList = occuringStruct.getVarFloatAndVarIntAndVarString();
floatIntStringList.add(new Integer(42));
floatIntStringList.add(new Float(4.2f));
floatIntStringList.add("Goofus and Gallant");
try {
/*boolean result =*/
validation.setOccuringStruct(occuringStruct);
fail("Set OccuringStruct should have thrown ProtocolException");
} catch (WebServiceException e) {
String expected = "'{\"http://apache.org/schema_validation/types\":varFloat}' is expected.";
assertTrue(e.getMessage().indexOf(expected) != -1);
}
try {
// The server will attempt to return an invalid ComplexStruct
// When validation is disabled on the server side, we'll get the
// exception while unmarshalling the invalid response.
/*complexStruct =*/
validation.getComplexStruct("Hello");
fail("Get ComplexStruct should have thrown ProtocolException");
} catch (WebServiceException e) {
String expected = "'{\"http://apache.org/schema_validation/types\":elem2}' is expected.";
assertTrue("Found message " + e.getMessage(),
e.getMessage().indexOf(expected) != -1);
}
try {
// The server will attempt to return an invalid OccuringStruct
// When validation is disabled on the server side, we'll get the
// exception while unmarshalling the invalid response.
/*occuringStruct =*/
validation.getOccuringStruct("World");
fail("Get OccuringStruct should have thrown ProtocolException");
} catch (WebServiceException e) {
String expected = "'{\"http://apache.org/schema_validation/types\":varFloat}' is expected.";
assertTrue(e.getMessage().indexOf(expected) != -1);
}
SomeRequest req = new SomeRequest();
req.setId("9999999999");
try {
validation.doSomething(req);
fail("Should have faulted");
} catch (DoSomethingFault e) {
assertEquals("1234", e.getFaultInfo().getErrorCode());
}
req.setId("8888888888");
try {
validation.doSomething(req);
fail("Should have faulted");
} catch (DoSomethingFault e) {
fail("Should not have happened");
} catch (WebServiceException e) {
String expected = "Value '1' is not facet-valid";
assertTrue(e.getMessage().indexOf(expected) != -1);
}
}
@Test
public void testRequestFailedSchemaValidation() throws Exception {
assertFailedRequestValidation(Boolean.TRUE);
}
@Test
public void testFailedRequestSchemaValidationTypeBoth() throws Exception {
assertFailedRequestValidation(SchemaValidationType.BOTH.name());
}
@Test
public void testFailedSchemaValidationSchemaValidationTypeOut() throws Exception {
assertFailedRequestValidation(SchemaValidationType.OUT.name());
}
@Test
public void testIgnoreRequestSchemaValidationNone() throws Exception {
assertIgnoredRequestValidation(SchemaValidationType.NONE.name());
}
@Test
public void testIgnoreRequestSchemaValidationResponseOnly() throws Exception {
assertIgnoredRequestValidation(SchemaValidationType.IN.name());
}
@Test
public void testIgnoreRequestSchemaValidationFalse() throws Exception {
assertIgnoredRequestValidation(Boolean.FALSE);
}
@Test
public void testResponseFailedSchemaValidation() throws Exception {
assertFailureResponseValidation(Boolean.TRUE);
}
@Test
public void testResponseSchemaFailedValidationBoth() throws Exception {
assertFailureResponseValidation(SchemaValidationType.BOTH.name());
}
@Test
public void testResponseSchemaFailedValidationIn() throws Exception {
assertFailureResponseValidation(SchemaValidationType.IN.name());
}
@Test
public void testIgnoreResponseSchemaFailedValidationNone() throws Exception {
assertIgnoredResponseValidation(SchemaValidationType.NONE.name());
}
@Test
public void testIgnoreResponseSchemaFailedValidationFalse() throws Exception {
assertIgnoredResponseValidation(Boolean.FALSE);
}
@Test
public void testIgnoreResponseSchemaFailedValidationOut() throws Exception {
assertIgnoredResponseValidation(SchemaValidationType.OUT.name());
}
@Test
public void testHeaderValidation() throws Exception {
URL wsdl = getClass().getResource("/wsdl/schema_validation.wsdl");
assertNotNull(wsdl);
SchemaValidationService service = new SchemaValidationService(wsdl, serviceName);
assertNotNull(service);
String smsg = "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'><soap:Header>"
+ "<SomeHeader soap:mustUnderstand='1' xmlns='http://apache.org/schema_validation/types'>"
+ "<id>1111111111</id></SomeHeader>"
+ "</soap:Header><soap:Body><SomeRequestWithHeader xmlns='http://apache.org/schema_validation/types'>"
+ "<id>1111111111</id></SomeRequestWithHeader></soap:Body></soap:Envelope>";
Dispatch<Source> dsp = service.createDispatch(SchemaValidationService.SoapPort, Source.class, Mode.MESSAGE);
updateAddressPort(dsp, PORT);
dsp.invoke(new StreamSource(new StringReader(smsg)));
}
private SomeResponse execute(SchemaValidation service, String id) throws Exception {
SomeRequest request = new SomeRequest();
request.setId(id);
return service.doSomething(request);
}
private void assertFailureResponseValidation(Object validationConfig) throws Exception {
SchemaValidation service = createService(validationConfig);
SomeResponse response = execute(service, "1111111111"); // valid request
assertEquals(response.getTransactionId(), "aaaaaaaaaa");
try {
execute(service, "1234567890"); // valid request, but will result in invalid response
fail("should catch marshall exception as the invalid incoming message per schema");
} catch (Exception e) {
assertTrue(e.getMessage().contains("Unmarshalling Error"));
assertTrue(e.getMessage().contains("is not facet-valid with respect to pattern"));
}
((java.io.Closeable)service).close();
}
private void assertIgnoredRequestValidation(Object validationConfig) throws Exception {
SchemaValidation service = createService(validationConfig);
// this is an invalid request but validation is turned off.
SomeResponse response = execute(service, "1234567890aaaa");
assertEquals(response.getTransactionId(), "aaaaaaaaaa");
((java.io.Closeable)service).close();
}
private void assertIgnoredResponseValidation(Object validationConfig) throws Exception {
SchemaValidation service = createService(validationConfig);
// the request will result in invalid response but validation is turned off
SomeResponse response = execute(service, "1234567890");
assertEquals(response.getTransactionId(), "aaaaaaaaaaxxx");
((java.io.Closeable)service).close();
}
private void assertFailedRequestValidation(Object validationConfig) throws Exception {
SchemaValidation service = createService(validationConfig);
SomeResponse response = execute(service, "1111111111");
assertEquals(response.getTransactionId(), "aaaaaaaaaa");
try {
execute(service, "1234567890aaa");
fail("should catch marshall exception as the invalid outgoing message per schema");
} catch (Exception e) {
assertTrue(e.getMessage().contains("Marshalling Error"));
assertTrue(e.getMessage().contains("is not facet-valid with respect to pattern"));
}
((java.io.Closeable)service).close();
}
private SchemaValidation createService(Object validationConfig) throws Exception {
return createService(validationConfig, "SoapPort");
}
private SchemaValidation createService(Object validationConfig, String postfix) throws Exception {
URL wsdl = getClass().getResource("/wsdl/schema_validation.wsdl");
assertNotNull(wsdl);
SchemaValidationService service = new SchemaValidationService(wsdl, serviceName);
assertNotNull(service);
SchemaValidation validation = service.getPort(portName, SchemaValidation.class);
setAddress(validation, "http://localhost:" + PORT + "/SoapContext/" + postfix);
((BindingProvider)validation).getRequestContext().put(Message.SCHEMA_VALIDATION_ENABLED, validationConfig);
((BindingProvider)validation).getResponseContext().put(Message.SCHEMA_VALIDATION_ENABLED, validationConfig);
new LoggingFeature().initialize((Client)validation, getBus());
return validation;
}
}