]> source.dussan.org Git - sonarqube.git/blob
ace43c929e5c35cea37008f40445b09a497882f4
[sonarqube.git] /
1 /*
2  * SonarQube, open source software quality management tool.
3  * Copyright (C) 2008-2013 SonarSource
4  * mailto:contact AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.plugins.core.sensors;
21
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;
37
38 import java.util.Collections;
39
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.*;
44
45 public class ReviewWorkflowDecoratorTest extends AbstractDaoTestCase {
46   private ReviewWorkflowDecorator decorator;
47   private ReviewNotifications notifications;
48
49   @Before
50   public void init() {
51     notifications = mock(ReviewNotifications.class);
52     ResourcePersister persister = mock(ResourcePersister.class);
53     Snapshot snapshot = new Snapshot();
54     snapshot.setId(1);
55     snapshot.setResourceId(100);
56     when(persister.getSnapshot(any(Resource.class))).thenReturn(snapshot);
57
58     decorator = new ReviewWorkflowDecorator(notifications, new ReviewDao(getMyBatis()), persister);
59   }
60
61   @Test
62   public void shouldExecuteOnProject() {
63     Project project = mock(Project.class);
64     when(project.isLatestAnalysis()).thenReturn(true);
65     assertThat(decorator.shouldExecuteOnProject(project)).isTrue();
66   }
67
68   @Test
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();
73   }
74
75   @Test
76   public void shouldCloseReviewsOnResolvedViolations() {
77     setupData("shouldCloseReviewsOnResolvedViolations");
78     DecoratorContext context = mock(DecoratorContext.class);
79     when(context.getViolations(any(ViolationQuery.class))).thenReturn(Collections.<Violation>emptyList());
80
81     Resource resource = new JavaFile("org.foo.Bar");
82     decorator.decorate(resource, context);
83
84     verify(notifications, times(2)).notifyClosed(any(ReviewDto.class), any(Project.class), eq(resource));
85     checkTables("shouldCloseReviewsOnResolvedViolations", new String[]{"updated_at"}, "reviews");
86   }
87
88   @Test
89   public void shouldCloseResolvedManualViolations() {
90     setupData("shouldCloseResolvedManualViolations");
91     DecoratorContext context = mock(DecoratorContext.class);
92     when(context.getViolations(any(ViolationQuery.class))).thenReturn(Collections.<Violation>emptyList());
93
94     Resource resource = new JavaFile("org.foo.Bar");
95     decorator.decorate(resource, context);
96
97     verify(notifications).notifyClosed(any(ReviewDto.class), any(Project.class), eq(resource));
98     checkTables("shouldCloseResolvedManualViolations", new String[]{"updated_at"}, "reviews");
99   }
100
101   @Test
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));
108
109     Resource resource = new JavaFile("org.foo.Bar");
110     decorator.decorate(resource, context);
111
112     verify(notifications).notifyReopened(any(ReviewDto.class), any(Project.class), eq(resource));
113     checkTables("shouldReopenViolations", new String[]{"updated_at"}, "reviews");
114   }
115
116   @Test
117   public void hasUpToDateInformation() {
118     assertThat(ReviewWorkflowDecorator.hasUpToDateInformation(
119       new ReviewDto().setTitle("Design").setLine(30),
120       new Violation(new Rule()).setMessage("Design").setLineId(30)))
121       .isTrue();
122
123
124     // different title
125     assertThat(ReviewWorkflowDecorator.hasUpToDateInformation(
126       new ReviewDto().setTitle("Design").setLine(30),
127       new Violation(new Rule()).setMessage("Other").setLineId(30))
128     ).isFalse();
129
130     // different line
131     assertThat(ReviewWorkflowDecorator.hasUpToDateInformation(
132       new ReviewDto().setTitle("Design").setLine(300),
133       new Violation(new Rule()).setMessage("Design").setLineId(200)))
134       .isFalse();
135
136     assertThat(ReviewWorkflowDecorator.hasUpToDateInformation(
137       new ReviewDto().setTitle("Design").setLine(300),
138       new Violation(new Rule()).setMessage("Design").setLineId(null)))
139       .isFalse();
140   }
141 }