Search in sources :

Example 16 with ApiQuery

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

the class ApisResourceTest method shouldSearchApis.

@Test
public void shouldSearchApis() throws TechnicalException {
    ApiEntity searchedApi = new ApiEntity();
    searchedApi.setLifecycleState(ApiLifecycleState.PUBLISHED);
    searchedApi.setName("3");
    searchedApi.setId("3");
    doReturn(new HashSet<>(Arrays.asList(searchedApi))).when(apiService).search(any(), anyMap());
    final Response response = target("/_search").queryParam("q", "3").request().post(Entity.json(null));
    assertEquals(HttpStatusCode.OK_200, response.getStatus());
    ArgumentCaptor<ApiQuery> queryCaptor = ArgumentCaptor.forClass(ApiQuery.class);
    Mockito.verify(apiService).findPublishedByUser(eq(USER_NAME), queryCaptor.capture());
    final ApiQuery query = queryCaptor.getValue();
    assertNull(query.getContextPath());
    assertNull(query.getLabel());
    assertNull(query.getVersion());
    assertNull(query.getName());
    assertNull(query.getTag());
    ArgumentCaptor<String> basePathCaptor = ArgumentCaptor.forClass(String.class);
    Mockito.verify(apiMapper, Mockito.times(1)).computeApiLinks(basePathCaptor.capture(), ArgumentCaptor.forClass(Date.class).capture());
    final String expectedBasePath = target().getUri().toString();
    final List<String> bastPathList = basePathCaptor.getAllValues();
    assertTrue(bastPathList.contains(expectedBasePath + "/3"));
    ArgumentCaptor<ApiEntity> apiEntityCaptor = ArgumentCaptor.forClass(ApiEntity.class);
    Mockito.verify(apiMapper, Mockito.times(1)).convert(apiEntityCaptor.capture());
    assertEquals("3", apiEntityCaptor.getValue().getName());
    ApisResponse apiResponse = response.readEntity(ApisResponse.class);
    assertEquals(1, apiResponse.getData().size());
}
Also used : Response(javax.ws.rs.core.Response) ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Test(org.junit.Test)

Example 17 with ApiQuery

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

the class ApiService_SearchTest method shouldSearchPaginated.

@Test
public void shouldSearchPaginated() {
    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(apiRepository.search(new ApiCriteria.Builder().environmentId("DEFAULT").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 ApiQuery apiQuery = new ApiQuery();
    final Page<ApiEntity> apiPage = apiService.search(apiQuery, new SortableImpl("name", false), new PageableImpl(2, 1));
    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) ApiCriteria(io.gravitee.repository.management.api.search.ApiCriteria) ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) 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 18 with ApiQuery

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

the class ApiInformationsResource method getApiInformations.

@GET
@Produces({ MediaType.APPLICATION_JSON })
@RequirePortalAuth
public Response getApiInformations(@Context Request request, @PathParam("apiId") String apiId) {
    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))) {
        List<ApiHeaderEntity> all = apiService.getPortalHeaders(apiId);
        List<ApiInformation> information = all.stream().map(apiHeaderEntity -> {
            ApiInformation ai = new ApiInformation();
            ai.setName(apiHeaderEntity.getName());
            ai.setValue(apiHeaderEntity.getValue());
            return ai;
        }).collect(Collectors.toList());
        return Response.ok(information).build();
    }
    throw new ApiNotFoundException(apiId);
}
Also used : ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) PathParam(javax.ws.rs.PathParam) Context(javax.ws.rs.core.Context) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Collection(java.util.Collection) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) RequirePortalAuth(io.gravitee.rest.api.portal.rest.security.RequirePortalAuth) Collectors(java.util.stream.Collectors) ApiService(io.gravitee.rest.api.service.ApiService) Inject(javax.inject.Inject) List(java.util.List) MediaType(io.gravitee.common.http.MediaType) ApiInformation(io.gravitee.rest.api.portal.rest.model.ApiInformation) Response(javax.ws.rs.core.Response) Request(javax.ws.rs.core.Request) ApiHeaderEntity(io.gravitee.rest.api.model.api.header.ApiHeaderEntity) Collections(java.util.Collections) ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) ApiHeaderEntity(io.gravitee.rest.api.model.api.header.ApiHeaderEntity) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) ApiInformation(io.gravitee.rest.api.portal.rest.model.ApiInformation) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) RequirePortalAuth(io.gravitee.rest.api.portal.rest.security.RequirePortalAuth)

