Search in sources :

Example 66 with Identifier

use of org.wso2.carbon.apimgt.api.model.Identifier in project carbon-apimgt by wso2.

the class AbstractAPIManagerTestCase method testGetWsdl.

@Test
public void testGetWsdl() throws APIManagementException, RegistryException, IOException {
    Resource resourceMock = Mockito.mock(Resource.class);
    AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(registry);
    APIIdentifier identifier = getAPIIdentifier(SAMPLE_API_NAME, API_PROVIDER, SAMPLE_API_VERSION);
    String wsdlName = identifier.getProviderName() + "--" + identifier.getApiName() + identifier.getVersion() + ".wsdl";
    String wsdlResourcePath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getApiName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion() + RegistryConstants.PATH_SEPARATOR + wsdlName;
    Resource resource = new ResourceImpl(wsdlResourcePath, new ResourceDO());
    Mockito.when(registry.get(wsdlResourcePath)).thenThrow(RegistryException.class).thenReturn(resource);
    Mockito.when(registry.resourceExists(wsdlResourcePath)).thenReturn(true);
    try {
        abstractAPIManager.getWSDL(identifier);
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().contains("Error while getting wsdl file from the registry"));
    }
    String wsdlContent = "sample wsdl";
    resource.setContent(wsdlContent);
    InputStream inputStream = new ArrayInputStream();
    Mockito.when(resourceMock.getContentStream()).thenReturn(inputStream);
    Assert.assertEquals(wsdlContent, IOUtils.toString(abstractAPIManager.getWSDL(identifier).getContent()));
    PowerMockito.mockStatic(IOUtils.class);
}
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) ArrayInputStream(org.apache.derby.iapi.services.io.ArrayInputStream) InputStream(java.io.InputStream) Resource(org.wso2.carbon.registry.core.Resource) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) ArrayInputStream(org.apache.derby.iapi.services.io.ArrayInputStream) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 67 with Identifier

use of org.wso2.carbon.apimgt.api.model.Identifier in project carbon-apimgt by wso2.

the class AbstractAPIManagerTestCase method testGetDocumentationContent.

@Test
public void testGetDocumentationContent() throws APIManagementException, org.wso2.carbon.user.api.UserStoreException, RegistryException {
    int tenantId = -1234;
    UserRegistry registry = Mockito.mock(UserRegistry.class);
    AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapperExtended(genericArtifactManager, registryService, registry, tenantManager);
    Mockito.when(tenantManager.getTenantId(Mockito.anyString())).thenThrow(UserStoreException.class).thenReturn(tenantId);
    APIIdentifier identifier = getAPIIdentifier(SAMPLE_API_NAME, API_PROVIDER, SAMPLE_API_VERSION);
    String docName = "doc1";
    try {
        abstractAPIManager.getDocumentationContent(identifier, docName);
        Assert.fail("User store exception not thrown for error scenario");
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().contains("Failed to get document content found for documentation:"));
    }
    Mockito.when(registryService.getGovernanceSystemRegistry(Mockito.anyInt())).thenThrow(RegistryException.class).thenReturn(registry);
    try {
        abstractAPIManager.getDocumentationContent(identifier, docName);
        Assert.fail("Registry exception not thrown for error scenario");
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().contains("No document content found for documentation:"));
    }
    Mockito.when(registry.resourceExists(Mockito.anyString())).thenReturn(false, true, true);
    String docContent = abstractAPIManager.getDocumentationContent(identifier, docName);
    Assert.assertNull(docContent);
    String docObject = "samlple doc content";
    Resource resource = new ResourceImpl();
    resource.setContent(docObject.getBytes(StandardCharsets.UTF_8));
    Mockito.when(registry.get(Mockito.anyString())).thenReturn(resource);
    docContent = abstractAPIManager.getDocumentationContent(identifier, docName);
    Assert.assertEquals(docContent, docObject);
    abstractAPIManager = new AbstractAPIManagerWrapper(genericArtifactManager, registryService, registry, tenantManager);
    docContent = abstractAPIManager.getDocumentationContent(identifier, docName);
    Assert.assertEquals(docContent, docObject);
    Mockito.when(registryService.getGovernanceUserRegistry(Mockito.anyString(), Mockito.anyInt())).thenReturn(registry);
    abstractAPIManager.tenantDomain = SAMPLE_TENANT_DOMAIN_1;
    docContent = abstractAPIManager.getDocumentationContent(identifier, docName);
    Assert.assertEquals(docContent, docObject);
}
Also used : ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) UserStoreException(org.wso2.carbon.user.core.UserStoreException) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) 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 68 with Identifier

