From 3c653a2018b745f32b2c6ed9a7ecea9edb153d08 Mon Sep 17 00:00:00 2001 From: Fabrice Bellingard Date: Wed, 20 Apr 2011 17:07:07 +0200 Subject: [PATCH] SONAR-2347 Add unit test to ReviewsDecorator --- .../core/sensors/ReviewsDecorator.java | 11 +++-- .../core/sensors/ReviewsDecoratorTest.java | 46 +++++++------------ .../sensors/ReviewsDecoratorTest/fixture.xml | 37 +++++++++------ ...ewWithoutCorrespondingViolation-result.xml | 22 +++++++++ .../org/sonar/test/persistence/sonar-test.ddl | 25 ++++++++++ 5 files changed, 93 insertions(+), 48 deletions(-) create mode 100644 plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/ReviewsDecoratorTest/shouldCloseReviewWithoutCorrespondingViolation-result.xml diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ReviewsDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ReviewsDecorator.java index c0b5b7ef94c..c4be5daf4f0 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ReviewsDecorator.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ReviewsDecorator.java @@ -19,15 +19,12 @@ */ package org.sonar.plugins.core.sensors; -import javax.persistence.EntityManager; -import javax.persistence.EntityTransaction; import javax.persistence.Query; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.batch.Decorator; import org.sonar.api.batch.DecoratorContext; -import org.sonar.api.batch.DependedUpon; import org.sonar.api.batch.DependsUpon; import org.sonar.api.database.DatabaseSession; import org.sonar.api.database.model.Snapshot; @@ -60,12 +57,16 @@ public class ReviewsDecorator implements Decorator { if (currentSnapshot != null) { int resourceId = currentSnapshot.getResourceId(); int snapshotId = currentSnapshot.getId(); - Query query = databaseSession.createNativeQuery("UPDATE reviews SET status='closed' " + "WHERE resource_id = " + resourceId - + " AND rule_failure_permanent_id NOT IN " + "(SELECT permanent_id FROM rule_failures WHERE snapshot_id = " + snapshotId + ")"); + Query query = databaseSession.createNativeQuery(generateSqlRequest(resourceId, snapshotId)); int rowUpdated = query.executeUpdate(); LOG.debug("- {} reviews set to 'closed' on resource #{}", rowUpdated, resourceId); databaseSession.commit(); } } + protected String generateSqlRequest(int resourceId, int snapshotId) { + return "UPDATE reviews SET status='closed' " + "WHERE resource_id = " + resourceId + " AND rule_failure_permanent_id NOT IN " + + "(SELECT permanent_id FROM rule_failures WHERE snapshot_id = " + snapshotId + ")"; + } + } diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ReviewsDecoratorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ReviewsDecoratorTest.java index 256cca68c04..53c42d1b06a 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ReviewsDecoratorTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ReviewsDecoratorTest.java @@ -19,40 +19,28 @@ */ package org.sonar.plugins.core.sensors; -import org.junit.Ignore; -import org.junit.Test; -import org.sonar.api.database.model.Snapshot; -import org.sonar.api.resources.File; -import org.sonar.api.resources.Resource; -import org.sonar.api.utils.DateUtils; -import org.sonar.batch.components.PastSnapshot; -import org.sonar.batch.components.TimeMachineConfiguration; -import org.sonar.batch.index.ResourcePersister; -import org.sonar.jpa.test.AbstractDbUnitTestCase; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; -import java.text.ParseException; -import java.util.Arrays; +import java.sql.Statement; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; +import org.junit.Test; +import org.sonar.test.persistence.DatabaseTestCase; -public class ReviewsDecoratorTest extends AbstractDbUnitTestCase { +public class ReviewsDecoratorTest extends DatabaseTestCase { @Test - @Ignore("DBUnit needs Hibernate mapping...") - public void shouldSaveConfigurationInSnapshotsTable() throws ParseException { + public void shouldCloseReviewWithoutCorrespondingViolation() throws Exception { setupData("fixture"); - - File resource = new File("Foo"); - Snapshot snapshot = new Snapshot(); - snapshot.setId(666); - snapshot.setResourceId(111); - ResourcePersister persister = mock(ResourcePersister.class); - when(persister.getSnapshot(resource)).thenReturn(snapshot); - - ReviewsDecorator reviewsDecorator = new ReviewsDecorator(persister, getSession()); - reviewsDecorator.decorate(resource, null); - - //checkTables("shouldSaveConfigurationInSnapshotsTable", "snapshots"); + + ReviewsDecorator reviewsDecorator = new ReviewsDecorator(null, null); + String sqlRequest = reviewsDecorator.generateSqlRequest(666, 222); + System.out.println(sqlRequest); + + Statement stmt = getConnection().createStatement(); + int count = stmt.executeUpdate(sqlRequest); + + assertThat(count, is(1)); + assertTables("shouldCloseReviewWithoutCorrespondingViolation", "reviews"); } } diff --git a/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/ReviewsDecoratorTest/fixture.xml b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/ReviewsDecoratorTest/fixture.xml index ea16316886c..d57973770cb 100644 --- a/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/ReviewsDecoratorTest/fixture.xml +++ b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/ReviewsDecoratorTest/fixture.xml @@ -2,37 +2,46 @@ + project_id="555" + status="P" islast="false"/> + project_id="555" + status="P" islast="false"/> + project_id="666" + status="P" islast="false"/> + project_id="666" + status="P" islast="false"/> + permanent_id="1" + snapshot_id="11" + rule_id="1" failure_level="1"/> + permanent_id="2" + snapshot_id="22" + rule_id="1" failure_level="1"/> + permanent_id="3" + snapshot_id="22" + rule_id="1" failure_level="1"/> + permanent_id="1" + snapshot_id="111" + rule_id="1" failure_level="1"/> + permanent_id="3" + snapshot_id="222" + rule_id="1" failure_level="1"/> + + + + + + \ No newline at end of file diff --git a/sonar-testing-harness/src/main/resources/org/sonar/test/persistence/sonar-test.ddl b/sonar-testing-harness/src/main/resources/org/sonar/test/persistence/sonar-test.ddl index eab1ca343af..c5125179cbf 100644 --- a/sonar-testing-harness/src/main/resources/org/sonar/test/persistence/sonar-test.ddl +++ b/sonar-testing-harness/src/main/resources/org/sonar/test/persistence/sonar-test.ddl @@ -481,3 +481,28 @@ create table WIDGET_PROPERTIES ( ); CREATE INDEX WIDGET_PROPERTIES_WIDGETS ON WIDGET_PROPERTIES (WIDGET_ID); +CREATE TABLE REVIEWS ( + ID INTEGER NOT NULL, + CREATED_AT TIMESTAMP, + UPDATED_AT TIMESTAMP, + USER_ID INTEGER, + ASSIGNEE_ID INTEGER, + TITLE VARCHAR(500), + REVIEW_TYPE VARCHAR(10), + STATUS VARCHAR(10), + SEVERITY VARCHAR(10), + RULE_FAILURE_PERMANENT_ID INTEGER, + RESOURCE_ID INTEGER, + RESOURCE_LINE INTEGER, + primary key (id) +); + +CREATE TABLE REVIEW_COMMENTS ( + ID INTEGER NOT NULL, + CREATED_AT TIMESTAMP, + UPDATED_AT TIMESTAMP, + REVIEW_ID INTEGER, + USER_ID INTEGER, + REVIEW_TEXT CLOB(2147483647), + primary key (id) +); -- 2.39.5