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 com.google.common.collect.Lists; 024import com.google.common.collect.Maps; 025import java.io.InputStream; 026import java.util.List; 027import java.util.Map; 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 com.google.common.base.Preconditions.checkNotNull; 035import static java.nio.charset.StandardCharsets.UTF_8; 036import static java.util.Collections.emptyList; 037import static java.util.Collections.singletonList; 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 = Maps.newHashMap(); 047 private final Map<String, Part> parts = Maps.newHashMap(); 048 private String mediaType = "application/json"; 049 private String path; 050 051 @Override 052 public String method() { 053 return "GET"; 054 } 055 056 @Override 057 public String getMediaType() { 058 return mediaType; 059 } 060 061 public SimpleGetRequest setMediaType(String mediaType) { 062 checkNotNull(mediaType); 063 this.mediaType = mediaType; 064 return this; 065 } 066 067 @Override 068 public boolean hasParam(String key) { 069 return params.keySet().contains(key); 070 } 071 072 @Override 073 public String param(String key) { 074 return params.get(key); 075 } 076 077 @Override 078 public List<String> multiParam(String key) { 079 String value = params.get(key); 080 return value == null ? emptyList() : singletonList(value); 081 } 082 083 @Override 084 @CheckForNull 085 public List<String> paramAsStrings(String key) { 086 String value = param(key); 087 if (value == null) { 088 return null; 089 } 090 return Lists.newArrayList(Splitter.on(',').omitEmptyStrings().trimResults().split(value)); 091 } 092 093 @Override 094 public InputStream paramAsInputStream(String key) { 095 return IOUtils.toInputStream(param(key), UTF_8); 096 } 097 098 public SimpleGetRequest setParam(String key, @Nullable String value) { 099 if (value != null) { 100 params.put(key, value); 101 } 102 return this; 103 } 104 105 public Map<String, String> getParams() { 106 return params; 107 } 108 109 @Override 110 public Part paramAsPart(String key) { 111 return parts.get(key); 112 } 113 114 public SimpleGetRequest setPart(String key, InputStream input, String fileName) { 115 parts.put(key, new PartImpl(input, fileName)); 116 return this; 117 } 118 119 @Override 120 public LocalConnector localConnector() { 121 throw new UnsupportedOperationException(); 122 } 123 124 @Override 125 public String getPath() { 126 return path; 127 } 128 129 public SimpleGetRequest setPath(String path) { 130 this.path = path; 131 return this; 132 } 133 134}