Search in sources :

Example 1 with NewSubscriptionEntity

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();
}
Also used : NewSubscriptionEntity(io.gravitee.rest.api.model.NewSubscriptionEntity) PlanEntity(io.gravitee.rest.api.model.PlanEntity) Subscription(io.gravitee.rest.api.management.rest.model.Subscription) Permissions(io.gravitee.rest.api.management.rest.security.Permissions)

Example 2 with NewSubscriptionEntity

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));
}
Also used : Response(javax.ws.rs.core.Response) NewSubscriptionEntity(io.gravitee.rest.api.model.NewSubscriptionEntity) ApiKeyEntity(io.gravitee.rest.api.model.ApiKeyEntity) Test(org.junit.Test)

Aggregations

NewSubscriptionEntity (io.gravitee.rest.api.model.NewSubscriptionEntity)2 Subscription (io.gravitee.rest.api.management.rest.model.Subscription)1 Permissions (io.gravitee.rest.api.management.rest.security.Permissions)1 ApiKeyEntity (io.gravitee.rest.api.model.ApiKeyEntity)1 PlanEntity (io.gravitee.rest.api.model.PlanEntity)1 Response (javax.ws.rs.core.Response)1 Test (org.junit.Test)1