Search in sources :

Example 21 with ResourceImpl

use of org.wso2.carbon.registry.core.ResourceImpl in project carbon-apimgt by wso2.

the class AbstractAPIManagerTestCase method testAddResourceFile.

@Test
public void testAddResourceFile() throws APIManagementException, RegistryException, IOException {
    Identifier identifier = Mockito.mock(Identifier.class);
    AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(registry);
    Resource resource = new ResourceImpl();
    Mockito.when(registry.newResource()).thenReturn(resource);
    String resourcePath = "/test";
    String contentType = "sampleType";
    ResourceFile resourceFile = new ResourceFile(null, contentType);
    try {
        abstractAPIManager.addResourceFile(identifier, resourcePath, resourceFile);
        Assert.fail("Registry exception not thrown for error scenario");
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().contains("Error while adding the resource to the registry"));
    }
    InputStream in = IOUtils.toInputStream("sample content", "UTF-8");
    resourceFile = new ResourceFile(in, contentType);
    String returnedPath = abstractAPIManager.addResourceFile(identifier, resourcePath, resourceFile);
    Assert.assertTrue(returnedPath.contains(resourcePath) && returnedPath.contains("/t/"));
    abstractAPIManager.tenantDomain = SAMPLE_TENANT_DOMAIN;
    returnedPath = abstractAPIManager.addResourceFile(identifier, resourcePath, resourceFile);
    Assert.assertTrue(returnedPath.contains(resourcePath) && !returnedPath.contains("/t/"));
}
Also used : ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) Identifier(org.wso2.carbon.apimgt.api.model.Identifier) ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ArrayInputStream(org.apache.derby.iapi.services.io.ArrayInputStream) InputStream(java.io.InputStream) Resource(org.wso2.carbon.registry.core.Resource) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 22 with ResourceImpl

use of org.wso2.carbon.registry.core.ResourceImpl in project carbon-apimgt by wso2.

the class AbstractAPIManagerTestCase method testGetWsdlById.

@Test
public void testGetWsdlById() throws RegistryException, APIManagementException, IOException {
    String resourceId = SAMPLE_RESOURCE_ID;
    AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(registry);
    Collection parentCollection = new CollectionImpl();
    String wsdlResourcepath = APIConstants.API_WSDL_RESOURCE;
    String resourcePath = wsdlResourcepath + "/wsdl1";
    parentCollection.setChildren(new String[] { resourcePath });
    Mockito.when(registry.get(wsdlResourcepath)).thenReturn(parentCollection);
    Resource resource = new ResourceImpl(resourcePath, new ResourceDO());
    Mockito.when(registry.get(resourcePath)).thenThrow(RegistryException.class).thenReturn(resource);
    Mockito.when(registry.resourceExists(wsdlResourcepath)).thenReturn(false, true);
    Assert.assertNull(abstractAPIManager.getWsdlById(resourceId));
    resource.setUUID(resourceId);
    try {
        abstractAPIManager.getWsdlById(resourceId);
        Assert.fail("Exception not thrown for error scenario");
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().contains("Error while accessing registry objects"));
    }
    String wsdlContent = "sample wsdl";
    resource.setContent(wsdlContent);
    Wsdl wsdl = abstractAPIManager.getWsdlById(resourceId);
    Assert.assertNotNull(wsdl);
    PowerMockito.mockStatic(IOUtils.class);
    PowerMockito.when(IOUtils.toString((InputStream) Mockito.any(), Mockito.anyString())).thenThrow(IOException.class);
    // covers logged only exception;
    abstractAPIManager.getWsdlById(resourceId);
}
Also used : ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) CollectionImpl(org.wso2.carbon.registry.core.CollectionImpl) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ResourceDO(org.wso2.carbon.registry.core.jdbc.dataobjects.ResourceDO) Resource(org.wso2.carbon.registry.core.Resource) Collection(org.wso2.carbon.registry.core.Collection) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) Wsdl(org.wso2.carbon.apimgt.api.model.Wsdl) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 23 with ResourceImpl

use of org.wso2.carbon.registry.core.ResourceImpl in project carbon-apimgt by wso2.

the class AbstractAPIManagerTestCase method testGetAllDocumentation.

