Search in sources :

Example 11 with ScopesDAO

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

the class APIProviderImplTest method testCheckIfAPIExists.

@Test
public void testCheckIfAPIExists() throws APIManagementException, UserStoreException, RegistryException, XMLStreamException {
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.1");
    Mockito.when(APIUtil.getAPIPath(apiId)).thenReturn("testPath");
    PowerMockito.mockStatic(MultitenantUtils.class);
    PowerMockito.when(MultitenantUtils.getTenantDomain(Mockito.anyString())).thenReturn("abc.org");
    // Mock Config system registry
    ServiceReferenceHolder sh = TestUtils.getServiceReferenceHolder();
    RegistryService registryService = Mockito.mock(RegistryService.class);
    PowerMockito.when(sh.getRegistryService()).thenReturn(registryService);
    UserRegistry systemReg = Mockito.mock(UserRegistry.class);
    RealmService realmService = Mockito.mock(RealmService.class);
    TenantManager tm = Mockito.mock(TenantManager.class);
    PowerMockito.when(sh.getRealmService()).thenReturn(realmService);
    PowerMockito.when(realmService.getTenantManager()).thenReturn(tm);
    PowerMockito.when(tm.getTenantId(Matchers.anyString())).thenReturn(-1234);
    PowerMockito.when(sh.getRegistryService()).thenReturn(registryService);
    PowerMockito.when(registryService.getGovernanceSystemRegistry(-1234)).thenReturn(systemReg);
    Mockito.when(systemReg.resourceExists("testPath")).thenReturn(true);
    Assert.assertEquals(true, apiProvider.checkIfAPIExists(apiId));
    PowerMockito.when(MultitenantUtils.getTenantDomain(Mockito.anyString())).thenReturn("carbon.super");
    apiProvider.tenantDomain = "carbon.super1";
    PowerMockito.when(registryService.getGovernanceUserRegistry("admin", -1234)).thenReturn(systemReg);
    Assert.assertEquals(true, apiProvider.checkIfAPIExists(apiId));
    apiProvider.tenantDomain = null;
    apiProvider.registry = systemReg;
    Assert.assertEquals(true, apiProvider.checkIfAPIExists(apiId));
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) RealmService(org.wso2.carbon.user.core.service.RealmService) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 12 with ScopesDAO

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

the class APIProviderImplTest method testAddAPINameWithIllegalCharacters.

@Test
public void testAddAPINameWithIllegalCharacters() throws APIManagementException, GovernanceException {
    APIIdentifier apiId = new APIIdentifier("admin", "API2&", "1.0.2");
    API api = new API(apiId);
    api.setContext("/test");
    api.setStatus(APIConstants.CREATED);
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    Mockito.when(artifactManager.newGovernanceArtifact(any(QName.class))).thenReturn(artifact);
    Mockito.when(APIUtil.createAPIArtifactContent(artifact, api)).thenReturn(artifact);
    try {
        apiProvider.addAPI(api);
        Assert.fail("Exception was expected, but wasn't thrown");
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().contains("API Name contains one or more illegal characters"));
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) QName(javax.xml.namespace.QName) 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 13 with ScopesDAO

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

the class APIProviderImplTest method testGetProvider.

