Search in sources :

Example 6 with ScopesDAO

use of org.wso2.carbon.apimgt.impl.dao.ScopesDAO in project carbon-apimgt by wso2.

the class APIProviderImplTest method testUpdateAPIforStateChange_ToRetired.

@Test
public void testUpdateAPIforStateChange_ToRetired() throws RegistryException, UserStoreException, APIManagementException, FaultGatewaysException, WorkflowException, APIPersistenceException, XMLStreamException {
    APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.0");
    Set<String> environments = new HashSet<String>();
    environments.add("Production");
    environments.add("Sandbox");
    API api = new API(apiId);
    api.setContext("/test");
    api.setStatus(APIConstants.CREATED);
    api.setAsDefaultVersion(true);
    api.setEnvironments(environments);
    api.setOrganization("carbon.super");
    Mockito.when(apimgtDAO.getPublishedDefaultVersion(apiId)).thenReturn("1.0.0");
    Map<String, String> failedGWEnv = new HashMap<String, String>();
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apiPersistenceInstance, apimgtDAO, scopesDAO, null, null);
    Mockito.when(artifactManager.newGovernanceArtifact(any(QName.class))).thenReturn(artifact);
    Mockito.when(APIUtil.createAPIArtifactContent(artifact, api)).thenReturn(artifact);
    RegistryService registryService = Mockito.mock(RegistryService.class);
    UserRegistry userRegistry = Mockito.mock(UserRegistry.class);
    ServiceReferenceHolder serviceReferenceHolder = TestUtils.getServiceReferenceHolder();
    RealmService realmService = Mockito.mock(RealmService.class);
    TenantManager tenantManager = Mockito.mock(TenantManager.class);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    Mockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
    Mockito.when(registryService.getConfigSystemRegistry(Mockito.anyInt())).thenReturn(userRegistry);
    Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
    Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
    PublisherAPI publisherAPI = Mockito.mock(PublisherAPI.class);
    PowerMockito.when(apiPersistenceInstance.addAPI(any(Organization.class), any(PublisherAPI.class))).thenReturn(publisherAPI);
    apiProvider.addAPI(api);
    // Mock Updating API
    Resource apiSourceArtifact = Mockito.mock(Resource.class);
    Mockito.when(apiSourceArtifact.getUUID()).thenReturn("12640983654");
    String apiSourcePath = "path";
    PowerMockito.when(APIUtil.getAPIPath(api.getId())).thenReturn(apiSourcePath);
    PowerMockito.when(apiProvider.registry.get(apiSourcePath)).thenReturn(apiSourceArtifact);
    Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_STATUS)).thenReturn("CREATED");
    Mockito.when(artifactManager.getGenericArtifact(apiSourceArtifact.getUUID())).thenReturn(artifact);
    PowerMockito.when(APIUtil.createAPIArtifactContent(artifact, api)).thenReturn(artifact);
    Mockito.when(artifact.getId()).thenReturn("12640983654");
    PowerMockito.when(GovernanceUtils.getArtifactPath(apiProvider.registry, "12640983654")).thenReturn(apiSourcePath);
    Mockito.doNothing().when(artifactManager).updateGenericArtifact(artifact);
    WorkflowExecutorFactory wfe = PowerMockito.mock(WorkflowExecutorFactory.class);
    Mockito.when(WorkflowExecutorFactory.getInstance()).thenReturn(wfe);
    boolean status = apiProvider.updateAPIforStateChange(api, APIConstants.CREATED, APIConstants.RETIRED);
    Assert.assertEquals(2, api.getEnvironments().size());
    Assert.assertEquals(true, status);
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) WorkflowExecutorFactory(org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutorFactory) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) RealmService(org.wso2.carbon.user.core.service.RealmService) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) API(org.wso2.carbon.apimgt.api.model.API) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager) HashSet(java.util.HashSet) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 7 with ScopesDAO

