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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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.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.HashMap;
  28. import java.util.Map;
  29. import java.util.Optional;
  30. import org.sonarqube.ws.MediaTypes;
  31. import static java.util.Objects.requireNonNull;
  32. public class MockWsResponse extends BaseResponse {
  33. private static final String CONTENT_TYPE_HEADER = "Content-Type";
  34. private int code = HttpURLConnection.HTTP_OK;
  35. private String requestUrl;
  36. private byte[] content;
  37. private final Map<String, String> headers = new HashMap<>();
  38. @Override
  39. public int code() {
  40. return code;
  41. }
  42. public MockWsResponse setCode(int code) {
  43. this.code = code;
  44. return this;
  45. }
  46. @Override
  47. public String contentType() {
  48. return requireNonNull(headers.get(CONTENT_TYPE_HEADER));
  49. }
  50. @Override
  51. public Optional<String> header(String name) {
  52. return Optional.ofNullable(headers.get(name));
  53. }
  54. public MockWsResponse setContentType(String contentType) {
  55. headers.put(CONTENT_TYPE_HEADER, contentType);
  56. return this;
  57. }
  58. public MockWsResponse setRequestUrl(String requestUrl) {
  59. this.requestUrl = requestUrl;
  60. return this;
  61. }
  62. public MockWsResponse setContent(byte[] b) {
  63. this.content = b;
  64. return this;
  65. }
  66. public MockWsResponse setContent(String s) {
  67. this.content = s.getBytes(StandardCharsets.UTF_8);
  68. return this;
  69. }
  70. @Override
  71. public boolean hasContent() {
  72. return content != null;
  73. }
  74. @Override
  75. public String requestUrl() {
  76. requireNonNull(requestUrl);
  77. return requestUrl;
  78. }
  79. @Override
  80. public InputStream contentStream() {
  81. requireNonNull(content);
  82. return new ByteArrayInputStream(content);
  83. }
  84. @Override
  85. public Reader contentReader() {
  86. requireNonNull(content);
  87. return new StringReader(new String(content, StandardCharsets.UTF_8));
  88. }
  89. @Override
  90. public String content() {
  91. requireNonNull(content);
  92. return new String(content, StandardCharsets.UTF_8);
  93. }
  94. public static MockWsResponse createJson(String json) {
  95. return new MockWsResponse()
  96. .setContentType(MediaTypes.JSON)
  97. .setContentType(json);
  98. }
  99. }