001/* 002 * SonarQube 003 * Copyright (C) 2009-2016 SonarSource SA 004 * mailto:contact AT sonarsource DOT com 005 * 006 * This program is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 3 of the License, or (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public License 017 * along with this program; if not, write to the Free Software Foundation, 018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 019 */ 020package org.sonarqube.ws.client; 021 022import com.google.common.base.Throwables; 023import java.io.ByteArrayInputStream; 024import java.io.IOException; 025import java.io.InputStream; 026import java.io.Reader; 027import java.io.StringReader; 028import java.net.HttpURLConnection; 029import java.nio.charset.StandardCharsets; 030import org.apache.commons.io.IOUtils; 031import org.sonarqube.ws.MediaTypes; 032 033import static java.util.Objects.requireNonNull; 034 035public class MockWsResponse extends BaseResponse { 036 037 private int code = HttpURLConnection.HTTP_OK; 038 private String requestUrl; 039 private byte[] content; 040 private String contentType; 041 042 @Override 043 public int code() { 044 return code; 045 } 046 047 public MockWsResponse setCode(int code) { 048 this.code = code; 049 return this; 050 } 051 052 @Override 053 public String contentType() { 054 requireNonNull(contentType); 055 return contentType; 056 } 057 058 public MockWsResponse setContentType(String contentType) { 059 this.contentType = contentType; 060 return this; 061 } 062 063 public MockWsResponse setRequestUrl(String requestUrl) { 064 this.requestUrl = requestUrl; 065 return this; 066 } 067 068 public MockWsResponse setContent(InputStream is) { 069 try { 070 return setContent(IOUtils.toByteArray(is)); 071 } catch (IOException e) { 072 throw Throwables.propagate(e); 073 } 074 } 075 076 public MockWsResponse setContent(byte[] b) { 077 this.content = b; 078 return this; 079 } 080 081 public MockWsResponse setContent(String s) { 082 this.content = s.getBytes(StandardCharsets.UTF_8); 083 return this; 084 } 085 086 @Override 087 public boolean hasContent() { 088 return content != null; 089 } 090 091 @Override 092 public String requestUrl() { 093 requireNonNull(requestUrl); 094 return requestUrl; 095 } 096 097 @Override 098 public InputStream contentStream() { 099 requireNonNull(content); 100 return new ByteArrayInputStream(content); 101 } 102 103 @Override 104 public Reader contentReader() { 105 requireNonNull(content); 106 return new StringReader(new String(content, StandardCharsets.UTF_8)); 107 } 108 109 @Override 110 public String content() { 111 requireNonNull(content); 112 return new String(content, StandardCharsets.UTF_8); 113 } 114 115 public static MockWsResponse createJson(String json) { 116 return new MockWsResponse() 117 .setContentType(MediaTypes.JSON) 118 .setContentType(json); 119 } 120}