import javax.xml.namespace.QName;
import java.util.List;
public class WSDLTest extends BaseTestCase {
public void testAddWSDL() throws Exception {
WsdlManager wsdlManager = new WsdlManager(registry);
Wsdl wsdl = wsdlManager.newWsdl("http://svn.wso2.org/repos/wso2/trunk/carbon/components/governance/org.wso2.carbon.governance.api/src/test/resources/test-resources/wsdl/BizService.wsdl");
wsdl.addAttribute("creator", "it is me");
wsdl.addAttribute("version", "0.01");
wsdlManager.addWsdl(wsdl);
Wsdl newWsdl = wsdlManager.getWsdl(wsdl.getId());
assertEquals(wsdl.getWsdlElement().toString(), newWsdl.getWsdlElement().toString());
assertEquals("it is me", newWsdl.getAttribute("creator"));
assertEquals("0.01", newWsdl.getAttribute("version"));
// change the target namespace and check
String oldWSDLPath = newWsdl.getPath();
assertEquals(oldWSDLPath, "/wsdls/com/foo/BizService.wsdl");
assertTrue(registry.resourceExists("/wsdls/com/foo/BizService.wsdl"));
OMElement wsdlElement = newWsdl.getWsdlElement();
wsdlElement.addAttribute("targetNamespace", "http://ww2.wso2.org/test", null);
wsdlElement.declareNamespace("http://ww2.wso2.org/test", "tns");
wsdlManager.updateWsdl(newWsdl);
assertEquals("/wsdls/org/wso2/ww2/test/BizService.wsdl", newWsdl.getPath());
//assertFalse(registry.resourceExists("/wsdls/http/foo/com/BizService.wsdl"));
// doing an update without changing anything.
wsdlManager.updateWsdl(newWsdl);
assertEquals("/wsdls/org/wso2/ww2/test/BizService.wsdl", newWsdl.getPath());
assertEquals("0.01", newWsdl.getAttribute("version"));
newWsdl = wsdlManager.getWsdl(wsdl.getId());
assertEquals("it is me", newWsdl.getAttribute("creator"));
assertEquals("0.01", newWsdl.getAttribute("version"));
// adding a new schema to the wsdl.
wsdlElement = newWsdl.getWsdlElement();
OMElement schemaElement = evaluateXPathToElement("//xsd:schema", wsdlElement);
OMFactory factory = OMAbstractFactory.getOMFactory();
OMElement importElement = factory.createOMElement(
new QName("http://www.w3.org/2001/XMLSchema", "import"));
importElement.addAttribute("schemaLocation",
"http://svn.wso2.org/repos/wso2/trunk/carbon/components/governance/org.wso2.carbon.governance.api/src/test/resources/test-resources/xsd/purchasing_dup.xsd", null);
schemaElement.addChild(importElement);
importElement.addAttribute("namespace", "http://bar.org/purchasing_dup", null);
wsdlManager.updateWsdl(newWsdl);
Schema[] schemas = newWsdl.getAttachedSchemas();
assertEquals("/schemas/org/bar/purchasing_dup/purchasing_dup.xsd",
schemas[schemas.length - 1].getPath());
Wsdl[] wsdls = wsdlManager.findWsdls(new WsdlFilter() {
public boolean matches(Wsdl wsdl) throws GovernanceException {
Schema[] schemas = wsdl.getAttachedSchemas();
for (Schema schema: schemas) {
if (schema.getPath().equals("/schemas/org/bar/purchasing_dup/purchasing_dup.xsd")) {
return true;
}
}
return false;
}
});
assertEquals(1, wsdls.length);
assertEquals(newWsdl.getId(), wsdls[0].getId());
// deleting the wsdl
wsdlManager.removeWsdl(newWsdl.getId());
Wsdl deletedWsdl = wsdlManager.getWsdl(newWsdl.getId());
assertNull(deletedWsdl);
// add again
Wsdl anotherWsdl = wsdlManager.newWsdl("http://svn.wso2.org/repos/wso2/trunk/carbon/components/governance/org.wso2.carbon.governance.api/src/test/resources/test-resources/wsdl/BizService.wsdl");
anotherWsdl.addAttribute("creator", "it is not me");
anotherWsdl.addAttribute("version", "0.02");
wsdlManager.addWsdl(anotherWsdl);
// and delete the wsdl
wsdlManager.removeWsdl(anotherWsdl.getId());
assertNull(wsdlManager.getWsdl(anotherWsdl.getId()));
}