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.base.Predicate;
23 import com.google.common.collect.Lists;
24 import org.junit.Test;
25 import org.mockito.ArgumentMatcher;
26 import org.sonar.api.batch.DecoratorContext;
27 import org.sonar.api.resources.Project;
28 import org.sonar.api.rules.Rule;
29 import org.sonar.api.rules.RuleFinder;
30 import org.sonar.api.rules.RulePriority;
31 import org.sonar.api.rules.Violation;
32 import org.sonar.api.utils.DateUtils;
33 import org.sonar.core.review.ReviewDao;
34 import org.sonar.core.review.ReviewDto;
36 import java.util.Date;
38 import static org.mockito.Matchers.anyVararg;
39 import static org.mockito.Matchers.argThat;
40 import static org.mockito.Matchers.eq;
41 import static org.mockito.Mockito.mock;
42 import static org.mockito.Mockito.times;
43 import static org.mockito.Mockito.verify;
44 import static org.mockito.Mockito.when;
46 public class ManualViolationInjectorTest {
49 public void shouldInjectManualViolationsDefinedByReviews() {
50 final Date createdAt = DateUtils.parseDate("2011-12-25");
51 ReviewDto review = new ReviewDto().setRuleId(3).setViolationPermanentId(100).setCreatedAt(createdAt).setSeverity(RulePriority.BLOCKER.toString());
52 ReviewDao dao = mock(ReviewDao.class);
53 when(dao.selectOpenByResourceId(eq(100L), (Predicate<ReviewDto>[])anyVararg())).thenReturn(Lists.newArrayList(review));
54 RuleFinder ruleFinder = mock(RuleFinder.class);
55 when(ruleFinder.findById(3)).thenReturn(new Rule());
56 DecoratorContext context = mock(DecoratorContext.class);
57 ManualViolationInjector injector = new ManualViolationInjector(dao, ruleFinder);
59 injector.decorate(new Project("key").setId(100), context);
61 verify(context, times(1)).saveViolation(argThat(new ArgumentMatcher<Violation>() {
63 public boolean matches(Object o) {
64 Violation v = (Violation) o;
65 return v.getPermanentId() == 100 && v.getRule() != null && v.isManual() && v.getCreatedAt().equals(createdAt)
66 && v.getSeverity().equals(RulePriority.BLOCKER);