use of org.wso2.carbon.apimgt.core.exception.GatewayException in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testUpdateAPIException.
@Test(description = "Exception when updating API")
public void testUpdateAPIException() throws APIManagementException {
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
APIGateway gateway = Mockito.mock(APIGateway.class);
IdentityProvider identityProvider = Mockito.mock(IdentityProvider.class);
PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
Policy apiPolicy = new APIPolicy(APIMgtConstants.DEFAULT_API_POLICY);
apiPolicy.setUuid(UUID.randomUUID().toString());
Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.api, APIMgtConstants.DEFAULT_API_POLICY)).thenReturn(apiPolicy);
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(identityProvider, apiDAO, apiLifecycleManager, gatewaySourceGenerator, gateway, policyDAO);
API.APIBuilder api = SampleTestObjectCreator.createDefaultAPI();
String uuid = api.getId();
Mockito.when(apiDAO.getAPI(uuid)).thenReturn(api.build());
Mockito.when(identityProvider.getRoleId(ADMIN_ROLE)).thenReturn(ADMIN_ROLE_ID);
Mockito.when(identityProvider.getRoleId(DEVELOPER_ROLE)).thenReturn(DEVELOPER_ROLE_ID);
// APIMgtDAOException
Mockito.doThrow(new APIMgtDAOException("Error occurred while updating the API - " + api.getName())).when(apiDAO).updateAPI(uuid, api.build());
Mockito.when(apiDAO.getApiSwaggerDefinition(api.getId())).thenReturn(SampleTestObjectCreator.apiDefinition);
try {
apiPublisher.updateAPI(api);
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Error occurred while updating the API - " + api.getName());
}
// ParseException
try {
apiPublisher.updateAPI(api.apiPermission("data{{]"));
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Error occurred while parsing the permission json from swagger - " + api.getName());
}
// GatewayException
Mockito.doThrow(GatewayException.class).when(gateway).updateAPI(api.apiPermission("").build());
try {
apiPublisher.updateAPI(api.apiPermission(""));
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Error occurred while updating API - " + api.getName() + " in gateway");
}
// Error path
// When Parse Exception is thrown during getAPIByUUID - replacing group ids with names
Mockito.when(apiDAO.getAPI(uuid)).thenReturn(api.apiPermission("data{{]").build());
try {
apiPublisher.updateAPI(api.apiPermission("data{{]"));
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Error occurred while parsing the permission json string for API " + api.getName());
}
}
use of org.wso2.carbon.apimgt.core.exception.GatewayException in project carbon-apimgt by wso2.
the class APIGatewayPublisherImplTestCase method testDeleteAPIForException.
@Test(expected = GatewayException.class)
public void testDeleteAPIForException() throws GatewayException, ContainerBasedGatewayException {
APIGatewayPublisherImpl apiGatewayPublisher = new APIGatewayPublisherImpl();
Broker broker = Mockito.mock(BrokerImpl.class, Mockito.RETURNS_DEEP_STUBS);
BrokerUtil.initialize(broker);
ContainerBasedGatewayGenerator containerBasedGatewayGenerator = Mockito.mock(ContainerBasedGatewayGenerator.class);
apiGatewayPublisher.setContainerBasedGatewayGenerator(containerBasedGatewayGenerator);
Mockito.doThrow(ContainerBasedGatewayException.class).when(containerBasedGatewayGenerator).removeContainerBasedGateway(Mockito.any(), Mockito.any());
List<String> labels = new ArrayList<>();
labels.add("label");
API api = SampleTestObjectCreator.createDefaultAPI().lifeCycleStatus(APIStatus.PUBLISHED.getStatus()).hasOwnGateway(true).labels(labels).build();
apiGatewayPublisher.deleteAPI(api);
}
use of org.wso2.carbon.apimgt.core.exception.GatewayException in project carbon-apimgt by wso2.
the class APIGatewayPublisherImplTestCase method testDeleteAPIWhenLabelAreEmpty.
@Test
public void testDeleteAPIWhenLabelAreEmpty() throws GatewayException, ContainerBasedGatewayException {
APIGatewayPublisherImpl apiGatewayPublisher = new APIGatewayPublisherImpl();
Broker broker = Mockito.mock(BrokerImpl.class, Mockito.RETURNS_DEEP_STUBS);
BrokerUtil.initialize(broker);
ContainerBasedGatewayGenerator containerBasedGatewayGenerator = Mockito.mock(ContainerBasedGatewayGenerator.class);
apiGatewayPublisher.setContainerBasedGatewayGenerator(containerBasedGatewayGenerator);
List<String> labels = new ArrayList<>();
API api = SampleTestObjectCreator.createDefaultAPI().lifeCycleStatus(APIStatus.PUBLISHED.getStatus()).hasOwnGateway(true).labels(labels).build();
apiGatewayPublisher.deleteAPI(api);
Mockito.verify(containerBasedGatewayGenerator, Mockito.times(0)).removeContainerBasedGateway("label", api);
}
use of org.wso2.carbon.apimgt.core.exception.GatewayException in project carbon-apimgt by wso2.
the class APIGatewayPublisherImplTestCase method testDeleteAPIWhenAPIHasOwnGateway.
@Test
public void testDeleteAPIWhenAPIHasOwnGateway() throws GatewayException, ContainerBasedGatewayException {
APIGatewayPublisherImpl apiGatewayPublisher = new APIGatewayPublisherImpl();
Broker broker = Mockito.mock(BrokerImpl.class, Mockito.RETURNS_DEEP_STUBS);
BrokerUtil.initialize(broker);
ContainerBasedGatewayGenerator containerBasedGatewayGenerator = Mockito.mock(ContainerBasedGatewayGenerator.class);
apiGatewayPublisher.setContainerBasedGatewayGenerator(containerBasedGatewayGenerator);
List<String> labels = new ArrayList<>();
labels.add("label");
API api = SampleTestObjectCreator.createDefaultAPI().lifeCycleStatus(APIStatus.PUBLISHED.getStatus()).hasOwnGateway(true).labels(labels).build();
apiGatewayPublisher.deleteAPI(api);
Mockito.verify(containerBasedGatewayGenerator, Mockito.times(1)).removeContainerBasedGateway("label", api);
}
use of org.wso2.carbon.apimgt.core.exception.GatewayException in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImpl method subscriptionsSubscriptionIdDelete.
/**
* Delete a subscription
*
* @param subscriptionId Id of the subscription
* @param ifMatch If-Match header value
* @param ifUnmodifiedSince If-Unmodified-Since header value
* @param request msf4j request object
* @return 200 OK response if the deletion was successful
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response subscriptionsSubscriptionIdDelete(String subscriptionId, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
try {
APIStore apiStore = RestApiUtil.getConsumer(username);
String existingFingerprint = subscriptionsSubscriptionIdGetFingerprint(subscriptionId, null, null, request);
if (!StringUtils.isEmpty(ifMatch) && !StringUtils.isEmpty(existingFingerprint) && !ifMatch.contains(existingFingerprint)) {
return Response.status(Response.Status.PRECONDITION_FAILED).build();
}
apiStore.deleteAPISubscription(subscriptionId);
} catch (GatewayException e) {
String errorMessage = "Failed to remove subscription :" + subscriptionId + " from gateway";
log.error(errorMessage, e);
return Response.status(Response.Status.ACCEPTED).build();
} catch (APIManagementException e) {
String errorMessage = "Error while deleting subscription";
HashMap<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.SUBSCRIPTION_ID, subscriptionId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
return Response.ok().build();
}
Aggregations