Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/main/java/com/dougnoel/sentinel/apis/API.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
package com.dougnoel.sentinel.apis;

import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.client.utils.URIBuilder;
import com.dougnoel.sentinel.configurations.Configuration;
import com.dougnoel.sentinel.enums.YAMLObjectType;
import com.dougnoel.sentinel.exceptions.IOException;
import com.dougnoel.sentinel.system.YAMLObject;

import io.cucumber.java.Scenario;
import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.servers.Server;
import io.swagger.v3.parser.core.models.SwaggerParseResult;

public class API extends YAMLObject {
private Request request = new Request();
protected Map<Scenario,ResponseSet> responses = new HashMap<>();

/**
* Constructor
Expand Down Expand Up @@ -44,8 +48,33 @@ protected URIBuilder getURIBuilder(String passedText) {
}
}

/**
* Returns the currently constructed request for this API.
*
* @return com.dougnoel.sentinel.apis.Request the currently constructed request.
*/
public Request getRequest() {
return request;
}

/**
* Stores a Response based on the key passed.
*
* @param key String the key to store the Response under
* @param com.dougnoel.sentinel.apis.Response the Response object to store
*/
public void setResponse(String key, Response response) {
responses.put(key, response);
}

/**
* Returns the stored response for the given key.
*
* @param key String the key the Response is stored under
* @return com.dougnoel.sentinel.apis.Response the Response object requested
*/
public Response getResponse(String key) {
return responses.get(key);
}

}
34 changes: 28 additions & 6 deletions src/main/java/com/dougnoel/sentinel/apis/APIManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.dougnoel.sentinel.enums.RequestType;
import com.dougnoel.sentinel.system.TestManager;

import io.cucumber.java.Scenario;

/**
* Tracks which API is currently being used and requests the APIFactory create it if it does not exist.
* @author [email protected]
Expand All @@ -14,6 +16,7 @@ public class APIManager {
//Only one API should be in use at a time. We are consciously not multi-threading.
private static API api = null;
private static Response response = null;
private static Scenario scenario = null;

private APIManager() {
// Exists only to defeat instantiation.
Expand Down Expand Up @@ -75,23 +78,42 @@ public static void addHeader(String name, String value) {
* @param endpoint the endpoint to send the request
*/
public static void sendRequest(RequestType type, String endpoint) {
getAPI().getRequest().createAndSendRequest(type, endpoint);
response = getAPI().getRequest().createAndSendRequest(type, endpoint);
getAPI().setResponse(endpoint, response);
}

/**
* Returns the most recent response.
* @return Response the response
* @return com.dougnoel.sentinel.apis.Response the Response object requested
*/
public static Response getResponse() {
return response;
}

/**
* Returns the response stored for the given endpoint under the currently active API.
*
* @param endpoint String the name of the endpoint the request was sent to
* @return com.dougnoel.sentinel.apis.Response the Response object requested
*/
public static Response getResponse(String endpoint) {
return getAPI().getResponse(endpoint);
}

/**
* Sets the most recent response.
* @param response Response the response
* @return Scenario the scenario
*/
public static void setResponse(Response response) {
APIManager.response = response;
public static Scenario getScenario() {
return scenario;
}

/**
* @param scenario Scenario the scenario to set
*/
public static void setScenario(Scenario scenario) {
APIManager.scenario = scenario;
}



}
18 changes: 10 additions & 8 deletions src/main/java/com/dougnoel/sentinel/apis/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,14 @@ public void setBody(String body) {
}

