This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.
Writing a Test Case for G-Reg
This page illustrates how to write a test to add a schema to the G-REG server and verify the existence of the added/uploaded schema to the server.
You can start writing your tests in the following location according to your greg server version:
E.g.:
products/greg/x.x.x/modules/integration/registry/tests-new/src/test/java/org/wso2/carbon/registry
Refer the table below where a SchemaUploadTestCase.java is created in a more common form (for illustration purposes only):
package org.wso2.carbon.registry.addresources.test; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import org.wso2.carbon.authenticator.stub.LoginAuthenticationExceptionException; import org.wso2.carbon.automation.api.clients.registry.ResourceAdminServiceClient; import org.wso2.carbon.automation.engine.context.AutomationContext; import org.wso2.carbon.automation.utils.registry.RegistryProviderUtil; import org.wso2.carbon.governance.api.schema.SchemaManager; import org.wso2.carbon.governance.api.schema.dataobjects.Schema; import org.wso2.carbon.registry.app.RemoteRegistry; import org.wso2.carbon.registry.core.Registry; import org.wso2.carbon.registry.core.exceptions.RegistryException; import org.wso2.carbon.registry.handler.stub.ExceptionException; import org.wso2.carbon.registry.resource.stub.ResourceAdminServiceExceptionException; import javax.activation.DataHandler; import javax.xml.xpath.XPathExpressionException; import java.io.File; import java.io.IOException; import java.net.URL; import java.rmi.RemoteException; import static org.testng.Assert.assertTrue; public class SchemaUploadTestCase { private static final Log log = LogFactory.getLog(SchemaUploadTestCase.class); private AutomationContext automationContext; private ResourceAdminServiceClient resourceAdminServiceClient; private RegistryProviderUtil registryProviderUtil = new RegistryProviderUtil(); private SchemaManager schemaManager; private String schemaId; private Registry governance; @BeforeClass(groups = {"wso2.greg"}) public void init() throws Exception {// 01 automationContext = new AutomationContext("GREG", "lbw001", "superTenant", "superAdmin"); int userId = 1; RemoteRegistry registry = registryProviderUtil.getRemoteRegistry(userId, "GREG"); governance = registryProviderUtil.getGovernanceRegistry(registry, userId); resourceAdminServiceClient = new ResourceAdminServiceClient(getBackendURL(),getSessionCookie()); } @AfterClass() public void DeleteLCResources() throws GovernanceException { schemaManager.removeSchema(schemaId); // 07 } @Test(groups = {"wso2.greg"}, description = "Add new schema") public void addSchema() throws IOException, ExceptionException, RegistryException, ResourceAdminServiceExceptionException { String resourceLocation = "xxx/xxx/xxx/xxx"; String filePath = resourceLocation + File.separator + "schema" + File.separator + "calculator.xsd"; DataHandler dh = new DataHandler(new URL("file:///" + filePath)); // 04 resourceAdminServiceClient.addSchema("Adding My Schema",dh); SchemaManager schemaManager = new SchemaManager(governance); // 05 Schema[] schemas = schemaManager.getAllSchemas(); boolean resourceFound = false; for (Schema schema : schemas) { if (schema.getQName().getLocalPart().equals("calculator.xsd")) { resourceFound = true; schemaId = schema.getId(); log.info("Schema ID is : "+ schemaId); } } assertTrue(resourceFound, "Schema Not Found"); // 06 } private String getBackendURL() throws XPathExpressionException { // 02 return automationContext.getContextUrls().getBackEndUrl(); } // 03 private String getSessionCookie() throws LoginAuthenticationExceptionException, XPathExpressionException, RemoteException { return automationContext.login(); } }
You need to place the schema to be uploaded to the server inside the resourceLocation.
E.g.:
resourceLocation = "products/greg/x.x.x/modules/integration/registry/tests-new/src/test/resources/artifacts/GREG/”
- Create a method called init() and initialize an automationContext object. Pass relevant parameters in accordance with the given values of the automation.xml.
- Derive the backendserviceurls and session cookies as shown in the getBackendURL() and getSessionCookie() methods.
- Create instances from the ResourceAdminServiceClient & RegistryProviderUtil classes as these instances are needed to add the schema to the server. Note the parameters that are needed to pass when creating an instance from ResourceAdminServiceClient.
- Start writing your test scenario under the @Test annotation. Create a DataHandler from the schema resource file as shown above and invoke the addResource(...) method of the resourceAdminServiceClient to add/upload the schema to the server.
- The steps involved in verifying the schema added to the server. By calling an instance from the SchemaManager class, you can seek the schema uploaded and if the schema was successfully uploaded, the assert statement shown in 06 will not encounter a failure.
@AfterClass you can remove the added schema as shown in 07 by invoking removeSchema(...) method using schemamanager instance.
You can now start writing numerous testing scenarios using SchemaUploadTestCase .java as a template.