Search in sources :

Example 6 with ApiEntity

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

the class FilteringServiceImpl method getCurrentUserSubscribedApis.

private FilteredEntities<ApiEntity> getCurrentUserSubscribedApis(Collection<ApiEntity> apis, boolean excluded) {
    // get Current user applications
    List<String> currentUserApplicationsId = applicationService.findByUser(getAuthenticatedUser().getUsername()).stream().map(ApplicationListItem::getId).collect(Collectors.toList());
    // find all subscribed apis for these applications
    SubscriptionQuery subscriptionQuery = new SubscriptionQuery();
    subscriptionQuery.setApplications(currentUserApplicationsId);
    List<String> subscribedApis = subscriptionService.search(subscriptionQuery).stream().map(SubscriptionEntity::getApi).distinct().collect(Collectors.toList());
    // filter apis list with subscribed apis list
    return new FilteredEntities<>(apis.stream().filter(api -> (!excluded && subscribedApis.contains(api.getId())) || (excluded && !subscribedApis.contains(api.getId()))).sorted((a1, a2) -> String.CASE_INSENSITIVE_ORDER.compare(a1.getName(), a2.getName())).collect(Collectors.toList()), null);
}
Also used : ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) java.util(java.util) TopApiService(io.gravitee.rest.api.service.TopApiService) SubscriptionStatus(io.gravitee.rest.api.model.SubscriptionStatus) RatingService(io.gravitee.rest.api.service.RatingService) Autowired(org.springframework.beans.factory.annotation.Autowired) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) FilterableItem(io.gravitee.rest.api.model.filtering.FilterableItem) SubscriptionService(io.gravitee.rest.api.service.SubscriptionService) Component(org.springframework.stereotype.Component) RatingSummaryEntity(io.gravitee.rest.api.model.RatingSummaryEntity) ApplicationService(io.gravitee.rest.api.service.ApplicationService) AbstractService(io.gravitee.rest.api.service.impl.AbstractService) TopApiEntity(io.gravitee.rest.api.model.TopApiEntity) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery) FilteringService(io.gravitee.rest.api.service.filtering.FilteringService) FilteredEntities(io.gravitee.rest.api.model.filtering.FilteredEntities) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) FilteredEntities(io.gravitee.rest.api.model.filtering.FilteredEntities) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery)

Example 7 with ApiEntity

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

the class PromotionTasksServiceImpl method convert.

private TaskEntity convert(PromotionEntity promotionEntity, boolean isUpdate, Optional<String> foundTargetApiId) {
    TaskEntity taskEntity = new TaskEntity();
    taskEntity.setType(TaskType.PROMOTION_APPROVAL);
    taskEntity.setCreatedAt(promotionEntity.getCreatedAt());
    ApiEntity apiEntity;
    try {
        apiEntity = objectMapper.readValue(promotionEntity.getApiDefinition(), ApiEntity.class);
    } catch (JsonProcessingException e) {
        logger.warn("Problem while deserializing api definition for promotion {}", promotionEntity.getId());
        throw new TechnicalManagementException();
    }
    Map<String, Object> data = new HashMap<>();
    data.put("apiName", apiEntity.getName());
    data.put("apiId", promotionEntity.getApiId());
    data.put("sourceEnvironmentName", promotionEntity.getSourceEnvName());
    data.put("targetEnvironmentName", promotionEntity.getTargetEnvName());
    data.put("authorDisplayName", promotionEntity.getAuthor().getDisplayName());
    data.put("authorEmail", promotionEntity.getAuthor().getEmail());
    data.put("authorPicture", promotionEntity.getAuthor().getPicture());
    data.put("promotionId", promotionEntity.getId());
    data.put("isApiUpdate", isUpdate);
    foundTargetApiId.ifPresent(targetApiId -> data.put("targetApiId", targetApiId));
    taskEntity.setData(data);
    return taskEntity;
}
Also used : TaskEntity(io.gravitee.rest.api.model.TaskEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 8 with ApiEntity

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

the class QualityMetricsServiceTest method shouldScore50PercentWithManualRules.

@Test
public void shouldScore50PercentWithManualRules() {
    when(parameterService.findAsBoolean(Key.API_QUALITY_METRICS_ENABLED, ParameterReferenceType.ENVIRONMENT)).thenReturn(Boolean.TRUE);
    Map<String, List<Object>> map = new HashMap<>();
    map.put(Key.API_QUALITY_METRICS_LOGO_WEIGHT.key(), singletonList(1));
    map.put(Key.API_QUALITY_METRICS_CATEGORIES_WEIGHT.key(), singletonList(1));
    when(parameterService.findAll(anyList(), any(Function.class), any(ParameterReferenceType.class))).thenReturn(map);
    ApiEntity api = mock(ApiEntity.class);
    when(api.getId()).thenReturn("apiID");
    when(apiQualityMetricLogo.isValid(any())).thenReturn(Boolean.TRUE);
    when(apiQualityMetricCategories.isValid(any())).thenReturn(Boolean.TRUE);
    final QualityRuleEntity qualityRule = mock(QualityRuleEntity.class);
    when(qualityRule.getId()).thenReturn("1");
    when(qualityRule.getWeight()).thenReturn(2);
    when(qualityRuleService.findAll()).thenReturn(singletonList(qualityRule));
    final ApiQualityRuleEntity apiQualityRule = mock(ApiQualityRuleEntity.class);
    when(apiQualityRule.getApi()).thenReturn("apiID");
    when(apiQualityRule.getQualityRule()).thenReturn("1");
    when(apiQualityRule.isChecked()).thenReturn(false);
    when(apiQualityRuleService.findByApi("apiID")).thenReturn(singletonList(apiQualityRule));
    ApiQualityMetricsEntity metrics = srv.getMetrics(api);
    assertEquals(0.5, metrics.getScore(), 0);
    assertFalse(metrics.getMetricsPassed().isEmpty());
    assertTrue(metrics.getMetricsPassed().get(Key.API_QUALITY_METRICS_LOGO_WEIGHT.key()));
    assertTrue(metrics.getMetricsPassed().get(Key.API_QUALITY_METRICS_CATEGORIES_WEIGHT.key()));
    assertFalse(metrics.getMetricsPassed().get("1"));
}
Also used : Function(java.util.function.Function) ParameterReferenceType(io.gravitee.rest.api.model.parameters.ParameterReferenceType) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) QualityRuleEntity(io.gravitee.rest.api.model.quality.QualityRuleEntity) ApiQualityRuleEntity(io.gravitee.rest.api.model.quality.ApiQualityRuleEntity) ApiQualityMetricsEntity(io.gravitee.rest.api.model.ApiQualityMetricsEntity) Collections.singletonList(java.util.Collections.singletonList) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) ApiQualityRuleEntity(io.gravitee.rest.api.model.quality.ApiQualityRuleEntity) Test(org.junit.Test)

