Search in sources :

Example 1 with ApiQualityRuleEntity

use of io.gravitee.rest.api.model.quality.ApiQualityRuleEntity 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 2 with ApiQualityRuleEntity

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

the class QualityMetricsServiceTest method shouldScore100PercentWithManualRules.

@Test
public void shouldScore100PercentWithManualRules() {
    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(1);
    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(true);
    when(apiQualityRuleService.findByApi("apiID")).thenReturn(singletonList(apiQualityRule));
    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()));
    assertTrue(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 3 with ApiQualityRuleEntity

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

the class QualityMetricsServiceImpl method getMetrics.

@Override
public ApiQualityMetricsEntity getMetrics(ApiEntity apiEntity) {
    if (!isApiMetricsEnabled()) {
        throw new ApiQualityMetricsDisableException();
    }
    Map<String, Integer> weights = getWeights().entrySet().stream().filter(e -> e.getValue() > 0).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    ApiQualityMetricsEntity result = new ApiQualityMetricsEntity();
    result.setMetricsPassed(new HashMap<>(weights.size()));
    double score = 0;
    double maxScore = 0;
    if (weights.isEmpty()) {
        result.setScore(1);
    } else {
        Map<String, ApiQualityMetric> apiMetrics = getApiMetricsMap();
        for (Map.Entry<String, Integer> weight : weights.entrySet()) {
            boolean passed = apiMetrics.get(weight.getKey()).isValid(apiEntity);
            result.getMetricsPassed().put(weight.getKey(), passed);
            score += weight.getValue() * (passed ? 1 : 0);
            maxScore += weight.getValue();
        }
        result.setScore((int) ((score / maxScore) * 100) / 100d);
    }
    // manual quality rules
    final List<QualityRuleEntity> qualityRules = qualityRuleService.findAll();
    if (qualityRules != null && !qualityRules.isEmpty()) {
        final List<ApiQualityRuleEntity> apiQualityRules = apiQualityRuleService.findByApi(apiEntity.getId());
        for (final QualityRuleEntity qualityRule : qualityRules) {
            if (qualityRule.getWeight() > 0) {
                final ApiQualityRuleEntity apiQualityRule = apiQualityRules.stream().filter(aqr -> apiEntity.getId().equals(aqr.getApi()) && qualityRule.getId().equals(aqr.getQualityRule())).findFirst().orElse(null);
                final boolean checked = apiQualityRule != null && apiQualityRule.isChecked();
                result.getMetricsPassed().put(qualityRule.getId(), checked);
                score += qualityRule.getWeight() * (checked ? 1 : 0);
                maxScore += qualityRule.getWeight();
                result.setScore((int) ((score / maxScore) * 100) / 100d);
            }
        }
    }
    return result;
}
Also used : ApiQualityMetricsEntity(io.gravitee.rest.api.model.ApiQualityMetricsEntity) ParameterService(io.gravitee.rest.api.service.ParameterService) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) QualityMetricsService(io.gravitee.rest.api.service.QualityMetricsService) ParameterReferenceType(io.gravitee.rest.api.model.parameters.ParameterReferenceType) QualityRuleService(io.gravitee.rest.api.service.QualityRuleService) ApiQualityMetric(io.gravitee.rest.api.service.quality.ApiQualityMetric) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) ApiQualityMetricsDisableException(io.gravitee.rest.api.service.exceptions.ApiQualityMetricsDisableException) Collectors(java.util.stream.Collectors) Key(io.gravitee.rest.api.model.parameters.Key) List(java.util.List) Component(org.springframework.stereotype.Component) ApiQualityRuleEntity(io.gravitee.rest.api.model.quality.ApiQualityRuleEntity) Map(java.util.Map) QualityRuleEntity(io.gravitee.rest.api.model.quality.QualityRuleEntity) ApiQualityMetricLoader(io.gravitee.rest.api.service.quality.ApiQualityMetricLoader) Collections(java.util.Collections) ApiQualityRuleService(io.gravitee.rest.api.service.ApiQualityRuleService) ApiQualityRuleEntity(io.gravitee.rest.api.model.quality.ApiQualityRuleEntity) QualityRuleEntity(io.gravitee.rest.api.model.quality.QualityRuleEntity) ApiQualityMetric(io.gravitee.rest.api.service.quality.ApiQualityMetric) ApiQualityRuleEntity(io.gravitee.rest.api.model.quality.ApiQualityRuleEntity) ApiQualityMetricsDisableException(io.gravitee.rest.api.service.exceptions.ApiQualityMetricsDisableException) ApiQualityMetricsEntity(io.gravitee.rest.api.model.ApiQualityMetricsEntity) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ApiQualityMetricsEntity (io.gravitee.rest.api.model.ApiQualityMetricsEntity)3 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)3 ParameterReferenceType (io.gravitee.rest.api.model.parameters.ParameterReferenceType)3 ApiQualityRuleEntity (io.gravitee.rest.api.model.quality.ApiQualityRuleEntity)3 QualityRuleEntity (io.gravitee.rest.api.model.quality.QualityRuleEntity)3 Collections.singletonList (java.util.Collections.singletonList)2 Function (java.util.function.Function)2 Test (org.junit.Test)2 ArgumentMatchers.anyList (org.mockito.ArgumentMatchers.anyList)2 Key (io.gravitee.rest.api.model.parameters.Key)1 ApiQualityRuleService (io.gravitee.rest.api.service.ApiQualityRuleService)1 ParameterService (io.gravitee.rest.api.service.ParameterService)1 QualityMetricsService (io.gravitee.rest.api.service.QualityMetricsService)1 QualityRuleService (io.gravitee.rest.api.service.QualityRuleService)1 ApiQualityMetricsDisableException (io.gravitee.rest.api.service.exceptions.ApiQualityMetricsDisableException)1 ApiQualityMetric (io.gravitee.rest.api.service.quality.ApiQualityMetric)1 ApiQualityMetricLoader (io.gravitee.rest.api.service.quality.ApiQualityMetricLoader)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1