@Test
public void testGetAllDocumentation() throws APIManagementException, RegistryException {
    Registry registry = Mockito.mock(UserRegistry.class);
    AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(genericArtifactManager, null, registry, null, apiMgtDAO);
    abstractAPIManager.registry = registry;
    GenericArtifact genericArtifact = getGenericArtifact(SAMPLE_API_NAME, API_PROVIDER, SAMPLE_API_VERSION, "sample");
    genericArtifact.setAttribute(APIConstants.DOC_TYPE, "Other");
    genericArtifact.setAttribute(APIConstants.DOC_SOURCE_TYPE, "URL");
    APIIdentifier identifier = getAPIIdentifier(SAMPLE_API_NAME, API_PROVIDER, SAMPLE_API_VERSION);
    String apiDocPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getApiName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion() + RegistryConstants.PATH_SEPARATOR + APIConstants.DOC_DIR + RegistryConstants.PATH_SEPARATOR;
    PowerMockito.mockStatic(APIUtil.class);
    PowerMockito.when(APIUtil.getAPIOrAPIProductDocPath(identifier)).thenReturn(apiDocPath);
    Resource resource1 = new ResourceImpl();
    resource1.setUUID(SAMPLE_RESOURCE_ID);
    Mockito.when(genericArtifact.getPath()).thenReturn("test");
    String docName = "sample";
    Documentation documentation = new Documentation(DocumentationType.HOWTO, docName);
    PowerMockito.when(APIUtil.getDocumentation(genericArtifact)).thenReturn(documentation);
    Mockito.when(registry.resourceExists(apiDocPath)).thenThrow(RegistryException.class).thenReturn(true);
    Mockito.when(apiMgtDAO.checkAPIUUIDIsARevisionUUID(Mockito.anyString())).thenReturn(null);
    try {
        abstractAPIManager.getAllDocumentation(identifier);
        Assert.fail("Registry exception not thrown for error scenario");
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().contains("Failed to get documentations for api"));
    }
    Resource resource2 = new ResourceImpl();
    resource2.setUUID(SAMPLE_RESOURCE_ID);
    Mockito.when(genericArtifactManager.getGenericArtifact(SAMPLE_RESOURCE_ID)).thenReturn(genericArtifact);
    String documentationName = "doc1";
    Collection documentCollection = new CollectionImpl();
    documentCollection.setChildren(new String[] { apiDocPath + documentationName, apiDocPath + APIConstants.INLINE_DOCUMENT_CONTENT_DIR });
    Mockito.when(registry.get(apiDocPath)).thenReturn(documentCollection);
    Mockito.when(registry.get(apiDocPath + documentationName)).thenReturn(resource2);
    PowerMockito.when(APIUtil.getDocumentation(genericArtifact)).thenReturn(documentation);
    List<Documentation> documentationList = abstractAPIManager.getAllDocumentation(identifier);
    Assert.assertNotNull(documentationList);
    Assert.assertEquals(documentationList.size(), 1);
    String contentPath = APIConstants.API_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getApiName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion() + RegistryConstants.PATH_SEPARATOR + APIConstants.DOC_DIR + RegistryConstants.PATH_SEPARATOR + RegistryConstants.PATH_SEPARATOR + documentationName;
    genericArtifact.setAttribute(APIConstants.DOC_SOURCE_TYPE, "In line");
    genericArtifact.setAttribute(APIConstants.DOC_NAME, documentationName);
    ResourceDO resourceDO = new ResourceDO();
    resourceDO.setLastUpdatedOn(12344567);
    Resource resource3 = new ResourceImpl(contentPath, resourceDO);
    Mockito.when(registry.get(contentPath)).thenReturn(resource3);
    documentationList = abstractAPIManager.getAllDocumentation(identifier);
    Assert.assertNotNull(documentationList);
    Assert.assertEquals(documentationList.size(), 1);
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) ResourceDO(org.wso2.carbon.registry.core.jdbc.dataobjects.ResourceDO) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) CollectionImpl(org.wso2.carbon.registry.core.CollectionImpl) Collection(org.wso2.carbon.registry.core.Collection) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 24 with ResourceImpl

use of org.wso2.carbon.registry.core.ResourceImpl in project carbon-apimgt by wso2.

the class AbstractAPIManagerTestCase method testGetThumbnailLastUpdatedTime.