@Test
public void testGetProvider() throws APIManagementException, RegistryException {
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    Mockito.when(APIUtil.getMountedPath((RegistryContext) Mockito.anyObject(), Mockito.anyString())).thenReturn("testPath");
    UserRegistry userReg = Mockito.mock(UserRegistry.class);
    Resource resource = Mockito.mock(Resource.class);
    Mockito.when(apiProvider.registry.get("testPath/providers/testProvider")).thenReturn(resource);
    Mockito.when(resource.getUUID()).thenReturn("testID");
    PowerMockito.when(APIUtil.getArtifactManager(userReg, APIConstants.API_KEY)).thenReturn(artifactManager);
    GenericArtifact providerArtifact = Mockito.mock(GenericArtifact.class);
    Mockito.when(artifactManager.getGenericArtifact("testID")).thenReturn(providerArtifact);
    Provider provider = Mockito.mock(Provider.class);
    Mockito.when(APIUtil.getProvider(providerArtifact)).thenReturn(provider);
    Assert.assertNotNull(apiProvider.getProvider("testProvider"));
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Provider(org.wso2.carbon.apimgt.api.model.Provider) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 14 with ScopesDAO

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

the class APIProviderImplTest method testGetSecurityAuditAttributesFromTenantConfig.

/**
 * This method tests the retrieval of API Security Audit Properties from the Tenant Config
 * (i.e. tenant-conf.json)
 * @throws RegistryException
 * @throws APIManagementException
 */
@Test
public void testGetSecurityAuditAttributesFromTenantConfig() throws RegistryException, APIManagementException {
    // Instantiating required variables
    final int tenantId = -1234;
    String apiToken = "1234f0ca-9879-112f-0e8f-a098e0do12456";
    String collectionId = "467f8ca-40f8-4baf-8b0f-c6854ed04653";
    boolean overrideGlobal = true;
    // Sample JSONObject for mocking
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("apiToken", apiToken);
    jsonObject.put("collectionId", collectionId);
    jsonObject.put("overrideGlobal", overrideGlobal);
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
    PowerMockito.mockStatic(ServiceReferenceHolder.class);
    RegistryService registryService = Mockito.mock(RegistryService.class);
    UserRegistry userRegistry = Mockito.mock(UserRegistry.class);
    Resource resource = Mockito.mock(Resource.class);
    Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    Mockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
    Mockito.when(registryService.getConfigSystemRegistry(tenantId)).thenReturn(userRegistry);
    PowerMockito.mockStatic(APIUtil.class);
    Mockito.when(APIUtil.getSecurityAuditAttributesFromRegistry(superTenantDomain)).thenReturn(jsonObject);
    // Pass the mock values to the method call
    JSONObject jsonObject1 = apiProvider.getSecurityAuditAttributesFromConfig("admin");
    // Compare the API Token and Collection ID returned from the method call
    Assert.assertEquals(jsonObject1.get(APIConstants.SECURITY_AUDIT_API_TOKEN), apiToken);
    Assert.assertEquals(jsonObject1.get(APIConstants.SECURITY_AUDIT_COLLECTION_ID), collectionId);
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) JSONObject(org.json.simple.JSONObject) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 15 with ScopesDAO

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

the class APIProviderImplTest method testUpdateAPIsInExternalAPIStores.

@Test
public void testUpdateAPIsInExternalAPIStores() throws APIManagementException {
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.1");
    API api = new API(apiId);
    api.setContext("/test");
    api.setStatus(APIConstants.CREATED);
    api.setUuid(UUID.randomUUID().toString());
    APIPublisher publisher = Mockito.mock(APIPublisher.class);
    Set<APIStore> apiStores = new HashSet<APIStore>();
    APIStore apiStore = new APIStore();
    apiStore.setDisplayName("testName");
    apiStore.setName("testStoreName");
    apiStore.setPublisher(publisher);
    apiStore.setEndpoint("testEndpoint");
    apiStore.setType("testType");
    apiStores.add(apiStore);
    APIStore apiStore1 = new APIStore();
    apiStore1.setDisplayName("testName1");
    apiStore1.setName("testStoreName1");
    apiStore1.setEndpoint("testEndpoint");
    apiStore1.setType("testType");
    apiStore1.setPublisher(publisher);
    apiStores.add(apiStore1);
    PowerMockito.when(APIUtil.getExternalStores(-1)).thenReturn(apiStores);
    PowerMockito.when(APIUtil.isAPIsPublishToExternalAPIStores(-1)).thenReturn(true);
    Mockito.when(apimgtDAO.getExternalAPIStoresDetails(api.getUuid())).thenReturn(apiStores);
    Mockito.when(publisher.isAPIAvailable(api, apiStore)).thenReturn(true);
    Mockito.when(publisher.isAPIAvailable(api, apiStore1)).thenReturn(true);
    Mockito.when(APIUtil.getExternalAPIStore(apiStore.getName(), -1)).thenReturn(apiStore);
    Mockito.when(APIUtil.getExternalAPIStore(apiStore1.getName(), -1)).thenReturn(apiStore1);
    Assert.assertTrue(apiProvider.updateAPIsInExternalAPIStores(api, apiStores, true));
}
Also used : APIPublisher(org.wso2.carbon.apimgt.api.model.APIPublisher) 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) APIStore(org.wso2.carbon.apimgt.api.model.APIStore) HashSet(java.util.HashSet) 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