All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.micromata.genome.tpsb.httpmockup.testbuilder.HttpScenario Maven / Gradle / Ivy

The newest version!
//
// Copyright (C) 2010-2016 Micromata GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package de.micromata.genome.tpsb.httpmockup.testbuilder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import de.micromata.genome.tpsb.CommonTestBuilder;
import de.micromata.genome.tpsb.builder.IniLikeScenario;
import de.micromata.genome.tpsb.httpmockup.MockHttpServletRequest;
import de.micromata.genome.tpsb.httpmockup.MockHttpServletResponse;
import de.micromata.genome.util.matcher.Matcher;
import de.micromata.genome.util.matcher.norm.NormBooleanMatcherFactory;
import de.micromata.genome.util.types.Pair;

public class HttpScenario extends IniLikeScenario
{
  private static final String HTTP_REQUEST_SECTION = "HttpRequest";

  private static final String HTTP_REQUEST_PARAMETER_SECTION = "HttpRequestParameters";

  private static final String HTTP_RESPONSE_SECTION = "HttpReponse";

  private HttpParser requestparser;

  public HttpScenario(String text)
  {
    super(text);
    requestparser = new HttpParser(getSectionContent(HTTP_REQUEST_SECTION));
  }

  public MockHttpServletRequest getRequest()
  {
    MockHttpServletRequest ret = new MockHttpServletRequest();
    ret.setMethod(requestparser.getMethod());
    ret.setPathInfo(requestparser.getUrl());
    for (Pair hp : requestparser.getHeaders()) {
      ret.addHeader(hp.getKey(), hp.getValue());
    }
    ret.setRequestData(org.apache.commons.codec.binary.StringUtils.getBytesUtf8(requestparser.getBody()));
    List> params = new ArrayList>(requestparser.getRequestParameters());
    for (Map.Entry me : getSectionAsProperties(HTTP_REQUEST_PARAMETER_SECTION).entrySet()) {
      params.add(Pair.make(me.getKey(), me.getValue()));
    }
    Map> pm = new HashMap>();

    for (Pair p : params) {
      List val = pm.get(p.getKey());
      if (val == null) {
        val = new ArrayList();
        pm.put(p.getKey(), val);
      }
      val.add(p.getValue());
    }
    for (Map.Entry> me : pm.entrySet()) {
      ret.getParameterMap().put(me.getKey(), me.getValue().toArray(new String[] {}));
    }
    return ret;
  }

  private > void checkMatches(String key, String expected, String given, T testBuilder)
  {
    if (StringUtils.isBlank(expected) == true) {
      return;
    }
    if (matches(expected, given) == false) {
      testBuilder.fail(key + " not matches. expected: " + expected + " but is " + given);
    }
  }

  private boolean matches(String expected, String given)
  {
    Matcher matcher = new NormBooleanMatcherFactory().createMatcher(expected);
    return matcher.match(given);
  }

  public > void checkExpected(T testBuilder, MockHttpServletResponse resp)
  {
    Map props = getSectionAsProperties(HTTP_RESPONSE_SECTION);
    String status = props.get("status");
    checkMatches("status", status, "" + resp.getStatus(), testBuilder);

    for (Map.Entry me : props.entrySet()) {
      if (me.getKey().startsWith("header_") == true) {
        List tl = resp.getHeaderMap().get(me.getKey().substring("header_".length()));
        String ts = "";
        if (tl != null && tl.isEmpty() == false) {
          ts = tl.get(0).toString();
        }

        checkMatches(me.getKey(), me.getValue(), ts, testBuilder);
      }
    }
    String body = props.get("body");
    checkMatches("body", body, resp.getOutputString(), testBuilder);
  }
}