2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2013 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * SonarQube is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.plugins.core.sensors;
22 import com.google.common.collect.Lists;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.sonar.api.batch.DecoratorContext;
26 import org.sonar.api.database.model.Snapshot;
27 import org.sonar.api.resources.JavaFile;
28 import org.sonar.api.resources.Project;
29 import org.sonar.api.resources.Resource;
30 import org.sonar.api.rules.Rule;
31 import org.sonar.api.rules.Violation;
32 import org.sonar.api.violations.ViolationQuery;
33 import org.sonar.batch.index.ResourcePersister;
34 import org.sonar.core.persistence.AbstractDaoTestCase;
35 import org.sonar.core.review.ReviewDao;
36 import org.sonar.core.review.ReviewDto;
38 import java.util.Collections;
40 import static org.fest.assertions.Assertions.assertThat;
41 import static org.mockito.Matchers.any;
42 import static org.mockito.Matchers.eq;
43 import static org.mockito.Mockito.*;
45 public class ReviewWorkflowDecoratorTest extends AbstractDaoTestCase {
46 private ReviewWorkflowDecorator decorator;
47 private ReviewNotifications notifications;
51 notifications = mock(ReviewNotifications.class);
52 ResourcePersister persister = mock(ResourcePersister.class);
53 Snapshot snapshot = new Snapshot();
55 snapshot.setResourceId(100);
56 when(persister.getSnapshot(any(Resource.class))).thenReturn(snapshot);
58 decorator = new ReviewWorkflowDecorator(notifications, new ReviewDao(getMyBatis()), persister);
62 public void shouldExecuteOnProject() {
63 Project project = mock(Project.class);
64 when(project.isLatestAnalysis()).thenReturn(true);
65 assertThat(decorator.shouldExecuteOnProject(project)).isTrue();
69 public void shouldExecuteOnProject_not_if_past_inspection() {
70 Project project = mock(Project.class);
71 when(project.isLatestAnalysis()).thenReturn(false);
72 assertThat(decorator.shouldExecuteOnProject(project)).isFalse();
76 public void shouldCloseReviewsOnResolvedViolations() {
77 setupData("shouldCloseReviewsOnResolvedViolations");
78 DecoratorContext context = mock(DecoratorContext.class);
79 when(context.getViolations(any(ViolationQuery.class))).thenReturn(Collections.<Violation>emptyList());
81 Resource resource = new JavaFile("org.foo.Bar");
82 decorator.decorate(resource, context);
84 verify(notifications, times(2)).notifyClosed(any(ReviewDto.class), any(Project.class), eq(resource));
85 checkTables("shouldCloseReviewsOnResolvedViolations", new String[]{"updated_at"}, "reviews");
89 public void shouldCloseResolvedManualViolations() {
90 setupData("shouldCloseResolvedManualViolations");
91 DecoratorContext context = mock(DecoratorContext.class);
92 when(context.getViolations(any(ViolationQuery.class))).thenReturn(Collections.<Violation>emptyList());
94 Resource resource = new JavaFile("org.foo.Bar");
95 decorator.decorate(resource, context);
97 verify(notifications).notifyClosed(any(ReviewDto.class), any(Project.class), eq(resource));
98 checkTables("shouldCloseResolvedManualViolations", new String[]{"updated_at"}, "reviews");
102 public void shouldReopenViolations() {
103 setupData("shouldReopenViolations");
104 DecoratorContext context = mock(DecoratorContext.class);
105 Violation violation = new Violation(new Rule());
106 violation.setPermanentId(1000);
107 when(context.getViolations(any(ViolationQuery.class))).thenReturn(Lists.newArrayList(violation));
109 Resource resource = new JavaFile("org.foo.Bar");
110 decorator.decorate(resource, context);
112 verify(notifications).notifyReopened(any(ReviewDto.class), any(Project.class), eq(resource));
113 checkTables("shouldReopenViolations", new String[]{"updated_at"}, "reviews");
117 public void hasUpToDateInformation() {
118 assertThat(ReviewWorkflowDecorator.hasUpToDateInformation(
119 new ReviewDto().setTitle("Design").setLine(30),
120 new Violation(new Rule()).setMessage("Design").setLineId(30)))
125 assertThat(ReviewWorkflowDecorator.hasUpToDateInformation(
126 new ReviewDto().setTitle("Design").setLine(30),
127 new Violation(new Rule()).setMessage("Other").setLineId(30))
131 assertThat(ReviewWorkflowDecorator.hasUpToDateInformation(
132 new ReviewDto().setTitle("Design").setLine(300),
133 new Violation(new Rule()).setMessage("Design").setLineId(200)))
136 assertThat(ReviewWorkflowDecorator.hasUpToDateInformation(
137 new ReviewDto().setTitle("Design").setLine(300),
138 new Violation(new Rule()).setMessage("Design").setLineId(null)))