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