@Test
public void testGetThumbnailLastUpdatedTime() throws APIManagementException, org.wso2.carbon.user.api.UserStoreException, RegistryException {
    APIIdentifier identifier = new APIIdentifier(SAMPLE_API_NAME, API_PROVIDER, SAMPLE_API_VERSION);
    Mockito.when(registry.resourceExists(Mockito.anyString())).thenReturn(true, false, true);
    ResourceDO resourceDO = new ResourceDO();
    resourceDO.setLastUpdatedOn(34579002);
    Resource resource = new ResourceImpl("test/", resourceDO);
    Mockito.when(registry.get(Mockito.anyString())).thenThrow(RegistryException.class).thenReturn(resource);
    AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(registry);
    try {
        abstractAPIManager.getThumbnailLastUpdatedTime(identifier);
        Assert.fail("Registry exception not thrown for error scenario");
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().contains("Error while loading API icon from the registry"));
    }
    Assert.assertNull(abstractAPIManager.getThumbnailLastUpdatedTime(identifier));
    Assert.assertEquals(abstractAPIManager.getThumbnailLastUpdatedTime(identifier), "34579002");
}
Also used : ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ResourceDO(org.wso2.carbon.registry.core.jdbc.dataobjects.ResourceDO) Resource(org.wso2.carbon.registry.core.Resource) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 25 with ResourceImpl

use of org.wso2.carbon.registry.core.ResourceImpl in project carbon-apimgt by wso2.

the class AbstractAPIManagerTestCase method testDeleteWsdl.

@Test
public void testDeleteWsdl() throws APIManagementException, RegistryException {
    AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(registry);
    Collection parentCollection = new CollectionImpl();
    String wsdlResourcePath = APIConstants.API_WSDL_RESOURCE;
    String resourcePath = wsdlResourcePath + "/wsdl1";
    parentCollection.setChildren(new String[] { resourcePath });
    Mockito.when(registry.get(wsdlResourcePath)).thenReturn(parentCollection);
    Resource resource = new ResourceImpl(resourcePath, new ResourceDO());
    Mockito.when(registry.get(resourcePath)).thenThrow(RegistryException.class).thenReturn(resource);
    Mockito.when(registry.resourceExists(wsdlResourcePath)).thenReturn(true);
    try {
        abstractAPIManager.deleteWsdl(SAMPLE_RESOURCE_ID);
        Assert.fail("Exception not thrown for error scenario");
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().contains("Error while accessing registry objects"));
    }
    Mockito.when(registry.resourceExists(resourcePath)).thenReturn(true, true, true, true, false);
    resource.setUUID(SAMPLE_RESOURCE_ID);
    Mockito.doThrow(RegistryException.class).doNothing().when(registry).delete(resourcePath);
    try {
        abstractAPIManager.deleteWsdl(SAMPLE_RESOURCE_ID);
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().contains("Failed to delete wsdl"));
    }
    Assert.assertFalse(abstractAPIManager.deleteWsdl(SAMPLE_RESOURCE_ID));
    Assert.assertTrue(abstractAPIManager.deleteWsdl(SAMPLE_RESOURCE_ID));
}
Also used : ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) CollectionImpl(org.wso2.carbon.registry.core.CollectionImpl) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ResourceDO(org.wso2.carbon.registry.core.jdbc.dataobjects.ResourceDO) Resource(org.wso2.carbon.registry.core.Resource) Collection(org.wso2.carbon.registry.core.Collection) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

Resource (org.wso2.carbon.registry.core.Resource)43 ResourceImpl (org.wso2.carbon.registry.core.ResourceImpl)43 Test (org.junit.Test)37 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)36 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)20 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)20 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)20 Registry (org.wso2.carbon.registry.core.Registry)17 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)17 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)16 Matchers.anyString (org.mockito.Matchers.anyString)12 Collection (org.wso2.carbon.registry.core.Collection)12 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)10 API (org.wso2.carbon.apimgt.api.model.API)9 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)9 CollectionImpl (org.wso2.carbon.registry.core.CollectionImpl)9 Tag (org.wso2.carbon.registry.core.Tag)9 ResourceDO (org.wso2.carbon.registry.core.jdbc.dataobjects.ResourceDO)9 InputStream (java.io.InputStream)7 ArrayInputStream (org.apache.derby.iapi.services.io.ArrayInputStream)6