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.

MockWsResponse.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.io.ByteArrayInputStream;
  22. import java.io.InputStream;
  23. import java.io.Reader;
  24. import java.io.StringReader;
  25. import java.net.HttpURLConnection;
  26. import java.nio.charset.StandardCharsets;
  27. import java.util.Collections;
  28. import java.util.HashMap;
  29. import java.util.List;
  30. import java.util.Map;
  31. import java.util.Optional;
  32. import org.sonarqube.ws.MediaTypes;
  33. import static java.util.Objects.requireNonNull;
  34. import static java.util.stream.Collectors.*;
  35. public class MockWsResponse extends BaseResponse {
  36. private static final String CONTENT_TYPE_HEADER = "Content-Type";
  37. private static final String SQ_TOKEN_EXPIRATION_HEADER = "SonarQube-Authentication-Token-Expiration";
  38. private int code = HttpURLConnection.HTTP_OK;
  39. private String requestUrl;
  40. private byte[] content;
  41. private final Map<String, String> headers = new HashMap<>();
  42. @Override
  43. public int code() {
  44. return code;
  45. }
  46. public MockWsResponse setCode(int code) {
  47. this.code = code;
  48. return this;
  49. }
  50. @Override
  51. public String contentType() {
  52. return requireNonNull(headers.get(CONTENT_TYPE_HEADER));
  53. }
  54. @Override
  55. public Optional<String> header(String name) {
  56. return Optional.ofNullable(headers.get(name));
  57. }
  58. @Override
  59. public Map<String, List<String>> headers() {
  60. return headers.entrySet()
  61. .stream()
  62. .collect(toMap(Map.Entry::getKey, e -> Collections.singletonList(e.getValue())));
  63. }
  64. public MockWsResponse setContentType(String contentType) {
  65. headers.put(CONTENT_TYPE_HEADER, contentType);
  66. return this;
  67. }
  68. public MockWsResponse setExpirationDate(String expirationDate) {
  69. headers.put(SQ_TOKEN_EXPIRATION_HEADER, expirationDate);
  70. return this;
  71. }
  72. public MockWsResponse setRequestUrl(String requestUrl) {
  73. this.requestUrl = requestUrl;
  74. return this;
  75. }
  76. public MockWsResponse setContent(byte[] b) {
  77. this.content = b;
  78. return this;
  79. }
  80. public MockWsResponse setContent(String s) {
  81. this.content = s.getBytes(StandardCharsets.UTF_8);
  82. return this;
  83. }
  84. public MockWsResponse setHeader(String key, String value) {
  85. this.headers.put(key, value);
  86. return this;
  87. }
  88. @Override
  89. public boolean hasContent() {
  90. return content != null;
  91. }
  92. @Override
  93. public String requestUrl() {
  94. requireNonNull(requestUrl);
  95. return requestUrl;
  96. }
  97. @Override
  98. public InputStream contentStream() {
  99. requireNonNull(content);
  100. return new ByteArrayInputStream(content);
  101. }
  102. @Override
  103. public Reader contentReader() {
  104. requireNonNull(content);
  105. return new StringReader(new String(content, StandardCharsets.UTF_8));
  106. }
  107. @Override
  108. public String content() {
  109. requireNonNull(content);
  110. return new String(content, StandardCharsets.UTF_8);
  111. }
  112. public static MockWsResponse createJson(String json) {
  113. return new MockWsResponse()
  114. .setContentType(MediaTypes.JSON)
  115. .setContentType(json);
  116. }
  117. }