You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ComponentTagsActionTest.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.issue.ws;
  21. import com.google.common.collect.ImmutableMap;
  22. import java.util.Map;
  23. import org.junit.Test;
  24. import org.mockito.ArgumentCaptor;
  25. import org.mockito.Mockito;
  26. import org.sonar.api.server.ws.WebService.Action;
  27. import org.sonar.api.server.ws.WebService.Param;
  28. import org.sonar.server.issue.index.IssueQuery;
  29. import org.sonar.server.issue.index.IssueQueryFactory;
  30. import org.sonar.server.issue.index.IssueIndex;
  31. import org.sonar.server.ws.TestResponse;
  32. import org.sonar.server.ws.WsActionTester;
  33. import org.sonar.server.issue.SearchRequest;
  34. import static org.assertj.core.api.Assertions.assertThat;
  35. import static org.mockito.ArgumentMatchers.any;
  36. import static org.mockito.ArgumentMatchers.eq;
  37. import static org.mockito.Mockito.mock;
  38. import static org.mockito.Mockito.when;
  39. import static org.sonar.test.JsonAssert.assertJson;
  40. public class ComponentTagsActionTest {
  41. private IssueIndex service = mock(IssueIndex.class);
  42. private IssueQueryFactory issueQueryFactory = mock(IssueQueryFactory.class, Mockito.RETURNS_DEEP_STUBS);
  43. private ComponentTagsAction underTest = new ComponentTagsAction(service, issueQueryFactory);
  44. private WsActionTester tester = new WsActionTester(underTest);
  45. @Test
  46. public void should_define() {
  47. Action action = tester.getDef();
  48. assertThat(action.description()).isNotEmpty();
  49. assertThat(action.responseExampleAsString()).isNotEmpty();
  50. assertThat(action.isPost()).isFalse();
  51. assertThat(action.isInternal()).isTrue();
  52. assertThat(action.handler()).isEqualTo(underTest);
  53. assertThat(action.params()).hasSize(3);
  54. Param query = action.param("componentUuid");
  55. assertThat(query.isRequired()).isTrue();
  56. assertThat(query.description()).isNotEmpty();
  57. assertThat(query.exampleValue()).isNotEmpty();
  58. Param createdAfter = action.param("createdAfter");
  59. assertThat(createdAfter.isRequired()).isFalse();
  60. assertThat(createdAfter.description()).isNotEmpty();
  61. assertThat(createdAfter.exampleValue()).isNotEmpty();
  62. Param pageSize = action.param("ps");
  63. assertThat(pageSize.isRequired()).isFalse();
  64. assertThat(pageSize.defaultValue()).isEqualTo("10");
  65. assertThat(pageSize.description()).isNotEmpty();
  66. assertThat(pageSize.exampleValue()).isNotEmpty();
  67. }
  68. @Test
  69. public void should_return_empty_list() {
  70. TestResponse response = tester.newRequest()
  71. .setParam("componentUuid", "polop")
  72. .execute();
  73. assertJson(response.getInput()).isSimilarTo("{\"tags\":[]}");
  74. }
  75. @Test
  76. public void should_return_tag_list() {
  77. Map<String, Long> tags = ImmutableMap.<String, Long>builder()
  78. .put("convention", 2771L)
  79. .put("brain-overload", 998L)
  80. .put("cwe", 89L)
  81. .put("bug", 32L)
  82. .put("cert", 2L)
  83. .build();
  84. ArgumentCaptor<SearchRequest> captor = ArgumentCaptor.forClass(SearchRequest.class);
  85. when(issueQueryFactory.create(captor.capture())).thenReturn(mock(IssueQuery.class));
  86. when(service.countTags(any(IssueQuery.class), eq(5))).thenReturn(tags);
  87. TestResponse response = tester.newRequest()
  88. .setParam("componentUuid", "polop")
  89. .setParam("ps", "5")
  90. .execute();
  91. assertJson(response.getInput()).isSimilarTo(getClass().getResource("ComponentTagsActionTest/component-tags.json"));
  92. assertThat(captor.getValue().getComponentUuids()).containsOnly("polop");
  93. assertThat(captor.getValue().getResolved()).isFalse();
  94. assertThat(captor.getValue().getCreatedAfter()).isNull();
  95. }
  96. @Test
  97. public void should_return_tag_list_with_created_after() {
  98. Map<String, Long> tags = ImmutableMap.<String, Long>builder()
  99. .put("convention", 2771L)
  100. .put("brain-overload", 998L)
  101. .put("cwe", 89L)
  102. .put("bug", 32L)
  103. .put("cert", 2L)
  104. .build();
  105. ArgumentCaptor<SearchRequest> captor = ArgumentCaptor.forClass(SearchRequest.class);
  106. when(issueQueryFactory.create(captor.capture())).thenReturn(mock(IssueQuery.class));
  107. when(service.countTags(any(IssueQuery.class), eq(5))).thenReturn(tags);
  108. String componentUuid = "polop";
  109. String createdAfter = "2011-04-25";
  110. TestResponse response = tester.newRequest()
  111. .setParam("componentUuid", componentUuid)
  112. .setParam("createdAfter", createdAfter)
  113. .setParam("ps", "5")
  114. .execute();
  115. assertJson(response.getInput()).isSimilarTo(getClass().getResource("ComponentTagsActionTest/component-tags.json"));
  116. assertThat(captor.getValue().getComponentUuids()).containsOnly(componentUuid);
  117. assertThat(captor.getValue().getResolved()).isFalse();
  118. assertThat(captor.getValue().getCreatedAfter()).isEqualTo(createdAfter);
  119. }
  120. }