]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9271 Test WS api/issues/authors and its json example
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 23 May 2017 12:24:58 +0000 (14:24 +0200)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Wed, 24 May 2017 14:10:07 +0000 (16:10 +0200)
server/sonar-server/src/main/java/org/sonar/server/issue/ws/AuthorsAction.java
server/sonar-server/src/main/resources/org/sonar/server/issue/ws/authors-example.json
server/sonar-server/src/test/java/org/sonar/server/issue/ws/AuthorsActionTest.java
server/sonar-server/src/test/resources/org/sonar/server/issue/ws/AuthorsActionTest/authors.json [deleted file]

index 86bdf038f62fd1e3ee698c320dcbb6dc8260edae..61f43a6dbbc1b1b26240ca0d02dc2fe7909f23bb 100644 (file)
@@ -38,6 +38,23 @@ public class AuthorsAction implements IssuesWsAction {
     this.service = service;
   }
 
+  @Override
+  public void define(WebService.NewController controller) {
+    NewAction action = controller.createAction(ACTION_AUTHORS)
+      .setSince("5.1")
+      .setDescription("Search SCM accounts which match a given query")
+      .setResponseExample(Resources.getResource(this.getClass(), "authors-example.json"))
+      .setHandler(this);
+
+    action.createParam(Param.TEXT_QUERY)
+      .setDescription("A pattern to match SCM accounts against")
+      .setExampleValue("luke");
+    action.createParam(Param.PAGE_SIZE)
+      .setDescription("The size of the list to return")
+      .setExampleValue("25")
+      .setDefaultValue("10");
+  }
+
   @Override
   public void handle(Request request, Response response) throws Exception {
     String query = request.param(Param.TEXT_QUERY);
@@ -55,21 +72,4 @@ public class AuthorsAction implements IssuesWsAction {
       json.endArray().endObject();
     }
   }
-
-  @Override
-  public void define(WebService.NewController controller) {
-    NewAction action = controller.createAction(ACTION_AUTHORS)
-      .setSince("5.1")
-      .setDescription("Search SCM accounts which match a given query")
-      .setResponseExample(Resources.getResource(this.getClass(), "authors-example.json"))
-      .setHandler(this);
-
-    action.createParam(Param.TEXT_QUERY)
-      .setDescription("A pattern to match SCM accounts against")
-      .setExampleValue("luke");
-    action.createParam(Param.PAGE_SIZE)
-      .setDescription("The size of the list to return")
-      .setExampleValue("25")
-      .setDefaultValue("10");
-  }
 }
index 77500549a827021ad406b56c778b4b79dc795de4..67f3e8330eb059e8257f5ca8f1a0094e4d2354e2 100644 (file)
@@ -1,15 +1,6 @@
 {
-  "authors": {
-    "more": true,
-    "results": [
-      {
-        "key": "leia.organa",
-        "text": "leia.organa"
-      },
-      {
-        "key": "luke@skywalker.name",
-        "text": "luke@skywalker.name"
-      }
-    ]
-  }
+  "authors": [
+    "luke.skywalker",
+    "leia.organa"
+  ]
 }
index 2e6af4158942db197dad07004ae0bf5626cabe98..4221b769458bfc29de59cdb724bcea9ff43108fc 100644 (file)
  */
 package org.sonar.server.issue.ws;
 
-import org.junit.Before;
+import org.junit.Rule;
 import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Mock;
-import org.mockito.runners.MockitoJUnitRunner;
+import org.sonar.api.config.MapSettings;
 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.ws.WsTester;
+import org.sonar.server.issue.index.IssueIndex;
+import org.sonar.server.issue.index.IssueIndexDefinition;
+import org.sonar.server.issue.index.IssueIndexer;
+import org.sonar.server.issue.index.IssueIteratorFactory;
+import org.sonar.server.permission.index.AuthorizationTypeSupport;
+import org.sonar.server.tester.UserSessionRule;
+import org.sonar.server.ws.WsActionTester;
 
