]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8798 move listAuthors and listTagsForComponent to AuthorsAction and ComponentTa...
authorDaniel Schwarz <daniel.schwarz@sonarsource.com>
Fri, 28 Jul 2017 09:35:59 +0000 (11:35 +0200)
committerDaniel Schwarz <bartfastiel@users.noreply.github.com>
Wed, 9 Aug 2017 13:09:54 +0000 (15:09 +0200)
server/sonar-server/src/main/java/org/sonar/server/issue/IssueService.java [deleted file]
server/sonar-server/src/main/java/org/sonar/server/issue/ws/AuthorsAction.java
server/sonar-server/src/main/java/org/sonar/server/issue/ws/ComponentTagsAction.java
server/sonar-server/src/main/java/org/sonar/server/issue/ws/IssueWsModule.java
server/sonar-server/src/test/java/org/sonar/server/issue/IssueServiceMediumTest.java [deleted file]
server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueIndexTest.java
server/sonar-server/src/test/java/org/sonar/server/issue/ws/AuthorsActionTest.java
server/sonar-server/src/test/java/org/sonar/server/issue/ws/ComponentTagsActionTest.java
server/sonar-server/src/test/java/org/sonar/server/issue/ws/IssueWsModuleTest.java

diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/IssueService.java b/server/sonar-server/src/main/java/org/sonar/server/issue/IssueService.java
deleted file mode 100644 (file)
index b38f7f9..0000000
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2017 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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.issue;
-
-import java.util.List;
-import java.util.Map;
-import javax.annotation.Nullable;
-import org.sonar.api.ce.ComputeEngineSide;
-import org.sonar.api.server.ServerSide;
-import org.sonar.server.issue.index.IssueIndex;
-
-@ServerSide
-@ComputeEngineSide
-public class IssueService {
-
-  private final IssueIndex issueIndex;
-
-  public IssueService(IssueIndex issueIndex) {
-    this.issueIndex = issueIndex;
-  }
-
-  public List<String> listAuthors(@Nullable String textQuery, int pageSize) {
-    IssueQuery query = IssueQuery.builder()
-      .checkAuthorization(false)
-      .build();
-    return issueIndex.listAuthors(query, textQuery, pageSize);
-  }
-
-  public Map<String, Long> listTagsForComponent(IssueQuery query, int pageSize) {
-    return issueIndex.countTags(query, pageSize);
-  }
-
-}
index 61f43a6dbbc1b1b26240ca0d02dc2fe7909f23bb..659c3f8f3887f6c5fa573d83d419a949c0eaaf1e 100644 (file)
 package org.sonar.server.issue.ws;
 
 import com.google.common.io.Resources;
+import java.util.List;
+import javax.annotation.Nullable;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
 import org.sonar.api.server.ws.WebService.NewAction;
 import org.sonar.api.server.ws.WebService.Param;
 import org.sonar.api.utils.text.JsonWriter;
