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.1KB

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