Example 19 with ApiQuery

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

the class ApiMetricsResource method getApiMetricsByApiId.

@GET
@Produces({ MediaType.APPLICATION_JSON })
@RequirePortalAuth
public Response getApiMetricsByApiId(@Context Request request, @PathParam("apiId") String apiId) {
    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))) {
        Number healthRatio = getHealthRatio(apiId);
        Number nbHits = getNbHits(apiId);
        Number subscribers = getApiNbSubscribers(apiId);
        ApiMetrics metrics = new ApiMetrics();
        metrics.setHealth(healthRatio);
        metrics.setHits(nbHits);
        metrics.setSubscribers(subscribers);
        return Response.ok(metrics).build();
    }
    throw new ApiNotFoundException(apiId);
}
Also used : ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) ApiMetrics(io.gravitee.rest.api.portal.rest.model.ApiMetrics) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) RequirePortalAuth(io.gravitee.rest.api.portal.rest.security.RequirePortalAuth)

Example 20 with ApiQuery

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

the class ApiPageResource method getPageByApiIdAndPageId.

@GET
@Produces(MediaType.APPLICATION_JSON)
@RequirePortalAuth
public Response getPageByApiIdAndPageId(@HeaderParam("Accept-Language") String acceptLang, @PathParam("apiId") String apiId, @PathParam("pageId") String pageId, @QueryParam("include") List<String> include) {
    final ApiQuery apiQuery = new ApiQuery();
    apiQuery.setIds(Collections.singletonList(apiId));
    if (accessControlService.canAccessApiFromPortal(apiId)) {
        final String acceptedLocale = HttpHeadersUtil.getFirstAcceptedLocaleName(acceptLang);
        PageEntity pageEntity = pageService.findById(pageId, acceptedLocale);
        if (accessControlService.canAccessPageFromPortal(apiId, pageEntity)) {
            pageService.transformSwagger(pageEntity, apiId);
            if (!isAuthenticated() && pageEntity.getMetadata() != null) {
                pageEntity.getMetadata().clear();
            }
            Page page = pageMapper.convert(uriInfo.getBaseUriBuilder(), apiId, pageEntity);
            if (include.contains(INCLUDE_CONTENT)) {
                page.setContent(pageEntity.getContent());
            }
            page.setLinks(pageMapper.computePageLinks(PortalApiLinkHelper.apiPagesURL(uriInfo.getBaseUriBuilder(), apiId, pageId), PortalApiLinkHelper.apiPagesURL(uriInfo.getBaseUriBuilder(), apiId, page.getParent())));
            return Response.ok(page).build();
        } else {
            throw new UnauthorizedAccessException();
        }
    }
    throw new ApiNotFoundException(apiId);
}
Also used : PageEntity(io.gravitee.rest.api.model.PageEntity) ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) UnauthorizedAccessException(io.gravitee.rest.api.service.exceptions.UnauthorizedAccessException) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) Page(io.gravitee.rest.api.portal.rest.model.Page) RequirePortalAuth(io.gravitee.rest.api.portal.rest.security.RequirePortalAuth)

Aggregations

ApiQuery (io.gravitee.rest.api.model.api.ApiQuery)26 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)19 ApiNotFoundException (io.gravitee.rest.api.service.exceptions.ApiNotFoundException)17 RequirePortalAuth (io.gravitee.rest.api.portal.rest.security.RequirePortalAuth)8 Produces (javax.ws.rs.Produces)7 Response (javax.ws.rs.core.Response)7 RatingEntity (io.gravitee.rest.api.model.RatingEntity)6 Collectors (java.util.stream.Collectors)6 Inject (javax.inject.Inject)6 MediaType (io.gravitee.common.http.MediaType)5 Permissions (io.gravitee.rest.api.portal.rest.security.Permissions)5 GET (javax.ws.rs.GET)5 RatingNotFoundException (io.gravitee.rest.api.service.exceptions.RatingNotFoundException)4 Collections (java.util.Collections)4 PaginationParam (io.gravitee.rest.api.portal.rest.resource.param.PaginationParam)3 java.util (java.util)3 List (java.util.List)3 Context (javax.ws.rs.core.Context)3 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)2 NewRatingEntity (io.gravitee.rest.api.model.NewRatingEntity)2