-import java.util.Arrays;
+import static java.util.Collections.emptySet;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE;
+import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY;
+import static org.sonar.test.JsonAssert.assertJson;
 
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-
-@RunWith(MockitoJUnitRunner.class)
 public class AuthorsActionTest {
+  @Rule
+  public DbTester db = DbTester.create();
+  @Rule
+  public EsTester es = new EsTester(new IssueIndexDefinition(new MapSettings()));
+  @Rule
+  public UserSessionRule userSession = UserSessionRule.standalone();
+
+  private IssueIndexer issueIndexer = new IssueIndexer(es.client(), 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));
+
+  @Test
+  public void json_example() {
+    db.issues().insertIssue(issue -> issue.setAuthorLogin("luke.skywalker"));
+    db.issues().insertIssue(issue -> issue.setAuthorLogin("leia.organa"));
+    issueIndexer.indexOnStartup(emptySet());
+
+    String result = ws.newRequest().execute().getInput();
+
+    assertJson(result).isSimilarTo(ws.getDef().responseExampleAsString());
+  }
 
-  WebService.Controller controller;
+  @Test
+  public void search_by_query() {
+    String leia = "leia.organa";
+    String luke = "luke.skywalker";
+    db.issues().insertIssue(issue -> issue.setAuthorLogin(leia));
+    db.issues().insertIssue(issue -> issue.setAuthorLogin(luke));
+    issueIndexer.indexOnStartup(emptySet());
 
-  WsTester tester;
+    String result = ws.newRequest()
+      .setParam(TEXT_QUERY, "leia")
+      .execute().getInput();
 
-  @Mock
-  IssueService service;
+    assertThat(result).contains(leia).doesNotContain(luke);
+  }
 
-  @Before
-  public void setUp() {
-    tester = new WsTester(new IssuesWs(new AuthorsAction(service)));
-    controller = tester.controller("api/issues");
+  @Test
+  public void set_page_size() {
+    String han = "han.solo";
+    String leia = "leia.organa";
+    String luke = "luke.skywalker";
+    db.issues().insertIssue(issue -> issue.setAuthorLogin(han));
+    db.issues().insertIssue(issue -> issue.setAuthorLogin(leia));
+    db.issues().insertIssue(issue -> issue.setAuthorLogin(luke));
+    issueIndexer.indexOnStartup(emptySet());
+
+    String result = ws.newRequest()
+      .setParam(PAGE_SIZE, "2")
+      .execute().getInput();
+
+    assertThat(result).contains(han, leia).doesNotContain(luke);
   }
 
   @Test
-  public void fetch_authors() throws Exception {
-    String query = "luke";
-    int pageSize = 42;
-    when(service.listAuthors(query, pageSize))
-      .thenReturn(Arrays.asList("luke.skywalker", "luke@skywalker.name"));
-
-    tester.newGetRequest("api/issues", "authors")
-      .setParam("q", query)
-      .setParam("ps", Integer.toString(pageSize))
-      .execute()
-      .assertJson(getClass(), "authors.json");
-
-    verify(service).listAuthors(query, pageSize);
+  public void definition() {
+    WebService.Action definition = ws.getDef();
+
+    assertThat(definition.key()).isEqualTo("authors");
+    assertThat(definition.since()).isEqualTo("5.1");
+    assertThat(definition.isInternal()).isFalse();
+    assertThat(definition.responseExampleAsString()).isNotEmpty();
+    assertThat(definition.params()).extracting(WebService.Param::key).containsOnly("q", "ps");
   }
 }
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/issue/ws/AuthorsActionTest/authors.json b/server/sonar-server/src/test/resources/org/sonar/server/issue/ws/AuthorsActionTest/authors.json
deleted file mode 100644 (file)
index 190c076..0000000
+++ /dev/null
@@ -1,6 +0,0 @@
-{
-  "authors": [
-    "luke.skywalker",
-    "luke@skywalker.name"
-  ]
-}