Example 9 with ApiEntity

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

the class QualityMetricsServiceTest method shouldScore100Percent.

@Test
public void shouldScore100Percent() {
    when(parameterService.findAsBoolean(Key.API_QUALITY_METRICS_ENABLED, ParameterReferenceType.ENVIRONMENT)).thenReturn(Boolean.TRUE);
    Map<String, List<Object>> map = new HashMap<>();
    map.put(Key.API_QUALITY_METRICS_LOGO_WEIGHT.key(), singletonList(1));
    map.put(Key.API_QUALITY_METRICS_CATEGORIES_WEIGHT.key(), singletonList(1));
    when(parameterService.findAll(anyList(), any(Function.class), any(ParameterReferenceType.class))).thenReturn(map);
    ApiEntity api = mock(ApiEntity.class);
    when(apiQualityMetricLogo.isValid(any())).thenReturn(Boolean.TRUE);
    when(apiQualityMetricCategories.isValid(any())).thenReturn(Boolean.TRUE);
    ApiQualityMetricsEntity metrics = srv.getMetrics(api);
    assertEquals(1, metrics.getScore(), 0);
    assertFalse(metrics.getMetricsPassed().isEmpty());
    assertTrue(metrics.getMetricsPassed().get(Key.API_QUALITY_METRICS_LOGO_WEIGHT.key()));
    assertTrue(metrics.getMetricsPassed().get(Key.API_QUALITY_METRICS_CATEGORIES_WEIGHT.key()));
}
Also used : Function(java.util.function.Function) ParameterReferenceType(io.gravitee.rest.api.model.parameters.ParameterReferenceType) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) ApiQualityMetricsEntity(io.gravitee.rest.api.model.ApiQualityMetricsEntity) Collections.singletonList(java.util.Collections.singletonList) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) Test(org.junit.Test)

Example 10 with ApiEntity

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

the class QualityMetricsServiceTest method shouldThrowExceptionIfDisabled.

@Test(expected = ApiQualityMetricsDisableException.class)
public void shouldThrowExceptionIfDisabled() {
    when(parameterService.findAsBoolean(Key.API_QUALITY_METRICS_ENABLED, ParameterReferenceType.ENVIRONMENT)).thenReturn(Boolean.FALSE);
    ApiEntity api = mock(ApiEntity.class);
    srv.getMetrics(api);
    fail();
}
Also used : ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Test(org.junit.Test)

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