Search in sources :

Example 71 with ScopesDAO

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

the class APIProviderImplTest method testIsGlobalPolicyKeyTemplateExists.

@Test
public void testIsGlobalPolicyKeyTemplateExists() throws APIManagementException {
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    GlobalPolicy globalPolicy = Mockito.mock(GlobalPolicy.class);
    Mockito.when(apimgtDAO.isKeyTemplatesExist(globalPolicy)).thenReturn(true);
    assertTrue(apiProvider.isGlobalPolicyKeyTemplateExists(globalPolicy));
}
Also used : GlobalPolicy(org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 72 with ScopesDAO

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

the class APIProviderImplTest method testGetSecurityAuditAttributesFromGlobalConfig.

/**
 * This method tests the retrieval of API Security Audit Properties from the Global Config
 * (i.e. deployment.toml)
 * @throws APIManagementException
 */
@Test
public void testGetSecurityAuditAttributesFromGlobalConfig() throws APIManagementException {
    // Instantiate required variables
    String username = "admin";
    String apiToken = "2780f0ca-3423-435f-0e9f-a634e0do65915";
    String collectionId = "8750f8ca-34f9-4baf-8b9f-c6854ed06534";
    String global = "true";
    // Mock retrieving the tenant domain
    PowerMockito.mockStatic(MultitenantUtils.class);
    Mockito.when(MultitenantUtils.getTenantDomain(username)).thenReturn("carbon.super");
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    // Create a mock APIManagerConfiguration Object for retrieving properties from the deployment.toml
    APIManagerConfiguration apiManagerConfiguration = PowerMockito.mock(APIManagerConfiguration.class);
    ServiceReferenceHolder serviceReferenceHolder = PowerMockito.mock(ServiceReferenceHolder.class);
    APIManagerConfigurationService apiManagerConfigurationService = PowerMockito.mock(APIManagerConfigurationService.class);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
    Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
    // Mock the properties read from the deployment.toml
    Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.API_SECURITY_AUDIT_API_TOKEN)).thenReturn(apiToken);
    Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.API_SECURITY_AUDIT_CID)).thenReturn(collectionId);
    Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.API_SECURITY_AUDIT_GLOBAL)).thenReturn(global);
    // Pass the mocked properties into the getSecurityAuditAttributesFromConfig method
    JSONObject jsonObject = apiProvider.getSecurityAuditAttributesFromConfig(username);
    // Compare the properties from the returned object and the mocked ones.
    Assert.assertEquals(jsonObject.get(APIConstants.SECURITY_AUDIT_API_TOKEN), apiToken);
    Assert.assertEquals(jsonObject.get(APIConstants.SECURITY_AUDIT_COLLECTION_ID), collectionId);
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) JSONObject(org.json.simple.JSONObject) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 73 with ScopesDAO

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

the class APIProviderImplTest method testGetCustomInSequences.

@Test
public void testGetCustomInSequences() throws Exception {
    APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.1");
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    mockSequences(APIConstants.API_CUSTOM_INSEQUENCE_LOCATION, APIConstants.API_CUSTOM_SEQUENCE_TYPE_IN, apiId);
    List<String> sequenceList = apiProvider.getCustomInSequences(apiId);
    Assert.assertNotNull(sequenceList);
    Assert.assertEquals(2, sequenceList.size());
    Assert.assertTrue(sequenceList.contains("fault-seq"));
    Assert.assertTrue(sequenceList.contains("custom-fault-seq"));
    // OMException when building OMElement
    PowerMockito.when(APIUtil.buildOMElement(any(InputStream.class))).thenThrow(new OMException());
    apiProvider.getCustomInSequences(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(APIConstants.API_CUSTOM_INSEQUENCE_LOCATION)).thenThrow(org.wso2.carbon.registry.api.RegistryException.class);
    String msg = "Issue is in getting custom InSequences from the Registry";
    try {
        apiProvider.getCustomInSequences(apiId);
    } catch (APIManagementException e) {
        Assert.assertEquals(msg, e.getMessage());
    }
    // Registry Exception
    PowerMockito.when(registryService.getGovernanceSystemRegistry(Matchers.anyInt())).thenThrow(RegistryException.class);
    try {
        apiProvider.getCustomInSequences(apiId);
    } catch (APIManagementException e) {
        Assert.assertEquals(msg, 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 74 with ScopesDAO

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

the class APIProviderImplTest method testGetAPIsByProvider.

@Test
public void testGetAPIsByProvider() throws RegistryException, UserStoreException, APIManagementException {
    String providerId = "admin";
    API api1 = new API(new APIIdentifier("admin", "API1", "1.0.0"));
    API api2 = new API(new APIIdentifier("admin", "API2", "1.0.0"));
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    prepareForGetAPIsByProvider(artifactManager, apiProvider, providerId, api1, api2);
    List<API> apiResponse = apiProvider.getAPIsByProvider(providerId);
    Assert.assertEquals(2, apiResponse.size());
    Assert.assertEquals("API1", apiResponse.get(0).getId().getApiName());
    Mockito.when(apiProvider.registry.getAspectActions(Matchers.anyString(), Matchers.anyString())).thenThrow(RegistryException.class);
    try {
        apiProvider.getAPIsByProvider(providerId);
    } catch (APIManagementException e) {
        Assert.assertEquals("Failed to get APIs for provider : " + providerId, e.getMessage());
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) 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) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 75 with ScopesDAO

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

the class APIProviderImplTest method testGetApplicationPolicy.

@Test
public void testGetApplicationPolicy() throws APIManagementException {
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    PowerMockito.when(APIUtil.getTenantId("testUser")).thenReturn(1111);
    ApplicationPolicy applicationPolicy = Mockito.mock(ApplicationPolicy.class);
    Mockito.when(apimgtDAO.getApplicationPolicy("testPolicy", 1111)).thenReturn(applicationPolicy);
    assertNotNull(apiProvider.getApplicationPolicy("testUser", "testPolicy"));
}
Also used : ApplicationPolicy(org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy) 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