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 com.google.common.collect.Maps;
023import java.io.InputStream;
024import java.util.Map;
025import javax.annotation.Nullable;
026import org.apache.commons.io.IOUtils;
027import org.sonar.api.server.ws.LocalConnector;
028import org.sonar.api.server.ws.Request;
029
030import static com.google.common.base.Preconditions.checkNotNull;
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 String mediaType = "application/json";
041
042  @Override
043  public String method() {
044    return "GET";
045  }
046
047  @Override
048  public String getMediaType() {
049    return mediaType;
050  }
051
052  public SimpleGetRequest setMediaType(String mediaType) {
053    checkNotNull(mediaType);
054    this.mediaType = mediaType;
055    return this;
056  }
057
058  @Override
059  public boolean hasParam(String key) {
060    return params.keySet().contains(key);
061  }
062
063  @Override
064  public String param(String key) {
065    return params.get(key);
066  }
067
068  @Override
069  public InputStream paramAsInputStream(String key) {
070    return IOUtils.toInputStream(param(key));
071  }
072
073  public SimpleGetRequest setParam(String key, @Nullable String value) {
074    if (value != null) {
075      params.put(key, value);
076    }
077    return this;
078  }
079
080  public Map<String, String> getParams() {
081    return params;
082  }
083
084  @Override
085  public LocalConnector localConnector() {
086    throw new UnsupportedOperationException();
087  }
088}