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);
}
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);
}
}
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);
}
}
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);
}
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);
}
Aggregations