use of org.wso2.carbon.lcm.core.exception.LifecycleException in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testUpdateAPIStatusException.
@Test(description = "Exception when updating api status", expectedExceptions = APIManagementException.class)
public void testUpdateAPIStatusException() throws APIManagementException, LifecycleException {
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
WorkflowDAO workflowDAO = Mockito.mock(WorkflowDAO.class);
APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
APIGateway gateway = Mockito.mock(APIGateway.class);
APIPublisherImpl apiPublisher = getApiPublisherImpl(apiLifecycleManager, apiDAO, workflowDAO, gateway);
API api = SampleTestObjectCreator.createDefaultAPI().build();
String uuid = api.getId();
String lcState = api.getLifeCycleStatus();
String lifecycleId = api.getLifecycleInstanceId();
LifecycleState lifecycleState = SampleTestObjectCreator.getMockLifecycleStateObject(lifecycleId);
Mockito.when(apiLifecycleManager.getLifecycleDataForState(lifecycleId, lcState)).thenReturn(lifecycleState);
Mockito.when(apiDAO.getAPI(uuid)).thenReturn(api);
API.APIBuilder apiBuilder = new API.APIBuilder(api);
apiBuilder.lifecycleState(lifecycleState);
apiBuilder.updatedBy(USER);
api = apiBuilder.build();
Mockito.when(apiLifecycleManager.executeLifecycleEvent(APIStatus.CREATED.getStatus(), APIStatus.PUBLISHED.getStatus(), lifecycleId, USER, api)).thenThrow(new LifecycleException("Couldn't change the status of api ID " + uuid));
apiPublisher.updateAPIStatus(uuid, APIStatus.PUBLISHED.getStatus(), new HashMap<>());
}
use of org.wso2.carbon.lcm.core.exception.LifecycleException in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testAddApiWithEmptyTemplateIdAndApiDefinition.
@Test(description = "Test add api with empty templateId and api definition")
public void testAddApiWithEmptyTemplateIdAndApiDefinition() throws APIManagementException, LifecycleException {
Map<String, UriTemplate> uriTemplateMap = new HashMap();
UriTemplate.UriTemplateBuilder uriTemplateBuilder = new UriTemplate.UriTemplateBuilder();
uriTemplateBuilder.endpoint(Collections.emptyMap());
uriTemplateBuilder.templateId("");
uriTemplateBuilder.uriTemplate("/apis/");
uriTemplateBuilder.authType(APIMgtConstants.AUTH_APPLICATION_LEVEL_TOKEN);
uriTemplateBuilder.policy(APIUtils.getDefaultAPIPolicy());
uriTemplateBuilder.httpVerb("GET");
uriTemplateMap.put("", uriTemplateBuilder.build());
API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI().uriTemplates(uriTemplateMap).apiDefinition("");
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
Mockito.when(apiLifecycleManager.addLifecycle(APIMgtConstants.API_LIFECYCLE, USER)).thenReturn(new LifecycleState());
GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
APIGateway gateway = Mockito.mock(APIGateway.class);
PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
LabelDAO labelDao = Mockito.mock(LabelDAO.class);
Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.api, APIMgtConstants.DEFAULT_API_POLICY)).thenReturn(new APIPolicy(APIMgtConstants.DEFAULT_API_POLICY));
Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, GOLD_TIER)).thenReturn(new SubscriptionPolicy(GOLD_TIER));
Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, SILVER_TIER)).thenReturn(new SubscriptionPolicy(SILVER_TIER));
Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, BRONZE_TIER)).thenReturn(new SubscriptionPolicy(BRONZE_TIER));
APIPublisherImpl apiPublisher = getApiPublisherImpl(null, apiDAO, null, null, policyDAO, apiLifecycleManager, labelDao, null, null, null, gatewaySourceGenerator, gateway);
apiPublisher.addAPI(apiBuilder);
Mockito.verify(apiDAO, Mockito.times(1)).addAPI(apiBuilder.build());
Mockito.verify(apiLifecycleManager, Mockito.times(1)).addLifecycle(APIMgtConstants.API_LIFECYCLE, USER);
}
use of org.wso2.carbon.lcm.core.exception.LifecycleException in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testAddApiSpecificEndpoint.
@Test(description = "Test add api with Api Specific Endpoint", expectedExceptions = { APIManagementException.class })
public void testAddApiSpecificEndpoint() throws APIManagementException, LifecycleException {
/**
* this test method verify the API Add with correct API object get invoked correctly
*/
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
Endpoint globalEndpoint = new Endpoint.Builder().id(UUID.randomUUID().toString()).name("testEndpoint").applicableLevel(APIMgtConstants.GLOBAL_ENDPOINT).build();
Endpoint apiEndpoint = new Endpoint.Builder().id(UUID.randomUUID().toString()).name("apiEndpoint").applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).build();
Map<String, Endpoint> endpointMap = new HashMap();
endpointMap.put(APIMgtConstants.PRODUCTION_ENDPOINT, globalEndpoint);
endpointMap.put(APIMgtConstants.SANDBOX_ENDPOINT, apiEndpoint);
API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI().endpoint(endpointMap);
Mockito.when(apiLifecycleManager.addLifecycle(APIMgtConstants.API_LIFECYCLE, USER)).thenReturn(new LifecycleState());
APIGateway gateway = Mockito.mock(APIGateway.class);
PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
LabelDAO labelDao = Mockito.mock(LabelDAO.class);
Policy apiPolicy = new APIPolicy(UUID.randomUUID().toString(), APIMgtConstants.DEFAULT_API_POLICY);
Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.api, APIMgtConstants.DEFAULT_API_POLICY)).thenReturn(apiPolicy);
APIPublisherImpl apiPublisher = getApiPublisherImpl(null, apiDAO, null, null, policyDAO, apiLifecycleManager, labelDao, null, null, null, gatewaySourceGenerator, gateway);
Mockito.when(apiDAO.getEndpoint(globalEndpoint.getId())).thenReturn(globalEndpoint);
Mockito.when(apiDAO.getEndpointByName(apiEndpoint.getName())).thenReturn(null);
Mockito.when(apiDAO.isAPINameExists(apiBuilder.getName(), USER)).thenReturn(false);
Mockito.when(apiDAO.isEndpointAssociated(globalEndpoint.getId())).thenReturn(true);
apiPublisher.addAPI(apiBuilder);
apiPublisher.deleteEndpoint(globalEndpoint.getId());
Mockito.verify(apiDAO, Mockito.times(1)).addAPI(apiBuilder.build());
Mockito.verify(apiLifecycleManager, Mockito.times(1)).addLifecycle(APIMgtConstants.API_LIFECYCLE, USER);
Mockito.verify(apiDAO, Mockito.times(1)).getEndpointByName(apiEndpoint.getName());
Mockito.verify(apiDAO, Mockito.times(1)).isAPINameExists(apiBuilder.getName(), USER);
}
use of org.wso2.carbon.lcm.core.exception.LifecycleException in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testAddApiDefinitionErrorProtocolException.
@Test(description = "Test protocol exception when adding api definition from swagger resource", expectedExceptions = APIManagementException.class)
public void testAddApiDefinitionErrorProtocolException() throws APIManagementException, LifecycleException, IOException {
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
HttpURLConnection httpURLConnection = Mockito.mock(HttpURLConnection.class);
APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
Mockito.when(apiLifecycleManager.addLifecycle(APIMgtConstants.API_LIFECYCLE, USER)).thenReturn(new LifecycleState());
APIGateway gateway = Mockito.mock(APIGateway.class);
APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO, apiLifecycleManager, gateway);
Mockito.doThrow(new ProtocolException()).when(httpURLConnection).setRequestMethod(APIMgtConstants.HTTP_GET);
apiPublisher.addApiFromDefinition(httpURLConnection);
Mockito.verify(apiLifecycleManager, Mockito.times(0)).addLifecycle(APIMgtConstants.API_LIFECYCLE, USER);
}
use of org.wso2.carbon.lcm.core.exception.LifecycleException in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testAddApiWithEmptyUriTemplate.
@Test(description = "Test add api when uri templates are empty")
public void testAddApiWithEmptyUriTemplate() throws APIManagementException, LifecycleException {
API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI().uriTemplates(new HashMap<>());
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
Mockito.when(apiLifecycleManager.addLifecycle(APIMgtConstants.API_LIFECYCLE, USER)).thenReturn(new LifecycleState());
GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
APIGateway gateway = Mockito.mock(APIGateway.class);
PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
LabelDAO labelDao = Mockito.mock(LabelDAO.class);
Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.api, APIMgtConstants.DEFAULT_API_POLICY)).thenReturn(new APIPolicy(APIMgtConstants.DEFAULT_API_POLICY));
Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, GOLD_TIER)).thenReturn(new SubscriptionPolicy(GOLD_TIER));
Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, SILVER_TIER)).thenReturn(new SubscriptionPolicy(SILVER_TIER));
Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, BRONZE_TIER)).thenReturn(new SubscriptionPolicy(BRONZE_TIER));
APIPublisherImpl apiPublisher = getApiPublisherImpl(null, apiDAO, null, null, policyDAO, apiLifecycleManager, labelDao, null, null, null, gatewaySourceGenerator, gateway);
apiPublisher.addAPI(apiBuilder);
Mockito.verify(apiDAO, Mockito.times(1)).addAPI(apiBuilder.build());
Mockito.verify(apiLifecycleManager, Mockito.times(1)).addLifecycle(APIMgtConstants.API_LIFECYCLE, USER);
}
Aggregations