diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2013-04-12 14:12:23 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2013-04-12 14:12:23 +0200 |
commit | dc5ecf80639df3951adf3925df4b1f95e502db5b (patch) | |
tree | e647ca1ed9badc03d4955ce96c4ef334f2793678 /sonar-core/src/test | |
parent | 779eb8f0c2ea746d1310e7c8ae4b9db66ff6ac07 (diff) | |
download | sonarqube-dc5ecf80639df3951adf3925df4b1f95e502db5b.tar.gz sonarqube-dc5ecf80639df3951adf3925df4b1f95e502db5b.zip |
SONAR-3755 refactor IssueFinder
Diffstat (limited to 'sonar-core/src/test')
3 files changed, 37 insertions, 155 deletions
diff --git a/sonar-core/src/test/java/org/sonar/core/issue/DefaultIssueFinderTest.java b/sonar-core/src/test/java/org/sonar/core/issue/DefaultIssueFinderTest.java index 0f35300aaa0..c1a2447894d 100644 --- a/sonar-core/src/test/java/org/sonar/core/issue/DefaultIssueFinderTest.java +++ b/sonar-core/src/test/java/org/sonar/core/issue/DefaultIssueFinderTest.java @@ -17,19 +17,19 @@ * License along with Sonar; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 */ - package org.sonar.core.issue; import org.junit.Before; import org.junit.Test; import org.sonar.api.issue.Issue; +import org.sonar.api.issue.IssueFinder; import org.sonar.api.issue.IssueQuery; import org.sonar.api.rules.Rule; import org.sonar.api.rules.RuleFinder; import org.sonar.core.resource.ResourceDao; import org.sonar.core.resource.ResourceDto; -import java.util.Collection; +import java.util.List; import static com.google.common.collect.Lists.newArrayList; import static org.fest.assertions.Assertions.assertThat; @@ -56,16 +56,18 @@ public class DefaultIssueFinderTest { public void should_find_issues() { IssueQuery issueQuery = mock(IssueQuery.class); - IssueDto issue1 = new IssueDto().setId(1L).setRuleId(1).setResourceId(1); - IssueDto issue2 = new IssueDto().setId(2L).setRuleId(1).setResourceId(1); - Collection<IssueDto> dtoList = newArrayList(issue1, issue2); + IssueDto issue1 = new IssueDto().setId(1L).setRuleId(50).setResourceId(123); + IssueDto issue2 = new IssueDto().setId(2L).setRuleId(50).setResourceId(123); + List<IssueDto> dtoList = newArrayList(issue1, issue2); when(issueDao.select(issueQuery)).thenReturn(dtoList); - when(ruleFinder.findById(anyInt())).thenReturn(Rule.create("repo", "key")); - when(resourceDao.getResource(anyInt())).thenReturn(new ResourceDto().setKey("componentKey")); + Rule rule = Rule.create("repo", "key"); + rule.setId(50); + when(ruleFinder.findById(anyInt())).thenReturn(rule); + when(resourceDao.getResource(anyInt())).thenReturn(new ResourceDto().setKey("componentKey").setId(123L)); - Collection<Issue> issues = finder.find(issueQuery); - assertThat(issues).hasSize(2); - Issue issue = issues.iterator().next(); + IssueFinder.Results results = finder.find(issueQuery); + assertThat(results.issues()).hasSize(2); + Issue issue = results.issues().iterator().next(); assertThat(issue.componentKey()).isEqualTo("componentKey"); assertThat(issue.ruleKey()).isEqualTo("key"); assertThat(issue.ruleRepositoryKey()).isEqualTo("repo"); @@ -74,7 +76,7 @@ public class DefaultIssueFinderTest { @Test public void should_find_by_key() { IssueDto issueDto = new IssueDto().setId(1L).setRuleId(1).setResourceId(1); - when(issueDao.findByUuid("key")).thenReturn(issueDto); + when(issueDao.selectByKey("key")).thenReturn(issueDto); when(ruleFinder.findById(anyInt())).thenReturn(Rule.create("repo", "key")); when(resourceDao.getResource(anyInt())).thenReturn(new ResourceDto().setKey("componentKey")); diff --git a/sonar-core/src/test/java/org/sonar/core/issue/IssueDaoTest.java b/sonar-core/src/test/java/org/sonar/core/issue/IssueDaoTest.java index 3cf6e80e3d8..bcfd51f9e47 100644 --- a/sonar-core/src/test/java/org/sonar/core/issue/IssueDaoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/issue/IssueDaoTest.java @@ -36,10 +36,10 @@ import static org.fest.assertions.Assertions.assertThat; public class IssueDaoTest extends AbstractDaoTestCase { - private IssueDao dao; + IssueDao dao; @Before - public void createDao() { + public void setUp() { dao = new IssueDao(getMyBatis()); } @@ -67,13 +67,13 @@ public class IssueDaoTest extends AbstractDaoTestCase { dao.insert(issueDto); - checkTables("insert", new String[] {"id", "created_at", "updated_at", "closed_at"}, "issues"); + checkTables("insert", new String[]{"id", "created_at", "updated_at", "closed_at"}, "issues"); } @Test public void update() { setupData("update"); - Collection<IssueDto> issues = newArrayList(dao.findById(100L)); + Collection<IssueDto> issues = newArrayList(dao.selectById(100L)); IssueDto issue = issues.iterator().next(); issue.setLine(1000); issue.setResolution("NEW_RESOLUTION"); @@ -96,7 +96,7 @@ public class IssueDaoTest extends AbstractDaoTestCase { public void should_find_issue_by_id() { setupData("shared"); - IssueDto issue = dao.findById(100L); + IssueDto issue = dao.selectById(100L); assertThat(issue.getId()).isEqualTo(100L); assertThat(issue.getUuid()).isEqualTo("100"); assertThat(issue.getResourceId()).isEqualTo(400); @@ -124,7 +124,7 @@ public class IssueDaoTest extends AbstractDaoTestCase { public void should_find_issue_by_uuid() { setupData("shared"); - IssueDto issue = dao.findByUuid("100"); + IssueDto issue = dao.selectByKey("100"); assertThat(issue).isNotNull(); } @@ -132,36 +132,36 @@ public class IssueDaoTest extends AbstractDaoTestCase { public void should_select_by_parameter() { setupData("select"); - IssueQuery issueQuery = new IssueQuery.Builder().keys(newArrayList("100")).build(); - assertThat(dao.select(issueQuery)).hasSize(1); + IssueQuery query = IssueQuery.builder().keys(newArrayList("100")).build(); + assertThat(dao.select(query)).hasSize(1); - issueQuery = new IssueQuery.Builder().componentKeys(newArrayList("key")).build(); - assertThat(dao.select(issueQuery)).hasSize(2); + query = IssueQuery.builder().components(newArrayList("key")).build(); + assertThat(dao.select(query)).hasSize(2); - issueQuery = new IssueQuery.Builder().resolutions(newArrayList("FALSE-POSITIVE")).build(); - assertThat(dao.select(issueQuery)).hasSize(1); + query = IssueQuery.builder().resolutions(newArrayList("FALSE-POSITIVE")).build(); + assertThat(dao.select(query)).hasSize(1); - issueQuery = new IssueQuery.Builder().status(newArrayList("OPEN")).build(); - assertThat(dao.select(issueQuery)).hasSize(2); + query = IssueQuery.builder().statuses(newArrayList("OPEN")).build(); + assertThat(dao.select(query)).hasSize(2); - issueQuery = new IssueQuery.Builder().severities(newArrayList("BLOCKER")).build(); - assertThat(dao.select(issueQuery)).hasSize(4); + query = IssueQuery.builder().severities(newArrayList("BLOCKER")).build(); + assertThat(dao.select(query)).hasSize(4); - issueQuery = new IssueQuery.Builder().userLogins(newArrayList("user")).build(); - assertThat(dao.select(issueQuery)).hasSize(1); + query = IssueQuery.builder().userLogins(newArrayList("user")).build(); + assertThat(dao.select(query)).hasSize(1); - issueQuery = new IssueQuery.Builder().assigneeLogins(newArrayList("user")).build(); - assertThat(dao.select(issueQuery)).hasSize(5); + query = IssueQuery.builder().assigneeLogins(newArrayList("user")).build(); + assertThat(dao.select(query)).hasSize(5); - issueQuery = new IssueQuery.Builder().userLogins(newArrayList("user")).status(newArrayList("OPEN")).build(); - assertThat(dao.select(issueQuery)).hasSize(1); + query = IssueQuery.builder().userLogins(newArrayList("user")).statuses(newArrayList("OPEN")).build(); + assertThat(dao.select(query)).hasSize(1); } @Test public void should_return_issues_from_resource_tree() { setupData("select-with-component-children"); - IssueQuery issueQuery = new IssueQuery.Builder().componentKeys(newArrayList("key")).build(); + IssueQuery issueQuery = IssueQuery.builder().components(newArrayList("key")).build(); List<IssueDto> issues = newArrayList(dao.select(issueQuery)); assertThat(issues).hasSize(2); assertThat(issues.get(0).getId()).isEqualTo(100); @@ -172,7 +172,7 @@ public class IssueDaoTest extends AbstractDaoTestCase { public void should_select_without_parameter_return_all_issues() { setupData("select"); - IssueQuery issueQuery = new IssueQuery.Builder().build(); + IssueQuery issueQuery = IssueQuery.builder().build(); assertThat(dao.select(issueQuery)).hasSize(5); } diff --git a/sonar-core/src/test/java/org/sonar/core/issue/IssueFilterTest.java b/sonar-core/src/test/java/org/sonar/core/issue/IssueFilterTest.java deleted file mode 100644 index 814938fbe0c..00000000000 --- a/sonar-core/src/test/java/org/sonar/core/issue/IssueFilterTest.java +++ /dev/null @@ -1,120 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar 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. - * - * Sonar 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 Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ - -package org.sonar.core.issue; - -import org.junit.Before; -import org.junit.Test; -import org.sonar.api.issue.Issue; -import org.sonar.api.issue.IssueFinder; -import org.sonar.api.issue.IssueQuery; - -import java.util.List; -import java.util.Map; - -import static com.google.common.collect.Maps.newHashMap; -import static org.fest.assertions.Assertions.assertThat; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyString; -import static org.mockito.Mockito.*; - -public class IssueFilterTest { - - private IssueFilter issueFilter; - private IssueFinder issueFinder; - - @Before - public void before() { - issueFinder = mock(IssueFinder.class); - issueFilter = new IssueFilter(issueFinder); - } - - @Test - public void should_call_find() { - Map<String, String> map = newHashMap(); - issueFilter.execute(map); - verify(issueFinder).find(any(IssueQuery.class)); - } - - @Test - public void should_call_find_by_key() { - issueFilter.execute("key"); - verify(issueFinder).findByKey("key"); - } - - @Test - public void should_not_call_find_by_key_with_empty_key() { - Issue issue = issueFilter.execute(""); - assertThat(issue).isNull(); - verify(issueFinder, never()).findByKey(anyString()); - } - - @Test - public void should_create_empty_issue_query() { - Map<String, String> map = newHashMap(); - IssueQuery issueQuery = issueFilter.createIssueQuery(map); - assertThat(issueQuery.componentKeys()).isEmpty(); - } - - @Test - public void should_create_empty_issue_query_if_value_is_null() { - Map<String, String> map = newHashMap(); - map.put("components", null); - IssueQuery issueQuery = issueFilter.createIssueQuery(map); - assertThat(issueQuery.componentKeys()).isEmpty(); - } - - @Test - public void should_create_issue_query() { - Map<String, String> map = newHashMap(); - map.put("keys", "keys"); - map.put("severities", "severities"); - map.put("minSeverity", "MINOR"); - map.put("status", "status"); - map.put("resolutions", "resolutions"); - map.put("components", "key"); - map.put("rules", "rules"); - map.put("userLogins", "userLogins"); - map.put("assigneeLogins", "assigneeLogins"); - map.put("limit", "1"); - - IssueQuery issueQuery = issueFilter.createIssueQuery(map); - assertThat(issueQuery.keys()).isNotEmpty(); - assertThat(issueQuery.severities()).isNotEmpty(); - assertThat(issueQuery.minSeverity()).isEqualTo("MINOR"); - assertThat(issueQuery.status()).isNotEmpty(); - assertThat(issueQuery.resolutions()).isNotEmpty(); - assertThat(issueQuery.rules()).isNotEmpty(); - assertThat(issueQuery.userLogins()).isNotEmpty(); - assertThat(issueQuery.assigneeLogins()).isNotEmpty(); - assertThat(issueQuery.limit()).isEqualTo(1); - } - - @Test - public void should_split_property_list() { - Map<String, String> map = newHashMap(); - map.put("components", "key1,key2"); - IssueQuery issueQuery = issueFilter.createIssueQuery(map); - List<String> components = issueQuery.componentKeys(); - assertThat(components).hasSize(2); - assertThat(components).containsOnly("key1", "key2"); - } - -} |