Search in sources :

Example 6 with Plan

use of io.gravitee.rest.api.portal.rest.model.Plan 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 7 with Plan

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

the class ApiResourceNotAuthenticatedTest method init.

@Before
public void init() {
    resetAllMocks();
    mockApi = new ApiEntity();
    mockApi.setId(API);
    doReturn(mockApi).when(apiService).findById(API);
    when(accessControlService.canAccessApiFromPortal(API)).thenReturn(true);
    when(accessControlService.canAccessApiFromPortal(mockApi)).thenReturn(true);
    doReturn(Arrays.asList(new PageEntity())).when(pageService).search(any(), eq(GraviteeContext.getCurrentEnvironment()));
    PlanEntity plan1 = new PlanEntity();
    plan1.setId("A");
    plan1.setStatus(PlanStatus.PUBLISHED);
    PlanEntity plan2 = new PlanEntity();
    plan2.setId("B");
    plan2.setStatus(PlanStatus.PUBLISHED);
    PlanEntity plan3 = new PlanEntity();
    plan3.setId("C");
    plan3.setStatus(PlanStatus.CLOSED);
    doReturn(new HashSet<PlanEntity>(Arrays.asList(plan1, plan2, plan3))).when(planService).findByApi(API);
    doReturn(new Api()).when(apiMapper).convert(any());
    doReturn(new Page()).when(pageMapper).convert(any());
    doReturn(new Plan()).when(planMapper).convert(any());
}
Also used : PageEntity(io.gravitee.rest.api.model.PageEntity) PlanEntity(io.gravitee.rest.api.model.PlanEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Page(io.gravitee.rest.api.portal.rest.model.Page) Api(io.gravitee.rest.api.portal.rest.model.Api) Plan(io.gravitee.rest.api.portal.rest.model.Plan) Before(org.junit.Before)

Example 8 with Plan

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

the class ApiPlansResourceTest method shouldGetNoApiPlan.

@Test
public void shouldGetNoApiPlan() {
    doReturn(false).when(groupService).isUserAuthorizedToAccessApiData(any(), any(), any());
    final Response response = target(API).path("plans").request().get();
    assertEquals(OK_200, response.getStatus());
    PlansResponse plansResponse = response.readEntity(PlansResponse.class);
    List<Plan> plans = plansResponse.getData();
    assertNotNull(plans);
    assertEquals(0, plans.size());
}
Also used : PlansResponse(io.gravitee.rest.api.portal.rest.model.PlansResponse) ErrorResponse(io.gravitee.rest.api.portal.rest.model.ErrorResponse) Response(javax.ws.rs.core.Response) PlansResponse(io.gravitee.rest.api.portal.rest.model.PlansResponse) Plan(io.gravitee.rest.api.portal.rest.model.Plan) Test(org.junit.Test)

Example 9 with Plan

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

the class ApiPlansResourceTest method shouldGetEmptyListPrivateAPIAndNoReadPermission.

@Test
public void shouldGetEmptyListPrivateAPIAndNoReadPermission() {
    doReturn(false).when(permissionService).hasPermission(any(), any(), any());
    ApiEntity mockApi = new ApiEntity();
    mockApi.setId(API);
    mockApi.setVisibility(Visibility.PRIVATE);
    doReturn(mockApi).when(apiService).findById(API);
    Set<ApiEntity> mockApis = new HashSet<>(Arrays.asList(mockApi));
    doReturn(mockApis).when(apiService).findPublishedByUser(any());
    final Response response = target(API).path("plans").request().get();
    assertEquals(OK_200, response.getStatus());
    PlansResponse plansResponse = response.readEntity(PlansResponse.class);
    List<Plan> plans = plansResponse.getData();
    assertNotNull(plans);
    assertEquals(0, plans.size());
}
Also used : PlansResponse(io.gravitee.rest.api.portal.rest.model.PlansResponse) ErrorResponse(io.gravitee.rest.api.portal.rest.model.ErrorResponse) Response(javax.ws.rs.core.Response) PlansResponse(io.gravitee.rest.api.portal.rest.model.PlansResponse) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Plan(io.gravitee.rest.api.portal.rest.model.Plan) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 10 with Plan

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

the class ApiResourceNotAuthenticatedTest method callResourceAndCheckResult.

private void callResourceAndCheckResult(Integer expectedTotalPage, Integer expectedTotalPlan) {
    final Response response = target(API).queryParam("include", "pages", "plans").request().get();
    assertEquals(OK_200, response.getStatus());
    Api responseApi = response.readEntity(Api.class);
    assertNotNull(responseApi);
    List<Page> pages = responseApi.getPages();
    assertNotNull(pages);
    assertEquals(expectedTotalPage.intValue(), pages.size());
    List<Plan> plans = responseApi.getPlans();
    assertNotNull(plans);
    assertEquals(expectedTotalPlan.intValue(), plans.size());
}
Also used : Response(javax.ws.rs.core.Response) Page(io.gravitee.rest.api.portal.rest.model.Page) Api(io.gravitee.rest.api.portal.rest.model.Api) Plan(io.gravitee.rest.api.portal.rest.model.Plan)

Aggregations

Plan (io.gravitee.rest.api.portal.rest.model.Plan)10 Response (javax.ws.rs.core.Response)7 Test (org.junit.Test)6 ErrorResponse (io.gravitee.rest.api.portal.rest.model.ErrorResponse)5 PlansResponse (io.gravitee.rest.api.portal.rest.model.PlansResponse)5 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)3 PlanEntity (io.gravitee.rest.api.model.PlanEntity)2 Api (io.gravitee.rest.api.portal.rest.model.Api)2 Page (io.gravitee.rest.api.portal.rest.model.Page)2 MediaType (io.gravitee.common.http.MediaType)1 PageEntity (io.gravitee.rest.api.model.PageEntity)1 PlanStatus (io.gravitee.rest.api.model.PlanStatus)1 Visibility (io.gravitee.rest.api.model.Visibility)1 ApiQuery (io.gravitee.rest.api.model.api.ApiQuery)1 API_PLAN (io.gravitee.rest.api.model.permissions.RolePermission.API_PLAN)1 READ (io.gravitee.rest.api.model.permissions.RolePermissionAction.READ)1 PlanMapper (io.gravitee.rest.api.portal.rest.mapper.PlanMapper)1 PaginationParam (io.gravitee.rest.api.portal.rest.resource.param.PaginationParam)1 RequirePortalAuth (io.gravitee.rest.api.portal.rest.security.RequirePortalAuth)1 GroupService (io.gravitee.rest.api.service.GroupService)1