use of io.gravitee.rest.api.model.TaskEntity in project gravitee-management-rest-api by gravitee-io.
the class PromotionTasksServiceImpl method convert.
private TaskEntity convert(PromotionEntity promotionEntity, boolean isUpdate, Optional<String> foundTargetApiId) {
TaskEntity taskEntity = new TaskEntity();
taskEntity.setType(TaskType.PROMOTION_APPROVAL);
taskEntity.setCreatedAt(promotionEntity.getCreatedAt());
ApiEntity apiEntity;
try {
apiEntity = objectMapper.readValue(promotionEntity.getApiDefinition(), ApiEntity.class);
} catch (JsonProcessingException e) {
logger.warn("Problem while deserializing api definition for promotion {}", promotionEntity.getId());
throw new TechnicalManagementException();
}
Map<String, Object> data = new HashMap<>();
data.put("apiName", apiEntity.getName());
data.put("apiId", promotionEntity.getApiId());
data.put("sourceEnvironmentName", promotionEntity.getSourceEnvName());
data.put("targetEnvironmentName", promotionEntity.getTargetEnvName());
data.put("authorDisplayName", promotionEntity.getAuthor().getDisplayName());
data.put("authorEmail", promotionEntity.getAuthor().getEmail());
data.put("authorPicture", promotionEntity.getAuthor().getPicture());
data.put("promotionId", promotionEntity.getId());
data.put("isApiUpdate", isUpdate);
foundTargetApiId.ifPresent(targetApiId -> data.put("targetApiId", targetApiId));
taskEntity.setData(data);
return taskEntity;
}
use of io.gravitee.rest.api.model.TaskEntity 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.TaskEntity 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.TaskEntity in project gravitee-management-rest-api by gravitee-io.
the class PromotionTasksServiceImpl method getPromotionTasksForEnvironments.
@NotNull
private List<TaskEntity> getPromotionTasksForEnvironments(List<EnvironmentEntity> environments, boolean selectUpdatePromotion) {
if (environments.isEmpty()) {
return emptyList();
}
List<String> envCockpitIds = environments.stream().map(EnvironmentEntity::getCockpitId).filter(Objects::nonNull).collect(toList());
final PromotionQuery promotionQuery = new PromotionQuery();
promotionQuery.setStatuses(Collections.singletonList(PromotionEntityStatus.TO_BE_VALIDATED));
promotionQuery.setTargetEnvCockpitIds(envCockpitIds);
final Page<PromotionEntity> promotionsPage = promotionService.search(promotionQuery, new SortableImpl("created_at", false), null);
final PromotionQuery previousPromotionsQuery = new PromotionQuery();
previousPromotionsQuery.setStatuses(Collections.singletonList(PromotionEntityStatus.ACCEPTED));
previousPromotionsQuery.setTargetEnvCockpitIds(envCockpitIds);
previousPromotionsQuery.setTargetApiExists(true);
List<PromotionEntity> previousPromotions = promotionService.search(previousPromotionsQuery, new SortableImpl("created_at", false), null).getContent();
final Map<String, List<String>> promotionByApiWithTargetApiId = previousPromotions.stream().collect(groupingBy(PromotionEntity::getApiId, Collectors.mapping(PromotionEntity::getTargetApiId, toList())));
return promotionsPage.getContent().stream().map(promotionEntity -> {
Optional<String> foundTargetApiId = promotionByApiWithTargetApiId.getOrDefault(promotionEntity.getApiId(), emptyList()).stream().filter(StringUtils::hasText).findFirst();
boolean isUpdate = foundTargetApiId.isPresent() && apiService.exists(foundTargetApiId.get());
return convert(promotionEntity, isUpdate, foundTargetApiId);
}).filter(taskEntity -> ((Boolean) ((Map<String, Object>) taskEntity.getData()).getOrDefault("isApiUpdate", false) == selectUpdatePromotion)).collect(toList());
}
use of io.gravitee.rest.api.model.TaskEntity 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();
}
Aggregations