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.

BaseRequestTest.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.sonarqube.ws.client;
  21. import java.util.Arrays;
  22. import java.util.List;
  23. import org.assertj.core.data.MapEntry;
  24. import org.junit.Test;
  25. import org.sonarqube.ws.MediaTypes;
  26. import static java.util.Arrays.asList;
  27. import static java.util.Collections.singletonList;
  28. import static org.assertj.core.api.Assertions.assertThat;
  29. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  30. import static org.assertj.core.data.MapEntry.entry;
  31. public class BaseRequestTest {
  32. private FakeRequest underTest = new FakeRequest("api/foo");
  33. @Test
  34. public void test_defaults() {
  35. assertThat(underTest.getMethod()).isEqualTo(WsRequest.Method.GET);
  36. assertThat(underTest.getParams()).isEmpty();
  37. assertThat(underTest.getMediaType()).isEqualTo(MediaTypes.JSON);
  38. assertThat(underTest.getPath()).isEqualTo("api/foo");
  39. assertThat(underTest.getWriteTimeOutInMs()).isEmpty();
  40. }
  41. @Test
  42. public void set_write_timeout() {
  43. underTest.setWriteTimeOutInMs(30_000);
  44. assertThat(underTest.getWriteTimeOutInMs()).hasValue(30_000);
  45. }
  46. @Test
  47. public void setMediaType() {
  48. underTest.setMediaType(MediaTypes.PROTOBUF);
  49. assertThat(underTest.getMediaType()).isEqualTo(MediaTypes.PROTOBUF);
  50. }
  51. @Test
  52. public void keep_order_of_params() {
  53. assertThat(underTest.getParams()).isEmpty();
  54. assertThat(underTest.getParameters().getKeys()).isEmpty();
  55. underTest.setParam("keyB", "b");
  56. assertThat(underTest.getParams()).containsExactly(entry("keyB", "b"));
  57. assertParameters(entry("keyB", "b"));
  58. assertMultiValueParameters(entry("keyB", singletonList("b")));
  59. underTest.setParam("keyA", "a");
  60. assertThat(underTest.getParams()).containsExactly(entry("keyB", "b"), entry("keyA", "a"));
  61. assertParameters(entry("keyB", "b"), entry("keyA", "a"));
  62. assertMultiValueParameters(entry("keyB", singletonList("b")), entry("keyA", singletonList("a")));
  63. underTest.setParam("keyC", asList("c1", "c2", "c3"));
  64. assertParameters(entry("keyB", "b"), entry("keyA", "a"), entry("keyC", "c1"));
  65. assertMultiValueParameters(
  66. entry("keyB", singletonList("b")),
  67. entry("keyA", singletonList("a")),
  68. entry("keyC", asList("c1", "c2", "c3")));
  69. }
  70. @Test
  71. public void skip_null_value_in_multi_param() {
  72. underTest.setParam("key", asList("v1", null, "", "v3"));
  73. assertMultiValueParameters(entry("key", asList("v1", "", "v3")));
  74. }
  75. @Test
  76. public void null_param_value() {
  77. Boolean nullBool = null;
  78. underTest.setParam("key", nullBool);
  79. assertThat(underTest.getParams()).isEmpty();
  80. }
  81. @Test
  82. public void fail_if_null_param_key() {
  83. assertThatThrownBy(() -> underTest.setParam(null, "val"))
  84. .isInstanceOf(IllegalArgumentException.class);
  85. }
  86. @Test
  87. public void headers_are_empty_by_default() {
  88. assertThat(underTest.getHeaders().getNames()).isEmpty();
  89. }
  90. @Test
  91. public void set_and_get_headers() {
  92. underTest.setHeader("foo", "fooz");
  93. underTest.setHeader("bar", "barz");
  94. assertThat(underTest.getHeaders().getNames()).containsExactlyInAnyOrder("foo", "bar");
  95. assertThat(underTest.getHeaders().getValue("foo")).hasValue("fooz");
  96. assertThat(underTest.getHeaders().getValue("bar")).hasValue("barz");
  97. assertThat(underTest.getHeaders().getValue("xxx")).isEmpty();
  98. }
  99. private void assertParameters(MapEntry<String, String>... values) {
  100. Parameters parameters = underTest.getParameters();
  101. assertThat(parameters.getKeys()).extracting(key -> MapEntry.entry(key, parameters.getValue(key))).containsExactly(values);
  102. }
  103. private void assertMultiValueParameters(MapEntry<String, List<String>>... expectedParameters) {
  104. Parameters parameters = underTest.getParameters();
  105. String[] expectedKeys = Arrays.stream(expectedParameters).map(MapEntry::getKey).toArray(String[]::new);
  106. assertThat(parameters.getKeys()).containsExactly(expectedKeys);
  107. Arrays.stream(expectedParameters).forEach(expectedParameter -> {
  108. assertThat(parameters.getValues(expectedParameter.getKey())).containsExactly(expectedParameter.getValue().toArray(new String[0]));
  109. });
  110. }
  111. private static class FakeRequest extends BaseRequest<FakeRequest> {
  112. FakeRequest(String path) {
  113. super(path);
  114. }
  115. @Override
  116. public Method getMethod() {
  117. return Method.GET;
  118. }
  119. }
  120. }