Search in sources :

Example 96 with APIConsumer

use of org.wso2.carbon.apimgt.api.APIConsumer in project carbon-apimgt by wso2.

the class APIConsumerImplTest method testGetPublishedAPIsByProvider1.

@Test
public void testGetPublishedAPIsByProvider1() throws APIManagementException, RegistryException, org.wso2.carbon.user.core.UserStoreException {
    APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(userRegistry, apiMgtDAO);
    String providerId = "1";
    API api = new API(new APIIdentifier(API_PROVIDER, SAMPLE_API_NAME, SAMPLE_API_VERSION));
    API api1 = new API(new APIIdentifier(API_PROVIDER, "pizza_api", "2.0.0"));
    PowerMockito.when(APIUtil.isAllowDisplayMultipleVersions()).thenReturn(true, false);
    PowerMockito.when(APIUtil.isAllowDisplayAPIsWithMultipleStatus()).thenReturn(true, false);
    PowerMockito.when(APIUtil.getArtifactManager(userRegistry, APIConstants.API_KEY)).thenReturn(genericArtifactManager);
    PowerMockito.when(APIUtil.getMountedPath(Mockito.any(), Mockito.anyString())).thenReturn("system/governance");
    PowerMockito.when(APIUtil.getAPI(Mockito.any())).thenReturn(api);
    PowerMockito.when(APIUtil.replaceEmailDomainBack(Mockito.anyString())).thenReturn(providerId);
    PowerMockito.when(APIUtil.getLcStateFromArtifact((GovernanceArtifact) Mockito.any())).thenReturn(APIConstants.PUBLISHED);
    GenericArtifact genericArtifact1 = new GenericArtifactImpl(new QName("local"), "artifact1");
    GenericArtifact genericArtifact2 = new GenericArtifactImpl(new QName("local"), "artifact2");
    GenericArtifact[] genericArtifacts = new GenericArtifact[] { genericArtifact1, genericArtifact2 };
    Mockito.when(genericArtifactManager.findGenericArtifacts((Map<String, List<String>>) Mockito.any())).thenThrow(GovernanceException.class).thenReturn(genericArtifacts);
    PowerMockito.mockStatic(GovernanceUtils.class);
    PowerMockito.when(GovernanceUtils.getArtifactPath(Mockito.any(), Mockito.anyString())).thenReturn("/path1");
    PowerMockito.when(RegistryUtils.getAbsolutePath(Mockito.any(), Mockito.anyString())).thenReturn("/path1");
    Assert.assertNull(apiConsumer.getPublishedAPIsByProvider("1", "test_user", 5, API_PROVIDER, "John"));
    Assert.assertEquals(0, apiConsumer.getPublishedAPIsByProvider("1", "test_user", 5, API_PROVIDER, "John").size());
    Mockito.when(authorizationManager.isUserAuthorized(Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(true);
    Resource resource = new ResourceImpl();
    resource.setUUID(UUID.randomUUID().toString());
    Mockito.when(userRegistry.get(Mockito.anyString())).thenReturn(resource);
    GenericArtifact genericArtifact = Mockito.mock(GenericArtifactImpl.class);
    Mockito.when(genericArtifactManager.getGenericArtifact(Mockito.anyString())).thenReturn(genericArtifact);
    Assert.assertEquals(1, apiConsumer.getPublishedAPIsByProvider("1", "test_user", 1, API_PROVIDER, "John").size());
    api.setVisibility("specific_to_roles");
    PowerMockito.when(MultitenantUtils.getTenantDomain(Mockito.anyString())).thenReturn("carbon.super", "carbon.super", SAMPLE_TENANT_DOMAIN_1);
    PowerMockito.when(APIUtil.getAPI((GenericArtifact) Mockito.any())).thenReturn(api, api1);
    Assert.assertEquals(1, apiConsumer.getPublishedAPIsByProvider("1", "test_user", 5, API_PROVIDER, "John").size());
    Mockito.when(authorizationManager.isRoleAuthorized(Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(true);
    Assert.assertEquals(1, apiConsumer.getPublishedAPIsByProvider("1", "", 5, API_PROVIDER, "John").size());
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) QName(javax.xml.namespace.QName) Resource(org.wso2.carbon.registry.core.Resource) GovernanceException(org.wso2.carbon.governance.api.exception.GovernanceException) Matchers.anyString(org.mockito.Matchers.anyString) GenericArtifactImpl(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifactImpl) ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) DevPortalAPI(org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 97 with APIConsumer

use of org.wso2.carbon.apimgt.api.APIConsumer in project carbon-apimgt by wso2.

the class APIConsumerImplTest method testGetAllPaginatedAPIsByStatus.

@Test
public void testGetAllPaginatedAPIsByStatus() throws Exception {
    Registry userRegistry = Mockito.mock(Registry.class);
    APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(userRegistry, apiMgtDAO);
    System.setProperty(CARBON_HOME, "");
    APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
    APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
    Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
    Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
    Mockito.when(apiManagerConfiguration.getFirstProperty(Mockito.anyString())).thenReturn("10", "20");
    PowerMockito.mockStatic(GovernanceUtils.class);
    PowerMockito.mockStatic(GovernanceUtils.class);
    PowerMockito.doNothing().when(APIUtil.class, "loadTenantRegistry", Mockito.anyInt());
    GenericArtifactManager artifactManager = Mockito.mock(GenericArtifactManager.class);
    PowerMockito.when(APIUtil.isAllowDisplayMultipleVersions()).thenReturn(false, true);
    PowerMockito.when(APIUtil.getArtifactManager((UserRegistry) (Mockito.anyObject()), Mockito.anyString())).thenReturn(artifactManager);
    GenericArtifact artifact = Mockito.mock(GenericArtifact.class);
    GenericArtifact[] genericArtifacts = new GenericArtifact[] { artifact };
    Mockito.when(artifactManager.findGenericArtifacts(Mockito.anyMap())).thenReturn(genericArtifacts);
    APIIdentifier apiId1 = new APIIdentifier("admin", "API1", "1.0.0");
    API api = new API(apiId1);
    Mockito.when(APIUtil.getAPI(artifact)).thenReturn(api);
    String artifactPath = "artifact/path";
    PowerMockito.when(GovernanceUtils.getArtifactPath(userRegistry, artifact.getId())).thenReturn(artifactPath);
    Tag tag = new Tag();
    Tag[] tags = new Tag[] { tag };
    Mockito.when(userRegistry.getTags(artifactPath)).thenReturn(tags);
    assertNotNull(apiConsumer.getAllPaginatedAPIsByStatus(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, 0, 10, "testStatus", false));
    assertNotNull(apiConsumer.getAllPaginatedAPIsByStatus(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, 0, 10, "testStatus", true));
    // artifact manager null path
    PowerMockito.when(APIUtil.getArtifactManager((UserRegistry) (Mockito.anyObject()), Mockito.anyString())).thenReturn(null);
    assertNotNull(apiConsumer.getAllPaginatedAPIsByStatus(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, 0, 10, "testStatus", true));
    // generic artifact null path
    PowerMockito.when(APIUtil.getArtifactManager((UserRegistry) (Mockito.anyObject()), Mockito.anyString())).thenReturn(artifactManager);
    Mockito.when(artifactManager.findGenericArtifacts(Mockito.anyMap())).thenReturn(null);
    assertNotNull(apiConsumer.getAllPaginatedAPIsByStatus(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, 0, 10, "testStatus", true));
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) Matchers.anyString(org.mockito.Matchers.anyString) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) DevPortalAPI(org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI) API(org.wso2.carbon.apimgt.api.model.API) Tag(org.wso2.carbon.registry.core.Tag) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 98 with APIConsumer

use of org.wso2.carbon.apimgt.api.APIConsumer in project carbon-apimgt by wso2.

the class APIConsumerImplTest method testRequestApprovalForApplicationRegistration.

@Test
public void testRequestApprovalForApplicationRegistration() throws APIManagementException, UserStoreException {
    Scope scope1 = new Scope();
    scope1.setName("api_view");
    Scope scope2 = new Scope();
    scope2.setName("api_create");
    Set<Scope> scopes = new HashSet<Scope>();
    scopes.add(scope1);
    scopes.add(scope2);
    PowerMockito.when(MultitenantUtils.getTenantDomain(Mockito.anyString())).thenReturn("abc.org");
    KeyManagerConfigurationDTO keyManagerConfigurationsDto = new KeyManagerConfigurationDTO();
    keyManagerConfigurationsDto.setEnabled(true);
    Mockito.when(apiMgtDAO.getKeyManagerConfigurationByName("abc.org", "default")).thenReturn(keyManagerConfigurationsDto);
    Mockito.when(tenantManager.getTenantId(Mockito.anyString())).thenThrow(UserStoreException.class).thenReturn(-1234, 1);
    APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(apiMgtDAO);
    Application app = new Application("app1", new Subscriber("1"));
    app.setGroupId("2");
    app.setUUID(UUID.randomUUID().toString());
    Mockito.when(userStoreManager.getRoleListOfUser(Mockito.anyString())).thenThrow(UserStoreException.class).thenReturn(new String[] { "role1", "role2" });
    Application application = Mockito.mock(Application.class);
    Subscriber subscriber = Mockito.mock(Subscriber.class);
    Mockito.when(subscriber.getName()).thenReturn("1");
    Mockito.when(application.getSubscriber()).thenReturn(subscriber);
    Mockito.when(ApplicationUtils.retrieveApplication(Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(application);
    try {
        apiConsumer.requestApprovalForApplicationRegistration("1", app, "access", "identity.com/auth", null, "3600", "api_view", null, "default", null, false);
        Assert.fail("API management exception not thrown for invalid token type");
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().contains("Invalid Token Type"));
    }
    scope1.setRoles("role1");
    scope2.setRoles("role2");
    OAuthApplicationInfo oAuthApplicationInfo = new OAuthApplicationInfo();
    OAuthAppRequest oAuthAppRequest = new OAuthAppRequest();
    oAuthAppRequest.setOAuthApplicationInfo(oAuthApplicationInfo);
    application = new Application("app1", new Subscriber("1"));
    BDDMockito.when(ApplicationUtils.createOauthAppRequest(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(oAuthAppRequest);
    BDDMockito.when(ApplicationUtils.retrieveApplication(Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(application);
    Map<String, Object> result = apiConsumer.requestApprovalForApplicationRegistration("1", app, APIConstants.API_KEY_TYPE_PRODUCTION, "identity.com/auth", null, "3600", "api_view", null, "default", null, false);
    Assert.assertEquals(result.size(), 10);
    Assert.assertEquals(result.get("keyState"), "APPROVED");
    result = apiConsumer.requestApprovalForApplicationRegistration("1", app, APIConstants.API_KEY_TYPE_SANDBOX, "", null, "3600", "api_view", null, "default", null, false);
    Assert.assertEquals(result.size(), 10);
    Assert.assertEquals(result.get("keyState"), "APPROVED");
}
Also used : KeyManagerConfigurationDTO(org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO) Matchers.anyString(org.mockito.Matchers.anyString) Scope(org.wso2.carbon.apimgt.api.model.Scope) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) OAuthAppRequest(org.wso2.carbon.apimgt.api.model.OAuthAppRequest) OAuthApplicationInfo(org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo) UserStoreException(org.wso2.carbon.user.api.UserStoreException) JSONObject(org.json.simple.JSONObject) Application(org.wso2.carbon.apimgt.api.model.Application) HashSet(java.util.HashSet) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 99 with APIConsumer

use of org.wso2.carbon.apimgt.api.APIConsumer in project carbon-apimgt by wso2.

the class APIConsumerImplTest method testTokenTypeChangeWhenUpdatingApplications.

@Test
public void testTokenTypeChangeWhenUpdatingApplications() throws APIManagementException {
    Application oldApplication = new Application("app1", new Subscriber("sub1"));
    oldApplication.setTier("tier1");
    oldApplication.setOrganization("testorg");
    Application newApplication = new Application("app1", new Subscriber("sub1"));
    newApplication.setOrganization("testorg");
    newApplication.setTier("tier2");
    Mockito.when(apiMgtDAO.getApplicationById(Mockito.anyInt())).thenReturn(oldApplication);
    Mockito.when(apiMgtDAO.getApplicationByUUID(Mockito.anyString())).thenReturn(oldApplication);
    APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(apiMgtDAO);
    Map<String, Tier> tierMap = new HashMap<>();
    tierMap.put("tier1", new Tier("tier1"));
    tierMap.put("tier2", new Tier("tier2"));
    PowerMockito.when(APIUtil.getTiers(APIConstants.TIER_APPLICATION_TYPE, "testorg")).thenReturn(tierMap);
    PowerMockito.when(APIUtil.findTier(tierMap.values(), "tier2")).thenReturn(new Tier("tier2"));
    // When token type of existing application is 'JWT' and request body contains 'OAUTH' as the token type.
    oldApplication.setTokenType(APIConstants.TOKEN_TYPE_JWT);
    newApplication.setTokenType(APIConstants.TOKEN_TYPE_OAUTH);
    try {
        // An exception will be thrown during this operation.
        apiConsumer.updateApplication(newApplication);
        Assert.fail("API management exception not thrown for error scenario");
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().contains("Cannot change application token type from " + APIConstants.TOKEN_TYPE_JWT + " to " + newApplication.getTokenType()));
    }
    // When token type of existing application is 'JWT' and request body contains 'JWT' as the token type.
    oldApplication.setTokenType(APIConstants.TOKEN_TYPE_JWT);
    newApplication.setTokenType(APIConstants.TOKEN_TYPE_JWT);
    try {
        // Token type of newApplication will not change during this operation.
        apiConsumer.updateApplication(newApplication);
        Assert.assertEquals(APIConstants.TOKEN_TYPE_JWT, newApplication.getTokenType());
    } catch (APIManagementException e) {
        Assert.fail("API management exception is thrown due to an error");
    }
    // When token type of existing application is 'OAUTH' and request body contains 'OAUTH' as the token type.
    oldApplication.setTokenType(APIConstants.TOKEN_TYPE_OAUTH);
    newApplication.setTokenType(APIConstants.TOKEN_TYPE_OAUTH);
    try {
        // Token type of newApplication will not change during this operation.
        apiConsumer.updateApplication(newApplication);
        Assert.assertEquals(APIConstants.TOKEN_TYPE_OAUTH, newApplication.getTokenType());
    } catch (APIManagementException e) {
        Assert.fail("API management exception is thrown due to an error");
    }
    // When token type of existing application is 'OAUTH' and request body contains 'JWT' as the token type.
    oldApplication.setTokenType(APIConstants.TOKEN_TYPE_OAUTH);
    newApplication.setTokenType(APIConstants.TOKEN_TYPE_JWT);
    try {
        // Token type of newApplication will not change during this operation.
        apiConsumer.updateApplication(newApplication);
        Assert.assertEquals(APIConstants.TOKEN_TYPE_JWT, newApplication.getTokenType());
    } catch (APIManagementException e) {
        Assert.fail("API management exception is thrown due to an error");
    }
    // When token type of existing application is 'DEFAULT' and request body contains 'OAUTH' as the token type.
    oldApplication.setTokenType(APIConstants.DEFAULT_TOKEN_TYPE);
    newApplication.setTokenType(APIConstants.TOKEN_TYPE_OAUTH);
    try {
        // Token type of newApplication will change to 'DEFAULT' during this operation.
        apiConsumer.updateApplication(newApplication);
        Assert.assertEquals(APIConstants.DEFAULT_TOKEN_TYPE, newApplication.getTokenType());
    } catch (APIManagementException e) {
        Assert.fail("API management exception is thrown due to an error");
    }
    // When token type of existing application is 'DEFAULT' and request body contains 'JWT' as the token type.
    oldApplication.setTokenType(APIConstants.DEFAULT_TOKEN_TYPE);
    newApplication.setTokenType(APIConstants.TOKEN_TYPE_JWT);
    try {
        // Token type of newApplication will not change during this operation.
        apiConsumer.updateApplication(newApplication);
        Assert.assertEquals(APIConstants.TOKEN_TYPE_JWT, newApplication.getTokenType());
    } catch (APIManagementException e) {
        Assert.fail("API management exception is thrown due to an error");
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) Tier(org.wso2.carbon.apimgt.api.model.Tier) HashMap(java.util.HashMap) Matchers.anyString(org.mockito.Matchers.anyString) Application(org.wso2.carbon.apimgt.api.model.Application) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 100 with APIConsumer

use of org.wso2.carbon.apimgt.api.APIConsumer in project carbon-apimgt by wso2.

the class APIConsumerImplTest method testGetAllPaginatedPublishedAPIs.

@Test
public void testGetAllPaginatedPublishedAPIs() throws Exception {
    APIConsumerImpl apiConsumer = new APIConsumerImplWrapper();
    PowerMockito.doNothing().when(APIUtil.class, "loadTenantRegistry", Mockito.anyInt());
    PowerMockito.when(APIUtil.isAllowDisplayMultipleVersions()).thenReturn(false, true);
    System.setProperty(CARBON_HOME, "");
    GenericArtifactManager artifactManager = Mockito.mock(GenericArtifactManager.class);
    PowerMockito.when(APIUtil.getArtifactManager(apiConsumer.registry, APIConstants.API_KEY)).thenReturn(artifactManager);
    GenericArtifact artifact = Mockito.mock(GenericArtifact.class);
    GenericArtifact[] genericArtifacts = new GenericArtifact[] { artifact };
    Mockito.when(artifactManager.findGenericArtifacts(Mockito.anyMap())).thenReturn(genericArtifacts);
    APIIdentifier apiId1 = new APIIdentifier("admin", "API1", "1.0.0");
    API api = new API(apiId1);
    Mockito.when(APIUtil.getAPI(artifact)).thenReturn(api);
    assertNotNull(apiConsumer.getAllPaginatedPublishedAPIs(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, 0, 10));
    assertNotNull(apiConsumer.getAllPaginatedPublishedAPIs(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, 0, 10));
    // artifact manager null path
    PowerMockito.when(APIUtil.getArtifactManager((UserRegistry) (Mockito.anyObject()), Mockito.anyString())).thenReturn(null);
    assertNotNull(apiConsumer.getAllPaginatedPublishedAPIs(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, 0, 10));
    // generic artifact null path
    PowerMockito.when(APIUtil.getArtifactManager((UserRegistry) (Mockito.anyObject()), Mockito.anyString())).thenReturn(artifactManager);
    Mockito.when(artifactManager.findGenericArtifacts(Mockito.anyMap())).thenReturn(null);
    assertNotNull(apiConsumer.getAllPaginatedPublishedAPIs(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, 0, 10));
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) DevPortalAPI(org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI) API(org.wso2.carbon.apimgt.api.model.API) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)91 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)79 Application (org.wso2.carbon.apimgt.api.model.Application)50 Test (org.junit.Test)46 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)46 HashMap (java.util.HashMap)32 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)29 ArrayList (java.util.ArrayList)28 API (org.wso2.carbon.apimgt.api.model.API)28 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)28 JSONObject (org.json.simple.JSONObject)23 ExportedApplication (org.wso2.carbon.apimgt.rest.api.store.v1.models.ExportedApplication)23 Subscriber (org.wso2.carbon.apimgt.api.model.Subscriber)20 Map (java.util.Map)19 Matchers.anyString (org.mockito.Matchers.anyString)19 ApiTypeWrapper (org.wso2.carbon.apimgt.api.model.ApiTypeWrapper)18 Tier (org.wso2.carbon.apimgt.api.model.Tier)18 DevPortalAPI (org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI)15 URI (java.net.URI)13 URISyntaxException (java.net.URISyntaxException)13