Search in sources :

Example 71 with ApiEntity

use of io.gravitee.rest.api.model.api.ApiEntity in project gravitee-management-rest-api by gravitee-io.

the class TicketServiceTest method shouldFindById.

@Test
public void shouldFindById() throws TechnicalException {
    Ticket ticket = new Ticket();
    ticket.setId("ticket1");
    ticket.setApi(API_ID);
    ticket.setApplication(APPLICATION_ID);
    ticket.setSubject(EMAIL_SUBJECT);
    ticket.setContent(EMAIL_CONTENT);
    ticket.setCreatedAt(new Date());
    ticket.setFromUser(USERNAME);
    ApiEntity apiEntity = new ApiEntity();
    apiEntity.setName("apiName");
    ApplicationEntity appEntity = new ApplicationEntity();
    appEntity.setName("appName");
    when(ticketRepository.findById("ticket1")).thenReturn(Optional.of(ticket));
    when(apiService.findById(API_ID)).thenReturn(apiEntity);
    when(applicationService.findById(APPLICATION_ID)).thenReturn(appEntity);
    TicketEntity ticketEntity = ticketService.findById("ticket1");
    assertEquals("ticket1", ticketEntity.getId());
    assertEquals("apiName", ticketEntity.getApi());
    assertEquals("appName", ticketEntity.getApplication());
}
Also used : Ticket(io.gravitee.repository.management.model.Ticket) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Test(org.junit.Test)

Example 72 with ApiEntity

use of io.gravitee.rest.api.model.api.ApiEntity in project gravitee-management-rest-api by gravitee-io.

the class TicketServiceTest method shouldSearchForTicketsFromUser.

@Test
public void shouldSearchForTicketsFromUser() 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);
    ApiEntity apiEntity = new ApiEntity();
    ApplicationEntity appEntity = new ApplicationEntity();
    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))).thenReturn(new Page<>(ticketList, 0, 20, 20));
    when(apiService.findById(API_ID)).thenReturn(apiEntity);
    when(applicationService.findById(APPLICATION_ID)).thenReturn(appEntity);
    TicketQuery query = new TicketQuery();
    query.setFromUser("fromUser");
    Page<TicketEntity> searchResult = ticketService.search(query, new SortableImpl("subject", true), new PageableImpl(1, Integer.MAX_VALUE));
    assertEquals(searchResult.getContent().size(), 20);
    assertEquals(searchResult.getPageNumber(), 1);
    assertEquals(searchResult.getTotalElements(), 20);
}
Also used : PageableImpl(io.gravitee.rest.api.model.common.PageableImpl) Ticket(io.gravitee.repository.management.model.Ticket) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) TicketQuery(io.gravitee.rest.api.model.api.TicketQuery) SortableImpl(io.gravitee.rest.api.model.common.SortableImpl) Pageable(io.gravitee.repository.management.api.search.Pageable) Sortable(io.gravitee.repository.management.api.search.Sortable) TicketCriteria(io.gravitee.repository.management.api.search.TicketCriteria) Test(org.junit.Test)

Example 73 with ApiEntity

use of io.gravitee.rest.api.model.api.ApiEntity in project gravitee-management-rest-api by gravitee-io.

the class PermissionsResource method getCurrentUserPermissions.

@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getCurrentUserPermissions(@QueryParam("apiId") String apiId, @QueryParam("applicationId") String applicationId) {
    final String userId = getAuthenticatedUser();
    if (apiId != null) {
        ApiQuery apiQuery = new ApiQuery();
        apiQuery.setIds(Collections.singletonList(apiId));
        Set<ApiEntity> publishedByUser = apiService.findPublishedByUser(getAuthenticatedUserOrNull(), apiQuery);
        ApiEntity apiEntity = publishedByUser.stream().filter(a -> a.getId().equals(apiId)).findFirst().orElseThrow(() -> new ApiNotFoundException(apiId));
        Map<String, char[]> permissions;
        permissions = membershipService.getUserMemberPermissions(apiEntity, userId);
        return Response.ok(permissions).build();
    } else if (applicationId != null) {
        ApplicationListItem applicationListItem = applicationService.findByUser(getAuthenticatedUser()).stream().filter(a -> a.getId().equals(applicationId)).findFirst().orElseThrow(() -> new ApplicationNotFoundException(applicationId));
        ApplicationEntity application = applicationService.findById(applicationListItem.getId());
        Map<String, char[]> permissions;
        permissions = membershipService.getUserMemberPermissions(application, userId);
        return Response.ok(permissions).build();
    }
    throw new BadRequestException("One of the two parameters appId or applicationId must not be null.");
}
Also used : ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) MembershipService(io.gravitee.rest.api.service.MembershipService) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) Set(java.util.Set) ApiService(io.gravitee.rest.api.service.ApiService) Inject(javax.inject.Inject) MediaType(io.gravitee.common.http.MediaType) QueryParam(javax.ws.rs.QueryParam) Response(javax.ws.rs.core.Response) Map(java.util.Map) ApplicationService(io.gravitee.rest.api.service.ApplicationService) BadRequestException(javax.ws.rs.BadRequestException) ApplicationNotFoundException(io.gravitee.rest.api.service.exceptions.ApplicationNotFoundException) Collections(java.util.Collections) ApplicationEntity(io.gravitee.rest.api.model.ApplicationEntity) ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) ApplicationNotFoundException(io.gravitee.rest.api.service.exceptions.ApplicationNotFoundException) ApplicationEntity(io.gravitee.rest.api.model.ApplicationEntity) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) BadRequestException(javax.ws.rs.BadRequestException) Map(java.util.Map) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 74 with ApiEntity

use of io.gravitee.rest.api.model.api.ApiEntity in project gravitee-management-rest-api by gravitee-io.

the class ApiPlansResource method getApiPlansByApiId.

@GET
@Produces(MediaType.APPLICATION_JSON)
@RequirePortalAuth
public Response getApiPlansByApiId(@PathParam("apiId") String apiId, @BeanParam PaginationParam paginationParam) {
    String username = getAuthenticatedUserOrNull();
    final ApiQuery apiQuery = new ApiQuery();
    apiQuery.setIds(Collections.singletonList(apiId));
    Collection<ApiEntity> userApis = apiService.findPublishedByUser(username, apiQuery);
    if (userApis.stream().anyMatch(a -> a.getId().equals(apiId))) {
        ApiEntity apiEntity = apiService.findById(apiId);
        if (Visibility.PUBLIC.equals(apiEntity.getVisibility()) || hasPermission(API_PLAN, apiId, READ)) {
            List<Plan> plans = planService.findByApi(apiId).stream().filter(plan -> PlanStatus.PUBLISHED.equals(plan.getStatus())).filter(plan -> groupService.isUserAuthorizedToAccessApiData(apiEntity, plan.getExcludedGroups(), username)).sorted(Comparator.comparingInt(PlanEntity::getOrder)).map(p -> planMapper.convert(p)).collect(Collectors.toList());
            return createListResponse(plans, paginationParam);
        } else {
            return createListResponse(emptyList(), paginationParam);
        }
    }
    throw new ApiNotFoundException(apiId);
}
Also used : PlanMapper(io.gravitee.rest.api.portal.rest.mapper.PlanMapper) PathParam(javax.ws.rs.PathParam) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Plan(io.gravitee.rest.api.portal.rest.model.Plan) RequirePortalAuth(io.gravitee.rest.api.portal.rest.security.RequirePortalAuth) PlanStatus(io.gravitee.rest.api.model.PlanStatus) Inject(javax.inject.Inject) PaginationParam(io.gravitee.rest.api.portal.rest.resource.param.PaginationParam) API_PLAN(io.gravitee.rest.api.model.permissions.RolePermission.API_PLAN) ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) READ(io.gravitee.rest.api.model.permissions.RolePermissionAction.READ) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) GroupService(io.gravitee.rest.api.service.GroupService) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) PlanEntity(io.gravitee.rest.api.model.PlanEntity) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) Collectors(java.util.stream.Collectors) BeanParam(javax.ws.rs.BeanParam) List(java.util.List) MediaType(io.gravitee.common.http.MediaType) Response(javax.ws.rs.core.Response) Visibility(io.gravitee.rest.api.model.Visibility) PlanService(io.gravitee.rest.api.service.PlanService) Comparator(java.util.Comparator) Collections(java.util.Collections) ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Plan(io.gravitee.rest.api.portal.rest.model.Plan) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) RequirePortalAuth(io.gravitee.rest.api.portal.rest.security.RequirePortalAuth)

Example 75 with ApiEntity

use of io.gravitee.rest.api.model.api.ApiEntity in project gravitee-management-rest-api by gravitee-io.

the class ApiRatingsResource method createApiRating.

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Permissions({ @Permission(value = RolePermission.API_RATING, acls = RolePermissionAction.CREATE) })
public Response createApiRating(@PathParam("apiId") String apiId, @Valid RatingInput ratingInput) {
    if (ratingInput == null) {
        throw new BadRequestException("Input must not be null.");
    }
    final ApiQuery apiQuery = new ApiQuery();
    apiQuery.setIds(Collections.singletonList(apiId));
    Collection<ApiEntity> userApis = apiService.findPublishedByUser(getAuthenticatedUserOrNull(), apiQuery);
    if (userApis.stream().anyMatch(a -> a.getId().equals(apiId))) {
        NewRatingEntity rating = new NewRatingEntity();
        rating.setApi(apiId);
        rating.setComment(ratingInput.getComment());
        rating.setTitle(ratingInput.getTitle());
        rating.setRate(ratingInput.getValue().byteValue());
        RatingEntity createdRating = ratingService.create(rating);
        return Response.status(Status.CREATED).entity(ratingMapper.convert(createdRating, uriInfo)).build();
    }
    throw new ApiNotFoundException(apiId);
}
Also used : NewRatingEntity(io.gravitee.rest.api.model.NewRatingEntity) ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) RatingEntity(io.gravitee.rest.api.model.RatingEntity) NewRatingEntity(io.gravitee.rest.api.model.NewRatingEntity) Permissions(io.gravitee.rest.api.portal.rest.security.Permissions)

Aggregations

ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)273 Test (org.junit.Test)180 Response (javax.ws.rs.core.Response)65 Before (org.junit.Before)46 ApiQuery (io.gravitee.rest.api.model.api.ApiQuery)35 Error (io.gravitee.rest.api.portal.rest.model.Error)32 java.util (java.util)30 Collectors (java.util.stream.Collectors)27 Mockito.doReturn (org.mockito.Mockito.doReturn)27 Mockito (org.mockito.Mockito)25 ArgumentMatchers (org.mockito.ArgumentMatchers)24 UpdateApiEntity (io.gravitee.rest.api.model.api.UpdateApiEntity)23 Assert (org.junit.Assert)22 Collections.singletonList (java.util.Collections.singletonList)21 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)20 IOException (java.io.IOException)20 HttpStatusCode (io.gravitee.common.http.HttpStatusCode)19 Entity (javax.ws.rs.client.Entity)19 HashSet (java.util.HashSet)18 Api (io.gravitee.repository.management.model.Api)17