private void processEvents(DbSession session, Component component, Long analysisDate) { Function<Event, EventDto> eventToEventDto = event -> newBaseEvent(component, analysisDate) .setName(event.getName()) .setCategory(convertCategory(event.getCategory())) .setDescription(event.getDescription()) .setData(event.getData()); // FIXME bulk insert for (EventDto batchEventDto : StreamSupport.stream(eventRepository.getEvents(component).spliterator(), false).map(eventToEventDto).collect(Collectors.toList())) { dbClient.eventDao().insert(session, batchEventDto); } }
private void saveVersionEvent(DbSession session, Component component, Long analysisDate) { String version = component.getProjectAttributes().getVersion(); deletePreviousEventsHavingSameVersion(session, version, component); dbClient.eventDao().insert(session, newBaseEvent(component, analysisDate) .setName(version) .setCategory(EventDto.CATEGORY_VERSION)); }
@Test public void ws_response_with_updated_name() { SnapshotDto analysis = createAnalysisAndLogInAsProjectAdministrator("5.6"); EventDto originalEvent = db.events().insertEvent(newEvent(analysis).setUuid("E1").setName("Original Name")); ProjectAnalyses.Event result = call("E1", "name").getEvent(); assertThat(result.getName()).isEqualTo("name"); assertThat(result.hasDescription()).isFalse(); assertThat(result.getCategory()).isEqualTo(OTHER.name()); assertThat(result.getAnalysis()).isEqualTo(originalEvent.getAnalysisUuid()); assertThat(result.getKey()).isEqualTo("E1"); }
@Test public void update_name_in_db() { SnapshotDto analysis = createAnalysisAndLogInAsProjectAdministrator("5.6"); EventDto originalEvent = db.events().insertEvent(newEvent(analysis).setUuid("E1").setName("Original Name")); call("E1", "name"); EventDto newEvent = dbClient.eventDao().selectByUuid(dbSession, "E1").get(); assertThat(newEvent.getName()).isEqualTo("name"); assertThat(newEvent.getDescription()).isNull(); assertThat(newEvent.getCategory()).isEqualTo(originalEvent.getCategory()); assertThat(newEvent.getDate()).isEqualTo(originalEvent.getDate()); assertThat(newEvent.getCreatedAt()).isEqualTo(originalEvent.getCreatedAt()); }
private EventDto toDbEvent(CreateEventRequest request, SnapshotDto analysis) { checkArgument(isNotBlank(request.getName()), "A non empty name is required"); return new EventDto() .setUuid(uuidFactory.create()) .setAnalysisUuid(analysis.getUuid()) .setComponentUuid(analysis.getComponentUuid()) .setCategory(request.getCategory().getLabel()) .setName(request.getName()) .setCreatedAt(system.now()) .setDate(analysis.getCreatedAt()); }
@Test public void update_name_only_in_db() { SnapshotDto analysis = createAnalysisAndLogInAsProjectAdministrator("5.6"); EventDto originalEvent = db.events().insertEvent(newEvent(analysis).setUuid("E1").setName("Original Name").setDescription("Original Description")); call("E1", "name"); EventDto newEvent = dbClient.eventDao().selectByUuid(dbSession, "E1").get(); assertThat(newEvent.getName()).isEqualTo("name"); assertThat(newEvent.getDescription()).isEqualTo(originalEvent.getDescription()); }
@Test public void limit_version_name_length_to_100_for_analysis_events() { SnapshotDto analysis = createAnalysisAndLogInAsProjectAdministrator("5.6"); db.events().insertEvent(newEvent(analysis).setUuid("E1").setCategory(OTHER.getLabel()).setName("E1 name")); db.events().insertEvent(newEvent(analysis).setUuid("E2").setCategory(VERSION.getLabel()).setName("E2 name")); call("E1", repeat("a", 100)); call("E1", repeat("a", 101)); call("E2", repeat("a", 100)); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Version length (101) is longer than the maximum authorized (100). 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' was provided"); call("E2", repeat("a", 101)); }
public static EventDto newEvent(SnapshotDto analysis) { requireNonNull(analysis.getUuid()); requireNonNull(analysis.getComponentUuid()); return new EventDto() .setAnalysisUuid(analysis.getUuid()) .setComponentUuid(analysis.getComponentUuid()) .setUuid(randomAlphanumeric(40)) .setName(randomAlphanumeric(400)) .setDescription(null) .setCategory("Other") .setComponentUuid(analysis.getComponentUuid()) .setCreatedAt(System.currentTimeMillis()) .setDate(System.currentTimeMillis()); } }
@Test public void feed_period_by_previous_version_with_first_analysis_when_previous_snapshot_is_the_last_one() { OrganizationDto organization = dbTester.organizations().insert(); ComponentDto project = dbTester.components().insertPrivateProject(organization); SnapshotDto analysis = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1226379600000L).setVersion("0.9").setLast(true)); // 2008-11-11 dbTester.events().insertEvent(newEvent(analysis).setName("0.9").setCategory(CATEGORY_VERSION)); when(system2Mock.now()).thenReturn(november30th2008.getTime()); when(analysisMetadataHolder.isFirstAnalysis()).thenReturn(false); setupRoot(project, "1.1"); settings.setProperty("sonar.leak.period", "previous_version"); underTest.execute(new TestComputationStepContext()); assertPeriod(LEAK_PERIOD_MODE_PREVIOUS_VERSION, null, analysis.getCreatedAt(), analysis.getUuid()); verifyDebugLogs("Resolving first analysis as new code period as there is only one existing version"); }
private EventDto newVersionEventDto(ComponentDto project, long date, String name) { return new EventDto().setUuid(uuidFactory.create()).setComponentUuid(project.uuid()) .setAnalysisUuid("analysis_uuid") .setCategory(CATEGORY_VERSION) .setName(name).setDate(date).setCreatedAt(date); }
@Test public void fail_if_other_event_with_same_name_on_same_analysis() { SnapshotDto analysis = createAnalysisAndLogInAsProjectAdministrator("5.6"); db.events().insertEvent(newEvent(analysis).setUuid("E1").setCategory(OTHER.getLabel()).setName("E1 name")); db.events().insertEvent(newEvent(analysis).setUuid("E2").setCategory(OTHER.getLabel()).setName("E2 name")); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("An 'Other' event with the same name already exists on analysis '" + analysis.getUuid() + "'"); call("E2", "E1 name"); }
/** * SONAR-11492 */ @Test public void feed_period_by_version_with_only_one_existing_version() { OrganizationDto organization = dbTester.organizations().insert(); ComponentDto project = dbTester.components().insertPrivateProject(organization); SnapshotDto analysis1 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1226379600000L).setVersion("0.9").setLast(true)); // 2008-11-11 dbTester.events().insertEvent(newEvent(analysis1).setName("0.9").setCategory(CATEGORY_VERSION)); when(system2Mock.now()).thenReturn(november30th2008.getTime()); when(analysisMetadataHolder.isFirstAnalysis()).thenReturn(false); setupRoot(project, "0.9"); settings.setProperty("sonar.leak.period", "0.9"); underTest.execute(new TestComputationStepContext()); // Analysis form 2008-11-11 assertPeriod(LEAK_PERIOD_MODE_VERSION, "0.9", analysis1.getCreatedAt(), analysis1.getUuid()); verifyDebugLogs("Resolving new code period by version: 0.9"); }
@Test public void insert() { dbTester.prepareDbUnit(getClass(), "empty.xml"); underTest.insert(dbTester.getSession(), new EventDto() .setUuid("E1") .setAnalysisUuid("uuid_1") .setComponentUuid("ABCD") .setName("1.0") .setCategory(EventDto.CATEGORY_VERSION) .setDescription("Version 1.0") .setData("some data") .setDate(1413407091086L) .setCreatedAt(1225630680000L)); dbTester.getSession().commit(); dbTester.assertDbUnit(getClass(), "insert-result.xml", new String[] {"id"}, "events"); }
@Test public void feed_period_by_previous_version_with_first_analysis_when_no_previous_version_found() { OrganizationDto organization = dbTester.organizations().insert(); ComponentDto project = dbTester.components().insertPrivateProject(organization); SnapshotDto analysis1 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1226379600000L).setVersion("1.1").setLast(false)); // 2008-11-11 SnapshotDto analysis2 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1227934800000L).setVersion("1.1").setLast(true)); // 2008-11-29 dbTester.events().insertEvent(newEvent(analysis2).setName("1.1").setCategory(CATEGORY_VERSION)); when(system2Mock.now()).thenReturn(november30th2008.getTime()); when(analysisMetadataHolder.isFirstAnalysis()).thenReturn(false); setupRoot(project, "1.1"); settings.setProperty("sonar.leak.period", "previous_version"); underTest.execute(new TestComputationStepContext()); assertPeriod(LEAK_PERIOD_MODE_PREVIOUS_VERSION, null, analysis1.getCreatedAt(), analysis1.getUuid()); verifyDebugLogs("Resolving first analysis as new code period as there is only one existing version"); }
@Test public void ignore_unprocessed_snapshots() { OrganizationDto organization = dbTester.organizations().insert(); ComponentDto project = dbTester.components().insertPrivateProject(organization); SnapshotDto analysis1 = dbTester.components() .insertSnapshot(project, snapshot -> snapshot.setStatus(STATUS_UNPROCESSED).setCreatedAt(1226379600000L).setLast(false));// 2008-11-11 SnapshotDto analysis2 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setStatus(STATUS_PROCESSED).setVersion("not provided").setCreatedAt(1226379600000L).setLast(false));// 2008-11-29 dbTester.events().insertEvent(newEvent(analysis1).setName("not provided").setCategory(CATEGORY_VERSION).setDate(analysis1.getCreatedAt())); dbTester.events().insertEvent(newEvent(analysis2).setName("not provided").setCategory(CATEGORY_VERSION).setDate(analysis2.getCreatedAt())); when(analysisMetadataHolder.getAnalysisDate()).thenReturn(november30th2008.getTime()); when(analysisMetadataHolder.isFirstAnalysis()).thenReturn(false); setupRoot(project); settings.setProperty("sonar.leak.period", "100"); underTest.execute(new TestComputationStepContext()); assertPeriod(LEAK_PERIOD_MODE_DAYS, "100", analysis2.getCreatedAt(), analysis2.getUuid()); verifyDebugLogs("Resolving new code period by 100 days: 2008-08-22"); }
@Test public void feed_period_by_previous_version_with_previous_version_deleted() { OrganizationDto organization = dbTester.organizations().insert(); ComponentDto project = dbTester.components().insertPrivateProject(organization); SnapshotDto analysis1 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1226379600000L).setVersion("0.9").setLast(false)); // 2008-11-11 SnapshotDto analysis2 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1226494680000L).setVersion("1.0").setLast(false)); // 2008-11-12 SnapshotDto analysis3 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1227157200000L).setVersion("1.1").setLast(false)); // 2008-11-20 dbTester.events().insertEvent(newEvent(analysis1).setName("0.9").setCategory(CATEGORY_VERSION)); // The "1.0" version was deleted from the history dbTester.events().insertEvent(newEvent(analysis3).setName("1.1").setCategory(CATEGORY_VERSION)); when(system2Mock.now()).thenReturn(november30th2008.getTime()); when(analysisMetadataHolder.isFirstAnalysis()).thenReturn(false); setupRoot(project, "1.1"); settings.setProperty("sonar.leak.period", "previous_version"); underTest.execute(new TestComputationStepContext()); // Analysis form 2008-11-11 assertPeriod(LEAK_PERIOD_MODE_PREVIOUS_VERSION, "0.9", analysis1.getCreatedAt(), analysis1.getUuid()); }
@Test public void feed_period_by_version() { OrganizationDto organization = dbTester.organizations().insert(); ComponentDto project = dbTester.components().insertPrivateProject(organization); SnapshotDto analysis1 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1226379600000L).setVersion("0.9").setLast(false)); // 2008-11-11 SnapshotDto analysis2 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1226494680000L).setVersion("1.0").setLast(false)); // 2008-11-12 SnapshotDto analysis3 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1227157200000L).setVersion("1.1").setLast(false)); // 2008-11-20 SnapshotDto analysis4 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1227358680000L).setVersion("1.1").setLast(false)); // 2008-11-22 SnapshotDto analysis5 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1227934800000L).setVersion("1.1").setLast(true)); // 2008-11-29 dbTester.events().insertEvent(newEvent(analysis1).setName("0.9").setCategory(CATEGORY_VERSION)); dbTester.events().insertEvent(newEvent(analysis2).setName("1.0").setCategory(CATEGORY_VERSION)); dbTester.events().insertEvent(newEvent(analysis5).setName("1.1").setCategory(CATEGORY_VERSION)); when(system2Mock.now()).thenReturn(november30th2008.getTime()); when(analysisMetadataHolder.isFirstAnalysis()).thenReturn(false); setupRoot(project, "1.1"); settings.setProperty("sonar.leak.period", "1.0"); underTest.execute(new TestComputationStepContext()); // Analysis form 2008-11-11 assertPeriod(LEAK_PERIOD_MODE_VERSION, "1.0", analysis2.getCreatedAt(), analysis2.getUuid()); verifyDebugLogs("Resolving new code period by version: 1.0"); }
@Test public void return_events() { OrganizationDto organizationDto = db.organizations().insert(); ComponentDto project = db.components().insertComponent(ComponentTesting.newPrivateProjectDto(organizationDto).setDbKey("P1")); userSession.addProjectPermission(UserRole.USER, project); SnapshotDto a1 = db.components().insertSnapshot(newAnalysis(project).setUuid("A1")); SnapshotDto a42 = db.components().insertSnapshot(newAnalysis(ComponentTesting.newPrivateProjectDto(organizationDto)).setUuid("A42")); EventDto e1 = db.events().insertEvent(newEvent(a1).setUuid("E1").setName("N1").setCategory(EventCategory.QUALITY_GATE.getLabel()).setDescription("D1")); EventDto e2 = db.events().insertEvent(newEvent(a1).setUuid("E2").setName("N2").setCategory(VERSION.getLabel()).setDescription("D2")); db.events().insertEvent(newEvent(a42)); List<Analysis> result = call("P1").getAnalysesList(); assertThat(result).hasSize(1); List<Event> events = result.get(0).getEventsList(); assertThat(events).hasSize(2); assertThat(events).extracting(Event::getKey, wsToDbCategory(), Event::getName, Event::getDescription).containsOnly( tuple(e1.getUuid(), e1.getCategory(), e1.getName(), e1.getDescription()), tuple(e2.getUuid(), e2.getCategory(), e2.getName(), e2.getDescription())); }
@Test public void json_example() { ComponentDto project = db.components().insertPrivateProject(); SnapshotDto analysis = db.components().insertSnapshot(newAnalysis(project).setUuid("A2")); db.events().insertEvent(newEvent(analysis) .setUuid("E1") .setCategory(OTHER.getLabel()) .setName("Original Name") .setDescription("Original Description")); logInAsProjectAdministrator(project); String result = ws.newRequest() .setParam(PARAM_EVENT, "E1") .setParam(PARAM_NAME, "My Custom Event") .execute().getInput(); assertJson(result).isSimilarTo(getClass().getResource("update_event-example.json")); }
@Test public void feed_period_by_previous_version() { OrganizationDto organization = dbTester.organizations().insert(); ComponentDto project = dbTester.components().insertPrivateProject(organization); SnapshotDto analysis1 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1226379600000L).setVersion("0.9").setLast(false)); // 2008-11-11 SnapshotDto analysis2 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1226494680000L).setVersion("1.0").setLast(false)); // 2008-11-12 SnapshotDto analysis3 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1227157200000L).setVersion("1.1").setLast(false)); // 2008-11-20 SnapshotDto analysis4 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1227358680000L).setVersion("1.1").setLast(false)); // 2008-11-22 SnapshotDto analysis5 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1227934800000L).setVersion("1.1").setLast(true)); // 2008-11-29 dbTester.events().insertEvent(newEvent(analysis1).setName("0.9").setCategory(CATEGORY_VERSION).setDate(analysis1.getCreatedAt())); dbTester.events().insertEvent(newEvent(analysis2).setName("1.0").setCategory(CATEGORY_VERSION).setDate(analysis2.getCreatedAt())); dbTester.events().insertEvent(newEvent(analysis5).setName("1.1").setCategory(CATEGORY_VERSION).setDate(analysis4.getCreatedAt())); when(system2Mock.now()).thenReturn(november30th2008.getTime()); when(analysisMetadataHolder.isFirstAnalysis()).thenReturn(false); setupRoot(project, "1.1"); settings.setProperty("sonar.leak.period", "previous_version"); underTest.execute(new TestComputationStepContext()); // Analysis form 2008-11-12 assertPeriod(LEAK_PERIOD_MODE_PREVIOUS_VERSION, "1.0", analysis2.getCreatedAt(), analysis2.getUuid()); verifyDebugLogs("Resolving new code period by previous version: 1.0"); }