Search in sources :

Example 61 with ScopesDAO

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

the class APIProviderImplTest method testGetAPILifeCycleData_WithException.

@Test
public void testGetAPILifeCycleData_WithException() 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");
    Mockito.when(LifecycleBeanPopulator.getLifecycleBean(path, (UserRegistry) apiProvider.registry, apiProvider.configRegistry)).thenThrow(new Exception("Failed to get LC data"));
    try {
        apiProvider.getAPILifeCycleData(identifier);
    } catch (APIManagementException e) {
        Assert.assertEquals("Failed to get LC data", e.getMessage());
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Resource(org.wso2.carbon.registry.core.Resource) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) XMLStreamException(javax.xml.stream.XMLStreamException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException) BlockConditionNotFoundException(org.wso2.carbon.apimgt.api.BlockConditionNotFoundException) FaultGatewaysException(org.wso2.carbon.apimgt.api.FaultGatewaysException) DuplicateAPIException(org.wso2.carbon.apimgt.api.model.DuplicateAPIException) IOException(java.io.IOException) APIMgtResourceAlreadyExistsException(org.wso2.carbon.apimgt.api.APIMgtResourceAlreadyExistsException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ArtifactSynchronizerException(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException) WorkflowException(org.wso2.carbon.apimgt.impl.workflow.WorkflowException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) GovernanceException(org.wso2.carbon.governance.api.exception.GovernanceException) OMException(org.apache.axiom.om.OMException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 62 with ScopesDAO

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

the class APIProviderImplTest method testGetGlobalPolicy.

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

Example 63 with ScopesDAO

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

the class APIProviderImplTest method testChangeLifeCycleStatusOfAPIProduct.

@Test
public void testChangeLifeCycleStatusOfAPIProduct() throws APIManagementException, FaultGatewaysException {
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    String provider = "admin";
    PrivilegedCarbonContext carbonContext = Mockito.mock(PrivilegedCarbonContext.class);
    PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(carbonContext);
    PowerMockito.doNothing().when(carbonContext).setUsername(Mockito.anyString());
    PowerMockito.doNothing().when(carbonContext).setTenantDomain(Mockito.anyString(), Mockito.anyBoolean());
    APIProduct product = createMockAPIProduct(provider);
    Mockito.when(apimgtDAO.getAPIProductId(product.getId())).thenReturn(1);
    WorkflowDTO workflowDTO = Mockito.mock(WorkflowDTO.class);
    Mockito.when(workflowDTO.getStatus()).thenReturn(WorkflowStatus.CREATED);
    Mockito.when(apimgtDAO.retrieveWorkflowFromInternalReference(Integer.toString(1), WorkflowConstants.WF_TYPE_AM_API_PRODUCT_STATE)).thenReturn(workflowDTO);
    APIStateChangeResponse response = apiProvider.changeLifeCycleStatus("carbon.super", new ApiTypeWrapper(product), "Publish", null);
    Assert.assertNotNull(response);
}
Also used : PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) WorkflowDTO(org.wso2.carbon.apimgt.impl.dto.WorkflowDTO) ApiTypeWrapper(org.wso2.carbon.apimgt.api.model.ApiTypeWrapper) APIStateChangeResponse(org.wso2.carbon.apimgt.api.model.APIStateChangeResponse) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 64 with ScopesDAO

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

the class APIProviderImplTest method testChangeLifeCycleStatus.

@Test
public void testChangeLifeCycleStatus() throws RegistryException, UserStoreException, APIManagementException, FaultGatewaysException, WorkflowException, XMLStreamException {
    APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.0");
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    prepareForChangeLifeCycleStatus(apiProvider, apimgtDAO, apiId, artifact);
    APIStateChangeResponse response1 = apiProvider.changeLifeCycleStatus(apiId, APIConstants.API_LC_ACTION_DEPRECATE, "org1");
    Assert.assertEquals("APPROVED", response1.getStateChangeStatus());
}
Also used : APIStateChangeResponse(org.wso2.carbon.apimgt.api.model.APIStateChangeResponse) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 65 with ScopesDAO

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

the class APIProviderImplTest method testIsAPIUpdateValid.

@Test
public void testIsAPIUpdateValid() throws RegistryException, UserStoreException, APIManagementException {
    API api = new API(new APIIdentifier("admin", "API1", "1.0.0"));
    api.setContext("/test");
    api.setStatus(APIConstants.CREATED);
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    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);
    // API Status is CREATED and user has permission
    Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_STATUS)).thenReturn("CREATED");
    Mockito.when(artifactManager.getGenericArtifact(apiSourceArtifact.getUUID())).thenReturn(artifact);
    PowerMockito.when(APIUtil.hasPermission(null, APIConstants.Permissions.API_PUBLISH)).thenReturn(true);
    PowerMockito.when(APIUtil.hasPermission(null, APIConstants.Permissions.API_CREATE)).thenReturn(true);
    boolean status = apiProvider.isAPIUpdateValid(api);
    Assert.assertTrue(status);
    // API Status is CREATED and user doesn't have permission
    PowerMockito.when(APIUtil.hasPermission(null, APIConstants.Permissions.API_PUBLISH)).thenReturn(false);
    PowerMockito.when(APIUtil.hasPermission(null, APIConstants.Permissions.API_CREATE)).thenReturn(false);
    status = apiProvider.isAPIUpdateValid(api);
    Assert.assertFalse(status);
    // API Status is PROTOTYPED and user has permission
    api.setStatus(APIConstants.PROTOTYPED);
    Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_STATUS)).thenReturn("PROTOTYPED");
    // Mockito.when(artifactManager.getGenericArtifact(apiSourceArtifact.getUUID())).thenReturn(artifact);
    PowerMockito.when(APIUtil.hasPermission(null, APIConstants.Permissions.API_PUBLISH)).thenReturn(true);
    PowerMockito.when(APIUtil.hasPermission(null, APIConstants.Permissions.API_CREATE)).thenReturn(true);
    status = apiProvider.isAPIUpdateValid(api);
    Assert.assertTrue(status);
    // API Status is PROTOTYPED and user doesn't have permission
    api.setStatus(APIConstants.PROTOTYPED);
    Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_STATUS)).thenReturn("PROTOTYPED");
    // Mockito.when(artifactManager.getGenericArtifact(apiSourceArtifact.getUUID())).thenReturn(artifact);
    PowerMockito.when(APIUtil.hasPermission(null, APIConstants.Permissions.API_PUBLISH)).thenReturn(false);
    PowerMockito.when(APIUtil.hasPermission(null, APIConstants.Permissions.API_CREATE)).thenReturn(false);
    status = apiProvider.isAPIUpdateValid(api);
    Assert.assertFalse(status);
    // API Status is DEPRECATED and has publish permission
    api.setStatus(APIConstants.DEPRECATED);
    Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_STATUS)).thenReturn("DEPRECATED");
    // Mockito.when(artifactManager.getGenericArtifact(apiSourceArtifact.getUUID())).thenReturn(artifact);
    PowerMockito.when(APIUtil.hasPermission(null, APIConstants.Permissions.API_PUBLISH)).thenReturn(true);
    status = apiProvider.isAPIUpdateValid(api);
    Assert.assertTrue(status);
    // API Status is DEPRECATED and doesn't have publish permission
    api.setStatus(APIConstants.DEPRECATED);
    Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_STATUS)).thenReturn("DEPRECATED");
    // Mockito.when(artifactManager.getGenericArtifact(apiSourceArtifact.getUUID())).thenReturn(artifact);
    PowerMockito.when(APIUtil.hasPermission(null, APIConstants.Permissions.API_PUBLISH)).thenReturn(false);
    status = apiProvider.isAPIUpdateValid(api);
    Assert.assertFalse(status);
    // API Status is RETIRED and has publish permission
    api.setStatus(APIConstants.RETIRED);
    Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_STATUS)).thenReturn("RETIRED");
    // Mockito.when(artifactManager.getGenericArtifact(apiSourceArtifact.getUUID())).thenReturn(artifact);
    PowerMockito.when(APIUtil.hasPermission(null, APIConstants.Permissions.API_PUBLISH)).thenReturn(true);
    status = apiProvider.isAPIUpdateValid(api);
    Assert.assertTrue(status);
    // API Status is RETIRED and doesn't have publish permission
    api.setStatus(APIConstants.RETIRED);
    Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_STATUS)).thenReturn("RETIRED");
    // Mockito.when(artifactManager.getGenericArtifact(apiSourceArtifact.getUUID())).thenReturn(artifact);
    PowerMockito.when(APIUtil.hasPermission(null, APIConstants.Permissions.API_PUBLISH)).thenReturn(false);
    status = apiProvider.isAPIUpdateValid(api);
    Assert.assertFalse(status);
}
Also used : Resource(org.wso2.carbon.registry.core.Resource) 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)

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