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.

UserMetricsActionTest.java 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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.metric.ws;
  21. import com.google.common.collect.Lists;
  22. import org.junit.Test;
  23. import org.sonar.api.measures.Metric;
  24. import org.sonar.api.measures.Metric.ValueType;
  25. import org.sonar.server.metric.MetricFinder;
  26. import org.sonar.server.ws.WsActionTester;
  27. import static org.assertj.core.api.Assertions.assertThat;
  28. import static org.mockito.Mockito.mock;
  29. import static org.mockito.Mockito.when;
  30. import static org.sonar.test.JsonAssert.assertJson;
  31. public class UserMetricsActionTest {
  32. MetricFinder metrics = mock(MetricFinder.class);
  33. WsActionTester ws = new WsActionTester(new UserMetricsAction(metrics));
  34. @Test
  35. public void test_definition() {
  36. assertThat(ws.getDef().key()).isEqualTo("user_metrics");
  37. assertThat(ws.getDef().isInternal()).isTrue();
  38. assertThat(ws.getDef().responseExampleAsString()).isNotEmpty();
  39. assertThat(ws.getDef().params()).isEmpty();
  40. }
  41. @Test
  42. public void should_list_manual_metrics() {
  43. Metric m1 = mock(Metric.class);
  44. when(m1.getUserManaged()).thenReturn(true);
  45. when(m1.getKey()).thenReturn("m1");
  46. when(m1.getName()).thenReturn("Metric 1");
  47. when(m1.getType()).thenReturn(ValueType.STRING);
  48. Metric m2 = mock(Metric.class);
  49. when(m2.getUserManaged()).thenReturn(false);
  50. Metric m3 = mock(Metric.class);
  51. when(m3.getUserManaged()).thenReturn(true);
  52. when(m3.getKey()).thenReturn("m3");
  53. when(m3.getName()).thenReturn("Metric 3");
  54. when(m3.getType()).thenReturn(ValueType.STRING);
  55. Metric m4 = mock(Metric.class);
  56. when(m4.getUserManaged()).thenReturn(true);
  57. when(m4.getKey()).thenReturn("m3");
  58. when(m4.getName()).thenReturn("Metric 4");
  59. when(m4.getType()).thenReturn(ValueType.INT);
  60. when(metrics.findAll()).thenReturn(Lists.newArrayList(m1, m2, m3, m4));
  61. assertJson(ws.newRequest().execute().getInput()).isSimilarTo(getClass().getResource("UserMetricsActionTest/app.json"));
  62. }
  63. }