private SnapshotDto getAnalysis(DbSession dbSession, EventDto event) { return dbClient.snapshotDao().selectByUuid(dbSession, event.getAnalysisUuid()) .orElseThrow(() -> new IllegalStateException(format("Analysis '%s' is not found", event.getAnalysisUuid()))); }
private SnapshotDto getSnapshot(DbSession dbSession, String analysisUuid) { Optional<SnapshotDto> snapshotDto = dbClient.snapshotDao().selectByUuid(dbSession, analysisUuid); return checkFoundWithOptional(snapshotDto, "Analysis with id '%s' is not found", analysisUuid); }
private Optional<Period> newPeriod(DbSession dbSession, String periodMode, EventDto previousVersion) { Optional<Period> period = dbClient.snapshotDao().selectByUuid(dbSession, previousVersion.getAnalysisUuid()) .map(dto -> newPeriod(periodMode, previousVersion.getName(), dto)); if (!period.isPresent()) { throw new IllegalStateException(format("Analysis '%s' for version event '%s' has been deleted", previousVersion.getAnalysisUuid(), previousVersion.getName())); } return period; }
private SnapshotDto getAnalysis(DbSession dbSession, CreateEventRequest request) { return dbClient.snapshotDao().selectByUuid(dbSession, request.getAnalysis()) .orElseThrow(() -> new NotFoundException(format("Analysis '%s' is not found", request.getAnalysis()))); }
private void verifyStatusAndIsLastFlag(String uuid, String expectedStatus, boolean expectedLastFlag) { Optional<SnapshotDto> analysis = underTest.selectByUuid(db.getSession(), uuid); assertThat(analysis.get().getStatus()).isEqualTo(expectedStatus); assertThat(analysis.get().getLast()).isEqualTo(expectedLastFlag); }
private void deleteEvent(DbSession dbSession, EventDto dbEvent) { if (VERSION.getLabel().equals(dbEvent.getCategory())) { SnapshotDto analysis = dbClient.snapshotDao().selectByUuid(dbSession, dbEvent.getAnalysisUuid()) .orElseThrow(() -> new IllegalStateException(format("Analysis '%s' not found", dbEvent.getAnalysisUuid()))); checkArgument(!analysis.getLast(), "Cannot delete the version event of last analysis"); analysis.setVersion(null); dbClient.snapshotDao().update(dbSession, analysis); } dbClient.eventDao().delete(dbSession, dbEvent.getUuid()); dbSession.commit(); }
private void verifyAnalysis(String uuid, String expectedStatus, boolean expectedLastFlag) { Optional<SnapshotDto> analysis = db.getDbClient().snapshotDao().selectByUuid(db.getSession(), uuid); assertThat(analysis.get().getStatus()).isEqualTo(expectedStatus); assertThat(analysis.get().getLast()).isEqualTo(expectedLastFlag); }
private void assertDataInDb(DbData data, boolean exists) { assertThat(dbClient.componentDao().selectByUuid(dbSession, data.project.uuid()).isPresent()).isEqualTo(exists); assertThat(dbClient.snapshotDao().selectByUuid(dbSession, data.snapshot.getUuid()).isPresent()).isEqualTo(exists); assertThat(dbClient.issueDao().selectByKey(dbSession, data.issue.getKey()).isPresent()).isEqualTo(exists); }
@Override public void handle(Request request, Response response) throws Exception { String analysisParam = request.mandatoryParam(PARAM_ANALYSIS); try (DbSession dbSession = dbClient.openSession(false)) { SnapshotDto analysis = dbClient.snapshotDao().selectByUuid(dbSession, analysisParam) .orElseThrow(() -> analysisNotFoundException(analysisParam)); if (STATUS_UNPROCESSED.equals(analysis.getStatus())) { throw analysisNotFoundException(analysisParam); } userSession.checkComponentUuidPermission(UserRole.ADMIN, analysis.getComponentUuid()); checkArgument(!analysis.getLast(), "The last analysis '%s' cannot be deleted", analysisParam); analysis.setStatus(STATUS_UNPROCESSED); dbClient.snapshotDao().update(dbSession, analysis); dbSession.commit(); } response.noContent(); }
@Test public void update() { SnapshotDto analysis = insertAnalysis("P1", "A1", STATUS_PROCESSED, false); db.commit(); analysis .setComponentUuid("P42") .setVersion("5.6.3") .setStatus(STATUS_UNPROCESSED); underTest.update(dbSession, analysis); SnapshotDto result = underTest.selectByUuid(dbSession, "A1").get(); assertThat(result.getVersion()).isEqualTo("5.6.3"); assertThat(result.getStatus()).isEqualTo(STATUS_UNPROCESSED); assertThat(result.getComponentUuid()).isEqualTo("P1"); }
@Test public void snashotDto_can_hold_version_larger_than_100_read_from_database() { SnapshotDto analysis = insertAnalysis("P1", "A1", STATUS_PROCESSED, false); db.commit(); String tooLongVersion = StringUtils.repeat("d", 200); db.executeUpdateSql("update snapshots set version='" + tooLongVersion + "' where uuid='" + analysis.getUuid() + "'"); db.commit(); assertThat(underTest.selectByUuid(dbSession, analysis.getUuid()) .map(SnapshotDto::getVersion)) .contains(tooLongVersion); }
@Test public void test_selectByUuid() { ComponentDto project = db.components().insertPrivateProject(); db.components().insertSnapshot(newAnalysis(project) .setUuid("ABCD") .setStatus("P") .setLast(true) .setPurgeStatus(1) .setPeriodMode("days") .setPeriodParam("30") .setPeriodDate(1500000000001L) .setVersion("2.1-SNAPSHOT") .setBuildDate(1500000000006L) .setCreatedAt(1403042400000L)); SnapshotDto result = underTest.selectByUuid(db.getSession(), "ABCD").get(); assertThat(result.getId()).isNotNull(); assertThat(result.getUuid()).isEqualTo("ABCD"); assertThat(result.getComponentUuid()).isEqualTo(project.uuid()); assertThat(result.getStatus()).isEqualTo("P"); assertThat(result.getLast()).isTrue(); assertThat(result.getPurgeStatus()).isEqualTo(1); assertThat(result.getVersion()).isEqualTo("2.1-SNAPSHOT"); assertThat(result.getPeriodMode()).isEqualTo("days"); assertThat(result.getPeriodModeParameter()).isEqualTo("30"); assertThat(result.getPeriodDate()).isEqualTo(1500000000001L); assertThat(result.getBuildDate()).isEqualTo(1500000000006L); assertThat(result.getCreatedAt()).isEqualTo(1403042400000L); assertThat(result.getVersion()).isEqualTo("2.1-SNAPSHOT"); assertThat(underTest.selectByUuid(db.getSession(), "DOES_NOT_EXIST").isPresent()).isFalse(); }
@Test public void create_other_event_with_ws_response() { ComponentDto project = ComponentTesting.newPrivateProjectDto(db.organizations().insert()); SnapshotDto analysis = db.components().insertProjectAndSnapshot(project); logInAsProjectAdministrator(project); CreateEventResponse result = call(OTHER.name(), "Project Import", analysis.getUuid()); SnapshotDto newAnalysis = dbClient.snapshotDao().selectByUuid(dbSession, analysis.getUuid()).get(); assertThat(analysis.getVersion()).isEqualTo(newAnalysis.getVersion()); ProjectAnalyses.Event wsEvent = result.getEvent(); assertThat(wsEvent.getKey()).isNotEmpty(); assertThat(wsEvent.getCategory()).isEqualTo(OTHER.name()); assertThat(wsEvent.getName()).isEqualTo("Project Import"); assertThat(wsEvent.hasDescription()).isFalse(); assertThat(wsEvent.getAnalysis()).isEqualTo(analysis.getUuid()); }
@Test public void update_OTHER_event_does_not_update_analysis_version() { SnapshotDto analysis = createAnalysisAndLogInAsProjectAdministrator("5.6"); db.events().insertEvent(newEvent(analysis).setUuid("E1").setCategory(OTHER.getLabel())); call("E1", "6.3"); SnapshotDto updatedAnalysis = dbClient.snapshotDao().selectByUuid(dbSession, analysis.getUuid()).get(); assertThat(updatedAnalysis.getVersion()).isEqualTo("5.6"); }
@Test public void update_VERSION_event_update_analysis_version() { SnapshotDto analysis = createAnalysisAndLogInAsProjectAdministrator("5.6"); db.events().insertEvent(newEvent(analysis).setUuid("E1").setCategory(VERSION.getLabel())); call("E1", "6.3"); SnapshotDto updatedAnalysis = dbClient.snapshotDao().selectByUuid(dbSession, analysis.getUuid()).get(); assertThat(updatedAnalysis.getVersion()).isEqualTo("6.3"); }
@Test public void create_version_event() { ComponentDto project = ComponentTesting.newPrivateProjectDto(db.organizations().insert()); SnapshotDto analysis = db.components().insertProjectAndSnapshot(project); logInAsProjectAdministrator(project); call(VERSION.name(), "5.6.3", analysis.getUuid()); Optional<SnapshotDto> newAnalysis = dbClient.snapshotDao().selectByUuid(dbSession, analysis.getUuid()); assertThat(newAnalysis.get().getVersion()).isEqualTo("5.6.3"); }
@Test public void delete_version_event() { ComponentDto project = db.components().insertPrivateProject(); SnapshotDto analysis = db.components().insertSnapshot(newAnalysis(project).setVersion("5.6.3").setLast(false)); db.events().insertEvent(newEvent(analysis).setUuid("E1").setCategory(VERSION.getLabel())); logInAsProjectAdministrator(project); call("E1"); SnapshotDto newAnalysis = dbClient.snapshotDao().selectByUuid(dbSession, analysis.getUuid()).get(); assertThat(newAnalysis.getVersion()).isNull(); }
private SnapshotDto getSnapshot(DbSession dbSession, String analysisUuid) { Optional<SnapshotDto> snapshotDto = dbClient.snapshotDao().selectByUuid(dbSession, analysisUuid); return checkFoundWithOptional(snapshotDto, "Analysis with id '%s' is not found", analysisUuid); }
private SnapshotDto getAnalysis(DbSession dbSession, CreateEventRequest request) { return dbClient.snapshotDao().selectByUuid(dbSession, request.getAnalysis()) .orElseThrow(() -> new NotFoundException(format("Analysis '%s' is not found", request.getAnalysis()))); }
private void deleteEvent(DbSession dbSession, EventDto dbEvent) { if (VERSION.getLabel().equals(dbEvent.getCategory())) { SnapshotDto analysis = dbClient.snapshotDao().selectByUuid(dbSession, dbEvent.getAnalysisUuid()) .orElseThrow(() -> new IllegalStateException(format("Analysis '%s' not found", dbEvent.getAnalysisUuid()))); checkArgument(!analysis.getLast(), "Cannot delete the version event of last analysis"); analysis.setVersion(null); dbClient.snapshotDao().update(dbSession, analysis); } dbClient.eventDao().delete(dbSession, dbEvent.getUuid()); dbSession.commit(); }