use of org.wso2.carbon.apimgt.api.model.webhooks.Subscription in project carbon-apimgt by wso2.
the class PoliciesApiServiceImpl method policiesThrottlingSubscriptionPost.
/**
* Add subscription level policy
*
* @param body DTO object including the Policy meta information
* @param request msf4j request object
* @return Response object
* @throws NotFoundException if an error occurred when particular resource does not exits in the system.
*/
@Override
public Response policiesThrottlingSubscriptionPost(SubscriptionThrottlePolicyDTO body, Request request) throws NotFoundException {
APIMgtAdminService.PolicyLevel tierLevel = APIMgtAdminService.PolicyLevel.subscription;
if (log.isDebugEnabled()) {
log.info("Received Subscription Policy POST request " + body + " with tierLevel = " + tierLevel);
}
try {
APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
SubscriptionPolicy subscriptionPolicy = SubscriptionThrottlePolicyMappingUtil.fromSubscriptionThrottlePolicyDTOToModel(body);
String policyId = apiMgtAdminService.addSubscriptionPolicy(subscriptionPolicy);
return Response.status(Response.Status.CREATED).entity(SubscriptionThrottlePolicyMappingUtil.fromSubscriptionThrottlePolicyToDTO(apiMgtAdminService.getSubscriptionPolicyByUuid(policyId))).build();
} catch (APIManagementException e) {
String errorMessage = "Error occurred while adding Subscription Policy. policy name: " + body.getPolicyName();
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.carbon.apimgt.api.model.webhooks.Subscription in project carbon-apimgt by wso2.
the class TestMappingUtilTestCase method testSubscriptionListToSubscriptionListDTOMapping.
@Test(description = "Subscription list to Subscription DTO list mapping")
void testSubscriptionListToSubscriptionListDTOMapping() {
Policy subscriptionPolicy1 = SampleTestObjectCreator.goldSubscriptionPolicy;
API api1 = SampleTestObjectCreator.createDefaultAPI().name("newName1").build();
Application application1 = SampleTestObjectCreator.createDefaultApplication();
application1.setName("newNameApp1");
String uuid1 = UUID.randomUUID().toString();
Subscription subscription1 = new Subscription(uuid1, application1, api1, subscriptionPolicy1);
subscription1.setStatus(APIMgtConstants.SubscriptionStatus.ACTIVE);
Policy subscriptionPolicy2 = SampleTestObjectCreator.silverSubscriptionPolicy;
API api2 = SampleTestObjectCreator.createDefaultAPI().name("newName2").build();
Application application2 = SampleTestObjectCreator.createDefaultApplication();
application1.setName("newNameApp2");
String uuid2 = UUID.randomUUID().toString();
Subscription subscription2 = new Subscription(uuid2, application2, api2, subscriptionPolicy2);
subscription2.setStatus(APIMgtConstants.SubscriptionStatus.ACTIVE);
List<Subscription> subscriptions = new ArrayList<>();
subscriptions.add(subscription1);
subscriptions.add(subscription2);
SubscriptionListDTO subscriptionListDTO = MappingUtil.fromSubscriptionListToDTO(subscriptions, 10, 0);
assertEquals((Integer) subscriptions.size(), subscriptionListDTO.getCount());
assertEquals(subscription1.getId(), subscriptionListDTO.getList().get(0).getSubscriptionId());
assertEquals(subscription1.getStatus().name(), subscriptionListDTO.getList().get(0).getSubscriptionStatus().name());
assertEquals(subscription1.getApplication().getId(), subscriptionListDTO.getList().get(0).getApplicationInfo().getApplicationId());
assertEquals(subscription1.getPolicy().getPolicyName(), subscriptionListDTO.getList().get(0).getPolicy());
assertEquals(subscription2.getId(), subscriptionListDTO.getList().get(1).getSubscriptionId());
assertEquals(subscription2.getStatus().name(), subscriptionListDTO.getList().get(1).getSubscriptionStatus().name());
assertEquals(subscription2.getApplication().getId(), subscriptionListDTO.getList().get(1).getApplicationInfo().getApplicationId());
assertEquals(subscription2.getPolicy().getPolicyName(), subscriptionListDTO.getList().get(1).getPolicy());
}
use of org.wso2.carbon.apimgt.api.model.webhooks.Subscription in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImplTestCase method testSubscriptionsUnblockSubscriptionPostException.
@Test
public void testSubscriptionsUnblockSubscriptionPostException() throws Exception {
printTestMethodName();
SubscriptionsApiServiceImpl subscriptionsApiService = new SubscriptionsApiServiceImpl();
APIPublisher apiPublisher = Mockito.mock(APIPublisherImpl.class);
List<Subscription> subscriptions = new ArrayList<>();
String sub1 = UUID.randomUUID().toString();
String sub2 = UUID.randomUUID().toString();
subscriptions.add(SampleTestObjectCreator.createSubscription(sub1));
subscriptions.add(SampleTestObjectCreator.createSubscription(sub2));
PowerMockito.mockStatic(RestAPIPublisherUtil.class);
PowerMockito.when(RestAPIPublisherUtil.getApiPublisher(USER)).thenReturn(apiPublisher);
String apiId = UUID.randomUUID().toString();
Mockito.doThrow(new APIManagementException("Error occurred", ExceptionCodes.SUBSCRIPTION_STATE_INVALID)).when(apiPublisher).getSubscriptionsByAPI(apiId);
Response response = subscriptionsApiService.subscriptionsGet(apiId, 10, 0, null, getRequest());
assertEquals(response.getStatus(), 400);
assertTrue(response.getEntity().toString().contains("Invalid state change for subscription"));
}
use of org.wso2.carbon.apimgt.api.model.webhooks.Subscription in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImplTestCase method testSubscriptionsBlockSubscriptionPostNotExist.
@Test
public void testSubscriptionsBlockSubscriptionPostNotExist() throws Exception {
printTestMethodName();
SubscriptionsApiServiceImpl subscriptionsApiService = new SubscriptionsApiServiceImpl();
APIPublisher apiPublisher = Mockito.mock(APIPublisherImpl.class);
PowerMockito.mockStatic(RestAPIPublisherUtil.class);
PowerMockito.when(RestAPIPublisherUtil.getApiPublisher(USER)).thenReturn(apiPublisher);
String subscriptionId = UUID.randomUUID().toString();
Mockito.doReturn(null).doThrow(new IllegalArgumentException()).when(apiPublisher).getSubscriptionByUUID(subscriptionId);
Response response = subscriptionsApiService.subscriptionsBlockSubscriptionPost(subscriptionId, null, null, null, getRequest());
assertEquals(response.getStatus(), 404);
assertTrue(response.getEntity().toString().contains("Subscription not found"));
}
use of org.wso2.carbon.apimgt.api.model.webhooks.Subscription in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImplTestCase method testSubscriptionsBlockSubscriptionPostException.
@Test
public void testSubscriptionsBlockSubscriptionPostException() throws Exception {
printTestMethodName();
SubscriptionsApiServiceImpl subscriptionsApiService = new SubscriptionsApiServiceImpl();
APIPublisher apiPublisher = Mockito.mock(APIPublisherImpl.class);
PowerMockito.mockStatic(RestAPIPublisherUtil.class);
PowerMockito.when(RestAPIPublisherUtil.getApiPublisher(USER)).thenReturn(apiPublisher);
String subscriptionId = UUID.randomUUID().toString();
Subscription subscription = SampleTestObjectCreator.createSubscription(subscriptionId);
subscription.setStatus(APIMgtConstants.SubscriptionStatus.REJECTED);
Mockito.doThrow(new APIManagementException("Error occurred", ExceptionCodes.SUBSCRIPTION_STATE_INVALID)).when(apiPublisher).getSubscriptionByUUID(subscriptionId);
Response response = subscriptionsApiService.subscriptionsBlockSubscriptionPost(subscriptionId, APIMgtConstants.SubscriptionStatus.BLOCKED.name(), null, null, getRequest());
assertEquals(response.getStatus(), 400);
assertTrue(response.getEntity().toString().contains("Invalid state change for subscription"));
}
Aggregations