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());
}
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());
}
}
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"));
}
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);
}
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());
}
}
Aggregations