Search in sources :

Example 16 with ServiceReferenceHolder

use of org.wso2.carbon.apimgt.gateway.internal.ServiceReferenceHolder 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 17 with ServiceReferenceHolder

use of org.wso2.carbon.apimgt.gateway.internal.ServiceReferenceHolder in project carbon-apimgt by wso2.

the class APIProviderImplTest method prepareForChangeLifeCycleStatus.

/**
 * This method can be used when invoking changeLifeCycleStatus()
 */
private void prepareForChangeLifeCycleStatus(APIProviderImplWrapper apiProvider, ApiMgtDAO apimgtDAO, APIIdentifier apiId, GenericArtifact apiArtifact) throws GovernanceException, APIManagementException, FaultGatewaysException, WorkflowException, XMLStreamException {
    Mockito.when(APIUtil.getAPIArtifact(apiId, apiProvider.registry)).thenReturn(apiArtifact);
    Mockito.when(apiArtifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER)).thenReturn("admin");
    Mockito.when(apiArtifact.getAttribute(APIConstants.API_OVERVIEW_NAME)).thenReturn("API1");
    Mockito.when(apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VERSION)).thenReturn("1.0.0");
    Mockito.when(apiArtifact.getLifecycleState()).thenReturn("CREATED");
    Mockito.when(apimgtDAO.getAPIID(apiUUID)).thenReturn(1);
    // Workflow has not started, this will trigger the executor
    WorkflowDTO wfDTO1 = Mockito.mock(WorkflowDTO.class);
    Mockito.when(wfDTO1.getStatus()).thenReturn(null);
    WorkflowDTO wfDTO2 = Mockito.mock(WorkflowDTO.class);
    Mockito.when(wfDTO2.getStatus()).thenReturn(WorkflowStatus.APPROVED);
    Mockito.when(apimgtDAO.retrieveWorkflowFromInternalReference("1", WorkflowConstants.WF_TYPE_AM_API_STATE)).thenReturn(wfDTO1, wfDTO2);
    ServiceReferenceHolder sh = TestUtils.getServiceReferenceHolder();
    APIManagerConfigurationService amConfigService = Mockito.mock(APIManagerConfigurationService.class);
    APIManagerConfiguration amConfig = Mockito.mock(APIManagerConfiguration.class);
    PowerMockito.when(sh.getAPIManagerConfigurationService()).thenReturn(amConfigService);
    PowerMockito.when(amConfigService.getAPIManagerConfiguration()).thenReturn(amConfig);
    WorkflowProperties workflowProperties = Mockito.mock(WorkflowProperties.class);
    Mockito.when(workflowProperties.getWorkflowCallbackAPI()).thenReturn("");
    Mockito.when(amConfig.getWorkflowProperties()).thenReturn(workflowProperties);
    WorkflowExecutorFactory wfe = PowerMockito.mock(WorkflowExecutorFactory.class);
    Mockito.when(WorkflowExecutorFactory.getInstance()).thenReturn(wfe);
    WorkflowExecutor apiStateWFExecutor = new APIStateChangeSimpleWorkflowExecutor();
    Mockito.when(wfe.getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_API_STATE)).thenReturn(apiStateWFExecutor);
    Mockito.when(APIUtil.isAnalyticsEnabled()).thenReturn(false);
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) WorkflowDTO(org.wso2.carbon.apimgt.impl.dto.WorkflowDTO) WorkflowExecutorFactory(org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutorFactory) APIStateChangeSimpleWorkflowExecutor(org.wso2.carbon.apimgt.impl.workflow.APIStateChangeSimpleWorkflowExecutor) WorkflowExecutor(org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutor) APIStateChangeSimpleWorkflowExecutor(org.wso2.carbon.apimgt.impl.workflow.APIStateChangeSimpleWorkflowExecutor) WorkflowProperties(org.wso2.carbon.apimgt.impl.dto.WorkflowProperties)

Example 18 with ServiceReferenceHolder

use of org.wso2.carbon.apimgt.gateway.internal.ServiceReferenceHolder 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 19 with ServiceReferenceHolder

use of org.wso2.carbon.apimgt.gateway.internal.ServiceReferenceHolder in project carbon-apimgt by wso2.

the class APIProviderImplTest method testGetCustomInSequences1.

@Test
public void testGetCustomInSequences1() 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();
    Assert.assertNotNull(sequenceList);
    Assert.assertEquals(1, sequenceList.size());
    // OMException when building OMElement
    PowerMockito.when(APIUtil.buildOMElement(any(InputStream.class))).thenThrow(new OMException());
    apiProvider.getCustomOutSequences(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 = "Error while processing the in in the registry";
    try {
        apiProvider.getCustomInSequences();
    } 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.getCustomInSequences();
    } 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 20 with ServiceReferenceHolder

use of org.wso2.carbon.apimgt.gateway.internal.ServiceReferenceHolder in project carbon-apimgt by wso2.

the class APIProviderImplTest method testIsSynapseGateway.

@Test
public void testIsSynapseGateway() throws APIManagementException {
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    ServiceReferenceHolder sh = PowerMockito.mock(ServiceReferenceHolder.class);
    APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
    APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(sh);
    Mockito.when(sh.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
    Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
    Mockito.when(apiManagerConfiguration.getFirstProperty(Mockito.anyString())).thenReturn("synapse");
    assertTrue(apiProvider.isSynapseGateway());
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

ServiceReferenceHolder (org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder)109 Test (org.junit.Test)105 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)102 RegistryService (org.wso2.carbon.registry.core.service.RegistryService)64 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)63 RealmService (org.wso2.carbon.user.core.service.RealmService)53 TenantManager (org.wso2.carbon.user.core.tenant.TenantManager)49 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)47 Resource (org.wso2.carbon.registry.core.Resource)40 APIManagerConfiguration (org.wso2.carbon.apimgt.impl.APIManagerConfiguration)38 APIManagerConfigurationService (org.wso2.carbon.apimgt.impl.APIManagerConfigurationService)30 InputStream (java.io.InputStream)29 PrivilegedCarbonContext (org.wso2.carbon.context.PrivilegedCarbonContext)28 Before (org.junit.Before)27 API (org.wso2.carbon.apimgt.api.model.API)24 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)23 ApiMgtDAO (org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO)22 HashMap (java.util.HashMap)21 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)21 QName (javax.xml.namespace.QName)19