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