use of io.gravitee.rest.api.model.permissions.RolePermissionAction.UPDATE in project gravitee-management-rest-api by gravitee-io.
the class PromotionTasksServiceImplTest method shouldGetPromotionTasks_withApiUpdate.
@Test
public void shouldGetPromotionTasks_withApiUpdate() throws JsonProcessingException {
PromotionEntity aPromotionEntity = getAPromotionEntity();
when(promotionService.search(argThat(query -> query != null && query.getStatuses().get(0) == PromotionEntityStatus.TO_BE_VALIDATED), any(), any())).thenReturn(new Page<>(singletonList(aPromotionEntity), 0, 0, 0));
when(environmentService.findByOrganization(any())).thenReturn(singletonList(getAnEnvironmentEntity()));
PromotionEntity previousPromotionEntity = getAPromotionEntity();
previousPromotionEntity.setTargetApiId("api#target");
when(promotionService.search(argThat(query -> query != null && query.getStatuses().get(0) == PromotionEntityStatus.ACCEPTED), any(), any())).thenReturn(new Page<>(singletonList(previousPromotionEntity), 0, 0, 0));
when(permissionService.hasPermission(RolePermission.ENVIRONMENT_API, "env#1", UPDATE)).thenReturn(true);
when(permissionService.hasPermission(RolePermission.ENVIRONMENT_API, "env#1", CREATE)).thenReturn(false);
when(objectMapper.readValue(aPromotionEntity.getApiDefinition(), ApiEntity.class)).thenReturn(getAnApiEntity());
when(apiService.exists("api#target")).thenReturn(true);
final List<TaskEntity> result = cut.getPromotionTasks("org#1");
assertThat(result).hasSize(1);
Map<String, Object> taskData = (Map<String, Object>) result.get(0).getData();
assertThat(taskData.get("apiName")).isEqualTo("API Name");
assertThat(taskData.get("sourceEnvironmentName")).isEqualTo("Source Env");
assertThat(taskData.get("targetEnvironmentName")).isEqualTo("Target Env");
assertThat(taskData.get("authorDisplayName")).isEqualTo("Author");
assertThat(taskData.get("authorEmail")).isEqualTo("author@gv.io");
assertThat(taskData.get("authorPicture")).isEqualTo("https://picture.png");
assertThat(taskData.get("apiId")).isEqualTo("api id");
assertThat(taskData.get("isApiUpdate")).isEqualTo(true);
assertThat(taskData.get("targetApiId")).isEqualTo("api#target");
}
use of io.gravitee.rest.api.model.permissions.RolePermissionAction.UPDATE in project gravitee-management-rest-api by gravitee-io.
the class PromotionTasksServiceImplTest method shouldGetPromotionTasks_withApiUpdate_andApiCreation.
@Test
public void shouldGetPromotionTasks_withApiUpdate_andApiCreation() throws JsonProcessingException {
PromotionEntity promotionEntity1 = getAPromotionEntity();
PromotionEntity promotionEntity2 = getAPromotionEntity();
when(promotionService.search(argThat(query -> query != null && query.getStatuses().get(0) == PromotionEntityStatus.TO_BE_VALIDATED), any(), any())).thenReturn(new Page<>(List.of(promotionEntity1, promotionEntity2), 0, 0, 0));
when(environmentService.findByOrganization(any())).thenReturn(singletonList(getAnEnvironmentEntity()));
PromotionEntity previousPromotionEntity = getAPromotionEntity();
previousPromotionEntity.setTargetApiId("api#target");
when(promotionService.search(argThat(query -> query != null && query.getStatuses().get(0) == PromotionEntityStatus.ACCEPTED), any(), any())).thenReturn(new Page<>(singletonList(previousPromotionEntity), 0, 0, 0));
when(permissionService.hasPermission(RolePermission.ENVIRONMENT_API, "env#1", UPDATE)).thenReturn(true);
when(permissionService.hasPermission(RolePermission.ENVIRONMENT_API, "env#1", CREATE)).thenReturn(false);
when(objectMapper.readValue(promotionEntity1.getApiDefinition(), ApiEntity.class)).thenReturn(getAnApiEntity());
when(objectMapper.readValue(promotionEntity2.getApiDefinition(), ApiEntity.class)).thenReturn(getAnApiEntity());
when(apiService.exists("api#target")).thenReturn(true);
final List<TaskEntity> result = cut.getPromotionTasks("org#1");
assertThat(result).hasSize(2);
}
use of io.gravitee.rest.api.model.permissions.RolePermissionAction.UPDATE in project gravitee-management-rest-api by gravitee-io.
the class PromotionTasksServiceImplTest method shouldGetPromotionTasks_withApiCreation.
@Test
public void shouldGetPromotionTasks_withApiCreation() throws JsonProcessingException {
PromotionEntity aPromotionEntity = getAPromotionEntity();
when(promotionService.search(argThat(query -> query != null && query.getStatuses().get(0) == PromotionEntityStatus.TO_BE_VALIDATED), any(), any())).thenReturn(new Page<>(singletonList(aPromotionEntity), 0, 0, 0));
when(environmentService.findByOrganization(any())).thenReturn(singletonList(getAnEnvironmentEntity()));
when(promotionService.search(argThat(query -> query != null && query.getStatuses().get(0) == PromotionEntityStatus.ACCEPTED), any(), any())).thenReturn(new Page<>(emptyList(), 0, 0, 0));
when(permissionService.hasPermission(RolePermission.ENVIRONMENT_API, "env#1", CREATE)).thenReturn(true);
when(permissionService.hasPermission(RolePermission.ENVIRONMENT_API, "env#1", UPDATE)).thenReturn(false);
when(objectMapper.readValue(aPromotionEntity.getApiDefinition(), ApiEntity.class)).thenReturn(getAnApiEntity());
final List<TaskEntity> result = cut.getPromotionTasks("org#1");
assertThat(result).hasSize(1);
Map<String, Object> taskData = (Map<String, Object>) result.get(0).getData();
assertThat(taskData.get("apiName")).isEqualTo("API Name");
assertThat(taskData.get("sourceEnvironmentName")).isEqualTo("Source Env");
assertThat(taskData.get("targetEnvironmentName")).isEqualTo("Target Env");
assertThat(taskData.get("authorDisplayName")).isEqualTo("Author");
assertThat(taskData.get("authorEmail")).isEqualTo("author@gv.io");
assertThat(taskData.get("authorPicture")).isEqualTo("https://picture.png");
assertThat(taskData.get("apiId")).isEqualTo("api id");
assertThat(taskData.get("isApiUpdate")).isEqualTo(false);
assertThat(taskData.get("targetApiId")).isNull();
}
use of io.gravitee.rest.api.model.permissions.RolePermissionAction.UPDATE in project gravitee-management-rest-api by gravitee-io.
the class PromotionTasksServiceImplTest method shouldGetPromotionTasks_withApiCreationBecauseItHasBeenDeleted.
@Test
public void shouldGetPromotionTasks_withApiCreationBecauseItHasBeenDeleted() throws JsonProcessingException {
PromotionEntity aPromotionEntity = getAPromotionEntity();
when(promotionService.search(argThat(query -> query != null && query.getStatuses().get(0) == PromotionEntityStatus.TO_BE_VALIDATED), any(), any())).thenReturn(new Page<>(singletonList(aPromotionEntity), 0, 0, 0));
when(environmentService.findByOrganization(any())).thenReturn(singletonList(getAnEnvironmentEntity()));
PromotionEntity previousPromotionEntity = getAPromotionEntity();
previousPromotionEntity.setTargetApiId("api#target");
when(promotionService.search(argThat(query -> query != null && query.getStatuses().get(0) == PromotionEntityStatus.ACCEPTED), any(), any())).thenReturn(new Page<>(singletonList(previousPromotionEntity), 0, 0, 0));
when(permissionService.hasPermission(RolePermission.ENVIRONMENT_API, "env#1", CREATE)).thenReturn(true);
when(permissionService.hasPermission(RolePermission.ENVIRONMENT_API, "env#1", UPDATE)).thenReturn(false);
when(objectMapper.readValue(aPromotionEntity.getApiDefinition(), ApiEntity.class)).thenReturn(getAnApiEntity());
when(apiService.exists("api#target")).thenReturn(false);
final List<TaskEntity> result = cut.getPromotionTasks("org#1");
assertThat(result).hasSize(1);
Map<String, Object> taskData = (Map<String, Object>) result.get(0).getData();
assertThat(taskData.get("apiName")).isEqualTo("API Name");
assertThat(taskData.get("sourceEnvironmentName")).isEqualTo("Source Env");
assertThat(taskData.get("targetEnvironmentName")).isEqualTo("Target Env");
assertThat(taskData.get("authorDisplayName")).isEqualTo("Author");
assertThat(taskData.get("authorEmail")).isEqualTo("author@gv.io");
assertThat(taskData.get("authorPicture")).isEqualTo("https://picture.png");
assertThat(taskData.get("apiId")).isEqualTo("api id");
assertThat(taskData.get("isApiUpdate")).isEqualTo(false);
}
use of io.gravitee.rest.api.model.permissions.RolePermissionAction.UPDATE in project gravitee-management-rest-api by gravitee-io.
the class PromotionServiceImpl method promote.
@Override
public PromotionEntity promote(String apiId, PromotionRequestEntity promotionRequest, String userId) {
// TODO: do we have to use filteredFields like for duplicate (i think no need members and groups)
// FIXME: can we get the version from target environment
String apiDefinition = apiDuplicatorService.exportAsJson(apiId, ApiSerializer.Version.DEFAULT.getVersion(), "id", "members", "groups");
EnvironmentEntity currentEnvironmentEntity = environmentService.findById(GraviteeContext.getCurrentEnvironment());
UserEntity author = userService.findById(userId);
PromotionQuery promotionQuery = new PromotionQuery();
promotionQuery.setStatuses(List.of(PromotionEntityStatus.CREATED, PromotionEntityStatus.TO_BE_VALIDATED));
promotionQuery.setApiId(apiId);
List<PromotionEntity> inProgressPromotions = search(promotionQuery, null, null).getContent().stream().filter(promotionEntity -> promotionEntity.getTargetEnvCockpitId().equals(promotionRequest.getTargetEnvCockpitId())).collect(Collectors.toList());
if (!inProgressPromotions.isEmpty()) {
throw new PromotionAlreadyInProgressException(inProgressPromotions.get(0).getId());
}
Promotion promotionToSave = convert(apiId, apiDefinition, currentEnvironmentEntity, promotionRequest, author);
promotionToSave.setId(UuidString.generateRandom());
Promotion createdPromotion = null;
try {
createdPromotion = promotionRepository.create(promotionToSave);
auditService.createApiAuditLog(createdPromotion.getApiId(), emptyMap(), PROMOTION_CREATED, createdPromotion.getCreatedAt(), null, createdPromotion);
} catch (TechnicalException exception) {
throw new TechnicalManagementException(String.format("An error occurs while trying to create a promotion request for API %s", apiId), exception);
}
PromotionEntity promotionEntity = convert(createdPromotion);
CockpitReply<PromotionEntity> cockpitReply = cockpitService.requestPromotion(promotionEntity);
promotionEntity.setStatus(cockpitReply.getStatus() != CockpitReplyStatus.SUCCEEDED ? PromotionEntityStatus.ERROR : PromotionEntityStatus.TO_BE_VALIDATED);
try {
promotionRepository.update(convert(promotionEntity));
} catch (TechnicalException exception) {
throw new TechnicalManagementException(String.format("An error occurs while trying to update promotion %s", promotionEntity.getId()), exception);
}
if (cockpitReply.getStatus() != CockpitReplyStatus.SUCCEEDED) {
throw new BridgeOperationException(BridgeOperation.PROMOTE_API);
}
return promotionEntity;
}
Aggregations