001: package org.netbeans.modules.xml.wsdl.model.readwrite;
002:
003: import java.io.File;
004: import java.util.ArrayList;
005: import java.util.Arrays;
006: import java.util.Collection;
007: import java.util.Iterator;
008: import java.util.List;
009: import junit.framework.*;
010: import org.netbeans.junit.NbTestCase;
011: import org.netbeans.modules.xml.schema.model.GlobalType;
012: import org.netbeans.modules.xml.wsdl.model.Binding;
013: import org.netbeans.modules.xml.wsdl.model.BindingFault;
014: import org.netbeans.modules.xml.wsdl.model.BindingInput;
015: import org.netbeans.modules.xml.wsdl.model.BindingOperation;
016: import org.netbeans.modules.xml.wsdl.model.BindingOutput;
017: import org.netbeans.modules.xml.wsdl.model.Definitions;
018: import org.netbeans.modules.xml.wsdl.model.Input;
019: import org.netbeans.modules.xml.wsdl.model.Message;
020: import org.netbeans.modules.xml.wsdl.model.Operation;
021: import org.netbeans.modules.xml.wsdl.model.Output;
022: import org.netbeans.modules.xml.wsdl.model.Part;
023: import org.netbeans.modules.xml.wsdl.model.Port;
024: import org.netbeans.modules.xml.wsdl.model.PortType;
025: import org.netbeans.modules.xml.wsdl.model.RequestResponseOperation;
026: import org.netbeans.modules.xml.wsdl.model.Service;
027: import org.netbeans.modules.xml.wsdl.model.TestCatalogModel;
028: import org.netbeans.modules.xml.wsdl.model.Types;
029: import org.netbeans.modules.xml.wsdl.model.Util;
030: import org.netbeans.modules.xml.wsdl.model.WSDLComponent;
031: import org.netbeans.modules.xml.wsdl.model.WSDLComponentFactory;
032: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
033: import org.netbeans.modules.xml.wsdl.model.extensions.soap.SOAPAddress;
034: import org.netbeans.modules.xml.wsdl.model.extensions.soap.SOAPBinding;
035: import org.netbeans.modules.xml.wsdl.model.extensions.soap.SOAPBody;
036: import org.netbeans.modules.xml.wsdl.model.extensions.soap.SOAPFault;
037: import org.netbeans.modules.xml.wsdl.model.extensions.soap.SOAPMessageBase;
038: import org.netbeans.modules.xml.wsdl.model.extensions.soap.SOAPOperation;
039: import org.netbeans.modules.xml.wsdl.model.extensions.xsd.WSDLSchema;
040: import org.netbeans.modules.xml.wsdl.model.visitor.FindWSDLComponent;
041: import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
042: import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
043:
044: /**
045: *
046: * @author Nam Nguyen
047: */
048: public class SimpleTest extends NbTestCase {
049:
050: public SimpleTest(String testName) {
051: super (testName);
052: }
053:
054: protected void setUp() throws Exception {
055: }
056:
057: protected void tearDown() throws Exception {
058: TestCatalogModel.getDefault().clearDocumentPool();
059: }
060:
061: public void testWrite() throws Exception {
062: WSDLModel model = Util.loadWSDLModel("resources/empty.wsdl");
063: WSDLComponentFactory fact = model.getFactory();
064:
065: model.startTransaction();
066: Definitions d = model.getDefinitions();
067: d.setName("HelloService");
068: d.setTargetNamespace("urn:HelloService/wsdl");
069: Types types = fact.createTypes();
070: d.setTypes(types);
071: WSDLSchema wsdlSchema = fact.createWSDLSchema();
072: types.addExtensibilityElement(wsdlSchema);
073: wsdlSchema.getSchemaModel().getSchema().setTargetNamespace(
074: "urn:HelloService/wsdl");
075:
076: Message m1 = fact.createMessage();
077: Message m2 = fact.createMessage();
078: d.addMessage(m1);
079: d.addMessage(m2);
080: m1.setName("HelloServiceSEI_sayHello");
081: m2.setName("HelloServiceSEI_sayHelloResponse");
082: Part p = fact.createPart();
083: p.setName("String_1"); //TODO test setType
084: p.setType(p.createSchemaReference(Util
085: .getPrimitiveType("string"), GlobalType.class));
086: m1.addPart(p);
087: p = fact.createPart();
088: p.setName("String_2");
089: p.setType(p.createSchemaReference(Util
090: .getPrimitiveType("string"), GlobalType.class));
091: m1.addPart(p);
092: p = fact.createPart();
093: p.setName("result");
094: p.setType(p.createSchemaReference(Util
095: .getPrimitiveType("string"), GlobalType.class));
096: m2.addPart(p);
097:
098: PortType pt = fact.createPortType();
099: d.addPortType(pt);
100: pt.setName("HelloServiceSEI");
101: RequestResponseOperation op = fact
102: .createRequestResponseOperation();
103: pt.addOperation(op);
104: op.setName("sayHello");
105: op.setParameterOrder(Arrays.asList(new String[] { "String_1",
106: "String_2" }));
107: Input in = fact.createInput();
108: op.setInput(in);
109: in.setMessage(in.createReferenceTo(m1, Message.class));
110: NamedComponentReference<Message> msgRef = in.getMessage();
111:
112: assertEquals("setTargetNamespace should declare prefix",
113: "tns:HelloServiceSEI_sayHello", msgRef.getRefString());
114:
115: Output out = fact.createOutput();
116: op.setOutput(out);
117: out.setMessage(out.createReferenceTo(m2, Message.class));
118:
119: Binding b = fact.createBinding();
120: d.addBinding(b);
121: b.setName("HelloServiceSEIBinding");
122: b.setType(b.createReferenceTo(pt, PortType.class));
123: BindingOperation bo = fact.createBindingOperation();
124: b.addBindingOperation(bo);
125: bo.setName("sayHello");
126: SOAPBinding sb = fact.createSOAPBinding();
127: sb.setTransportURI("http://schemas.xmlsoap.org/soap/http");
128: sb.setStyle(SOAPBinding.Style.RPC);
129: b.addExtensibilityElement(sb);
130: SOAPOperation soo = fact.createSOAPOperation();
131: bo.addExtensibilityElement(soo);
132: soo.setSoapAction("");
133:
134: assertTrue(b.getChildren().get(0) == sb);
135: assertTrue(b.getChildren().get(1) == bo);
136:
137: BindingInput bin = fact.createBindingInput();
138: bo.setBindingInput(bin);
139: SOAPBody body = fact.createSOAPBody();
140: bin.addExtensibilityElement(body);
141: body.setUse(SOAPMessageBase.Use.LITERAL);
142: body.setNamespace("urn:HelloService/wsdl");
143:
144: BindingOutput bout = fact.createBindingOutput();
145: bo.setBindingOutput(bout);
146: body = fact.createSOAPBody();
147: bout.addExtensibilityElement(body);
148: body.setUse(SOAPMessageBase.Use.LITERAL);
149: body.setNamespace("urn:HelloService/wsdl");
150:
151: BindingFault bfault = fact.createBindingFault();
152: bo.addBindingFault(bfault);
153: SOAPFault sFault = fact.createSOAPFault();
154: bfault.addExtensibilityElement(sFault);
155: sFault.setUse(SOAPMessageBase.Use.LITERAL);
156: sFault.setNamespace("urn:HelloService/wsdl");
157:
158: //test the order in which children were added in binding operation
159: List children = bo.getChildren();
160: Iterator it = children.iterator();
161: assertEquals("binding.operation.soapOperation", soo, it.next());
162: assertEquals("binding.operation.input", bin, it.next());
163: assertEquals("binding.operation.output", bout, it.next());
164: assertEquals("binding.operation.fault", bfault, it.next());
165:
166: Service service = fact.createService();
167: d.addService(service);
168: service.setName("HelloService");
169: Port port = fact.createPort();
170: service.addPort(port);
171: port.setName("HelloServiceSEIPort");
172: port.setBinding(port.createReferenceTo(b, Binding.class));
173: SOAPAddress sad = fact.createSOAPAddress();
174: port.addExtensibilityElement(sad);
175: sad.setLocation("REPLACE_WITH_ACTUAL_URL");
176:
177: model.endTransaction();
178:
179: //Util.dumpToFile(model.getBaseDocument(), new File("C:\\temp\\HelloService.wsdl"));
180: readAndCheck(model);
181: }
182:
183: public void testRead() throws Exception {
184: WSDLModel model = Util.loadWSDLModel(getTestResourcePath());
185: readAndCheck(model);
186: }
187:
188: private void readAndCheck(WSDLModel model) {
189: Definitions d = model.getDefinitions();
190: Collection<Message> messages = d.getMessages();
191: assertEquals("read.message.count", 2, messages.size());
192: Iterator<Message> it = messages.iterator();
193: it.next();
194: Message m = it.next();
195: AbstractDocumentComponent acm = (AbstractDocumentComponent) m;
196: String prefix = acm.getPeer().getPrefix();
197: assertTrue("wsdl prefix is not null or empty: " + prefix,
198: prefix == null || prefix.equals(""));
199: assertEquals("read.message.name",
200: "HelloServiceSEI_sayHelloResponse", m.getName());
201: List<Part> parts = new ArrayList(m.getParts());
202: assertEquals("read.message.part.name", "result", parts
203: .iterator().next().getName());
204: assertEquals("string", parts.get(0).getType().get().getName());
205:
206: Collection<PortType> porttypes = d.getPortTypes();
207: assertEquals("read.portType", 1, porttypes.size());
208: PortType pt = porttypes.iterator().next();
209: assertEquals("read.portType.name", "HelloServiceSEI", pt
210: .getName());
211: Operation op = pt.getOperations().iterator().next();
212: assertTrue("read.portType.operation",
213: op instanceof RequestResponseOperation);
214: assertEquals("read.portType.operation.parameterOrder",
215: "[String_1, String_2]", op.getParameterOrder()
216: .toString());
217:
218: Message m1 = d.getMessages().iterator().next();
219: assertEquals("message[1].name", "HelloServiceSEI_sayHello", m1
220: .getName());
221: assertEquals("message[1].parts.count=2", 2, m1.getParts()
222: .size());
223:
224: String xpath0 = "/definitions/message[1]";
225: WSDLComponent found = (WSDLComponent) new FindWSDLComponent()
226: .findComponent(model.getRootComponent(), xpath0);
227: assertTrue(xpath0, found instanceof Message);
228: Message m1x = (Message) found;
229: assertEquals("write.xpath", m1, m1x);
230:
231: RequestResponseOperation rro = (RequestResponseOperation) op;
232: assertEquals("operation.name", "sayHello", rro.getName());
233: Input in = rro.getInput();
234: assertEquals("portType.operation.input.message", messages
235: .iterator().next(), in.getMessage().get());
236:
237: Output out = rro.getOutput();
238: assertEquals("portType.operation.output.message", m.getName(),
239: out.getMessage().get().getName());
240:
241: Binding b = d.getBindings().iterator().next();
242: Collection<SOAPBinding> soapB = b
243: .getExtensibilityElements(SOAPBinding.class);
244: SOAPBinding sb = soapB.iterator().next();
245: assertEquals("binding.soap.style", SOAPBinding.Style.RPC, sb
246: .getStyle());
247: assertEquals("binding.soap.uri",
248: "http://schemas.xmlsoap.org/soap/http", sb
249: .getTransportURI());
250:
251: BindingOperation bo = b.getBindingOperations().iterator()
252: .next();
253: Collection<SOAPOperation> soapOps = bo
254: .getExtensibilityElements(SOAPOperation.class);
255: assertEquals("binding.soap.style", SOAPBinding.Style.RPC,
256: soapOps.iterator().next().getStyle());
257: assertEquals("binding.soap.uri", "", soapOps.iterator().next()
258: .getSoapAction());
259:
260: assertEquals("binding.type", pt, b.getType().get());
261: assertEquals("binding.name", "HelloServiceSEIBinding", b
262: .getName());
263: assertEquals("sayHelloRequest", bo.getBindingInput().getInput()
264: .get().getName());
265: SOAPBody body = (SOAPBody) bo.getBindingInput()
266: .getExtensibilityElements().iterator().next();
267: assertEquals("binding.operation.input", SOAPBody.Use.LITERAL,
268: body.getUse());
269: SOAPBody body2 = (SOAPBody) bo.getBindingOutput()
270: .getExtensibilityElements().iterator().next();
271: assertEquals("binding.operation.output",
272: "urn:HelloService/wsdl", body2.getNamespace());
273:
274: Collection<BindingFault> bFaults = bo.getBindingFaults();
275: assertEquals("binding.operation.faults", 1, bFaults.size());
276: BindingFault fault = bFaults.iterator().next();
277: SOAPFault sfault = (SOAPFault) fault.getExtensibilityElements()
278: .iterator().next();
279: assertEquals("binding.operation.fault",
280: "urn:HelloService/wsdl", sfault.getNamespace());
281:
282: Service s = d.getServices().iterator().next();
283: assertEquals("serice.name", "HelloService", s.getName());
284: Port p = s.getPorts().iterator().next();
285: assertEquals("service.port.name", "HelloServiceSEIPort", p
286: .getName());
287: Binding binding = p.getBinding().get();
288: assertEquals("service.port.binding", b, binding);
289: SOAPAddress soapAddress = p.getExtensibilityElements(
290: SOAPAddress.class).iterator().next();
291: assertEquals("service.port.soapAddress",
292: "REPLACE_WITH_ACTUAL_URL", soapAddress.getLocation());
293:
294: }
295:
296: public String getTestResourcePath() {
297: return "resources/HelloService.wsdl";
298: }
299:
300: public void testPrefixPreference() throws Exception {
301: WSDLModel model = Util.loadWSDLModel("resources/empty.wsdl");
302: model.startTransaction();
303: model.getDefinitions().setTypes(
304: model.getFactory().createTypes());
305: model.endTransaction();
306: assertNull(model.getDefinitions().getTypes().getPeer()
307: .getPrefix());
308: assertNull(model.getDefinitions().getTypes().getPeer()
309: .getAttribute("xmlns:wsdl"));
310: assertNull(model.getDefinitions().getTypes().getPeer()
311: .getAttribute("xmlns"));
312: //Util.dumpToFile(model.getBaseDocument(), new File(getWorkDir(), "test.wsdl"));
313: }
314: }
|