/**
* Construct a request, send it to the active API, and store the response for retrieval.
* Construct a request, send it to the active API, and return the response.
* Parameterization is handled at the cucumber step level.
*
* @param type com.dougnoel.sentinel.enums.RequestType the type of request to send
* @param endpoint the endpoint to send the request
* @return com.dougnoel.sentinel.apis.Response the response encapsulated in a Response object
*/
public void createAndSendRequest(RequestType type, String endpoint) {
public Response createAndSendRequest(RequestType type, String endpoint) {
try {
switch(type) {
case DELETE:
Expand All @@ -124,14 +125,16 @@ public void createAndSendRequest(RequestType type, String endpoint) {
}
setHeaders();
buildURI();
sendRequest();
return sendRequest();
}

/**
* Send the request, store the response for later retrieval, and reset the request so it can be used again
* by the API for another request.
* Returns the response from the server after passing the request to the API server.
* Clears out the request so that the request object can be used again.
*
* @return com.dougnoel.sentinel.apis.Response the response encapsulated in a Response object
*/
private void sendRequest() {
private Response sendRequest() {
HttpClient httpClient = HttpClientBuilder.create().build();
Response response;
try {
Expand All @@ -141,9 +144,8 @@ private void sendRequest() {
} catch (java.io.IOException e) {
throw new IOException(e);
}
log.trace("Response Code: {} Response: {}", response.getResponseCode(), response.getResponse());
APIManager.setResponse(response);
reset();
return response;
}

/**
Expand Down
51 changes: 49 additions & 2 deletions src/main/java/com/dougnoel/sentinel/apis/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.dougnoel.sentinel.strings.SentinelStringUtils;

/**
* Wrapper for an hhtp response for testing the response.
* Wrapper for an http response for testing the response.
* @author dougnoel
*
*/
Expand All @@ -19,7 +19,7 @@ public class Response {

/**
*
* @param httpResponse HttpResponse the aPI call response used to create this object
* @param httpResponse HttpResponse the API call response used to create this object
* @throws IOException if the parsing fails
*/
public Response(HttpResponse httpResponse) throws IOException {
Expand All @@ -29,8 +29,11 @@ public Response(HttpResponse httpResponse) throws IOException {

/**
* Returns the http response as a String
* @deprecated
* This method is no longer needed for comparison operations.
* @return String the http response
*/
@Deprecated
public String getResponse() {
return jsonResponse;
}
Expand Down Expand Up @@ -58,4 +61,48 @@ public void setResponseTime(Duration duration) {
public Duration getReponseTime() {
return responseTime;
}

/**
* Compares only the actual contents of the response when creating a hashcode.
* This was code automatically generated using Elcipse.
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((jsonResponse == null) ? 0 : jsonResponse.hashCode());
return result;
}

/**
* Compares only the actual contents of the response. Is also used by contains().
* This was code automatically generated using Elcipse.
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Response other = (Response) obj;
if (jsonResponse == null) {
if (other.jsonResponse != null)
return false;
}
else if (!jsonResponse.equals(other.jsonResponse))
return false;
return true;
}

/**
* @param s CharSequence
* @return boolean
* @see java.lang.String#contains(java.lang.CharSequence)
*/
public boolean contains(CharSequence s) {
return jsonResponse.contains(s);
}

}
48 changes: 48 additions & 0 deletions src/main/java/com/dougnoel/sentinel/apis/ResponseSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.dougnoel.sentinel.apis;

import java.util.ArrayList;

/**
* Stores a set of responses in order so that they can be retrieved based on when they were created.
* @author [email protected]
*
*/
public class ResponseSet {
protected ArrayList<Response> responses = new ArrayList<Response>();

/**
* Adds a response object to the list of responses we are tracking.
* @param response the response to store
*/
public void add(Response response) {
responses.add(response);
}

/**
* Returns the most recent response added to the list.
* @return Response the most recent response added to the list
*/
public Response getLatest() {
return responses.get(-1);
}

/**
* Returns the response added before the most recent response added.
* Used for comparing the current response to the previous one made.
* @return Response the most recent response added to the list
*/
public Response getPrevious() {
return responses.get(-2);
}

/**
* Returns the response stored based on a 0-based index.
* <br>0 returns the first response.
* <br>-1 returns the last response.
* @param index int the order the response was tored in the list
* @return rEsponse the requested response from the list
*/
public Response get(int index) {
return responses.get(index);
}
}
Loading