@Test public void toString_is_override() { QualityGate underTest = new QualityGate(QUALIGATE_ID, QUALIGATE_NAME, ImmutableSet.of(CONDITION_2)); assertThat(underTest.toString()).isEqualTo("QualityGate{id=qg_id, name='qg_name', conditions=[" + "Condition{metricKey='m2', operator=LESS_THAN, errorThreshold='2'}" + "]}"); }
@Test public void equals_is_based_on_all_fields() { assertThat(underTest).isEqualTo(underTest); assertThat(underTest).isEqualTo(new QualityGate(QUALIGATE_ID, QUALIGATE_NAME, ImmutableSet.of(CONDITION_2, CONDITION_1))); assertThat(underTest).isNotEqualTo(null); assertThat(underTest).isNotEqualTo(new Object()); assertThat(underTest).isNotEqualTo(new QualityGate("other_id", QUALIGATE_NAME, ImmutableSet.of(CONDITION_2, CONDITION_1))); assertThat(underTest).isNotEqualTo(new QualityGate(QUALIGATE_ID, "other_name", ImmutableSet.of(CONDITION_2, CONDITION_1))); assertThat(underTest).isNotEqualTo(new QualityGate(QUALIGATE_ID, QUALIGATE_NAME, emptySet())); assertThat(underTest).isNotEqualTo(new QualityGate(QUALIGATE_ID, QUALIGATE_NAME, ImmutableSet.of(CONDITION_1))); assertThat(underTest).isNotEqualTo(new QualityGate(QUALIGATE_ID, QUALIGATE_NAME, ImmutableSet.of(CONDITION_2))); assertThat(underTest).isNotEqualTo( new QualityGate(QUALIGATE_ID, QUALIGATE_NAME, ImmutableSet.of(CONDITION_1, CONDITION_2, new Condition("new", Condition.Operator.GREATER_THAN, "a")))); }
@Override public QualityGate loadQualityGate(DbSession dbSession, OrganizationDto organization, ComponentDto project, BranchDto branch) { if (branch.getBranchType() == BranchType.SHORT || branch.getBranchType() == BranchType.PULL_REQUEST) { return ShortLivingBranchQualityGate.GATE; } ComponentDto mainProject = project.getMainBranchProjectUuid() == null ? project : dbClient.componentDao().selectOrFailByKey(dbSession, project.getKey()); QualityGateDto gateDto = qGateFinder.getQualityGate(dbSession, organization, mainProject) .orElseThrow(() -> new IllegalStateException(format("Quality Gate not found for project %s", mainProject.getKey()))) .getQualityGate(); Collection<QualityGateConditionDto> conditionDtos = dbClient.gateConditionDao().selectForQualityGate(dbSession, gateDto.getId()); Set<Integer> metricIds = conditionDtos.stream().map(c -> (int) c.getMetricId()) .collect(toHashSet(conditionDtos.size())); Map<Integer, MetricDto> metricsById = dbClient.metricDao().selectByIds(dbSession, metricIds).stream() .collect(uniqueIndex(MetricDto::getId)); Set<Condition> conditions = conditionDtos.stream().map(conditionDto -> { String metricKey = metricsById.get((int) conditionDto.getMetricId()).getKey(); Condition.Operator operator = Condition.Operator.fromDbValue(conditionDto.getOperator()); return new Condition(metricKey, operator, conditionDto.getErrorThreshold()); }).collect(toHashSet(conditionDtos.size())); return new QualityGate(String.valueOf(gateDto.getId()), gateDto.getName(), conditions); }
@Test public void constructor_fails_with_NPE_if_conditions_contains_null() { expectedException.expect(NullPointerException.class); expectedException.expectMessage("condition can't be null"); Random random = new Random(); Set<Condition> conditions = Stream.of( IntStream.range(0, random.nextInt(5)) .mapToObj(i -> new Condition("m_before_" + i, Condition.Operator.GREATER_THAN, "10")), Stream.of((Condition) null), IntStream.range(0, random.nextInt(5)) .mapToObj(i -> new Condition("m_after_" + i, Condition.Operator.GREATER_THAN, "10"))) .flatMap(s -> s) .collect(Collectors.toSet()); expectedException.expect(NullPointerException.class); expectedException.expectMessage("condition can't be null"); new QualityGate("id", "name", conditions); }
@Test public void constructor_fails_with_NPE_if_name_is_null() { expectedException.expect(NullPointerException.class); expectedException.expectMessage("name can't be null"); new QualityGate("id", null, emptySet()); }
@Test public void hashcode_is_based_on_all_fields() { assertThat(underTest.hashCode()).isEqualTo(underTest.hashCode()); assertThat(underTest.hashCode()).isEqualTo(new QualityGate(QUALIGATE_ID, QUALIGATE_NAME, ImmutableSet.of(CONDITION_2, CONDITION_1)).hashCode()); assertThat(underTest.hashCode()).isNotEqualTo(null); assertThat(underTest.hashCode()).isNotEqualTo(new Object().hashCode()); assertThat(underTest.hashCode()).isNotEqualTo(new QualityGate("other_id", QUALIGATE_NAME, ImmutableSet.of(CONDITION_2, CONDITION_1)).hashCode()); assertThat(underTest.hashCode()).isNotEqualTo(new QualityGate(QUALIGATE_ID, "other_name", ImmutableSet.of(CONDITION_2, CONDITION_1)).hashCode()); assertThat(underTest.hashCode()).isNotEqualTo(new QualityGate(QUALIGATE_ID, QUALIGATE_NAME, emptySet()).hashCode()); assertThat(underTest.hashCode()).isNotEqualTo(new QualityGate(QUALIGATE_ID, QUALIGATE_NAME, ImmutableSet.of(CONDITION_1)).hashCode()); assertThat(underTest.hashCode()).isNotEqualTo(new QualityGate(QUALIGATE_ID, QUALIGATE_NAME, ImmutableSet.of(CONDITION_2)).hashCode()); assertThat(underTest.hashCode()).isNotEqualTo( new QualityGate(QUALIGATE_ID, QUALIGATE_NAME, ImmutableSet.of(CONDITION_1, CONDITION_2, new Condition("new", Condition.Operator.GREATER_THAN, "a"))).hashCode()); } }
@Test public void constructor_fails_with_NPE_if_id_is_null() { expectedException.expect(NullPointerException.class); expectedException.expectMessage("id can't be null"); new QualityGate(null, "name", emptySet()); }
@Test public void getMetricsRelatedTo() { Condition condition = new Condition("metric1", Condition.Operator.GREATER_THAN, "10"); QualityGate gate = new QualityGate("1", "foo", ImmutableSet.of(condition)); Set<String> result = underTest.getMetricsRelatedTo(gate); assertThat(result).containsExactlyInAnyOrder( // the metrics needed to compute the status of gate condition.getMetricKey(), // generated metrics CoreMetrics.ALERT_STATUS_KEY, CoreMetrics.QUALITY_GATE_DETAILS_KEY); }
@Test public void constructor_fails_with_NPE_if_conditions_is_null() { expectedException.expect(NullPointerException.class); expectedException.expectMessage("conditions can't be null"); new QualityGate("id", "name", null); }
@Test public void verify_getters_when_multiple_conditions() { QualityGate qualityGate = new QualityGate(QUALITY_GATE_ID, QUALITY_GATE_NAME, ImmutableSet.of(CONDITION_1, CONDITION_2)); EvaluatedQualityGate underTest = builder .setQualityGate(qualityGate) .setStatus(randomStatus) .addCondition(CONDITION_1, randomEvaluationStatus, randomValue) .addCondition(CONDITION_2, EvaluatedCondition.EvaluationStatus.ERROR, "bad") .build(); assertThat(underTest.getQualityGate()).isEqualTo(qualityGate); assertThat(underTest.getStatus()).isEqualTo(randomStatus); assertThat(underTest.getEvaluatedConditions()).containsOnly( new EvaluatedCondition(CONDITION_1, randomEvaluationStatus, randomValue), new EvaluatedCondition(CONDITION_2, EvaluatedCondition.EvaluationStatus.ERROR, "bad")); }
private static org.sonar.server.webhook.ProjectAnalysis convert(ProjectAnalysis projectAnalysis) { CeTask ceTask = new CeTask(projectAnalysis.getCeTask().getId(), CeTask.Status.valueOf(projectAnalysis.getCeTask().getStatus().name())); Analysis analysis = projectAnalysis.getAnalysis().map(a -> new Analysis(a.getAnalysisUuid(), a.getDate().getTime())).orElse(null); Branch branch = projectAnalysis.getBranch().map(b -> new Branch(b.isMain(), b.getName().orElse(null), Branch.Type.valueOf(b.getType().name()))).orElse(null); EvaluatedQualityGate qualityGate = Optional.ofNullable(projectAnalysis.getQualityGate()) .map(qg -> { EvaluatedQualityGate.Builder builder = EvaluatedQualityGate.newBuilder(); Set<Condition> conditions = qg.getConditions().stream() .map(q -> { Condition condition = new Condition(q.getMetricKey(), Condition.Operator.valueOf(q.getOperator().name()), q.getErrorThreshold()); builder.addCondition(condition, EvaluatedCondition.EvaluationStatus.valueOf(q.getStatus().name()), q.getStatus() == org.sonar.api.ce.posttask.QualityGate.EvaluationStatus.NO_VALUE ? null : q.getValue()); return condition; }) .collect(MoreCollectors.toSet()); return builder.setQualityGate(new org.sonar.server.qualitygate.QualityGate(qg.getId(), qg.getName(), conditions)) .setStatus(Metric.Level.valueOf(qg.getStatus().name())) .build(); }) .orElse(null); Long date = projectAnalysis.getAnalysis().map(a -> a.getDate().getTime()).orElse(null); Map<String, String> properties = projectAnalysis.getScannerContext().getProperties(); Project project = new Project(projectAnalysis.getProject().getUuid(), projectAnalysis.getProject().getKey(), projectAnalysis.getProject().getName()); return new org.sonar.server.webhook.ProjectAnalysis(project, ceTask, analysis, branch, qualityGate, date, properties); } }
@Test public void equals_is_based_on_all_fields() { EvaluatedQualityGate.Builder builder = this.builder .setQualityGate(ONE_CONDITION_QUALITY_GATE) .setStatus(Level.ERROR) .addCondition(CONDITION_1, EvaluatedCondition.EvaluationStatus.ERROR, "foo"); EvaluatedQualityGate underTest = builder.build(); assertThat(underTest).isEqualTo(builder.build()); assertThat(underTest).isNotSameAs(builder.build()); assertThat(underTest).isNotEqualTo(null); assertThat(underTest).isNotEqualTo(new Object()); assertThat(underTest).isNotEqualTo(builder.setQualityGate(new QualityGate("other_id", QUALITY_GATE_NAME, singleton(CONDITION_1))).build()); assertThat(underTest).isNotEqualTo(builder.setQualityGate(ONE_CONDITION_QUALITY_GATE).setStatus(Level.OK).build()); assertThat(underTest).isNotEqualTo(newBuilder() .setQualityGate(ONE_CONDITION_QUALITY_GATE) .setStatus(Level.ERROR) .addCondition(CONDITION_1, EvaluatedCondition.EvaluationStatus.OK, "foo") .build()); }
@Test public void refreshGateStatus_generates_gate_related_measures() { ComponentDto project = ComponentTesting.newPublicProjectDto(newOrganizationDto()); MetricDto conditionMetric = newMetricDto(); MetricDto statusMetric = newMetricDto().setKey(CoreMetrics.ALERT_STATUS_KEY); MetricDto detailsMetric = newMetricDto().setKey(CoreMetrics.QUALITY_GATE_DETAILS_KEY); Condition condition = new Condition(conditionMetric.getKey(), Condition.Operator.GREATER_THAN, "10"); QualityGate gate = new QualityGate("1", "foo", ImmutableSet.of(condition)); MeasureMatrix matrix = new MeasureMatrix(singleton(project), asList(conditionMetric, statusMetric, detailsMetric), emptyList()); EvaluatedQualityGate result = underTest.refreshGateStatus(project, gate, matrix); QualityGateEvaluator.Measures measures = qualityGateEvaluator.getCalledMeasures(); assertThat(measures.get(conditionMetric.getKey())).isEmpty(); assertThat(result.getStatus()).isEqualTo(Metric.Level.OK); assertThat(result.getEvaluatedConditions()) .extracting(EvaluatedCondition::getStatus) .containsExactly(EvaluatedCondition.EvaluationStatus.OK); assertThat(matrix.getMeasure(project, CoreMetrics.ALERT_STATUS_KEY).get().getDataAsString()).isEqualTo(Metric.Level.OK.name()); assertThat(matrix.getMeasure(project, CoreMetrics.QUALITY_GATE_DETAILS_KEY).get().getDataAsString()) .isNotEmpty() // json format .startsWith("{").endsWith("}"); }
@Test public void hashcode_is_based_on_all_fields() { EvaluatedQualityGate.Builder builder = this.builder .setQualityGate(ONE_CONDITION_QUALITY_GATE) .setStatus(Level.ERROR) .addCondition(CONDITION_1, EvaluatedCondition.EvaluationStatus.ERROR, "foo"); EvaluatedQualityGate underTest = builder.build(); assertThat(underTest.hashCode()).isEqualTo(builder.build().hashCode()); assertThat(underTest.hashCode()).isNotSameAs(builder.build().hashCode()); assertThat(underTest.hashCode()).isNotEqualTo(null); assertThat(underTest.hashCode()).isNotEqualTo(new Object().hashCode()); assertThat(underTest.hashCode()).isNotEqualTo(builder.setQualityGate(new QualityGate("other_id", QUALITY_GATE_NAME, singleton(CONDITION_1))).build().hashCode()); assertThat(underTest.hashCode()).isNotEqualTo(builder.setQualityGate(ONE_CONDITION_QUALITY_GATE).setStatus(Level.OK).build().hashCode()); assertThat(underTest.hashCode()).isNotEqualTo(newBuilder() .setQualityGate(ONE_CONDITION_QUALITY_GATE) .setStatus(Level.ERROR) .addCondition(CONDITION_1, EvaluatedCondition.EvaluationStatus.OK, "foo") .build().hashCode()); } }
@Test public void refreshGateStatus_provides_measures_to_evaluator() { ComponentDto project = ComponentTesting.newPublicProjectDto(newOrganizationDto()); MetricDto numericMetric = newMetricDto().setValueType(Metric.ValueType.FLOAT.name()); MetricDto numericNewMetric = newMetricDto().setValueType(Metric.ValueType.FLOAT.name()).setKey("new_metric"); MetricDto stringMetric = newMetricDto().setValueType(Metric.ValueType.STRING.name()); MetricDto statusMetric = newMetricDto().setKey(CoreMetrics.ALERT_STATUS_KEY); MetricDto detailsMetric = newMetricDto().setKey(CoreMetrics.QUALITY_GATE_DETAILS_KEY); QualityGate gate = new QualityGate("1", "foo", Collections.emptySet()); LiveMeasureDto numericMeasure = new LiveMeasureDto().setMetricId(numericMetric.getId()).setValue(1.23).setVariation(4.56).setComponentUuid(project.uuid()); LiveMeasureDto numericNewMeasure = new LiveMeasureDto().setMetricId(numericNewMetric.getId()).setValue(7.8).setVariation(8.9).setComponentUuid(project.uuid()); LiveMeasureDto stringMeasure = new LiveMeasureDto().setMetricId(stringMetric.getId()).setData("bar").setComponentUuid(project.uuid()); MeasureMatrix matrix = new MeasureMatrix(singleton(project), asList(statusMetric, detailsMetric, numericMetric, numericNewMetric, stringMetric), asList(numericMeasure, numericNewMeasure, stringMeasure)); underTest.refreshGateStatus(project, gate, matrix); QualityGateEvaluator.Measures measures = qualityGateEvaluator.getCalledMeasures(); QualityGateEvaluator.Measure loadedStringMeasure = measures.get(stringMetric.getKey()).get(); assertThat(loadedStringMeasure.getStringValue()).hasValue("bar"); assertThat(loadedStringMeasure.getValue()).isEmpty(); assertThat(loadedStringMeasure.getType()).isEqualTo(Metric.ValueType.STRING); QualityGateEvaluator.Measure loadedNumericMeasure = measures.get(numericMetric.getKey()).get(); assertThat(loadedNumericMeasure.getStringValue()).isEmpty(); assertThat(loadedNumericMeasure.getValue()).hasValue(1.23); assertThat(loadedNumericMeasure.getType()).isEqualTo(Metric.ValueType.FLOAT); QualityGateEvaluator.Measure loadedNumericNewMeasure = measures.get(numericNewMetric.getKey()).get(); assertThat(loadedNumericNewMeasure.getStringValue()).isEmpty(); assertThat(loadedNumericNewMeasure.getNewMetricValue()).hasValue(8.9); assertThat(loadedNumericNewMeasure.getType()).isEqualTo(Metric.ValueType.FLOAT); }
condition.getErrorThreshold()); webQualityGate = EvaluatedQualityGate.newBuilder() .setQualityGate(new org.sonar.server.qualitygate.QualityGate(qualityGate.getId(), qualityGate.getName(), Collections.singleton(qgCondition))) .setStatus(Metric.Level.valueOf(qualityGate.getStatus().name())) .addCondition(qgCondition, EvaluatedCondition.EvaluationStatus.valueOf(condition.getStatus().name()), condition.getValue())
@Override public QualityGate loadQualityGate(DbSession dbSession, OrganizationDto organization, ComponentDto project, BranchDto branch) { if (branch.getBranchType() == BranchType.SHORT || branch.getBranchType() == BranchType.PULL_REQUEST) { return ShortLivingBranchQualityGate.GATE; } ComponentDto mainProject = project.getMainBranchProjectUuid() == null ? project : dbClient.componentDao().selectOrFailByKey(dbSession, project.getKey()); QualityGateDto gateDto = qGateFinder.getQualityGate(dbSession, organization, mainProject).getQualityGate(); Collection<QualityGateConditionDto> conditionDtos = dbClient.gateConditionDao().selectForQualityGate(dbSession, gateDto.getId()); Set<Integer> metricIds = conditionDtos.stream().map(c -> (int) c.getMetricId()) .collect(toHashSet(conditionDtos.size())); Map<Integer, MetricDto> metricsById = dbClient.metricDao().selectByIds(dbSession, metricIds).stream() .collect(uniqueIndex(MetricDto::getId)); Set<Condition> conditions = conditionDtos.stream().map(conditionDto -> { String metricKey = metricsById.get((int) conditionDto.getMetricId()).getKey(); Condition.Operator operator = Condition.Operator.fromDbValue(conditionDto.getOperator()); boolean onLeak = Objects.equals(conditionDto.getPeriod(), 1); return new Condition(metricKey, operator, conditionDto.getErrorThreshold(), conditionDto.getWarningThreshold(), onLeak); }).collect(toHashSet(conditionDtos.size())); return new QualityGate(String.valueOf(gateDto.getId()), gateDto.getName(), conditions); }
private static org.sonar.server.webhook.ProjectAnalysis convert(ProjectAnalysis projectAnalysis) { CeTask ceTask = new CeTask(projectAnalysis.getCeTask().getId(), CeTask.Status.valueOf(projectAnalysis.getCeTask().getStatus().name())); Project project = new Project(projectAnalysis.getProject().getUuid(), projectAnalysis.getProject().getKey(), projectAnalysis.getProject().getName()); Analysis analysis = projectAnalysis.getAnalysis().map(a -> new Analysis(a.getAnalysisUuid(), a.getDate().getTime())).orElse(null); Branch branch = projectAnalysis.getBranch().map(b -> new Branch(b.isMain(), b.getName().orElse(null), Branch.Type.valueOf(b.getType().name()))).orElse(null); EvaluatedQualityGate qualityGate = Optional.ofNullable(projectAnalysis.getQualityGate()) .map(qg -> { EvaluatedQualityGate.Builder builder = EvaluatedQualityGate.newBuilder(); Set<Condition> conditions = qg.getConditions().stream() .map(q -> { Condition condition = new Condition(q.getMetricKey(), Condition.Operator.valueOf(q.getOperator().name()), q.getErrorThreshold(), q.getWarningThreshold(), q.isOnLeakPeriod()); builder.addCondition(condition, EvaluatedCondition.EvaluationStatus.valueOf(q.getStatus().name()), q.getStatus() == org.sonar.api.ce.posttask.QualityGate.EvaluationStatus.NO_VALUE ? null : q.getValue()); return condition; }) .collect(MoreCollectors.toSet()); return builder.setQualityGate(new org.sonar.server.qualitygate.QualityGate(qg.getId(), qg.getName(), conditions)) .setStatus(Metric.Level.valueOf(qg.getStatus().name())) .build(); }) .orElse(null); Long date = projectAnalysis.getAnalysis().map(a -> a.getDate().getTime()).orElse(null); Map<String, String> properties = projectAnalysis.getScannerContext().getProperties(); return new org.sonar.server.webhook.ProjectAnalysis(project, ceTask, analysis, branch, qualityGate, date, properties); } }