Search in sources :

Example 21 with APIConsumer

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

the class APIConsumerImplTest method testGetApplicationKeys.

@Test
public void testGetApplicationKeys() throws APIManagementException {
    APIKey apiKey1 = new APIKey();
    apiKey1.setConsumerKey(UUID.randomUUID().toString());
    apiKey1.setType(APIConstants.API_KEY_TYPE_PRODUCTION);
    apiKey1.setState(UUID.randomUUID().toString());
    APIKey apiKey2 = new APIKey();
    apiKey2.setConsumerKey(UUID.randomUUID().toString());
    apiKey2.setType(APIConstants.API_KEY_TYPE_SANDBOX);
    apiKey2.setState(UUID.randomUUID().toString());
    APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(apiMgtDAO);
    Map<String, String> consumerKeyMap = new HashMap<>();
    consumerKeyMap.put("default", apiKey1.getConsumerKey());
    Set<APIKey> apiKeys = new HashSet<>();
    apiKeys.add(apiKey1);
    apiKeys.add(apiKey2);
    Mockito.when(apiMgtDAO.getKeyMappingsFromApplicationId(Mockito.anyInt())).thenReturn(apiKeys);
    Mockito.when(apiMgtDAO.getConsumerkeyByApplicationIdAndKeyType(Mockito.anyInt(), Mockito.anyString())).thenReturn(consumerKeyMap, consumerKeyMap);
    AccessTokenInfo accessTokenInfo = new AccessTokenInfo();
    accessTokenInfo.setAccessToken(UUID.randomUUID().toString());
    Mockito.when(keyManager.getAccessTokenByConsumerKey(Mockito.anyString())).thenReturn(accessTokenInfo);
    Mockito.when(keyManagerConfigurationDTO.isEnabled()).thenReturn(true);
    assertNotNull(apiConsumer.getApplicationKeys(1));
    assertEquals(apiConsumer.getApplicationKeys(1).size(), 2);
    assertNotNull(apiConsumer.getApplicationKeys(1).iterator().next().getAccessToken());
}
Also used : APIKey(org.wso2.carbon.apimgt.api.model.APIKey) AccessTokenInfo(org.wso2.carbon.apimgt.api.model.AccessTokenInfo) HashMap(java.util.HashMap) Matchers.anyString(org.mockito.Matchers.anyString) HashSet(java.util.HashSet) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 22 with APIConsumer

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

the class APIConsumerImplTest method testGetSubscriber.

