Skip to content
This repository has been archived by the owner on Aug 20, 2021. It is now read-only.

Commit

Permalink
fix(management): deprecate API even with staging plan
Browse files Browse the repository at this point in the history
  • Loading branch information
leleueri authored and aelamrani committed Jul 22, 2020
1 parent ee958a2 commit 6a61bbb
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
Expand Up @@ -49,6 +49,8 @@ public interface PlanService {

PlanEntity depreciate(String plan);

PlanEntity depreciate(String plan, boolean allowStaging);

PlansConfigurationEntity getConfiguration();

PlanEntity create(String apiId, PlanEntity planEntity);
Expand Down
Expand Up @@ -851,7 +851,7 @@ public ApiEntity update(String apiId, UpdateApiEntity updateApiEntity) {
if (ApiLifecycleState.DEPRECATED.equals(api.getApiLifecycleState())) {
planService.findByApi(api.getId()).forEach(plan -> {
if (PlanStatus.PUBLISHED.equals(plan.getStatus()) || PlanStatus.STAGING.equals(plan.getStatus())) {
planService.depreciate(plan.getId());
planService.depreciate(plan.getId(), true);
}
});
notifierService.trigger(ApiHook.API_DEPRECATED, apiId,
Expand Down
Expand Up @@ -458,6 +458,11 @@ public PlanEntity publish(String planId) {

@Override
public PlanEntity depreciate(String planId) {
return depreciate(planId, false);
}

@Override
public PlanEntity depreciate(String planId, boolean allowStaging) {
try {
logger.debug("Depreciate plan {}", planId);

Expand All @@ -472,7 +477,7 @@ public PlanEntity depreciate(String planId) {
throw new PlanAlreadyDeprecatedException(planId);
} else if (plan.getStatus() == Plan.Status.CLOSED) {
throw new PlanAlreadyClosedException(planId);
} else if (plan.getStatus() == Plan.Status.STAGING) {
} else if (!allowStaging && plan.getStatus() == Plan.Status.STAGING) {
throw new PlanNotYetPublishedException(planId);
}

Expand Down
Expand Up @@ -88,11 +88,41 @@ public void shouldNotDepreciateBecauseNotPublished() throws TechnicalException {
planService.depreciate(PLAN_ID);
}

@Test
public void shouldDepreciateWithStagingPlanAndAllowStaging() throws TechnicalException {
when(plan.getStatus()).thenReturn(Plan.Status.STAGING);
when(plan.getType()).thenReturn(Plan.PlanType.API);
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.update(plan)).thenAnswer(returnsFirstArg());

planService.depreciate(PLAN_ID, true);

verify(plan, times(1)).setStatus(Plan.Status.DEPRECATED);
verify(planRepository, times(1)).update(plan);
}

@Test(expected = PlanNotYetPublishedException.class)
public void shouldNotDepreciateWithStagingPlanAndNotAllowStaging() throws TechnicalException {
when(plan.getStatus()).thenReturn(Plan.Status.STAGING);
when(plan.getType()).thenReturn(Plan.PlanType.API);
when(plan.getValidation()).thenReturn(Plan.PlanValidationType.AUTO);
when(plan.getApis()).thenReturn(Collections.singleton(API_ID));
when(planRepository.findById(PLAN_ID)).thenReturn(Optional.of(plan));

planService.depreciate(PLAN_ID, false);

verify(plan, times(1)).setStatus(Plan.Status.DEPRECATED);
verify(planRepository, times(1)).update(plan);
}

@Test(expected = TechnicalManagementException.class)
public void shouldNotDepreciateBecauseTechnicalException() throws TechnicalException {
when(planRepository.findById(PLAN_ID)).thenThrow(TechnicalException.class);

planService.depreciate(PLAN_ID);

}

@Test
Expand Down

0 comments on commit 6a61bbb

Please sign in to comment.