use of org.wso2.carbon.apimgt.core.exception.APIManagementException in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testDeleteApiWithZeroSubscriptionsLifeCycleException.
@Test(description = "Error occurred while disassociating the API with Lifecycle when deleting the API", expectedExceptions = APIManagementException.class)
public void testDeleteApiWithZeroSubscriptionsLifeCycleException() throws APIManagementException, LifecycleException, SQLException {
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
APISubscriptionDAO apiSubscriptionDAO = Mockito.mock(APISubscriptionDAO.class);
IdentityProvider identityProvider = Mockito.mock(IdentityProvider.class);
API api = SampleTestObjectCreator.createDefaultAPI().build();
String uuid = api.getId();
String lifecycleId = api.getLifecycleInstanceId();
Mockito.when(apiSubscriptionDAO.getSubscriptionCountByAPI(uuid)).thenReturn(0L);
APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
APIPublisherImpl apiPublisher = getApiPublisherImpl(identityProvider, apiDAO, apiSubscriptionDAO, apiLifecycleManager);
Mockito.when(apiDAO.getAPI(uuid)).thenReturn(api);
Mockito.when(identityProvider.getRoleName(SampleTestObjectCreator.DEVELOPER_ROLE_ID)).thenReturn(DEVELOPER_ROLE);
Mockito.when(apiDAO.getApiSwaggerDefinition(api.getId())).thenReturn(SampleTestObjectCreator.apiDefinition);
Mockito.when(identityProvider.getRoleName(SampleTestObjectCreator.ADMIN_ROLE_ID)).thenReturn(ADMIN_ROLE);
Mockito.doThrow(new LifecycleException("Error occurred while Disassociating the API with Lifecycle id " + uuid)).when(apiLifecycleManager).removeLifecycle(lifecycleId);
apiPublisher.deleteAPI(uuid);
}
use of org.wso2.carbon.apimgt.core.exception.APIManagementException in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testCheckIfAPIExistWhileGettingJDBCConnectionFailure.
@Test(description = "Check if api exist with invalid uuid", expectedExceptions = { APIMgtDAOException.class, APIManagementException.class })
public void testCheckIfAPIExistWhileGettingJDBCConnectionFailure() throws APIManagementException {
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO);
Mockito.when(apiDAO.isAPIExists("bbbbb")).thenThrow(new APIMgtDAOException(""));
apiPublisher.isAPIExists("bbbbb");
}
use of org.wso2.carbon.apimgt.core.exception.APIManagementException in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testUpdateApiEndpointOfUriTemplate.
@Test(description = "Test add api with production endpoint")
public void testUpdateApiEndpointOfUriTemplate() throws APIManagementException {
/**
* this test method verify the API Add with correct API object get invoked correctly
*/
Endpoint endpoint1 = new Endpoint.Builder().id(UUID.randomUUID().toString()).endpointConfig("http://localhost").name("endpoint1").applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).build();
Endpoint endpoint2 = new Endpoint.Builder().id(UUID.randomUUID().toString()).endpointConfig("http://localhost").name("endpoint2").applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).build();
Map<String, Endpoint> endpointMap = new HashMap<>();
endpointMap.put(APIMgtConstants.PRODUCTION_ENDPOINT, endpoint1);
endpointMap.put(APIMgtConstants.SANDBOX_ENDPOINT, endpoint2);
Map<String, UriTemplate> uriTemplateMap = SampleTestObjectCreator.getMockUriTemplates();
uriTemplateMap.forEach((s, uriTemplate) -> uriTemplateMap.replace(s, new UriTemplate.UriTemplateBuilder(uriTemplate).endpoint(endpointMap).build()));
API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI().id(UUID.randomUUID().toString()).endpoint(Collections.emptyMap()).uriTemplates(uriTemplateMap);
apiBuilder.apiPermission("");
apiBuilder.permissionMap(null);
apiBuilder.policies(Collections.emptySet());
apiBuilder.apiPolicy(null);
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
Mockito.when(apiDAO.getAPI(apiBuilder.getId())).thenReturn(apiBuilder.build());
GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.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));
APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO, apiLifecycleManager, gatewaySourceGenerator, gateway, policyDAO, labelDao);
Mockito.when(apiDAO.getEndpoint(endpoint1.getId())).thenReturn(endpoint1);
Mockito.when(apiDAO.getEndpoint(endpoint2.getId())).thenReturn(endpoint2);
Mockito.when(apiDAO.getApiSwaggerDefinition(apiBuilder.getId())).thenReturn(SampleTestObjectCreator.apiDefinition);
Mockito.when(apiDAO.getEndpoint(endpoint1.getName())).thenReturn(endpoint1);
Mockito.when(apiDAO.getEndpointByName(endpoint2.getName())).thenReturn(endpoint2);
apiPublisher.updateAPI(apiBuilder);
Mockito.verify(apiDAO, Mockito.times(1)).updateAPI(apiBuilder.getId(), apiBuilder.build());
}
use of org.wso2.carbon.apimgt.core.exception.APIManagementException in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testAddUpdateAPIFromWSDLArchive.
@Test(description = "Test adding an API from WSDL archive")
public void testAddUpdateAPIFromWSDLArchive() throws APIManagementException, LifecycleException, IOException {
API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI().endpoint(SampleTestObjectCreator.getMockEndpointMap());
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
APIGateway gateway = Mockito.mock(APIGateway.class);
APILifecycleManager apiLifecycleManager = getDefaultMockedAPILifecycleManager();
PolicyDAO policyDAO = getDefaultMockedPolicyDAO();
LabelDAO labelDao = Mockito.mock(LabelDAO.class);
APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO, apiLifecycleManager, gatewaySourceGenerator, gateway, policyDAO, labelDao);
String endpointId = apiBuilder.getEndpoint().get("production").getId();
Endpoint endpoint = new Endpoint.Builder().id(endpointId).name("testEndpoint").build();
Mockito.when(apiDAO.getEndpoint(endpointId)).thenReturn(endpoint);
apiPublisher.addAPIFromWSDLArchive(apiBuilder, SampleTestObjectCreator.createDefaultWSDL11ArchiveInputStream(), true);
Mockito.verify(apiDAO, Mockito.times(1)).addAPI(apiBuilder.build());
Mockito.verify(apiDAO, Mockito.times(1)).addOrUpdateWSDLArchive(Mockito.eq(apiBuilder.getId()), Mockito.anyObject(), Mockito.eq(USER));
apiPublisher.updateAPIWSDLArchive(apiBuilder.getId(), SampleTestObjectCreator.createAlternativeWSDL11ArchiveInputStream());
Mockito.verify(apiDAO, Mockito.times(2)).addOrUpdateWSDLArchive(Mockito.eq(apiBuilder.getId()), Mockito.anyObject(), Mockito.eq(USER));
}
use of org.wso2.carbon.apimgt.core.exception.APIManagementException in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testGetPolicyByName.
@Test(description = "Get all policy by name")
public void testGetPolicyByName() throws APIManagementException {
PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
Policy policy = Mockito.mock(Policy.class);
policy.setPolicyName(POLICY_NAME);
APIPublisherImpl apiPublisher = getApiPublisherImpl(policyDAO);
Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.application, POLICY_NAME)).thenReturn(policy);
apiPublisher.getPolicyByName(APIMgtAdminService.PolicyLevel.application, POLICY_NAME);
Mockito.verify(policyDAO, Mockito.times(1)).getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.application, POLICY_NAME);
// Error path
Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.application, POLICY_NAME)).thenThrow(APIMgtDAOException.class);
try {
apiPublisher.getPolicyByName(APIMgtAdminService.PolicyLevel.application, POLICY_NAME);
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Error while retrieving Policy for level: " + APIMgtAdminService.PolicyLevel.application + ", name: " + POLICY_NAME);
}
}
Aggregations