use of io.gravitee.rest.api.model.common.SortableImpl in project gravitee-management-rest-api by gravitee-io.
the class PromotionServiceImpl method processPromotion.
@Override
public PromotionEntity processPromotion(String promotion, boolean accepted, String user) {
try {
final Promotion existing = promotionRepository.findById(promotion).orElseThrow(() -> new PromotionNotFoundException(promotion));
EnvironmentEntity environment = environmentService.findByCockpitId(existing.getTargetEnvCockpitId());
existing.setStatus(accepted ? PromotionStatus.ACCEPTED : PromotionStatus.REJECTED);
final PromotionQuery promotionQuery = new PromotionQuery();
promotionQuery.setStatuses(Collections.singletonList(PromotionEntityStatus.ACCEPTED));
promotionQuery.setTargetEnvCockpitIds(singletonList(existing.getTargetEnvCockpitId()));
promotionQuery.setTargetApiExists(true);
promotionQuery.setApiId(existing.getApiId());
List<PromotionEntity> previousPromotions = search(promotionQuery, new SortableImpl("created_at", false), null).getContent();
// Should create a new API if there is no previous promotion for this API or if the API existed once (after a promotion) but has been deleted since
boolean shouldCreate = CollectionUtils.isEmpty(previousPromotions) || !apiService.exists(previousPromotions.get(0).getTargetApiId());
if (PromotionStatus.ACCEPTED.equals(existing.getStatus())) {
ApiEntity promoted = null;
// FIXME: All the methods should take then env id as input instead of relying on GraviteeContext.getCurrentEnv
GraviteeContext.setCurrentEnvironment(environment.getId());
if (shouldCreate) {
if (!permissionService.hasPermission(ENVIRONMENT_API, environment.getId(), CREATE)) {
throw new ForbiddenAccessException();
}
promoted = apiDuplicatorService.createWithImportedDefinition(existing.getApiDefinition(), user, GraviteeContext.getCurrentOrganization(), GraviteeContext.getCurrentEnvironment());
} else {
if (!permissionService.hasPermission(ENVIRONMENT_API, environment.getId(), UPDATE)) {
throw new ForbiddenAccessException();
}
PromotionEntity lastAcceptedPromotion = previousPromotions.get(0);
final ApiEntity existingApi = apiService.findById(lastAcceptedPromotion.getTargetApiId());
promoted = apiDuplicatorService.updateWithImportedDefinition(existingApi.getId(), existing.getApiDefinition(), user, GraviteeContext.getCurrentOrganization(), GraviteeContext.getCurrentEnvironment());
}
existing.setTargetApiId(promoted.getId());
}
final PromotionEntity promotionEntity = convert(existing);
final CockpitReply<PromotionEntity> cockpitReply = cockpitService.processPromotion(promotionEntity);
if (cockpitReply.getStatus() != CockpitReplyStatus.SUCCEEDED) {
throw new BridgeOperationException(BridgeOperation.PROMOTE_API);
}
final Promotion updated = promotionRepository.update(existing);
return convert(updated);
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to process promotion", ex);
throw new TechnicalManagementException("An error occurs while trying to process promotion", ex);
}
}
use of io.gravitee.rest.api.model.common.SortableImpl in project gravitee-management-rest-api by gravitee-io.
the class ApiService_FindByUserTest method shouldFindByUserPaginated.
@Test
public void shouldFindByUserPaginated() throws TechnicalException {
final Api api1 = new Api();
api1.setId("api1");
api1.setName("api1");
final Api api2 = new Api();
api2.setId("api2");
api2.setName("api2");
MembershipEntity membership1 = new MembershipEntity();
membership1.setId("id1");
membership1.setMemberId(USER_NAME);
membership1.setMemberType(MembershipMemberType.USER);
membership1.setReferenceId(api1.getId());
membership1.setReferenceType(MembershipReferenceType.API);
membership1.setRoleId("API_USER");
MembershipEntity membership2 = new MembershipEntity();
membership2.setId(api2.getId());
membership2.setMemberId(USER_NAME);
membership2.setMemberType(MembershipMemberType.USER);
membership2.setReferenceId("api2");
membership2.setReferenceType(MembershipReferenceType.API);
membership2.setRoleId("API_USER");
when(membershipService.getMembershipsByMemberAndReference(MembershipMemberType.USER, USER_NAME, MembershipReferenceType.API)).thenReturn(new HashSet<>(Arrays.asList(membership1, membership2)));
when(apiRepository.search(new ApiCriteria.Builder().environmentId("DEFAULT").ids(api1.getId(), api2.getId()).build())).thenReturn(Arrays.asList(api1, api2));
RoleEntity poRole = new RoleEntity();
poRole.setId("API_PRIMARY_OWNER");
when(roleService.findPrimaryOwnerRoleByOrganization(any(), any())).thenReturn(poRole);
MemberEntity poMember = new MemberEntity();
poMember.setId("admin");
poMember.setRoles(Collections.singletonList(poRole));
when(membershipService.getMembersByReferencesAndRole(MembershipReferenceType.API, Collections.singletonList(api1.getId()), "API_PRIMARY_OWNER")).thenReturn(new HashSet<>(singletonList(poMember)));
final Page<ApiEntity> apiPage = apiService.findByUser(USER_NAME, null, new SortableImpl("name", false), new PageableImpl(2, 1), false);
assertNotNull(apiPage);
assertEquals(1, apiPage.getContent().size());
assertEquals(api1.getId(), apiPage.getContent().get(0).getId());
assertEquals(2, apiPage.getPageNumber());
assertEquals(1, apiPage.getPageElements());
assertEquals(2, apiPage.getTotalElements());
}
use of io.gravitee.rest.api.model.common.SortableImpl in project gravitee-management-rest-api by gravitee-io.
the class TicketServiceTest method shouldNotSearchSearchIfRepositoryThrowException.
@Test(expected = TechnicalManagementException.class)
public void shouldNotSearchSearchIfRepositoryThrowException() throws TechnicalException {
Ticket ticket = new Ticket();
ticket.setId("generatedId");
ticket.setApi(API_ID);
ticket.setApplication(APPLICATION_ID);
ticket.setSubject(EMAIL_SUBJECT);
ticket.setContent(EMAIL_CONTENT);
ticket.setCreatedAt(new Date());
ticket.setFromUser(USERNAME);
List<Ticket> ticketList = new ArrayList<>();
for (int i = 0; i < 20; i++) {
Ticket t = new Ticket(ticket);
t.setId("ticket" + i);
ticketList.add(t);
}
when(ticketRepository.search(any(TicketCriteria.class), any(Sortable.class), any(Pageable.class))).thenThrow(new TechnicalException());
TicketQuery query = new TicketQuery();
query.setFromUser("fromUser");
ticketService.search(query, new SortableImpl("subject", true), new PageableImpl(1, Integer.MAX_VALUE));
}
Aggregations