From: Teryk Bellahsene Date: Tue, 23 May 2017 12:24:58 +0000 (+0200) Subject: SONAR-9271 Test WS api/issues/authors and its json example X-Git-Tag: 6.5-M1~167 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=ded7a2f9b284345e15c38fa7504d11a6419d7875;p=sonarqube.git SONAR-9271 Test WS api/issues/authors and its json example --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/AuthorsAction.java b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/AuthorsAction.java index 86bdf038f62..61f43a6dbbc 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/AuthorsAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/AuthorsAction.java @@ -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"); - } } diff --git a/server/sonar-server/src/main/resources/org/sonar/server/issue/ws/authors-example.json b/server/sonar-server/src/main/resources/org/sonar/server/issue/ws/authors-example.json index 77500549a82..67f3e8330eb 100644 --- a/server/sonar-server/src/main/resources/org/sonar/server/issue/ws/authors-example.json +++ b/server/sonar-server/src/main/resources/org/sonar/server/issue/ws/authors-example.json @@ -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" + ] } diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/ws/AuthorsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/ws/AuthorsActionTest.java index 2e6af415894..4221b769458 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/issue/ws/AuthorsActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/issue/ws/AuthorsActionTest.java @@ -19,49 +19,93 @@ */ 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 index 190c0760d08..00000000000 --- a/server/sonar-server/src/test/resources/org/sonar/server/issue/ws/AuthorsActionTest/authors.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "authors": [ - "luke.skywalker", - "luke@skywalker.name" - ] -}