use of org.wso2.carbon.apimgt.impl.dao.ScopesDAO in project carbon-apimgt by wso2.

the class APIProviderImplTest method testGetCustomApiOutSequences.

@Test
public void testGetCustomApiOutSequences() throws Exception {
    APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.1");
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    mockSequences(APIConstants.API_CUSTOM_OUTSEQUENCE_LOCATION, APIConstants.API_CUSTOM_SEQUENCE_TYPE_OUT, apiId);
    List<String> sequenceList = apiProvider.getCustomApiOutSequences(apiId);
    Assert.assertNotNull(sequenceList);
    Assert.assertEquals(1, sequenceList.size());
    // OMException when building OMElement
    PowerMockito.when(APIUtil.buildOMElement(any(InputStream.class))).thenThrow(new OMException());
    apiProvider.getCustomApiOutSequences(apiId);
    // org.wso2.carbon.registry.api.RegistryException
    ServiceReferenceHolder sh = PowerMockito.mock(ServiceReferenceHolder.class);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(sh);
    RegistryService registryService = Mockito.mock(RegistryService.class);
    PowerMockito.when(sh.getRegistryService()).thenReturn(registryService);
    UserRegistry registry = Mockito.mock(UserRegistry.class);
    PowerMockito.when(registryService.getGovernanceSystemRegistry(Matchers.anyInt())).thenReturn(registry);
    Mockito.when(registry.resourceExists(APIUtil.getSequencePath(apiId, APIConstants.API_CUSTOM_SEQUENCE_TYPE_OUT))).thenThrow(org.wso2.carbon.registry.api.RegistryException.class);
    String msg = "Error while processing the " + APIConstants.API_CUSTOM_SEQUENCE_TYPE_OUT + " sequences of " + apiId + " in the registry";
    try {
        apiProvider.getCustomApiOutSequences(apiId);
    } catch (APIManagementException e) {
        Assert.assertEquals(msg, e.getMessage());
    }
    // Registry Exception
    PowerMockito.when(registryService.getGovernanceSystemRegistry(Matchers.anyInt())).thenThrow(RegistryException.class);
    String msg1 = "Error while retrieving registry for tenant -1";
    try {
        apiProvider.getCustomApiOutSequences(apiId);
    } catch (APIManagementException e) {
        Assert.assertEquals(msg1, e.getMessage());
    }
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) InputStream(java.io.InputStream) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) OMException(org.apache.axiom.om.OMException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 8 with ScopesDAO

use of org.wso2.carbon.apimgt.impl.dao.ScopesDAO in project carbon-apimgt by wso2.

the class APIProviderImplTest method testSearchAPIs_NoProviderId.

@Test
public void testSearchAPIs_NoProviderId() throws APIManagementException, RegistryException {
    API api1 = new API(new APIIdentifier("admin", "API1", "1.0.1"));
    api1.setContext("api1context");
    api1.setStatus(APIConstants.PUBLISHED);
    api1.setDescription("API 1 Desciption");
    Set<URITemplate> uriTemplates = new HashSet<URITemplate>();
    URITemplate uriTemplate1 = new URITemplate();
    uriTemplate1.setHTTPVerb("POST");
    uriTemplate1.setAuthType("Application");
    uriTemplate1.setUriTemplate("/add");
    uriTemplate1.setThrottlingTier("Gold");
    uriTemplates.add(uriTemplate1);
    api1.setUriTemplates(uriTemplates);
    API api2 = new API(new APIIdentifier("admin", "API2", "1.0.0"));
    api2.setContext("api2context");
    api2.setStatus(APIConstants.CREATED);
    api2.setDescription("API 2 Desciption");
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    GenericArtifact genericArtifact1 = Mockito.mock(GenericArtifact.class);
    GenericArtifact genericArtifact2 = Mockito.mock(GenericArtifact.class);
    Mockito.when(genericArtifact1.getAttribute(APIConstants.API_OVERVIEW_NAME)).thenReturn("API1");
    Mockito.when(genericArtifact1.getAttribute(APIConstants.API_OVERVIEW_VERSION)).thenReturn("1.0.1");
    Mockito.when(genericArtifact1.getAttribute(APIConstants.API_OVERVIEW_CONTEXT)).thenReturn("api1context");
    Mockito.when(genericArtifact1.getAttribute(APIConstants.API_OVERVIEW_DESCRIPTION)).thenReturn("API 1 Desciption");
    Mockito.when(genericArtifact1.getAttribute(APIConstants.API_OVERVIEW_PROVIDER)).thenReturn("admin");
    Mockito.when(genericArtifact1.getAttribute(APIConstants.API_OVERVIEW_STATUS)).thenReturn("Published");
    Mockito.when(genericArtifact2.getAttribute(APIConstants.API_OVERVIEW_NAME)).thenReturn("API2");
    Mockito.when(genericArtifact2.getAttribute(APIConstants.API_OVERVIEW_VERSION)).thenReturn("1.0.0");
    Mockito.when(genericArtifact2.getAttribute(APIConstants.API_OVERVIEW_CONTEXT)).thenReturn("api2context");
    Mockito.when(genericArtifact2.getAttribute(APIConstants.API_OVERVIEW_DESCRIPTION)).thenReturn("API 2 Desciption");
    Mockito.when(genericArtifact2.getAttribute(APIConstants.API_OVERVIEW_PROVIDER)).thenReturn("admin");
    Mockito.when(genericArtifact2.getAttribute(APIConstants.API_OVERVIEW_STATUS)).thenReturn("Created");
    Mockito.when(APIUtil.getAPI(genericArtifact1, apiProvider.registry)).thenReturn(api1);
    Mockito.when(APIUtil.getAPI(genericArtifact2, apiProvider.registry)).thenReturn(api2);
    Mockito.when(APIUtil.getAPI(genericArtifact1)).thenReturn(api1);
    Mockito.when(APIUtil.getAPI(genericArtifact2)).thenReturn(api2);
    GenericArtifact[] genericArtifacts = { genericArtifact1, genericArtifact2 };
    Mockito.when(artifactManager.getAllGenericArtifacts()).thenReturn(genericArtifacts);
    // Search by Name matching both APIs
    List<API> foundApiList0 = apiProvider.searchAPIs("API", "Name", null);
    Assert.assertNotNull(foundApiList0);
    Assert.assertEquals(2, foundApiList0.size());
    // Search by exact name
    List<API> foundApiList1 = apiProvider.searchAPIs("API1", "Name", null);
    Assert.assertNotNull(foundApiList1);
    Assert.assertEquals(1, foundApiList1.size());
    // Search by exact provider
    List<API> foundApiList2 = apiProvider.searchAPIs("admin", "Provider", null);
    Assert.assertNotNull(foundApiList2);
    Assert.assertEquals(2, foundApiList2.size());
    // Search by exact version
    List<API> foundApiList3 = apiProvider.searchAPIs("1.0.0", "Version", null);
    Assert.assertNotNull(foundApiList3);
    Assert.assertEquals(1, foundApiList3.size());
    // Search by exact context
    List<API> foundApiList4 = apiProvider.searchAPIs("api1context", "Context", null);
    Assert.assertNotNull(foundApiList4);
    Assert.assertEquals(1, foundApiList4.size());
    // Search by exact context
    List<API> foundApiList5 = apiProvider.searchAPIs("api2context", "Context", null);
    Assert.assertNotNull(foundApiList5);
    Assert.assertEquals(1, foundApiList5.size());
    // Search by wrong context
    List<API> foundApiList6 = apiProvider.searchAPIs("test", "Context", null);
    Assert.assertNotNull(foundApiList6);
    Assert.assertEquals(0, foundApiList6.size());
    // Search by status
    List<API> foundApiList7 = apiProvider.searchAPIs("Published", "Status", null);
    Assert.assertNotNull(foundApiList7);
    Assert.assertEquals(1, foundApiList7.size());
    // Search by Description
    List<API> foundApiList8 = apiProvider.searchAPIs("API 1 Desciption", "Description", null);
    Assert.assertNotNull(foundApiList8);
    Assert.assertEquals(1, foundApiList8.size());
    // Search by Description
    List<API> foundApiList9 = apiProvider.searchAPIs("API", "Description", null);
    Assert.assertNotNull(foundApiList9);
    Assert.assertEquals(2, foundApiList9.size());
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) HashSet(java.util.HashSet) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 9 with ScopesDAO

use of org.wso2.carbon.apimgt.impl.dao.ScopesDAO in project carbon-apimgt by wso2.

the class APIProviderImplTest method testAddDocumentationContent.

@Test
public void testAddDocumentationContent() throws Exception {
    APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.0");
    API api = new API(apiId);
    String docName = "HowTo";
    Documentation doc = new Documentation(DocumentationType.HOWTO, docName);
    doc.setVisibility(DocumentVisibility.API_LEVEL);
    String docPath = "/apimgt/applicationdata/provider/admin/API1/1.0.0/documentation/contents";
    String documentationPath = docPath + docName;
    String contentPath = docPath + APIConstants.INLINE_DOCUMENT_CONTENT_DIR + RegistryConstants.PATH_SEPARATOR + docName;
    Mockito.when(APIUtil.getAPIDocPath(apiId)).thenReturn(docPath);
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    Resource docResource = Mockito.mock(Resource.class);
    Mockito.when(docResource.getUUID()).thenReturn("678ghk");
    Mockito.when(apiProvider.registry.get(documentationPath)).thenReturn(docResource);
    GenericArtifact docArtifact = Mockito.mock(GenericArtifact.class);
    PowerMockito.whenNew(GenericArtifactManager.class).withAnyArguments().thenReturn(artifactManager);
    Mockito.when(artifactManager.getGenericArtifact("678ghk")).thenReturn(docArtifact);
    Mockito.when(APIUtil.getDocumentation(docArtifact)).thenReturn(doc);
    Resource docContent = Mockito.mock(Resource.class);
    Mockito.when(apiProvider.registry.resourceExists(contentPath)).thenReturn(true, false);
    Mockito.when(apiProvider.registry.get(contentPath)).thenReturn(docContent);
    Mockito.when(apiProvider.registry.newResource()).thenReturn(docContent);
    apiProvider.addDocumentationContent(api, docName, "content");
    RegistryAuthorizationManager registryAuthorizationManager = Mockito.mock(RegistryAuthorizationManager.class);
    PowerMockito.whenNew(RegistryAuthorizationManager.class).withAnyArguments().thenReturn(registryAuthorizationManager);
    apiProvider.tenantDomain = "carbon.super";
    doc.setVisibility(DocumentVisibility.OWNER_ONLY);
    apiProvider.addDocumentationContent(api, docName, "content");
    doc.setVisibility(DocumentVisibility.PRIVATE);
    apiProvider.addDocumentationContent(api, docName, "content");
    Mockito.doThrow(RegistryException.class).when(apiProvider.registry).put(Matchers.anyString(), any(Resource.class));
    try {
        apiProvider.addDocumentationContent(api, docName, "content");
    } catch (APIManagementException e) {
        String msg = "Failed to add the documentation content of : " + docName + " of API :" + apiId.getApiName();
        Assert.assertEquals(msg, e.getMessage());
    }
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) RegistryAuthorizationManager(org.wso2.carbon.registry.core.jdbc.realm.RegistryAuthorizationManager) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) Resource(org.wso2.carbon.registry.core.Resource) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) API(org.wso2.carbon.apimgt.api.model.API) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 10 with ScopesDAO

