]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4514 Add unit tests
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 17 Jun 2014 10:18:54 +0000 (12:18 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 17 Jun 2014 16:23:19 +0000 (18:23 +0200)
sonar-server/src/main/java/org/sonar/server/rule/RubyRuleService.java
sonar-server/src/test/java/org/sonar/server/rule/RubyRuleServiceTest.java [new file with mode: 0644]

index 35e770447f0ad34680704aeab24782d4d9562106..ea792ca1eee5d84d7974b4fe35ed5274125f0422 100644 (file)
@@ -69,7 +69,7 @@ public class RubyRuleService implements ServerComponent, Startable {
   /**
    * Used in SQALE
    */
-  public PagedResult<org.sonar.server.rule.Rule> find(Map<String, Object> params) {
+  public PagedResult<Rule> find(Map<String, Object> params) {
     RuleQuery query = service.newRuleQuery();
     query.setQueryText(Strings.emptyToNull((String) params.get("searchQuery")));
     query.setKey(Strings.emptyToNull((String) params.get("key")));
@@ -84,7 +84,7 @@ public class RubyRuleService implements ServerComponent, Startable {
 
     QueryOptions options = new QueryOptions().setScroll(true);
     List<Rule> rules = newArrayList(service.search(query, options).scroll());
-    return new PagedResult<org.sonar.server.rule.Rule>(rules, PagingResult.create(Integer.MAX_VALUE, 1, rules.size()));
+    return new PagedResult<Rule>(rules, PagingResult.create(Integer.MAX_VALUE, 1, rules.size()));
   }
 
   /**
diff --git a/sonar-server/src/test/java/org/sonar/server/rule/RubyRuleServiceTest.java b/sonar-server/src/test/java/org/sonar/server/rule/RubyRuleServiceTest.java
new file mode 100644 (file)
index 0000000..9cfc9ca
--- /dev/null
@@ -0,0 +1,98 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package org.sonar.server.rule;
+
+import com.google.common.collect.ImmutableMap;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.sonar.api.rule.RuleKey;
+import org.sonar.server.rule.index.RuleQuery;
+import org.sonar.server.search.QueryOptions;
+import org.sonar.server.search.Result;
+import org.sonar.server.user.UserSession;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.*;
+
+@RunWith(MockitoJUnitRunner.class)
+public class RubyRuleServiceTest {
+
+  @Mock
+  RuleService ruleService;
+
+  @Mock
+  RuleUpdater updater;
+
+  RubyRuleService service;
+
+  @Before
+  public void setUp() throws Exception {
+    service = new RubyRuleService(ruleService, updater);
+  }
+
+  @Test
+  public void find_by_key() throws Exception {
+    service.findByKey("squid:S001");
+    verify(ruleService).getByKey(RuleKey.of("squid", "S001"));
+  }
+
+  @Test
+  public void update_rule() throws Exception {
+    when(ruleService.search(any(RuleQuery.class), any(QueryOptions.class))).thenReturn(mock(Result.class));
+
+    service.updateRule(ImmutableMap.<String, Object>of("ruleKey", "squid:S001"));
+
+    verify(updater).update(any(RuleUpdate.class), any(UserSession.class));
+  }
+
+  @Test
+  public void search_manual_rules() throws Exception {
+    when(ruleService.search(any(RuleQuery.class), any(QueryOptions.class))).thenReturn(mock(Result.class));
+
+    service.searchManualRules();
+
+    verify(ruleService).search(any(RuleQuery.class), any(QueryOptions.class));
+  }
+
+  @Test
+  public void create_manual_rules() throws Exception {
+    service.createManualRule(ImmutableMap.<String, Object>of("manualKey", "MY_MANUAL"));
+
+    verify(ruleService).create(any(NewRule.class));
+  }
+
+  @Test
+  public void update_manual_rules() throws Exception {
+    service.updateManualRule(ImmutableMap.<String, Object>of("ruleKey", "manual:MY_MANUAL"));
+
+    verify(ruleService).update(any(RuleUpdate.class));
+  }
+
+  @Test
+  public void delete_manual_rules() throws Exception {
+    service.deleteManualRule("manual:MY_MANUAL");
+
+    verify(ruleService).delete(RuleKey.of("manual", "MY_MANUAL"));
+  }
+}