Search in sources :

Example 1 with Plan

use of io.gravitee.repository.management.model.Plan in project gravitee-management-rest-api by gravitee-io.

the class PlanService_PublishTest method shouldNotPublishBecauseExistingOAuth2Plan.

@Test(expected = ClientIdBasedPlanAlreadyPublishedException.class)
public void shouldNotPublishBecauseExistingOAuth2Plan() throws TechnicalException {
    Plan keylessPlan = mock(Plan.class);
    when(keylessPlan.getStatus()).thenReturn(Plan.Status.PUBLISHED);
    when(keylessPlan.getSecurity()).thenReturn(Plan.PlanSecurityType.OAUTH2);
    when(plan.getStatus()).thenReturn(Plan.Status.STAGING);
    when(plan.getType()).thenReturn(Plan.PlanType.API);
    when(plan.getSecurity()).thenReturn(Plan.PlanSecurityType.OAUTH2);
    when(plan.getValidation()).thenReturn(Plan.PlanValidationType.AUTO);
    when(plan.getApis()).thenReturn(Collections.singleton(API_ID));
    when(planRepository.findById(PLAN_ID)).thenReturn(Optional.of(plan));
    when(planRepository.findByApi(API_ID)).thenReturn(Collections.singleton(keylessPlan));
    when(planRepository.update(plan)).thenAnswer(returnsFirstArg());
    when(subscriptionService.findByPlan(PLAN_ID)).thenReturn(Collections.singleton(subscription));
    planService.publish(PLAN_ID);
}
Also used : Plan(io.gravitee.repository.management.model.Plan) Test(org.junit.Test)

Example 2 with Plan

use of io.gravitee.repository.management.model.Plan in project gravitee-management-rest-api by gravitee-io.

the class PlanServiceImpl method close.

@Override
public PlanEntity close(String planId, String userId) {
    try {
        logger.debug("Close plan {}", planId);
        Optional<Plan> optPlan = planRepository.findById(planId);
        if (!optPlan.isPresent()) {
            throw new PlanNotFoundException(planId);
        }
        Plan plan = optPlan.get();
        Plan previousPlan = new Plan(plan);
        if (plan.getStatus() == Plan.Status.CLOSED) {
            throw new PlanAlreadyClosedException(planId);
        }
        // Update plan status
        plan.setStatus(Plan.Status.CLOSED);
        plan.setClosedAt(new Date());
        plan.setUpdatedAt(plan.getClosedAt());
        // Close active subscriptions and reject pending
        if (plan.getSecurity() != Plan.PlanSecurityType.KEY_LESS) {
            subscriptionService.findByPlan(planId).stream().filter(subscriptionEntity -> subscriptionEntity.getStatus() == SubscriptionStatus.ACCEPTED).forEach(subscription -> subscriptionService.close(subscription.getId()));
            final String planName = plan.getName();
            subscriptionService.findByPlan(planId).stream().filter(subscriptionEntity -> subscriptionEntity.getStatus() == SubscriptionStatus.PENDING).forEach(subscription -> {
                ProcessSubscriptionEntity processSubscriptionEntity = new ProcessSubscriptionEntity();
                processSubscriptionEntity.setId(subscription.getId());
                processSubscriptionEntity.setAccepted(false);
                processSubscriptionEntity.setReason("Plan " + planName + " has been closed.");
                subscriptionService.process(processSubscriptionEntity, userId);
            });
        }
        // Save plan
        plan = planRepository.update(plan);
        // Audit
        auditService.createApiAuditLog(plan.getApis().iterator().next(), Collections.singletonMap(PLAN, plan.getId()), PLAN_CLOSED, plan.getUpdatedAt(), previousPlan, plan);
        // reorder plan
        reorderedAndSavePlansAfterRemove(optPlan.get());
        return convert(plan);
    } catch (TechnicalException ex) {
        logger.error("An error occurs while trying to delete plan: {}", planId, ex);
        throw new TechnicalManagementException(String.format("An error occurs while trying to delete plan: %s", planId), ex);
    }
}
Also used : PlanQuery(io.gravitee.management.model.plan.PlanQuery) java.util(java.util) Logger(org.slf4j.Logger) SubscriptionService(io.gravitee.management.service.SubscriptionService) Collections.emptySet(java.util.Collections.emptySet) PlanService(io.gravitee.management.service.PlanService) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) AuditService(io.gravitee.management.service.AuditService) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) PlanRepository(io.gravitee.repository.management.api.PlanRepository) io.gravitee.management.service.exceptions(io.gravitee.management.service.exceptions) Plan(io.gravitee.repository.management.model.Plan) Component(org.springframework.stereotype.Component) UUID(io.gravitee.common.utils.UUID) io.gravitee.management.model(io.gravitee.management.model) PLAN(io.gravitee.repository.management.model.Audit.AuditProperties.PLAN) AuditEvent(io.gravitee.repository.management.model.Plan.AuditEvent) Path(io.gravitee.definition.model.Path) TypeReference(com.fasterxml.jackson.core.type.TypeReference) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Plan(io.gravitee.repository.management.model.Plan)