use of org.wso2.carbon.apimgt.api.model.Identifier in project carbon-apimgt by wso2.

the class AbstractAPIManagerTestCase method testGetApiSpecificMediationPolicy.

@Test
public void testGetApiSpecificMediationPolicy() throws RegistryException, APIManagementException, IOException, XMLStreamException {
    String parentCollectionPath = "config/mediation/";
    parentCollectionPath = parentCollectionPath.substring(0, parentCollectionPath.lastIndexOf("/"));
    Collection parentCollection = new CollectionImpl();
    parentCollection.setChildren(new String[] { parentCollectionPath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_CUSTOM_SEQUENCE_TYPE_OUT, parentCollectionPath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_CUSTOM_SEQUENCE_TYPE_FAULT, parentCollectionPath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_CUSTOM_SEQUENCE_TYPE_IN });
    Collection childCollection = new CollectionImpl();
    childCollection.setChildren(new String[] { "mediation1" });
    Mockito.when(registry.get(parentCollectionPath)).thenThrow(RegistryException.class).thenReturn(null, parentCollection);
    Mockito.when(registry.get(parentCollectionPath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_CUSTOM_SEQUENCE_TYPE_IN)).thenReturn(childCollection);
    Resource resource = new ResourceImpl("api/mediation/policy1", new ResourceDO());
    resource.setUUID(SAMPLE_RESOURCE_ID);
    String mediationPolicyContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<sequence xmlns=\"http://ws.apache.org/ns/synapse\" name=\"default-endpoint\">\n</sequence>";
    resource.setContent(mediationPolicyContent);
    Mockito.when(registry.get("mediation1")).thenReturn(resource);
    AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(registry);
    Identifier identifier = Mockito.mock(Identifier.class);
    try {
        abstractAPIManager.getApiSpecificMediationPolicy(identifier, parentCollectionPath, SAMPLE_RESOURCE_ID);
        Assert.fail("Registry exception not thrown for error scenario");
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().contains("Error while obtaining registry objects"));
    }
    Assert.assertNull(abstractAPIManager.getApiSpecificMediationPolicy(identifier, parentCollectionPath, SAMPLE_RESOURCE_ID));
    Assert.assertEquals(abstractAPIManager.getApiSpecificMediationPolicy(identifier, parentCollectionPath, SAMPLE_RESOURCE_ID).getName(), "default-endpoint");
    PowerMockito.mockStatic(IOUtils.class);
    PowerMockito.mockStatic(AXIOMUtil.class);
    PowerMockito.when(IOUtils.toString((InputStream) Mockito.any(), Mockito.anyString())).thenThrow(IOException.class).thenReturn(mediationPolicyContent);
    PowerMockito.when(AXIOMUtil.stringToOM(Mockito.anyString())).thenThrow(XMLStreamException.class);
    try {
        abstractAPIManager.getApiSpecificMediationPolicy(identifier, parentCollectionPath, SAMPLE_RESOURCE_ID);
        Assert.fail("IO exception not thrown for error scenario");
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().contains("Error occurred while converting content stream into string"));
    }
    try {
        abstractAPIManager.getApiSpecificMediationPolicy(identifier, parentCollectionPath, SAMPLE_RESOURCE_ID);
        Assert.fail("XMLStream exception  not thrown for error scenario");
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().contains("Error occurred while getting omElement out of mediation content"));
    }
    resource.setContent(null);
    try {
        abstractAPIManager.getApiSpecificMediationPolicy(identifier, parentCollectionPath, SAMPLE_RESOURCE_ID);
        Assert.fail("Registry exception not thrown for error scenario");
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().contains("Error occurred while accessing content stream of mediation"));
    }
}
Also used : ResourceDO(org.wso2.carbon.registry.core.jdbc.dataobjects.ResourceDO) ArrayInputStream(org.apache.derby.iapi.services.io.ArrayInputStream) InputStream(java.io.InputStream) Resource(org.wso2.carbon.registry.core.Resource) IOException(java.io.IOException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) Identifier(org.wso2.carbon.apimgt.api.model.Identifier) CollectionImpl(org.wso2.carbon.registry.core.CollectionImpl) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Collection(org.wso2.carbon.registry.core.Collection) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 69 with Identifier

