SONAR-2347 Add unit test to ReviewsDecorator

This commit is contained in:
Fabrice Bellingard 2011-04-20 17:07:07 +02:00
parent b77474c58f
commit 3c653a2018
5 changed files with 92 additions and 47 deletions

View File

@ -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 + ")";
}
}

View File

@ -19,40 +19,28 @@
*/
package org.sonar.plugins.core.sensors;
import org.junit.Ignore;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import java.sql.Statement;
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 org.sonar.test.persistence.DatabaseTestCase;
import java.text.ParseException;
import java.util.Arrays;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
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);
ReviewsDecorator reviewsDecorator = new ReviewsDecorator(null, null);
String sqlRequest = reviewsDecorator.generateSqlRequest(666, 222);
System.out.println(sqlRequest);
//checkTables("shouldSaveConfigurationInSnapshotsTable", "snapshots");
Statement stmt = getConnection().createStatement();
int count = stmt.executeUpdate(sqlRequest);
assertThat(count, is(1));
assertTables("shouldCloseReviewWithoutCorrespondingViolation", "reviews");
}
}

View File

@ -2,37 +2,46 @@
<snapshots
id="11"
project_id="555"/>
project_id="555"
status="P" islast="false"/>
<snapshots
id="111"
project_id="555"/>
project_id="555"
status="P" islast="false"/>
<snapshots
id="22"
project_id="666"/>
project_id="666"
status="P" islast="false"/>
<snapshots
id="222"
project_id="666"/>
project_id="666"
status="P" islast="false"/>
<rule_failures
id="1"
permament_id="1"
snapshot_id="11"/>
permanent_id="1"
snapshot_id="11"
rule_id="1" failure_level="1"/>
<rule_failures
id="2"
permament_id="2"
snapshot_id="22"/>
permanent_id="2"
snapshot_id="22"
rule_id="1" failure_level="1"/>
<rule_failures
id="3"
permament_id="3"
snapshot_id="22"/>
permanent_id="3"
snapshot_id="22"
rule_id="1" failure_level="1"/>
<rule_failures
id="4"
permament_id="1"
snapshot_id="111"/>
permanent_id="1"
snapshot_id="111"
rule_id="1" failure_level="1"/>
<rule_failures
id="5"
permament_id="3"
snapshot_id="222"/>
permanent_id="3"
snapshot_id="222"
rule_id="1" failure_level="1"/>
<reviews
id="1"

View File

@ -0,0 +1,22 @@
<dataset>
<reviews
id="1"
status="open"
rule_failure_permanent_id="1"
resource_id="555"
created_at="[null]" updated_at="[null]" user_id="[null]" assignee_id="[null]" title="[null]" review_type="[null]" severity="[null]" resource_line="[null]"/>
<reviews
id="2"
status="closed"
rule_failure_permanent_id="2"
resource_id="666"
created_at="[null]" updated_at="[null]" user_id="[null]" assignee_id="[null]" title="[null]" review_type="[null]" severity="[null]" resource_line="[null]"/>
<reviews
id="3"
status="open"
rule_failure_permanent_id="3"
resource_id="666"
created_at="[null]" updated_at="[null]" user_id="[null]" assignee_id="[null]" title="[null]" review_type="[null]" severity="[null]" resource_line="[null]"/>
</dataset>

View File

@ -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)
);