use of org.wso2.carbon.apimgt.impl.dao.ScopesDAO in project carbon-apimgt by wso2.

the class APIProviderImplTest method testGetAPILifeCycleData.

@Test
public void testGetAPILifeCycleData() throws Exception {
    APIIdentifier identifier = new APIIdentifier("admin-AT-carbon.super", "API1", "1.0.0");
    String path = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getApiName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion();
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    Resource apiSourceArtifact = Mockito.mock(Resource.class);
    Mockito.when(apiSourceArtifact.getUUID()).thenReturn("12640983654");
    PowerMockito.when(APIUtil.getAPIPath(identifier)).thenReturn(path);
    PowerMockito.when(apiProvider.registry.get(path)).thenReturn(apiSourceArtifact);
    Mockito.when(artifactManager.getGenericArtifact(apiSourceArtifact.getUUID())).thenReturn(artifact);
    Mockito.when(artifact.getLifecycleState()).thenReturn("Created");
    LifecycleBean bean = getLCBean();
    Mockito.when(LifecycleBeanPopulator.getLifecycleBean(path, (UserRegistry) apiProvider.registry, apiProvider.configRegistry)).thenReturn(bean);
    Map<String, Object> lcData = apiProvider.getAPILifeCycleData(identifier);
    List checkListItems = (List) lcData.get(APIConstants.LC_CHECK_ITEMS);
    Assert.assertEquals(2, checkListItems.size());
    if (checkListItems.get(0) instanceof CheckListItem) {
        CheckListItem checkListItem = (CheckListItem) checkListItems.get(0);
        Assert.assertTrue((APIConstants.RESUBSCRIBE_CHECK_LIST_ITEM).equals(checkListItem.getName()) || (APIConstants.DEPRECATE_CHECK_LIST_ITEM).equals(checkListItem.getName()));
        if ((APIConstants.RESUBSCRIBE_CHECK_LIST_ITEM).equals(checkListItem.getName())) {
            Assert.assertEquals("1", checkListItem.getOrder());
        } else {
            Assert.assertEquals("0", checkListItem.getOrder());
        }
        Assert.assertEquals("Created", checkListItem.getLifeCycleStatus());
    }
    if (checkListItems.get(1) instanceof CheckListItem) {
        CheckListItem checkListItem = (CheckListItem) checkListItems.get(1);
        Assert.assertTrue((APIConstants.RESUBSCRIBE_CHECK_LIST_ITEM).equals(checkListItem.getName()) || (APIConstants.DEPRECATE_CHECK_LIST_ITEM).equals(checkListItem.getName()));
        if ((APIConstants.RESUBSCRIBE_CHECK_LIST_ITEM).equals(checkListItem.getName())) {
            Assert.assertEquals("1", checkListItem.getOrder());
        } else {
            Assert.assertEquals("0", checkListItem.getOrder());
        }
        Assert.assertEquals("Created", checkListItem.getLifeCycleStatus());
    }
}
Also used : CheckListItem(org.wso2.carbon.governance.custom.lifecycles.checklist.util.CheckListItem) Resource(org.wso2.carbon.registry.core.Resource) LifecycleBean(org.wso2.carbon.governance.custom.lifecycles.checklist.beans.LifecycleBean) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) JSONObject(org.json.simple.JSONObject) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

Test (org.junit.Test)76 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)76 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)51 ServiceReferenceHolder (org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder)31 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)31 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)29 RegistryService (org.wso2.carbon.registry.core.service.RegistryService)29 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)25 API (org.wso2.carbon.apimgt.api.model.API)24 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)24 PublisherAPI (org.wso2.carbon.apimgt.persistence.dto.PublisherAPI)24 Resource (org.wso2.carbon.registry.core.Resource)20 TenantManager (org.wso2.carbon.user.core.tenant.TenantManager)18 RealmService (org.wso2.carbon.user.core.service.RealmService)17 QName (javax.xml.namespace.QName)16 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)15 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)15 HashSet (java.util.HashSet)13 InputStream (java.io.InputStream)12 OMException (org.apache.axiom.om.OMException)12