use of org.wso2.carbon.apimgt.api.model.Identifier in project carbon-apimgt by wso2.

the class AbstractAPIManagerTestCase method testGetAsyncApiDefinition.

@Test
public void testGetAsyncApiDefinition() throws Exception {
    Organization org = Mockito.mock(Organization.class);
    PowerMockito.whenNew(Organization.class).withArguments(SAMPLE_TENANT_DOMAIN, null).thenReturn(org);
    AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(apiPersistenceInstance);
    APIIdentifier identifier = getAPIIdentifier(SAMPLE_API_NAME, API_PROVIDER, SAMPLE_API_VERSION);
    identifier.setUuid(SAMPLE_RESOURCE_ID);
    PowerMockito.mockStatic(OASParserUtil.class);
    String asyncDefinition = "Sample Async Definition";
    PowerMockito.when(apiPersistenceInstance.getAsyncDefinition(org, SAMPLE_RESOURCE_ID)).thenReturn(asyncDefinition);
    Assert.assertEquals(abstractAPIManager.getAsyncAPIDefinition(SAMPLE_RESOURCE_ID, SAMPLE_TENANT_DOMAIN), asyncDefinition);
}
Also used : Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 70 with Identifier

use of org.wso2.carbon.apimgt.api.model.Identifier in project carbon-apimgt by wso2.

the class AbstractAPIManagerTestCase method testGetAllDocumentationWithLoggedUser.

@Test
public void testGetAllDocumentationWithLoggedUser() throws APIManagementException, org.wso2.carbon.user.api.UserStoreException, RegistryException {
    int tenantId = -1234;
    UserRegistry registry = Mockito.mock(UserRegistry.class);
    AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(genericArtifactManager, registryService, registry, tenantManager);
    Mockito.when(tenantManager.getTenantId(SAMPLE_TENANT_DOMAIN)).thenThrow(UserStoreException.class).thenReturn(tenantId);
    APIIdentifier identifier = getAPIIdentifier(SAMPLE_API_NAME, API_PROVIDER, SAMPLE_API_VERSION);
    String loggedInUser = "admin";
    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");
    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)).thenReturn(true);
    try {
        abstractAPIManager.getAllDocumentation(identifier, loggedInUser);
        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, loggedInUser)).thenReturn(documentation);
    List<Documentation> documentationList = abstractAPIManager.getAllDocumentation(identifier, loggedInUser);
    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, loggedInUser);
    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) ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) CollectionImpl(org.wso2.carbon.registry.core.CollectionImpl) UserStoreException(org.wso2.carbon.user.core.UserStoreException) 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)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)118 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)83 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)66 API (org.wso2.carbon.apimgt.api.model.API)42 Resource (org.wso2.carbon.registry.core.Resource)40 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)39 Test (org.junit.Test)36 PreparedStatement (java.sql.PreparedStatement)34 SQLException (java.sql.SQLException)34 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)34 Connection (java.sql.Connection)33 UserStoreException (org.wso2.carbon.user.core.UserStoreException)31 ResultSet (java.sql.ResultSet)29 ArrayList (java.util.ArrayList)29 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)29 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)27 IOException (java.io.IOException)26 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)26 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)25 HumanTaskException (org.wso2.carbon.humantask.core.engine.HumanTaskException)24