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