use of io.gravitee.rest.api.model.NewSubscriptionEntity in project gravitee-management-rest-api by gravitee-io.
the class ApplicationSubscriptionsResource method createSubscriptionWithApplication.
@POST
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Subscribe to a plan", notes = "User must have the MANAGE_SUBSCRIPTIONS permission to use this service")
@ApiResponses({ @ApiResponse(code = 201, message = "Subscription successfully created", response = Subscription.class), @ApiResponse(code = 500, message = "Internal server error") })
@Permissions({ @Permission(value = RolePermission.APPLICATION_SUBSCRIPTION, acls = RolePermissionAction.CREATE) })
public Response createSubscriptionWithApplication(@ApiParam(name = "plan", required = true) @NotNull @QueryParam("plan") String plan, NewSubscriptionEntity newSubscriptionEntity) {
// If no request message has been passed, the entity is not created
if (newSubscriptionEntity == null) {
newSubscriptionEntity = new NewSubscriptionEntity();
}
PlanEntity planEntity = planService.findById(plan);
if (planEntity.isCommentRequired() && (newSubscriptionEntity.getRequest() == null || newSubscriptionEntity.getRequest().isEmpty())) {
return Response.status(Response.Status.BAD_REQUEST).entity("Plan requires a consumer comment when subscribing").build();
}
newSubscriptionEntity.setApplication(application);
newSubscriptionEntity.setPlan(plan);
Subscription subscription = convert(subscriptionService.create(newSubscriptionEntity));
return Response.created(this.getRequestUriBuilder().path(subscription.getId()).replaceQueryParam("plan", null).build()).entity(subscription).build();
}
use of io.gravitee.rest.api.model.NewSubscriptionEntity in project gravitee-management-rest-api by gravitee-io.
the class SubscriptionsResourceTest method shouldCreateSubscription.
@Test
public void shouldCreateSubscription() {
SubscriptionInput subscriptionInput = new SubscriptionInput().application(APPLICATION).plan(PLAN).request("request");
final ApiKeyEntity apiKeyEntity = new ApiKeyEntity();
final Key key = new Key();
when(apiKeyService.findBySubscription(SUBSCRIPTION)).thenReturn(Collections.singletonList(apiKeyEntity));
when(keyMapper.convert(apiKeyEntity)).thenReturn(key);
final Response response = target().request().post(Entity.json(subscriptionInput));
assertEquals(OK_200, response.getStatus());
ArgumentCaptor<NewSubscriptionEntity> argument = ArgumentCaptor.forClass(NewSubscriptionEntity.class);
Mockito.verify(subscriptionService).create(argument.capture());
assertEquals(APPLICATION, argument.getValue().getApplication());
assertEquals(PLAN, argument.getValue().getPlan());
assertEquals("request", argument.getValue().getRequest());
final Subscription subscriptionResponse = response.readEntity(Subscription.class);
assertNotNull(subscriptionResponse);
assertEquals(SUBSCRIPTION, subscriptionResponse.getId());
assertNotNull(subscriptionResponse.getKeys());
assertEquals(1, subscriptionResponse.getKeys().size());
assertEquals(key, subscriptionResponse.getKeys().get(0));
}
Aggregations