@Test(groups = "wso2.greg")
  public void testAddCollections()
      throws ResourceAdminServiceExceptionException, RemoteException, XPathExpressionException {

    String fileType = "other";
    String description = "A test collection";
    resourceAdminClient.addCollection(PATH, CHILDS_CHILD_NAME, fileType, description);

    String authorUserName = resourceAdminClient.getResource(PARENT_PATH)[0].getAuthorUserName();
    assertTrue(
        automationContext
            .getContextTenant()
            .getContextUser()
            .getUserName()
            .equalsIgnoreCase(authorUserName),
        "Parent collection creation failure");

    authorUserName =
        resourceAdminClient.getResource(PARENT_PATH + CHILD_NAME)[0].getAuthorUserName();
    assertTrue(
        automationContext
            .getContextTenant()
            .getContextUser()
            .getUserName()
            .equalsIgnoreCase(authorUserName),
        "Child collection creation failure");
  }
  @Test(dependsOnMethods = "testAddCollections")
  public void testAddResource()
      throws MalformedURLException, ResourceAdminServiceExceptionException, RemoteException,
          XPathExpressionException {

    String path =
        FrameworkPathUtil.getSystemResourceLocation()
            + "artifacts"
            + File.separator
            + "GREG"
            + File.separator
            + "testresource.txt";
    DataHandler dataHandler = new DataHandler(new URL("file:///" + path));

    String fileType = "plain/text";
    resourceAdminClient.addResource(PATH + RES_NAME, fileType, RES_DESC, dataHandler);

    String authorUserName = resourceAdminClient.getResource(PATH + RES_NAME)[0].getAuthorUserName();
    assertTrue(
        automationContext
            .getContextTenant()
            .getContextUser()
            .getUserName()
            .equalsIgnoreCase(authorUserName),
        "Resource creation failure");
  }
  @Test(groups = "wso2.greg", description = "Add WSDL name with spaces")
  public void testAddNameWithSpacesWSDL()
      throws RemoteException, ResourceAdminServiceExceptionException, RegistryExceptionException {

    resourceAdminServiceClient.addWSDL(
        "Wsdl with spaces in the name",
        "to check wsdl addtion with spaces in its name",
        "http://svn.wso2.org/repos/wso2/people/krishantha/wsdl/wsdl%20with%20spaces%20in%20the%20name.wsdl");

    resourceAdminServiceClient.setDescription(wsdlPath, "this wsdl has spaces in its name");
  }
  @AfterClass(
      groups = "wso2.greg",
      alwaysRun = true,
      description = "cleaning up the artifacts added")
  public void tearDown()
      throws GovernanceException, RemoteException, ResourceAdminServiceExceptionException {

    resourceAdminServiceClient.deleteResource(associatedServicePath);
    resourceAdminServiceClient.deleteResource(associatedSchemaPath);
    resourceAdminServiceClient.deleteResource(wsdlPath);
    resourceAdminServiceClient.deleteResource(associatedEndpointPath);
    resourceAdminServiceClient = null;
  }
  @BeforeClass(alwaysRun = true)
  public void init() throws Exception {

    super.init(userMode);
    genericRestClient = new GenericRestClient();
    queryParamMap = new HashMap<>();
    headerMap = new HashMap<>();

    headerMap.put(AUTHORIZATION_HEADER, AUTHORIZATION_HEADER_VALUE);

    registryAPIUrl = automationContext.getContextUrls().getWebAppURLHttps();
    generalPath = "/resource/1.0.0/";
    String registryPath = "/_system/governance";
    resourceAdminServiceClient = new ResourceAdminServiceClient(backendURL, sessionCookie);
    String filePath =
        getTestArtifactLocation()
            + "artifacts"
            + File.separator
            + "GREG"
            + File.separator
            + "txt"
            + File.separator
            + "MyText.txt";
    DataHandler dh = new DataHandler(new URL("file:///" + filePath));
    path = registryPath + "/" + "MyText.txt";
    resourceAdminServiceClient.addResource(path, "text/xml", "desc", dh);
  }
 /** Adding a swagger document with invalid version. */
 @Test(
     groups = {"wso2.greg"},
     description = "Adding a swagger with unsupported version")
 public void testAddUnsupportedSwaggerVersion() {
   String fileName = "swagger3.json";
   String swaggerPath =
       FrameworkPathUtil.getSystemResourceLocation()
           + "artifacts"
           + File.separator
           + "GREG"
           + File.separator
           + "swagger"
           + File.separator
           + fileName;
   try {
     resourceAdminServiceClient.addSwagger(
         "Adding a swagger document with unsupported version. ",
         new DataHandler(new URL("file:///" + swaggerPath)));
   } catch (RemoteException | ResourceAdminServiceExceptionException e) {
     Assert.assertTrue(e.getMessage().contains("Unsupported swagger version."));
   } catch (MalformedURLException e) {
     log.error(URL_MALFORMED, e);
     Assert.fail(URL_MALFORMED);
   }
 }
  /** @throws Exception */
  @AfterClass()
  public void clear() throws Exception {

    String servicePathToDelete = absPath;
    if (wsRegistryServiceClient.resourceExists(servicePathToDelete)) {
      resourceAdminServiceClient.deleteResource(servicePathToDelete);
    }
    //		String schemaPathToDelete =
    // "/_system/governance/trunk/schemas/org/bar/purchasing/purchasing.xsd";
    //		if (wsRegistryServiceClient.resourceExists(schemaPathToDelete)) {
    //			resourceAdminServiceClient.deleteResource(schemaPathToDelete);
    //		}
    //		String wsdlPathToDelete =
    // "/_system/governance/trunk/wsdls/com/foo/IntergalacticService.wsdl";
    //		if (wsRegistryServiceClient.resourceExists(wsdlPathToDelete)) {
    //			resourceAdminServiceClient.deleteResource(wsdlPathToDelete);
    //		}
    lifeCycleManagementClient.deleteLifeCycle(LC_NAME);
    wsRegistryServiceClient = null;
    lifeCycleAdminServiceClient = null;
    lifeCycleManagementClient = null;
    governanceServiceClient = null;
    listMetadataServiceClient = null;
    resourceAdminServiceClient = null;
    userManagementClient = null;
    infoServiceAdminClient = null;
    humanTaskAdminClient = null;
  }
  @Test(groups = "wso2.greg", dependsOnMethods = "testAddDependencyFromOwnCollection")
  public void testAddWsdlAsADependency()
      throws MalformedURLException, ResourceAdminServiceExceptionException, RemoteException,
          AddAssociationRegistryExceptionException, XPathExpressionException {
    String resourcePath =
        FrameworkPathUtil.getSystemResourceLocation()
            + "artifacts"
            + File.separator
            + "GREG"
            + File.separator
            + "wsdl"
            + File.separator
            + "AmazonWebServices.wsdl";
    DataHandler dh = new DataHandler(new URL("file:///" + resourcePath));
    resourceAdminClient.addResource(
        ROOT + "AmazonWebServices.wsdl", "application/wsdl+xml", "testDesc", dh);
    assertTrue(
        resourceAdminClient
            .getResource(WSDL_PATH)[0]
            .getAuthorUserName()
            .contains(automationContext.getContextTenant().getContextUser().getUserName()),
        "WSDL has not been added");

    String dependencyType = "depends";
    String todo = "add";

    relationServiceClient.addAssociation(PATH + RES_NAME, dependencyType, WSDL_PATH, todo);

    DependenciesBean bean = relationServiceClient.getDependencies(PATH + RES_NAME);
    assertTrue(
        dependencyType.equalsIgnoreCase(bean.getAssociationBeans()[0].getAssociationType()),
        "Dependency type is not correct");

    assertTrue(
        WSDL_PATH.equalsIgnoreCase(bean.getAssociationBeans()[0].getDestinationPath()),
        "Target dependency is not correct");

    assertTrue(
        (PATH + RES_NAME).equalsIgnoreCase(bean.getAssociationBeans()[0].getSourcePath()),
        "Source dependency is not correct");

    // remove dependency
    relationServiceClient.addAssociation(PATH + RES_NAME, dependencyType, WSDL_PATH, "remove");
  }
  @Test(groups = "wso2.greg", dependsOnMethods = "testAddWsdlAsADependency")
  public void testAddPolicyAsADependency()
      throws MalformedURLException, ResourceAdminServiceExceptionException, RemoteException,
          AddAssociationRegistryExceptionException {

    Boolean nameExists = false;
    String path =
        FrameworkPathUtil.getSystemResourceLocation()
            + "artifacts"
            + File.separator
            + "GREG"
            + File.separator
            + "policy"
            + File.separator
            + "policy.xml";
    DataHandler dataHandler = new DataHandler(new URL("file:///" + path));
    resourceAdminClient.addPolicy("desc 1", dataHandler);
    PolicyBean policyBean = listMetaDataServiceClient.listPolicies();
    String[] names = policyBean.getName();

    for (String name : names) {
      if (name.equalsIgnoreCase("policy.xml")) {
        nameExists = true;
      }
    }

    assertTrue(nameExists, "Policy does not exist.");

    String[] policyNames = listMetaDataServiceClient.listPolicies().getPath();
    for (String policyName : policyNames) {
      if (policyName.contains("policy.xml")) {

        pathToPolicy = "/_system/governance" + policyName;
      }
    }

    String dependencyType = "depends";
    String todo = "add";
    relationServiceClient.addAssociation(PATH + RES_NAME, dependencyType, pathToPolicy, todo);

    DependenciesBean bean = relationServiceClient.getDependencies(PATH + RES_NAME);
    assertTrue(
        dependencyType.equalsIgnoreCase(bean.getAssociationBeans()[0].getAssociationType()),
        "Dependency type is not correct");

    assertTrue(
        pathToPolicy.equalsIgnoreCase(bean.getAssociationBeans()[0].getDestinationPath()),
        "Target dependency is not correct");

    assertTrue(
        (PATH + RES_NAME).equalsIgnoreCase(bean.getAssociationBeans()[0].getSourcePath()),
        "Source dependency is not correct");

    // remove dependency
    relationServiceClient.addAssociation(PATH + RES_NAME, dependencyType, pathToPolicy, "remove");
  }
  @Test(
      groups = "wso2.greg",
      description = "Delete artifacts ",
      dependsOnMethods = "deleteAllCApplications")
  public void deleteArtifacts()
      throws ApplicationAdminExceptionException, InterruptedException, RemoteException,
          ResourceAdminServiceExceptionException {

    resourceAdminServiceClient.deleteResource(
        "/_system/governance/trunk/services/net/restfulwebservices/www/servicecontracts/_2008/_01"
            + "/1.0.0/WeatherForecastService");
    resourceAdminServiceClient.deleteResource(
        "/_system/governance/trunk/endpoints/net/restfulwebservices/www/wcf/ep-WeatherForecastService-svc");
    resourceAdminServiceClient.deleteResource(
        "/_system/governance/trunk/schemas/com/microsoft/schemas/_2003");
    resourceAdminServiceClient.deleteResource(
        "/_system/governance/trunk/schemas/net/restfulwebservices/www");
    resourceAdminServiceClient.deleteResource(
        "/_system/governance/trunk/schemas/faultcontracts/gotlservices/_2008");
  }
  @Test(groups = "wso2.greg", description = "Create a service")
  public void testCreateService()
      throws XMLStreamException, IOException, AddServicesServiceRegistryExceptionException,
          ListMetadataServiceRegistryExceptionException, ResourceAdminServiceExceptionException {

    String servicePath =
        getTestArtifactLocation()
            + "artifacts"
            + File.separator
            + "GREG"
            + File.separator
            + "services"
            + File.separator
            + "intergalacticService16.metadata.xml";
    DataHandler dataHandler = new DataHandler(new URL("file:///" + servicePath));
    String mediaType = "application/vnd.wso2-service+xml";
    String description = "This is a test service";
    resourceAdminServiceClient.addResource(
        "/_system/governance/service2", mediaType, description, dataHandler);
    ResourceData[] data = resourceAdminServiceClient.getResource(absPath);
    assertNotNull(data, "Service not found");
  }
  @Test(
      groups = "wso2.greg",
      description = "verify WSDL name with spaces",
      dependsOnMethods = "testAddNameWithSpacesWSDL")
  public void testVerifyNameWithSpacesWSDL()
      throws RemoteException, ResourceAdminServiceExceptionException {

    assertTrue(
        resourceAdminServiceClient
            .getMetadata(wsdlPath)
            .getDescription()
            .contentEquals("this wsdl has spaces in its name"));
  }
  @AfterClass
  public void cleanUp() throws ResourceAdminServiceExceptionException, RemoteException {
    resourceAdminClient.deleteResource(PARENT_PATH.substring(0, PARENT_PATH.length() - 1));
    resourceAdminClient.deleteResource(RXT_LOCATION);
    resourceAdminClient.deleteResource(WSDL_PATH);
    resourceAdminClient.deleteResource(pathToPolicy);
    resourceAdminClient.deleteResource(RXT_RESOURCE_PATH);
    resourceAdminClient.deleteResource(ROOT + CHILDS_CHILD_NAME);
    resourceAdminClient.deleteResource(SERVICE_PATH);
    resourceAdminClient.deleteResource(SCHEMA_PATH);

    resourceAdminClient = null;
    relationServiceClient = null;
    listMetaDataServiceClient = null;
    governance = null;
  }
  @Test(groups = "wso2.greg", dependsOnMethods = "testAddSchemaAsADependency")
  public void testAddRxt()
      throws MalformedURLException, ResourceAdminServiceExceptionException, RemoteException,
          XPathExpressionException {
    String resourcePath =
        FrameworkPathUtil.getSystemResourceLocation()
            + "artifacts"
            + File.separator
            + "GREG"
            + File.separator
            + "rxt"
            + File.separator
            + "person.rxt";
    DataHandler dh = new DataHandler(new URL("file:///" + resourcePath));

    resourceAdminClient.addResource(RXT_LOCATION, RXT_FILE_TYPE, "TstDec", dh);

    assertTrue(
        resourceAdminClient
            .getResource(RXT_LOCATION)[0]
            .getAuthorUserName()
            .contains(automationContext.getContextTenant().getContextUser().getUserName()));
  }
  @Test(groups = "wso2.greg", dependsOnMethods = "testAddDependencyFromChildCollection")
  public void testMoveDependency()
      throws ResourceAdminServiceExceptionException, RemoteException,
          AddAssociationRegistryExceptionException, XPathExpressionException {
    resourceAdminClient.moveResource(PATH, PATH + CHILDS_CHILD_NAME, ROOT, CHILDS_CHILD_NAME);

    String authorUserName =
        resourceAdminClient.getResource(ROOT + CHILDS_CHILD_NAME)[0].getAuthorUserName();
    assertTrue(
        automationContext
            .getContextTenant()
            .getContextUser()
            .getUserName()
            .equalsIgnoreCase(authorUserName),
        "Collection move failure");

    String dependencyType = "depends";

    DependenciesBean bean = relationServiceClient.getDependencies(PATH + RES_NAME);
    assertTrue(
        dependencyType.equalsIgnoreCase(bean.getAssociationBeans()[0].getAssociationType()),
        "Dependency type is not correct");

    assertTrue(
        (ROOT + CHILDS_CHILD_NAME)
            .equalsIgnoreCase(bean.getAssociationBeans()[0].getDestinationPath()),
        "Target dependency is not correct");

    assertTrue(
        (PATH + RES_NAME).equalsIgnoreCase(bean.getAssociationBeans()[0].getSourcePath()),
        "Source dependency is not correct");

    // remove the dependency
    relationServiceClient.addAssociation(
        PATH + RES_NAME, dependencyType, ROOT + CHILDS_CHILD_NAME, "remove");
  }
  @Test(groups = "wso2.greg", dependsOnMethods = "testAddPolicyAsADependency")
  public void testAddSchemaAsADependency()
      throws MalformedURLException, ResourceAdminServiceExceptionException, RemoteException,
          AddAssociationRegistryExceptionException {
    String path =
        FrameworkPathUtil.getSystemResourceLocation()
            + "artifacts"
            + File.separator
            + "GREG"
            + File.separator
            + "schema"
            + File.separator
            + "books.xsd";
    DataHandler dataHandler = new DataHandler(new URL("file:///" + path));
    resourceAdminClient.addSchema("desc 1", dataHandler);
    SchemaBean schemaBean = listMetaDataServiceClient.listSchemas();

    String[] names2 = schemaBean.getName();
    Boolean nameExists = false;

    for (String name : names2) {
      if (name.equalsIgnoreCase("books.xsd")) {
        nameExists = true;
      }
    }
    assertTrue(nameExists, "Schema does not exist.");

    String dependencyType = "depends";
    String todo = "add";
    relationServiceClient.addAssociation(PATH + RES_NAME, dependencyType, SCHEMA_PATH, todo);

    DependenciesBean bean = relationServiceClient.getDependencies(PATH + RES_NAME);
    assertTrue(
        dependencyType.equalsIgnoreCase(bean.getAssociationBeans()[0].getAssociationType()),
        "Dependency type is not correct");

    assertTrue(
        SCHEMA_PATH.equalsIgnoreCase(bean.getAssociationBeans()[0].getDestinationPath()),
        "Target dependency is not correct");

    assertTrue(
        (PATH + RES_NAME).equalsIgnoreCase(bean.getAssociationBeans()[0].getSourcePath()),
        "Source dependency is not correct");

    // remove dependency
    relationServiceClient.addAssociation(PATH + RES_NAME, dependencyType, SCHEMA_PATH, "remove");
  }
  public void addService() throws Exception {
    String resourcePath =
        getTestArtifactLocation()
            + "artifacts"
            + File.separator
            + "GREG"
            + File.separator
            + "services"
            + File.separator
            + "intergalacticService.xml";

    DataHandler dataHandler = new DataHandler(new URL("file:///" + resourcePath));
    String mediaType = "application/vnd.wso2-service+xml";
    String description = "This is a test service";
    resourceAdminServiceClient.addResource(
        "/_system/governance/service1", mediaType, description, dataHandler);
  }
  @Test(
      groups = {"wso2.greg"},
      description = "add a wsdl")
  public void testAddGar()
      throws RegistryException, IOException, ResourceAdminServiceExceptionException {

    String filePath =
        getTestArtifactLocation()
            + "artifacts"
            + File.separator
            + "GREG"
            + File.separator
            + "gar"
            + File.separator
            + "myGar.gar";
    DataHandler dh = new DataHandler(new URL("file:///" + filePath));
    resourceAdminServiceClient.addResource(
        "/_system/governance/trunk/test/wsdls",
        "application/vnd.wso2.governance-archive",
        "desc",
        dh);
  }
 public void delete(String destPath)
     throws ResourceAdminServiceExceptionException, RemoteException, RegistryException {
   if (wsRegistryServiceClient.resourceExists(destPath)) {
     resourceAdminServiceClient.deleteResource(destPath);
   }
 }
 @AfterClass(alwaysRun = true)
 public void cleanUp()
     throws RegistryException, RemoteException, ResourceAdminServiceExceptionException {
   resourceAdminServiceClient.deleteResource(path);
 }