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.sonar.api.server.ws.internal; 021 022import static com.google.common.base.Preconditions.checkNotNull; 023 024import com.google.common.collect.Maps; 025import java.io.InputStream; 026import java.util.Map; 027import javax.annotation.Nullable; 028import org.apache.commons.io.IOUtils; 029import org.sonar.api.server.ws.LocalConnector; 030import org.sonar.api.server.ws.Request; 031 032/** 033 * Fake implementation of {@link org.sonar.api.server.ws.Request} used 034 * for testing. Call the method {@link #setParam(String, String)} to 035 * emulate some parameter values. 036 */ 037public class SimpleGetRequest extends Request { 038 039 private final Map<String, String> params = Maps.newHashMap(); 040 private final Map<String, Part> parts = Maps.newHashMap(); 041 private String mediaType = "application/json"; 042 private String path; 043 044 @Override 045 public String method() { 046 return "GET"; 047 } 048 049 @Override 050 public String getMediaType() { 051 return mediaType; 052 } 053 054 public SimpleGetRequest setMediaType(String mediaType) { 055 checkNotNull(mediaType); 056 this.mediaType = mediaType; 057 return this; 058 } 059 060 @Override 061 public boolean hasParam(String key) { 062 return params.keySet().contains(key); 063 } 064 065 @Override 066 public String param(String key) { 067 return params.get(key); 068 } 069 070 @Override 071 public InputStream paramAsInputStream(String key) { 072 return IOUtils.toInputStream(param(key)); 073 } 074 075 public SimpleGetRequest setParam(String key, @Nullable String value) { 076 if (value != null) { 077 params.put(key, value); 078 } 079 return this; 080 } 081 082 public Map<String, String> getParams() { 083 return params; 084 } 085 086 @Override 087 public Part paramAsPart(String key) { 088 return parts.get(key); 089 } 090 091 public SimpleGetRequest setPart(String key, InputStream input, String fileName) { 092 parts.put(key, new PartImpl(input, fileName)); 093 return this; 094 } 095 096 @Override 097 public LocalConnector localConnector() { 098 throw new UnsupportedOperationException(); 099 } 100 101 @Override 102 public String getPath() { 103 return path; 104 } 105 106 public SimpleGetRequest setPath(String path) { 107 this.path = path; 108 return this; 109 } 110 111}