
by.stub.yaml.stubs.StubHttpLifecycle Maven / Gradle / Ivy
/*
A Java-based HTTP stub server
Copyright (C) 2012 Alexander Zagniotov, Isa Goksu and Eric Mrak
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
*/
package by.stub.yaml.stubs;
import by.stub.utils.ReflectionUtils;
import by.stub.utils.StringUtils;
import java.util.LinkedList;
import java.util.List;
/**
* @author Alexander Zagniotov
* @since 6/14/12, 1:21 AM
*/
@SuppressWarnings("unchecked")
public class StubHttpLifecycle {
public static final StubHttpLifecycle NULL = null;
private String httpLifeCycleAsYaml;
private StubRequest request;
private Object response;
private int responseSequenceCounter = 0;
private String requestAsYaml;
private String responseAsYaml;
public StubHttpLifecycle() {
response = StubResponse.newStubResponse();
}
public void setRequest(final StubRequest request) {
this.request = request;
}
public void setResponse(final Object response) {
this.response = response;
}
public StubRequest getRequest() {
return request;
}
public StubResponse getResponse() {
return getActualStubbedResponse();
}
public List getAllResponses() {
if (response instanceof StubResponse) {
return new LinkedList() {{
add((StubResponse) response);
}};
}
return (LinkedList) response;
}
public boolean isRestricted() {
return StringUtils.isSet(getAuthorizationHeader());
}
public boolean hasNotAuthorized(final StubHttpLifecycle assertingLifecycle) {
final String stubbedAuthorization = getAuthorizationHeader();
final String assertingAuthorization = assertingLifecycle.getAuthorizationHeader();
return !stubbedAuthorization.equals(assertingAuthorization);
}
private String getAuthorizationHeader() {
return request.getHeaders().get(StubRequest.AUTH_HEADER);
}
public String getResourceId() {
return getAllResponses().get(0).getHeaders().get(StubResponse.STUBBY_RESOURCE_ID_HEADER);
}
public StubResponse getActualStubbedResponse() {
if (response instanceof StubResponse) {
return (StubResponse) response;
}
final List responses = (LinkedList) response;
if (responses.isEmpty()) {
return StubResponse.newStubResponse();
}
final StubResponse sequenceStubResponse = responses.get(responseSequenceCounter);
responseSequenceCounter = (responseSequenceCounter + 1 == responses.size() ? responseSequenceCounter = 0 : ++responseSequenceCounter);
return sequenceStubResponse;
}
public String getHttpLifeCycleAsYaml() {
return httpLifeCycleAsYaml;
}
public void setHttpLifeCycleAsYaml(final String httpLifeCycleAsYaml) {
this.httpLifeCycleAsYaml = httpLifeCycleAsYaml;
}
public String getRequestAsYaml() {
return requestAsYaml;
}
public void setRequestAsYaml(final String requestAsYaml) {
this.requestAsYaml = requestAsYaml;
}
public String getResponseAsYaml() {
return responseAsYaml;
}
public void setResponseAsYaml(final String responseAsYaml) {
this.responseAsYaml = responseAsYaml;
}
public void setResourceId(final int listIndex) {
for (final StubResponse response : getAllResponses()) {
response.addResourceIDHeader(listIndex);
}
}
public String getAjaxResponseContent(final StubTypes stubType, final String propertyName) throws Exception {
switch (stubType) {
case REQUEST:
return StringUtils.objectToString(ReflectionUtils.getPropertyValue(request, propertyName));
case RESPONSE:
return StringUtils.objectToString(ReflectionUtils.getPropertyValue(getResponse(), propertyName));
default:
return StringUtils.objectToString(ReflectionUtils.getPropertyValue(this, propertyName));
}
}
public String getAjaxResponseContent(final String propertyName, final int sequencedResponseId) throws Exception {
final List allResponses = getAllResponses();
final StubResponse sequencedResponse = allResponses.get(sequencedResponseId);
return StringUtils.objectToString(ReflectionUtils.getPropertyValue(sequencedResponse, propertyName));
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (!(o instanceof StubHttpLifecycle)) {
return false;
}
final StubHttpLifecycle that = (StubHttpLifecycle) o;
if (!request.equals(that.request)) {
return false;
}
return true;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy