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());
}
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);
}
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.");
}
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);
}
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);
}
Aggregations