Example 3 with Plan

use of io.gravitee.repository.management.model.Plan in project gravitee-management-rest-api by gravitee-io.

the class PlanServiceImpl method create.

@Override
public PlanEntity create(NewPlanEntity newPlan) {
    try {
        logger.debug("Create a new plan {} for API {}", newPlan.getName(), newPlan.getApi());
        Plan plan = new Plan();
        plan.setId(UUID.toString(UUID.random()));
        plan.setApis(Collections.singleton(newPlan.getApi()));
        plan.setName(newPlan.getName());
        plan.setDescription(newPlan.getDescription());
        plan.setCreatedAt(new Date());
        plan.setUpdatedAt(plan.getCreatedAt());
        plan.setType(Plan.PlanType.valueOf(newPlan.getType().name()));
        plan.setSecurity(Plan.PlanSecurityType.valueOf(newPlan.getSecurity().name()));
        plan.setSecurityDefinition(newPlan.getSecurityDefinition());
        plan.setStatus(Plan.Status.valueOf(newPlan.getStatus().name()));
        plan.setExcludedGroups(newPlan.getExcludedGroups());
        if (plan.getSecurity() == Plan.PlanSecurityType.KEY_LESS) {
            // There is no need for a validation when authentication is KEY_LESS, force to AUTO
            plan.setValidation(Plan.PlanValidationType.AUTO);
        } else {
            plan.setValidation(Plan.PlanValidationType.valueOf(newPlan.getValidation().name()));
        }
        plan.setCharacteristics(newPlan.getCharacteristics());
        String planPolicies = objectMapper.writeValueAsString(newPlan.getPaths());
        plan.setDefinition(planPolicies);
        plan = planRepository.create(plan);
        auditService.createApiAuditLog(newPlan.getApi(), Collections.singletonMap(PLAN, plan.getId()), PLAN_CREATED, plan.getCreatedAt(), null, plan);
        return convert(plan);
    } catch (TechnicalException ex) {
        logger.error("An error occurs while trying to create a plan {} for API {}", newPlan.getName(), newPlan.getApi(), ex);
        throw new TechnicalManagementException(String.format("An error occurs while trying to create a plan %s for API %s", newPlan.getName(), newPlan.getApi()), ex);
    } catch (JsonProcessingException jse) {
        logger.error("Unexpected error while generating plan definition", jse);
        throw new TechnicalManagementException(String.format("An error occurs while trying to create a plan %s for API %s", newPlan.getName(), newPlan.getApi()), jse);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Plan(io.gravitee.repository.management.model.Plan) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 4 with Plan

use of io.gravitee.repository.management.model.Plan in project gravitee-management-rest-api by gravitee-io.

the class PlanService_PublishTest method shouldNotPublishBecauseExistingKeylessPlan.

@Test(expected = KeylessPlanAlreadyPublishedException.class)
public void shouldNotPublishBecauseExistingKeylessPlan() throws TechnicalException {
    Plan keylessPlan = mock(Plan.class);
    when(keylessPlan.getStatus()).thenReturn(Plan.Status.PUBLISHED);
    when(keylessPlan.getSecurity()).thenReturn(Plan.PlanSecurityType.KEY_LESS);
    when(plan.getStatus()).thenReturn(Plan.Status.STAGING);
    when(plan.getType()).thenReturn(Plan.PlanType.API);
    when(plan.getSecurity()).thenReturn(Plan.PlanSecurityType.KEY_LESS);
    when(plan.getValidation()).thenReturn(Plan.PlanValidationType.AUTO);
    when(plan.getApis()).thenReturn(Collections.singleton(API_ID));
    when(planRepository.findById(PLAN_ID)).thenReturn(Optional.of(plan));
    when(planRepository.findByApi(API_ID)).thenReturn(Collections.singleton(keylessPlan));
    when(planRepository.update(plan)).thenAnswer(returnsFirstArg());
    when(subscriptionService.findByPlan(PLAN_ID)).thenReturn(Collections.singleton(subscription));
    planService.publish(PLAN_ID);
}
Also used : Plan(io.gravitee.repository.management.model.Plan) Test(org.junit.Test)

Example 5 with Plan

use of io.gravitee.repository.management.model.Plan in project gravitee-management-rest-api by gravitee-io.

the class PlanService_PublishTest method shouldPublishWithExistingKeylessPlan.

public void shouldPublishWithExistingKeylessPlan() throws TechnicalException {
    Plan keylessPlan = mock(Plan.class);
    when(keylessPlan.getStatus()).thenReturn(Plan.Status.PUBLISHED);
    when(keylessPlan.getSecurity()).thenReturn(Plan.PlanSecurityType.KEY_LESS);
    when(plan.getStatus()).thenReturn(Plan.Status.STAGING);
    when(plan.getType()).thenReturn(Plan.PlanType.API);
    when(plan.getSecurity()).thenReturn(Plan.PlanSecurityType.API_KEY);
    when(plan.getValidation()).thenReturn(Plan.PlanValidationType.AUTO);
    when(plan.getApis()).thenReturn(Collections.singleton(API_ID));
    when(planRepository.findById(PLAN_ID)).thenReturn(Optional.of(plan));
    when(planRepository.findByApi(API_ID)).thenReturn(Collections.singleton(keylessPlan));
    when(planRepository.update(plan)).thenAnswer(returnsFirstArg());
    when(subscriptionService.findByPlan(PLAN_ID)).thenReturn(Collections.singleton(subscription));
    planService.publish(PLAN_ID);
}
Also used : Plan(io.gravitee.repository.management.model.Plan)

Aggregations

Plan (io.gravitee.repository.management.model.Plan)7 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)4 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)4 TypeReference (com.fasterxml.jackson.core.type.TypeReference)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 UUID (io.gravitee.common.utils.UUID)2 Path (io.gravitee.definition.model.Path)2 io.gravitee.management.model (io.gravitee.management.model)2 PlanQuery (io.gravitee.management.model.plan.PlanQuery)2 AuditService (io.gravitee.management.service.AuditService)2 PlanService (io.gravitee.management.service.PlanService)2 SubscriptionService (io.gravitee.management.service.SubscriptionService)2 io.gravitee.management.service.exceptions (io.gravitee.management.service.exceptions)2 PlanRepository (io.gravitee.repository.management.api.PlanRepository)2 PLAN (io.gravitee.repository.management.model.Audit.AuditProperties.PLAN)2 AuditEvent (io.gravitee.repository.management.model.Plan.AuditEvent)2 IOException (java.io.IOException)2 java.util (java.util)2 Collections.emptySet (java.util.Collections.emptySet)2 Collectors (java.util.stream.Collectors)2