
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.StringUtils;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author Alexander Zagniotov
* @since 6/14/12, 1:21 AM
*/
@SuppressWarnings("unchecked")
public final class StubHttpLifecycle {
private final AtomicInteger resourceId = new AtomicInteger(-1);
public static final StubHttpLifecycle NULL = null;
private String marshalledYaml;
private StubRequest request;
private Object response;
private int responseSequenceCounter = 0;
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() {
final StubResponse actualStubbedResponse = getActualStubbedResponse();
actualStubbedResponse.addResourceIDHeader(resourceId.get());
return actualStubbedResponse;
}
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 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 getMarshalledYaml() {
return marshalledYaml;
}
public void setMarshalledYaml(final String marshalledYaml) {
this.marshalledYaml = marshalledYaml;
}
public void setResourceId(final int listIndex) {
resourceId.set(listIndex);
}
@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