您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

FormDataActionTest.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 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.edition.ws;
  21. import com.tngtech.java.junit.dataprovider.DataProviderRunner;
  22. import org.junit.Rule;
  23. import org.junit.Test;
  24. import org.junit.rules.ExpectedException;
  25. import org.junit.runner.RunWith;
  26. import org.sonar.api.platform.Server;
  27. import org.sonar.api.server.ws.WebService;
  28. import org.sonar.db.DbClient;
  29. import org.sonar.db.DbTester;
  30. import org.sonar.db.component.ComponentDto;
  31. import org.sonar.db.metric.MetricDto;
  32. import org.sonar.server.exceptions.ForbiddenException;
  33. import org.sonar.server.exceptions.UnauthorizedException;
  34. import org.sonar.server.tester.UserSessionRule;
  35. import org.sonar.server.ws.TestRequest;
  36. import org.sonar.server.ws.WsActionTester;
  37. import org.sonarqube.ws.Editions.FormDataResponse;
  38. import static org.assertj.core.api.Assertions.assertThat;
  39. import static org.mockito.Mockito.mock;
  40. import static org.mockito.Mockito.when;
  41. import static org.sonar.api.measures.CoreMetrics.NCLOC_KEY;
  42. import static org.sonar.api.measures.Metric.ValueType.INT;
  43. import static org.sonar.test.JsonAssert.assertJson;
  44. @RunWith(DataProviderRunner.class)
  45. public class FormDataActionTest {
  46. @Rule
  47. public ExpectedException expectedException = ExpectedException.none();
  48. @Rule
  49. public UserSessionRule userSessionRule = UserSessionRule.standalone();
  50. @Rule
  51. public DbTester db = DbTester.create();
  52. private Server server = mock(Server.class);
  53. private DbClient dbClient = db.getDbClient();
  54. private FormDataAction underTest = new FormDataAction(userSessionRule, server, dbClient);
  55. private WsActionTester ws = new WsActionTester(underTest);
  56. @Test
  57. public void definition() {
  58. WebService.Action def = ws.getDef();
  59. assertThat(def.key()).isEqualTo("form_data");
  60. assertThat(def.since()).isEqualTo("6.7");
  61. assertThat(def.isPost()).isFalse();
  62. assertThat(def.isInternal()).isTrue();
  63. assertThat(def.description()).isNotEmpty();
  64. assertThat(def.params()).isEmpty();
  65. }
  66. @Test
  67. public void request_fails_if_user_not_logged_in() {
  68. userSessionRule.anonymous();
  69. TestRequest request = ws.newRequest();
  70. expectedException.expect(UnauthorizedException.class);
  71. expectedException.expectMessage("Authentication is required");
  72. request.execute();
  73. }
  74. @Test
  75. public void request_fails_if_user_is_not_system_administer() {
  76. userSessionRule.logIn();
  77. TestRequest request = ws.newRequest();
  78. expectedException.expect(ForbiddenException.class);
  79. expectedException.expectMessage("Insufficient privileges");
  80. request.execute();
  81. }
  82. @Test
  83. public void json_example() {
  84. userSessionRule.logIn().setSystemAdministrator();
  85. when(server.getId()).thenReturn("uuid");
  86. setNcloc(12345L);
  87. String result = ws.newRequest().execute().getInput();
  88. assertJson(result).isSimilarTo(ws.getDef().responseExampleAsString());
  89. }
  90. @Test
  91. public void returns_server_id_and_nloc() {
  92. userSessionRule.logIn().setSystemAdministrator();
  93. when(server.getId()).thenReturn("myserver");
  94. long ncloc = 256L;
  95. setNcloc(ncloc);
  96. FormDataResponse expectedResponse = FormDataResponse.newBuilder()
  97. .setServerId("myserver")
  98. .setNcloc(ncloc)
  99. .build();
  100. FormDataResponse result = ws.newRequest().executeProtobuf(FormDataResponse.class);
  101. assertThat(result).isEqualTo(expectedResponse);
  102. }
  103. private void setNcloc(double ncloc) {
  104. ComponentDto project = db.components().insertMainBranch();
  105. MetricDto nclocMetric = db.measures().insertMetric(m -> m.setValueType(INT.toString()).setKey(NCLOC_KEY));
  106. db.measures().insertLiveMeasure(project, nclocMetric, m -> m.setValue(ncloc));
  107. }
  108. }