Search in sources :

Example 1 with Key

use of io.gravitee.rest.api.portal.rest.model.Key in project gravitee-management-rest-api by gravitee-io.

the class KeyMapperTest method testConvert.

@Test
public void testConvert() {
    // Test
    Key key = keyMapper.convert(apiKeyEntity);
    assertNotNull(key);
    assertEquals(API, key.getApi());
    assertEquals(APPLICATION, key.getApplication());
    assertEquals(now.toEpochMilli(), key.getCreatedAt().toInstant().toEpochMilli());
    assertEquals(KEY, key.getId());
    assertEquals(Boolean.FALSE, key.getPaused());
    assertEquals(PLAN, key.getPlan());
    assertEquals(Boolean.FALSE, key.getRevoked());
    assertNull(key.getRevokedAt());
}
Also used : Key(io.gravitee.rest.api.portal.rest.model.Key) Test(org.junit.Test)

Example 2 with Key

use of io.gravitee.rest.api.portal.rest.model.Key in project gravitee-management-rest-api by gravitee-io.

the class KeyMapperTest method testConvertWithoutPlan.

@Test
public void testConvertWithoutPlan() {
    apiKeyEntity.setPlan(UNKNOWN_PLAN);
    // Test
    Key key = keyMapper.convert(apiKeyEntity);
    assertNotNull(key);
    assertNull(key.getApi());
    assertEquals(APPLICATION, key.getApplication());
    assertEquals(now.toEpochMilli(), key.getCreatedAt().toInstant().toEpochMilli());
    assertEquals(KEY, key.getId());
    assertEquals(Boolean.FALSE, key.getPaused());
    assertEquals(UNKNOWN_PLAN, key.getPlan());
    assertEquals(Boolean.FALSE, key.getRevoked());
    assertNull(key.getRevokedAt());
}
Also used : Key(io.gravitee.rest.api.portal.rest.model.Key) Test(org.junit.Test)

Example 3 with Key

use of io.gravitee.rest.api.portal.rest.model.Key in project gravitee-management-rest-api by gravitee-io.

the class KeyMapper method convert.

public Key convert(ApiKeyEntity apiKeyEntity) {
    final Key keyItem = new Key();
    final String plan = apiKeyEntity.getPlan();
    try {
        PlanEntity planEntity = planService.findById(plan);
        keyItem.setApi(planEntity.getApi());
    } catch (PlanNotFoundException e) {
        LOGGER.warn("plan does not exist : {}", plan);
    }
    keyItem.setApplication(apiKeyEntity.getApplication());
    keyItem.setCreatedAt(apiKeyEntity.getCreatedAt().toInstant().atOffset(ZoneOffset.UTC));
    keyItem.setId(apiKeyEntity.getKey());
    keyItem.setPaused(apiKeyEntity.isPaused());
    keyItem.setPlan(plan);
    keyItem.setRevoked(apiKeyEntity.isRevoked());
    if (apiKeyEntity.isRevoked()) {
        keyItem.setRevokedAt(apiKeyEntity.getRevokedAt().toInstant().atOffset(ZoneOffset.UTC));
    }
    keyItem.setExpired(apiKeyEntity.isExpired());
    if (apiKeyEntity.getExpireAt() != null) {
        keyItem.setExpireAt(apiKeyEntity.getExpireAt().toInstant().atOffset(ZoneOffset.UTC));
    }
    return keyItem;
}
Also used : PlanEntity(io.gravitee.rest.api.model.PlanEntity) PlanNotFoundException(io.gravitee.rest.api.service.exceptions.PlanNotFoundException) Key(io.gravitee.rest.api.portal.rest.model.Key)

Example 4 with Key

use of io.gravitee.rest.api.portal.rest.model.Key in project gravitee-management-rest-api by gravitee-io.

the class SubscriptionsResource method createSubscription.

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createSubscription(@Valid @NotNull(message = "Input must not be null.") SubscriptionInput subscriptionInput) {
    if (hasPermission(RolePermission.APPLICATION_SUBSCRIPTION, subscriptionInput.getApplication(), RolePermissionAction.CREATE)) {
        NewSubscriptionEntity newSubscriptionEntity = new NewSubscriptionEntity();
        newSubscriptionEntity.setApplication(subscriptionInput.getApplication());
        newSubscriptionEntity.setPlan(subscriptionInput.getPlan());
        newSubscriptionEntity.setRequest(subscriptionInput.getRequest());
        newSubscriptionEntity.setGeneralConditionsAccepted(subscriptionInput.getGeneralConditionsAccepted());
        if (subscriptionInput.getGeneralConditionsContentRevision() != null) {
            final PageRevisionId generalConditionsContentRevision = new PageRevisionId(subscriptionInput.getGeneralConditionsContentRevision().getPageId(), subscriptionInput.getGeneralConditionsContentRevision().getRevision());
            newSubscriptionEntity.setGeneralConditionsContentRevision(generalConditionsContentRevision);
        }
        SubscriptionEntity createdSubscription = subscriptionService.create(newSubscriptionEntity);
        // For consumer convenience, fetch the keys just after the subscription has been created.
        List<Key> keys = apiKeyService.findBySubscription(createdSubscription.getId()).stream().sorted((o1, o2) -> o2.getCreatedAt().compareTo(o1.getCreatedAt())).map(keyMapper::convert).collect(Collectors.toList());
        final Subscription subscription = subscriptionMapper.convert(createdSubscription);
        subscription.setKeys(keys);
        return Response.ok(subscription).build();
    }
    throw new ForbiddenAccessException();
}
Also used : PageRevisionId(io.gravitee.rest.api.model.PageEntity.PageRevisionId) Subscription(io.gravitee.rest.api.portal.rest.model.Subscription) ForbiddenAccessException(io.gravitee.rest.api.service.exceptions.ForbiddenAccessException) Key(io.gravitee.rest.api.portal.rest.model.Key)

Example 5 with Key

use of io.gravitee.rest.api.portal.rest.model.Key in project gravitee-management-rest-api by gravitee-io.

the class SubscriptionKeysResourceTest method shouldRenewSubscription.

@Test
public void shouldRenewSubscription() {
    final Response response = target(SUBSCRIPTION).path("keys/_renew").request().post(null);
    assertEquals(HttpStatusCode.CREATED_201, response.getStatus());
    assertNull(response.getHeaders().getFirst(HttpHeaders.LOCATION));
    Mockito.verify(apiKeyService).renew(SUBSCRIPTION);
    Key key = response.readEntity(Key.class);
    assertNotNull(key);
    assertEquals(KEY, key.getId());
}
Also used : Response(javax.ws.rs.core.Response) Key(io.gravitee.rest.api.portal.rest.model.Key) Test(org.junit.Test)

Aggregations

Key (io.gravitee.rest.api.portal.rest.model.Key)8 SubscriptionEntity (io.gravitee.rest.api.model.SubscriptionEntity)3 Subscription (io.gravitee.rest.api.portal.rest.model.Subscription)3 Test (org.junit.Test)3 ApiKeyEntity (io.gravitee.rest.api.model.ApiKeyEntity)2 ForbiddenAccessException (io.gravitee.rest.api.service.exceptions.ForbiddenAccessException)2 Before (org.junit.Before)2 PageRevisionId (io.gravitee.rest.api.model.PageEntity.PageRevisionId)1 PlanEntity (io.gravitee.rest.api.model.PlanEntity)1 PlanNotFoundException (io.gravitee.rest.api.service.exceptions.PlanNotFoundException)1 Response (javax.ws.rs.core.Response)1