-import org.sonar.server.issue.IssueService;
+import org.sonar.server.issue.IssueQuery;
+import org.sonar.server.issue.index.IssueIndex;
 
 import static org.sonarqube.ws.client.issue.IssuesWsParameters.ACTION_AUTHORS;
 
 public class AuthorsAction implements IssuesWsAction {
 
-  private final IssueService service;
+  private final IssueIndex issueIndex;
 
-  public AuthorsAction(IssueService service) {
-    this.service = service;
+  public AuthorsAction(IssueIndex issueIndex) {
+    this.issueIndex = issueIndex;
   }
 
   @Override
@@ -65,11 +68,17 @@ public class AuthorsAction implements IssuesWsAction {
         .name("authors")
         .beginArray();
 
-      for (String login : service.listAuthors(query, pageSize)) {
+      for (String login : listAuthors(query, pageSize)) {
         json.value(login);
       }
 
       json.endArray().endObject();
     }
   }
+
+  public List<String> listAuthors(@Nullable String textQuery, int pageSize) {
+    return issueIndex.listAuthors(IssueQuery.builder()
+      .checkAuthorization(false)
+      .build(), textQuery, pageSize);
+  }
 }
index f3602ded9f14a5a92eaecd3e6111d96b30c0f4ff..45f0347a9ae4e1ca80ff7e1e6615b5f8079c9eee 100644 (file)
@@ -28,7 +28,7 @@ import org.sonar.api.server.ws.WebService.NewAction;
 import org.sonar.api.utils.text.JsonWriter;
 import org.sonar.server.issue.IssueQuery;
 import org.sonar.server.issue.IssueQueryFactory;
-import org.sonar.server.issue.IssueService;
+import org.sonar.server.issue.index.IssueIndex;
 import org.sonarqube.ws.client.issue.SearchWsRequest;
 
 import static java.util.Collections.singletonList;
@@ -42,11 +42,11 @@ import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_CREATED_AFT
  */
 public class ComponentTagsAction implements IssuesWsAction {
 
-  private final IssueService service;
+  private final IssueIndex issueIndex;
   private final IssueQueryFactory queryService;
 
-  public ComponentTagsAction(IssueService service, IssueQueryFactory queryService) {
-    this.service = service;
+  public ComponentTagsAction(IssueIndex issueIndex, IssueQueryFactory queryService) {
+    this.issueIndex = issueIndex;
     this.queryService = queryService;
   }
 
@@ -82,7 +82,7 @@ public class ComponentTagsAction implements IssuesWsAction {
     int pageSize = request.mandatoryParamAsInt(PAGE_SIZE);
     try (JsonWriter json = response.newJsonWriter()) {
       json.beginObject().name("tags").beginArray();
-      for (Map.Entry<String, Long> tag : service.listTagsForComponent(query, pageSize).entrySet()) {
+      for (Map.Entry<String, Long> tag : issueIndex.countTags(query, pageSize).entrySet()) {
         json.beginObject()
           .prop("key", tag.getKey())
           .prop("value", tag.getValue())
index 4f694963b21b5bc0fbfa3f2112e8f196fe48646f..f6c4d23e86e8a25e2d2d64719b23e131abea537a 100644 (file)
@@ -24,7 +24,6 @@ import org.sonar.server.issue.ActionFinder;
 import org.sonar.server.issue.IssueFieldsSetter;
 import org.sonar.server.issue.IssueFinder;
 import org.sonar.server.issue.IssueQueryFactory;
-import org.sonar.server.issue.IssueService;
 import org.sonar.server.issue.IssueUpdater;
 import org.sonar.server.issue.ServerIssueStorage;
 import org.sonar.server.issue.TransitionService;
@@ -44,7 +43,6 @@ public class IssueWsModule extends Module {
       IssueFieldsSetter.class,
       FunctionExecutor.class,
       IssueWorkflow.class,
-      IssueService.class,
       IssueQueryFactory.class,
       IssuesWs.class,
       AvatarResolverImpl.class,
diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/IssueServiceMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/IssueServiceMediumTest.java
deleted file mode 100644 (file)
index 3ab87f7..0000000
+++ /dev/null
@@ -1,174 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2017 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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.issue;
-
-import com.google.common.collect.ImmutableSet;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.ClassRule;
-import org.junit.Rule;
-import org.junit.Test;
-import org.sonar.api.issue.Issue;
-import org.sonar.db.DbClient;
-import org.sonar.db.DbSession;
-import org.sonar.db.component.ComponentDao;
-import org.sonar.db.component.ComponentDto;
-import org.sonar.db.component.ComponentTesting;
-import org.sonar.db.issue.IssueDao;
-import org.sonar.db.issue.IssueDto;
-import org.sonar.db.issue.IssueTesting;
-import org.sonar.db.organization.OrganizationDao;
-import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.organization.OrganizationTesting;
-import org.sonar.db.rule.RuleDao;
-import org.sonar.db.rule.RuleDto;
-import org.sonar.db.rule.RuleTesting;
-import org.sonar.server.issue.index.IssueIndex;
-import org.sonar.server.issue.index.IssueIndexer;
-import org.sonar.server.permission.index.PermissionIndexer;
-import org.sonar.server.rule.index.RuleIndexer;
-import org.sonar.server.tester.ServerTester;
-import org.sonar.server.tester.UserSessionRule;
-
-import static java.util.Arrays.asList;
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.entry;
-
-public class IssueServiceMediumTest {
-
-  @ClassRule
-  public static ServerTester tester = new ServerTester().withStartupTasks().withEsIndexes();
-  @Rule
-  public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester);
-
-  private DbClient db = tester.get(DbClient.class);
-  private IssueIndex issueIndex;
-  private DbSession session;
-  private IssueService service;
-  private RuleIndexer ruleIndexer;
-
-  @Before
-  public void setUp() {
-    tester.clearDbAndIndexes();
-    issueIndex = tester.get(IssueIndex.class);
-    session = db.openSession(false);
-    service = tester.get(IssueService.class);
-    ruleIndexer = tester.get(RuleIndexer.class);
-  }
-
-  @After
-  public void after() {
-    session.close();
-  }
-
-  @Test
-  public void list_component_tags() {
-    RuleDto rule = newRule();
-    ComponentDto project = newPublicProject();
-    ComponentDto file = newFile(project);
-    saveIssue(IssueTesting.newDto(rule, file, project).setTags(ImmutableSet.of("convention", "java8", "bug")));
-    saveIssue(IssueTesting.newDto(rule, file, project).setTags(ImmutableSet.of("convention", "bug")));
-    saveIssue(IssueTesting.newDto(rule, file, project));
-    saveIssue(IssueTesting.newDto(rule, file, project).setTags(ImmutableSet.of("convention", "java8", "bug")).setResolution(Issue.RESOLUTION_FIXED));
-    saveIssue(IssueTesting.newDto(rule, file, project).setTags(ImmutableSet.of("convention")));
-
-    assertThat(service.listTagsForComponent(projectQuery(project.uuid()), 5)).containsOnly(entry("convention", 3L), entry("bug", 2L), entry("java8", 1L));
-    assertThat(service.listTagsForComponent(projectQuery(project.uuid()), 2)).contains(entry("convention", 3L), entry("bug", 2L)).doesNotContainEntry("java8", 1L);
-    assertThat(service.listTagsForComponent(projectQuery("other"), 10)).isEmpty();
-  }
-
-  private IssueQuery projectQuery(String projectUuid) {
-    return IssueQuery.builder().projectUuids(asList(projectUuid)).resolved(false).build();
-  }
-
-  @Test
-  public void test_listAuthors() {
-    RuleDto rule = newRule();
-    ComponentDto project = newPublicProject();
-    ComponentDto file = newFile(project);
-    saveIssue(IssueTesting.newDto(rule, file, project).setAuthorLogin("luke.skywalker"));
-    saveIssue(IssueTesting.newDto(rule, file, project).setAuthorLogin("luke@skywalker.name"));
-    saveIssue(IssueTesting.newDto(rule, file, project));
-    saveIssue(IssueTesting.newDto(rule, file, project).setAuthorLogin("anakin@skywalker.name"));
-
-    assertThat(service.listAuthors(null, 5)).containsExactly("anakin@skywalker.name", "luke.skywalker", "luke@skywalker.name");
-    assertThat(service.listAuthors(null, 2)).containsExactly("anakin@skywalker.name", "luke.skywalker");
-    assertThat(service.listAuthors("uke", 5)).containsExactly("luke.skywalker", "luke@skywalker.name");
-    assertThat(service.listAuthors(null, 1)).containsExactly("anakin@skywalker.name");
-    assertThat(service.listAuthors(null, Integer.MAX_VALUE)).containsExactly("anakin@skywalker.name", "luke.skywalker", "luke@skywalker.name");
-  }
-
-  @Test
-  public void listAuthors_escapes_regexp_special_characters() {
-    saveIssue(IssueTesting.newDto(newRule(), newFile(newPublicProject()), newPublicProject()).setAuthorLogin("name++"));
-
-    assertThat(service.listAuthors("invalidRegexp[", 5)).isEmpty();
-    assertThat(service.listAuthors("nam+", 5)).isEmpty();
-    assertThat(service.listAuthors("name+", 5)).containsExactly("name++");
-    assertThat(service.listAuthors(".*", 5)).isEmpty();
-  }
-
-  private RuleDto newRule() {
-    return newRule(RuleTesting.newXooX1());
-  }
-
-  private RuleDto newRule(RuleDto rule) {
-    RuleDao ruleDao = tester.get(RuleDao.class);
-    ruleDao.insert(session, rule.getDefinition());
-    if (rule.getOrganizationUuid() != null) {
-      ruleDao.insertOrUpdate(session, rule.getMetadata().setRuleId(rule.getId()));
-    }
-    session.commit();
-    //FIXME ruleIndexer.commitAndIndex(dbSession, rule.getDefinition().getKey());
-    return rule;
-  }
-
-  private ComponentDto newPublicProject() {
-    OrganizationDto organization = OrganizationTesting.newOrganizationDto();
-    tester.get(OrganizationDao.class).insert(session, organization, false);
-    ComponentDto project = ComponentTesting.newPublicProjectDto(organization);
-    tester.get(ComponentDao.class).insert(session, project);
-    session.commit();
-
-    indexPermissions();
-    userSessionRule.logIn();
-
-    return project;
-  }
-
-  private void indexPermissions() {
-    PermissionIndexer permissionIndexer = tester.get(PermissionIndexer.class);
-    permissionIndexer.indexOnStartup(permissionIndexer.getIndexTypes());
-  }
-
-  private ComponentDto newFile(ComponentDto project) {
-    ComponentDto file = ComponentTesting.newFileDto(project, null);
-    tester.get(ComponentDao.class).insert(session, file);
-    session.commit();
-    return file;
-  }
-
-  private IssueDto saveIssue(IssueDto issue) {
-    tester.get(IssueDao.class).insert(session, issue);
-    session.commit();
-    tester.get(IssueIndexer.class).commitAndIndexIssues(session, asList(issue));
-    return issue;
-  }
-}
index 67f3e749b3fb3d34933ab77216a1b8892b184f25..b407a17cee9dbca8ca740e61184e17613ee0ae67 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.issue.index;
 
+import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Iterators;
 import java.util.ArrayList;
@@ -1157,6 +1158,42 @@ public class IssueIndexTest {
     underTest.listTags(organization, null, 501);
   }
 
+  @Test
+  public void test_listAuthors() {
+    OrganizationDto org = newOrganizationDto();
+    ComponentDto project = ComponentTesting.newPrivateProjectDto(org);
+    indexIssues(
+      newDoc("issue1", project).setAuthorLogin("luke.skywalker"),
+      newDoc("issue2", project).setAuthorLogin("luke@skywalker.name"),
+      newDoc("issue3", project).setAuthorLogin(null),
+      newDoc("issue4", project).setAuthorLogin("anakin@skywalker.name"));
+    IssueQuery query = IssueQuery.builder()
+      .checkAuthorization(false)
+      .build();
+
+    assertThat(underTest.listAuthors(query, null, 5)).containsExactly("anakin@skywalker.name", "luke.skywalker", "luke@skywalker.name");
+    assertThat(underTest.listAuthors(query, null, 2)).containsExactly("anakin@skywalker.name", "luke.skywalker");
+    assertThat(underTest.listAuthors(query, "uke", 5)).containsExactly("luke.skywalker", "luke@skywalker.name");
+    assertThat(underTest.listAuthors(query, null, 1)).containsExactly("anakin@skywalker.name");
+    assertThat(underTest.listAuthors(query, null, Integer.MAX_VALUE)).containsExactly("anakin@skywalker.name", "luke.skywalker", "luke@skywalker.name");
+  }
+
+  @Test
+  public void listAuthors_escapes_regexp_special_characters() {
+    OrganizationDto org = newOrganizationDto();
+    ComponentDto project = ComponentTesting.newPrivateProjectDto(org);
+    indexIssues(
+      newDoc("issue1", project).setAuthorLogin("name++"));
+    IssueQuery query = IssueQuery.builder()
+      .checkAuthorization(false)
+      .build();
+
+    assertThat(underTest.listAuthors(query, "invalidRegexp[", 5)).isEmpty();
+    assertThat(underTest.listAuthors(query, "nam+", 5)).isEmpty();
+    assertThat(underTest.listAuthors(query, "name+", 5)).containsExactly("name++");
+    assertThat(underTest.listAuthors(query, ".*", 5)).isEmpty();
+  }
+
   @Test
   public void filter_by_organization() {
     OrganizationDto org1 = newOrganizationDto();
@@ -1189,6 +1226,27 @@ public class IssueIndexTest {
     assertThatSearchReturnsEmpty(query);
   }
 
+  @Test
+  public void countTags() {
+    OrganizationDto org = newOrganizationDto();
+    ComponentDto project = ComponentTesting.newPrivateProjectDto(org);
+    indexIssues(
+      newDoc("issue1", project).setTags(ImmutableSet.of("convention", "java8", "bug")),
+      newDoc("issue2", project).setTags(ImmutableSet.of("convention", "bug")),
+      newDoc("issue3", project).setTags(emptyList()),
+      newDoc("issue4", project).setTags(ImmutableSet.of("convention", "java8", "bug")).setResolution(Issue.RESOLUTION_FIXED),
+      newDoc("issue5", project).setTags(ImmutableSet.of("convention"))
+    );
+
+    assertThat(underTest.countTags(projectQuery(project.uuid()), 5)).containsOnly(entry("convention", 3L), entry("bug", 2L), entry("java8", 1L));
+    assertThat(underTest.countTags(projectQuery(project.uuid()), 2)).contains(entry("convention", 3L), entry("bug", 2L)).doesNotContainEntry("java8", 1L);
+    assertThat(underTest.countTags(projectQuery("other"), 10)).isEmpty();
+  }
+
+  private IssueQuery projectQuery(String projectUuid) {
+    return IssueQuery.builder().projectUuids(singletonList(projectUuid)).resolved(false).build();
+  }
+
   private void verifyOrganizationFilter(String organizationUuid, String... expectedIssueKeys) {
     IssueQuery.Builder query = IssueQuery.builder().organizationUuid(organizationUuid);
     assertThatSearchReturnsOnly(query, expectedIssueKeys);
index e2830d2c3a0e56877330a760e719378dc112e119..2b344b0ba458d025e3a54d6790b7bcb2f4adc964 100644 (file)
@@ -26,7 +26,6 @@ import org.sonar.api.server.ws.WebService;
 import org.sonar.api.utils.System2;
 import org.sonar.db.DbTester;
 import org.sonar.server.es.EsTester;
-import org.sonar.server.issue.IssueService;
 import org.sonar.server.issue.index.IssueIndex;
 import org.sonar.server.issue.index.IssueIndexDefinition;
 import org.sonar.server.issue.index.IssueIndexer;
@@ -51,9 +50,8 @@ public class AuthorsActionTest {
 
   private IssueIndexer issueIndexer = new IssueIndexer(es.client(), db.getDbClient(), new IssueIteratorFactory(db.getDbClient()));
   private IssueIndex issueIndex = new IssueIndex(es.client(), System2.INSTANCE, userSession, new AuthorizationTypeSupport(userSession));
-  private IssueService issueService = new IssueService(issueIndex);
 
-  private WsActionTester ws = new WsActionTester(new AuthorsAction(issueService));
+  private WsActionTester ws = new WsActionTester(new AuthorsAction(issueIndex));
 
   @Test
   public void json_example() {
index f02c927b3a1f1792f897bf10b28072ec15a4eeac..b157ea73f717d132a5d5384cfcd2ecb397741acf 100644 (file)
@@ -28,7 +28,7 @@ import org.sonar.api.server.ws.WebService.Action;
 import org.sonar.api.server.ws.WebService.Param;
 import org.sonar.server.issue.IssueQuery;
 import org.sonar.server.issue.IssueQueryFactory;
-import org.sonar.server.issue.IssueService;
+import org.sonar.server.issue.index.IssueIndex;
 import org.sonar.server.ws.TestResponse;
 import org.sonar.server.ws.WsActionTester;
 import org.sonarqube.ws.client.issue.SearchWsRequest;
@@ -42,7 +42,7 @@ import static org.sonar.test.JsonAssert.assertJson;
 
 public class ComponentTagsActionTest {
 
-  private IssueService service = mock(IssueService.class);
+  private IssueIndex service = mock(IssueIndex.class);
   private IssueQueryFactory issueQueryFactory = mock(IssueQueryFactory.class, Mockito.RETURNS_DEEP_STUBS);
   private ComponentTagsAction underTest = new ComponentTagsAction(service, issueQueryFactory);
   private WsActionTester tester = new WsActionTester(underTest);
@@ -91,7 +91,7 @@ public class ComponentTagsActionTest {
       .build();
     ArgumentCaptor<SearchWsRequest> captor = ArgumentCaptor.forClass(SearchWsRequest.class);
     when(issueQueryFactory.create(captor.capture())).thenReturn(mock(IssueQuery.class));
-    when(service.listTagsForComponent(any(IssueQuery.class), eq(5))).thenReturn(tags);
+    when(service.countTags(any(IssueQuery.class), eq(5))).thenReturn(tags);
 
     TestResponse response = tester.newRequest()
       .setParam("componentUuid", "polop")
@@ -115,7 +115,7 @@ public class ComponentTagsActionTest {
       .build();
     ArgumentCaptor<SearchWsRequest> captor = ArgumentCaptor.forClass(SearchWsRequest.class);
     when(issueQueryFactory.create(captor.capture())).thenReturn(mock(IssueQuery.class));
-    when(service.listTagsForComponent(any(IssueQuery.class), eq(5))).thenReturn(tags);
+    when(service.countTags(any(IssueQuery.class), eq(5))).thenReturn(tags);
 
     String componentUuid = "polop";
     String createdAfter = "2011-04-25";
index fff219e6a1b7f380abe074b7faca4632b44e3139..a93c1f9e6f0febee2f6e542375c87cb1c5d10e80 100644 (file)
@@ -29,6 +29,6 @@ public class IssueWsModuleTest {
   public void verify_count_of_added_components() {
     ComponentContainer container = new ComponentContainer();
     new IssueWsModule().configure(container);
-    assertThat(container.size()).isEqualTo(2 + 30);
+    assertThat(container.size()).isEqualTo(2 + 29);
   }
 }