Search in sources :

Example 11 with SortableImpl

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);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Promotion(io.gravitee.repository.management.model.Promotion) SortableImpl(io.gravitee.rest.api.model.common.SortableImpl)

Example 12 with SortableImpl

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());
}
Also used : PageableImpl(io.gravitee.rest.api.model.common.PageableImpl) SortableImpl(io.gravitee.rest.api.model.common.SortableImpl) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Api(io.gravitee.repository.management.model.Api) Test(org.junit.Test)

Example 13 with SortableImpl

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));
}
Also used : PageableImpl(io.gravitee.rest.api.model.common.PageableImpl) Ticket(io.gravitee.repository.management.model.Ticket) SortableImpl(io.gravitee.rest.api.model.common.SortableImpl) Pageable(io.gravitee.repository.management.api.search.Pageable) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Sortable(io.gravitee.repository.management.api.search.Sortable) TicketCriteria(io.gravitee.repository.management.api.search.TicketCriteria) TicketQuery(io.gravitee.rest.api.model.api.TicketQuery) Test(org.junit.Test)

Aggregations

SortableImpl (io.gravitee.rest.api.model.common.SortableImpl)13 TicketQuery (io.gravitee.rest.api.model.api.TicketQuery)6 Test (org.junit.Test)6 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)5 PageableImpl (io.gravitee.rest.api.model.common.PageableImpl)5 Page (io.gravitee.common.data.domain.Page)4 Sortable (io.gravitee.rest.api.model.common.Sortable)4 TicketEntity (io.gravitee.rest.api.model.TicketEntity)3 Collections.singletonList (java.util.Collections.singletonList)3 Response (javax.ws.rs.core.Response)3 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)2 Pageable (io.gravitee.repository.management.api.search.Pageable)2 Sortable (io.gravitee.repository.management.api.search.Sortable)2 TicketCriteria (io.gravitee.repository.management.api.search.TicketCriteria)2 Api (io.gravitee.repository.management.model.Api)2 Ticket (io.gravitee.repository.management.model.Ticket)2 PagedResult (io.gravitee.rest.api.management.rest.model.PagedResult)2 OrderParam (io.gravitee.rest.api.management.rest.resource.param.OrderParam)2 EnvironmentEntity (io.gravitee.rest.api.model.EnvironmentEntity)2 PromotionEntity (io.gravitee.rest.api.model.promotion.PromotionEntity)2