@Test
public void testGetSubscriber() throws APIManagementException {
    APIConsumerImpl apiConsumer = new APIConsumerImplWrapper();
    when(apiMgtDAO.getSubscriber(Mockito.anyString())).thenReturn(new Subscriber(UUID.randomUUID().toString()));
    apiConsumer.apiMgtDAO = apiMgtDAO;
    assertNotNull(apiConsumer.getSubscriber(UUID.randomUUID().toString()));
    when(apiMgtDAO.getSubscriber(Mockito.anyString())).thenThrow(APIManagementException.class);
    try {
        apiConsumer.getSubscriber(UUID.randomUUID().toString());
        assertTrue(false);
    } catch (APIManagementException e) {
        assertEquals("Failed to get Subscriber", e.getMessage());
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 23 with APIConsumer

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

the class APIConsumerImplTest method testResumeWorkflow.

@Test
public void testResumeWorkflow() throws Exception {
    APIConsumerImpl apiConsumer = new APIConsumerImplWrapper();
    apiConsumer.apiMgtDAO = apiMgtDAO;
    WorkflowDTO workflowDTO = new WorkflowDTO();
    workflowDTO.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
    when(apiMgtDAO.retrieveWorkflow(Mockito.anyString())).thenReturn(workflowDTO);
    // Null input case
    assertNotNull(apiConsumer.resumeWorkflow(null));
    String[] args = { UUID.randomUUID().toString(), WorkflowStatus.CREATED.toString(), UUID.randomUUID().toString() };
    assertNotNull(apiConsumer.resumeWorkflow(args));
    Mockito.reset(apiMgtDAO);
    workflowDTO.setTenantDomain("wso2.com");
    when(apiMgtDAO.retrieveWorkflow(Mockito.anyString())).thenReturn(workflowDTO);
    JSONObject row = apiConsumer.resumeWorkflow(args);
    assertNotNull(row);
    Mockito.reset(apiMgtDAO);
    when(apiMgtDAO.retrieveWorkflow(Mockito.anyString())).thenThrow(APIManagementException.class);
    when(APIUtil.isStringArray(args)).thenReturn(true);
    row = apiConsumer.resumeWorkflow(args);
    assertEquals("Error while resuming the workflow. null", row.get("message"));
    // Workflow DAO null case
    Mockito.reset(apiMgtDAO);
    when(apiMgtDAO.retrieveWorkflow(Mockito.anyString())).thenReturn(null);
    row = apiConsumer.resumeWorkflow(args);
    assertNotNull(row);
    assertEquals(true, row.get("error"));
    assertEquals(500, row.get("statusCode"));
    // Invalid status test
    args[1] = "Invalid status";
    Mockito.reset(apiMgtDAO);
    when(apiMgtDAO.retrieveWorkflow(Mockito.anyString())).thenReturn(workflowDTO);
    row = apiConsumer.resumeWorkflow(args);
    assertEquals("Illegal argument provided. Valid values for status are APPROVED and REJECTED.", row.get("message"));
}
Also used : WorkflowDTO(org.wso2.carbon.apimgt.impl.dto.WorkflowDTO) SubscriptionWorkflowDTO(org.wso2.carbon.apimgt.impl.dto.SubscriptionWorkflowDTO) JSONObject(org.json.simple.JSONObject) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 24 with APIConsumer

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

the class APIConsumerImplTest method testGetApplicationsWithPagination.

@Test
public void testGetApplicationsWithPagination() throws APIManagementException {
    Application[] applications = new Application[] { new Application(1), new Application(2) };
    Mockito.when(apiMgtDAO.getApplicationsWithPagination((Subscriber) Mockito.any(), Mockito.anyString(), Mockito.anyInt(), Mockito.anyInt(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(applications);
    APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(apiMgtDAO);
    Assert.assertEquals(apiConsumer.getApplicationsWithPagination(new Subscriber("sub1"), "1", 0, 5, "", "", "ASC", "testorg").length, 2);
}
Also used : Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) Application(org.wso2.carbon.apimgt.api.model.Application) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 25 with APIConsumer

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

the class APIConsumerImplTest method testAddSubscriptionInvalidTier.

@Test
public void testAddSubscriptionInvalidTier() throws APIManagementException {
    API api = new API(new APIIdentifier(API_PROVIDER, "published_api", SAMPLE_API_VERSION));
    api.setSubscriptionAvailability(APIConstants.SUBSCRIPTION_TO_ALL_TENANTS);
    Application application = new Application(1);
    api.setStatus(APIConstants.PUBLISHED);
    Set<Tier> tiers = new HashSet<>();
    tiers.add(new Tier("tier1"));
    api.setAvailableTiers(tiers);
    ApiTypeWrapper apiTypeWrapper = new ApiTypeWrapper(api);
    apiTypeWrapper.setTier("tier2");
    Mockito.when(apiMgtDAO.addSubscription(Mockito.eq(apiTypeWrapper), Mockito.eq(application), Mockito.anyString(), Mockito.anyString())).thenReturn(1);
    SubscribedAPI subscribedAPI = new SubscribedAPI(UUID.randomUUID().toString());
    Mockito.when(apiMgtDAO.getSubscriptionById(1)).thenReturn(subscribedAPI);
    APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(apiMgtDAO, SAMPLE_TENANT_DOMAIN_1);
    try {
        apiConsumer.addSubscription(apiTypeWrapper, "sub1", application);
        Assert.fail("Invalid Tier error not thrown.");
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getErrorHandler().getErrorCode(), ExceptionCodes.SUBSCRIPTION_TIER_NOT_ALLOWED.getErrorCode());
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Tier(org.wso2.carbon.apimgt.api.model.Tier) ApiTypeWrapper(org.wso2.carbon.apimgt.api.model.ApiTypeWrapper) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) 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) Application(org.wso2.carbon.apimgt.api.model.Application) HashSet(java.util.HashSet) 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