001/* 002 * SonarQube 003 * Copyright (C) 2009-2017 SonarSource SA 004 * mailto:info 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.sonar.api.server.ws.internal; 021 022import com.google.common.base.Splitter; 023import java.io.InputStream; 024import java.util.HashMap; 025import java.util.List; 026import java.util.Map; 027import java.util.Optional; 028import javax.annotation.CheckForNull; 029import javax.annotation.Nullable; 030import org.apache.commons.io.IOUtils; 031import org.sonar.api.server.ws.LocalConnector; 032import org.sonar.api.server.ws.Request; 033 034import static java.nio.charset.StandardCharsets.UTF_8; 035import static java.util.Collections.emptyList; 036import static java.util.Collections.singletonList; 037import static java.util.Objects.requireNonNull; 038 039/** 040 * Fake implementation of {@link org.sonar.api.server.ws.Request} used 041 * for testing. Call the method {@link #setParam(String, String)} to 042 * emulate some parameter values. 043 */ 044public class SimpleGetRequest extends Request { 045 046 private final Map<String, String> params = new HashMap<>(); 047 private final Map<String, Part> parts = new HashMap<>(); 048 private final Map<String, String> headers = new HashMap<>(); 049 private String mediaType = "application/json"; 050 private String path; 051 052 @Override 053 public String method() { 054 return "GET"; 055 } 056 057 @Override 058 public String getMediaType() { 059 return mediaType; 060 } 061 062 public SimpleGetRequest setMediaType(String mediaType) { 063 requireNonNull(mediaType); 064 this.mediaType = mediaType; 065 return this; 066 } 067 068 @Override 069 public boolean hasParam(String key) { 070 return params.keySet().contains(key); 071 } 072 073 @Override 074 public String param(String key) { 075 return params.get(key); 076 } 077 078 @Override 079 public List<String> multiParam(String key) { 080 String value = params.get(key); 081 return value == null ? emptyList() : singletonList(value); 082 } 083 084 @Override 085 @CheckForNull 086 public List<String> paramAsStrings(String key) { 087 String value = param(key); 088 if (value == null) { 089 return null; 090 } 091 return Splitter.on(',').omitEmptyStrings().trimResults().splitToList(value); 092 } 093 094 @Override 095 public InputStream paramAsInputStream(String key) { 096 return IOUtils.toInputStream(param(key), UTF_8); 097 } 098 099 public SimpleGetRequest setParam(String key, @Nullable String value) { 100 if (value != null) { 101 params.put(key, value); 102 } 103 return this; 104 } 105 106 public Map<String, String> getParams() { 107 return params; 108 } 109 110 @Override 111 public Part paramAsPart(String key) { 112 return parts.get(key); 113 } 114 115 public SimpleGetRequest setPart(String key, InputStream input, String fileName) { 116 parts.put(key, new PartImpl(input, fileName)); 117 return this; 118 } 119 120 @Override 121 public LocalConnector localConnector() { 122 throw new UnsupportedOperationException(); 123 } 124 125 @Override 126 public String getPath() { 127 return path; 128 } 129 130 public SimpleGetRequest setPath(String path) { 131 this.path = path; 132 return this; 133 } 134 135 @Override 136 public Optional<String> header(String name) { 137 return Optional.ofNullable(headers.get(name)); 138 } 139 140 public SimpleGetRequest setHeader(String name, String value) { 141 headers.put(name, value); 142 return this; 143 } 144}