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