@Test public void fail_if_more_than_100_project_keys() { List<String> keys = IntStream.rangeClosed(1, 101) .mapToObj(i -> db.components().insertPrivateProject()) .map(ComponentDto::getDbKey) .collect(Collectors.toList()); MetricDto metric = db.measures().insertMetric(); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("101 projects provided, more than maximum authorized (100)"); call(keys, singletonList(metric.getKey())); }
@Test public void provided_project() { ComponentDto project = db.components().insertPrivateProject(); userSession.addProjectPermission(UserRole.USER, project); MetricDto metric = db.measures().insertMetric(m -> m.setValueType("INT")); ComponentWsResponse response = newRequest(project.getKey(), metric.getKey()); assertThat(response.getMetrics().getMetricsCount()).isEqualTo(1); assertThat(response.getPeriods().getPeriodsCount()).isEqualTo(0); assertThat(response.getComponent().getKey()).isEqualTo(project.getDbKey()); }
@Test public void do_not_verify_permissions_if_user_is_root() { MetricDto metric = db.measures().insertMetric(m -> m.setValueType(FLOAT.name())); ComponentDto project1 = db.components().insertPrivateProject(db.getDefaultOrganization()); db.measures().insertLiveMeasure(project1, metric, m -> m.setValue(15.5d)); userSession.setNonRoot(); SearchWsResponse result = call(singletonList(project1.getDbKey()), singletonList(metric.getKey())); assertThat(result.getMeasuresCount()).isEqualTo(0); userSession.setRoot(); result = call(singletonList(project1.getDbKey()), singletonList(metric.getKey())); assertThat(result.getMeasuresCount()).isEqualTo(1); }
@Test public void fail_if_unknown_metric() { ComponentDto project = db.components().insertPrivateProject(); userSession.addProjectPermission(UserRole.USER, project); MetricDto metric = db.measures().insertMetric(); expectedException.expect(BadRequestException.class); expectedException.expectMessage("The following metrics are not found: ncloc, violations"); call(singletonList(project.getDbKey()), newArrayList("violations", metric.getKey(), "ncloc")); }
@Test public void countNcloc_empty() { db.measures().insertMetric(m -> m.setKey("ncloc").setValueType(INT.toString())); db.measures().insertMetric(m -> m.setKey("lines").setValueType(INT.toString())); SumNclocDbQuery query = SumNclocDbQuery.builder() .setOnlyPrivateProjects(false) .setOrganizationUuid(db.getDefaultOrganization().getUuid()) .build(); long result = underTest.sumNclocOfBiggestLongLivingBranch(db.getSession(), query); assertThat(result).isEqualTo(0L); }
@Test public void fail_if_module() { ComponentDto project = db.components().insertPrivateProject(); ComponentDto module = db.components().insertComponent(newModuleDto(project)); userSession.addProjectPermission(UserRole.USER, project); MetricDto metric = db.measures().insertMetric(); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Only component of qualifiers [TRK, APP, VW, SVW] are allowed"); call(singletonList(module.getDbKey()), singletonList(metric.getKey())); }
@Test public void fail_if_directory() { ComponentDto project = db.components().insertPrivateProject(); ComponentDto dir = db.components().insertComponent(newDirectory(project, "dir")); userSession.addProjectPermission(UserRole.USER, project); MetricDto metric = db.measures().insertMetric(); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Only component of qualifiers [TRK, APP, VW, SVW] are allowed"); call(singletonList(dir.getDbKey()), singletonList(metric.getKey())); }
@Before public void setUp() throws Exception { intMetric = db.measures().insertMetric(m -> m.setValueType(Metric.ValueType.INT.name())); ratingMetric = db.measures().insertMetric(m -> m.setValueType(Metric.ValueType.RATING.name())); alertStatusMetric = db.measures().insertMetric(m -> m.setKey(CoreMetrics.ALERT_STATUS_KEY)); organization = db.organizations().insert(); project = db.components().insertMainBranch(organization); dir = db.components().insertComponent(ComponentTesting.newDirectory(project, "src/main/java")); file1 = db.components().insertComponent(ComponentTesting.newFileDto(project, dir)); file2 = db.components().insertComponent(ComponentTesting.newFileDto(project, dir)); }
@Test public void filter_projects_by_no_duplication_should_not_return_projects_with_duplication() { userSession.logIn(); OrganizationDto organizationDto = db.organizations().insert(); MetricDto coverage = db.measures().insertMetric(c -> c.setKey(COVERAGE).setValueType("PERCENT")); MetricDto duplications = db.measures().insertMetric(c -> c.setKey(DUPLICATED_LINES_DENSITY_KEY).setValueType("PERCENT")); insertProject(organizationDto, new Measure(duplications, c -> c.setValue(10d)), new Measure(coverage, c -> c.setValue(50d))); SearchProjectsWsResponse result = call(request.setFilter("duplicated_lines_density = NO_DATA")); assertThat(result.getComponentsList()).extracting(Component::getKey).isEmpty(); }
@Test public void use_deprecated_component_id_parameter() { ComponentDto project = db.components().insertPrivateProject(); userSession.addProjectPermission(UserRole.USER, project); userSession.addProjectPermission(USER, project); MetricDto metric = db.measures().insertMetric(m -> m.setValueType("INT")); ComponentWsResponse response = ws.newRequest() .setParam("componentId", project.uuid()) .setParam(PARAM_METRIC_KEYS, metric.getKey()) .executeProtobuf(ComponentWsResponse.class); assertThat(response.getComponent().getKey()).isEqualTo(project.getDbKey()); }
@Test public void use_deprecated_component_key_parameter() { ComponentDto project = db.components().insertPrivateProject(); userSession.addProjectPermission(UserRole.USER, project); userSession.addProjectPermission(USER, project); MetricDto metric = db.measures().insertMetric(m -> m.setValueType("INT")); ComponentWsResponse response = ws.newRequest() .setParam("componentKey", project.getKey()) .setParam(PARAM_METRIC_KEYS, metric.getKey()) .executeProtobuf(ComponentWsResponse.class); assertThat(response.getComponent().getKey()).isEqualTo(project.getDbKey()); }
private void setNcloc(double ncloc) { ComponentDto project = db.components().insertMainBranch(); MetricDto nclocMetric = db.measures().insertMetric(m -> m.setValueType(INT.toString()).setKey(NCLOC_KEY)); db.measures().insertLiveMeasure(project, nclocMetric, m -> m.setValue(ncloc)); } }
@Test public void return_error_if_project_does_not_exist() throws ParseException { MetricDto metric = db.measures().insertMetric(m -> m.setKey(BUGS_KEY)); TestResponse response = ws.newRequest() .setParam("project", "unknown") .setParam("metric", metric.getKey()) .execute(); checkError(response, "Project has not been found"); }
@Test public void non_main_branches_are_not_indexed() { OrganizationDto organization = dbTester.organizations().insert(); ComponentDto project = dbTester.components().insertPrivateProject(organization); MetricDto metric = dbTester.measures().insertMetric(m -> m.setValueType(INT.name()).setKey("ncloc")); dbTester.measures().insertLiveMeasure(project, metric, m -> m.setValue(10d)); ComponentDto branch = dbTester.components().insertProjectBranch(project, b -> b.setKey("feature/foo")); dbTester.measures().insertLiveMeasure(branch, metric, m -> m.setValue(20d)); Map<String, ProjectMeasures> docsById = createResultSetAndReturnDocsById(); assertThat(docsById).hasSize(1).containsOnlyKeys(project.uuid()); assertThat(docsById.get(project.uuid()).getMeasures().getNumericMeasures().get(metric.getKey())).isEqualTo(10d); }
@Test public void fail_when_component_does_not_exist() { MetricDto metric = db.measures().insertMetric(m -> m.setValueType("INT")); expectedException.expect(NotFoundException.class); expectedException.expectMessage("Component key 'project-key' not found"); ws.newRequest() .setParam(PARAM_COMPONENT, "project-key") .setParam(PARAM_METRIC_KEYS, metric.getKey()) .execute(); }
@Test public void filter_projects_by_no_duplication() { userSession.logIn(); OrganizationDto organizationDto = db.organizations().insert(); MetricDto coverage = db.measures().insertMetric(c -> c.setKey(COVERAGE).setValueType("PERCENT")); MetricDto duplications = db.measures().insertMetric(c -> c.setKey(DUPLICATED_LINES_DENSITY_KEY).setValueType("PERCENT")); ComponentDto project1 = insertProject(organizationDto, new Measure(coverage, c -> c.setValue(10d))); ComponentDto project2 = insertProject(organizationDto, new Measure(duplications, c -> c.setValue(0d))); ComponentDto project3 = insertProject(organizationDto, new Measure(duplications, c -> c.setValue(79d))); SearchProjectsWsResponse result = call(request.setFilter("duplicated_lines_density = NO_DATA")); assertThat(result.getComponentsList()).extracting(Component::getKey).containsExactlyInAnyOrder(project1.getDbKey()); }
@Test public void selectMeasure_map_fields() { MetricDto metric = db.measures().insertMetric(); ComponentDto project = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); underTest.insert(db.getSession(), newLiveMeasure(file, metric).setValue(3.14).setVariation(0.1).setData("text_value")); LiveMeasureDto result = underTest.selectMeasure(db.getSession(), file.uuid(), metric.getKey()).orElseThrow(() -> new IllegalArgumentException("Measure not found")); assertThat(result).as("Fail to map fields of %s", result.toString()).extracting( LiveMeasureDto::getProjectUuid, LiveMeasureDto::getComponentUuid, LiveMeasureDto::getMetricId, LiveMeasureDto::getValue, LiveMeasureDto::getVariation, LiveMeasureDto::getDataAsString, LiveMeasureDto::getTextValue) .contains(project.uuid(), file.uuid(), metric.getId(), 3.14, 0.1, "text_value", "text_value"); }
@Test public void return_error_if_measure_not_found() throws ParseException { ComponentDto project = db.components().insertPublicProject(); userSession.registerComponents(project); MetricDto metric = db.measures().insertMetric(m -> m.setKey(BUGS_KEY)); TestResponse response = ws.newRequest() .setParam("project", project.getKey()) .setParam("metric", metric.getKey()) .execute(); checkError(response, "Measure has not been found"); }
@Test public void filter_projects_by_ncloc() { userSession.logIn(); OrganizationDto organizationDto = db.organizations().insert(); MetricDto ncloc = db.measures().insertMetric(c -> c.setKey(NCLOC).setValueType(INT.name())); ComponentDto project1 = insertProject(organizationDto, new Measure(ncloc, c -> c.setValue(80d))); ComponentDto project2 = insertProject(organizationDto, new Measure(ncloc, c -> c.setValue(85d))); ComponentDto project3 = insertProject(organizationDto, new Measure(ncloc, c -> c.setValue(10d))); SearchProjectsWsResponse result = call(request.setFilter("ncloc <= 80")); assertThat(result.getComponentsList()).extracting(Component::getKey).containsExactlyInAnyOrder(project1.getDbKey(), project3.getDbKey()); }
@Test public void filter_projects_by_languages() { userSession.logIn(); OrganizationDto organizationDto = db.organizations().insert(); MetricDto languagesDistribution = db.measures().insertMetric(c -> c.setKey(NCLOC_LANGUAGE_DISTRIBUTION_KEY).setValueType("DATA")); ComponentDto project1 = insertProject(organizationDto, new Measure(languagesDistribution, c -> c.setValue(null).setData("<null>=2;java=6;xoo=18"))); ComponentDto project2 = insertProject(organizationDto, new Measure(languagesDistribution, c -> c.setValue(null).setData("java=3;xoo=9"))); ComponentDto project3 = insertProject(organizationDto, new Measure(languagesDistribution, c -> c.setValue(null).setData("xoo=1"))); ComponentDto project4 = insertProject(organizationDto, new Measure(languagesDistribution, c -> c.setValue(null).setData("<null>=1;java=5;xoo=13"))); SearchProjectsWsResponse result = call(request.setFilter("languages IN (java, js, <null>)")); assertThat(result.getComponentsList()).extracting(Component::getKey).containsExactlyInAnyOrder(project1.getDbKey(), project2.getDbKey(), project4.getDbKey()); }