Skip to content

Commit

Permalink
Fix queryPArams in defined request and accept plain text body
Browse files Browse the repository at this point in the history
  • Loading branch information
osvaldjr committed Sep 3, 2019
1 parent 9bff5c4 commit 5ea1615
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 61 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<groupId>io.github.osvaldjr</groupId>
<artifactId>easy-cucumber</artifactId>
<packaging>jar</packaging>
<version>0.0.13</version>
<version>0.0.14</version>
<name>io.github.osvaldjr:easy-cucumber</name>
<description>Easy Cucumber JVM Testing</description>

Expand Down
7 changes: 7 additions & 0 deletions src/main/java/io/github/osvaldjr/domains/StubbyRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public static class RequestBody implements Serializable {
private Object body;
private Map<String, String> headers = new HashMap<>();
private Map<String, String> queryParams = new HashMap<>();
private BodyType bodyType;
}

@Getter
Expand All @@ -37,5 +38,11 @@ public static class ResponseBody implements Serializable {
private Map<String, String> headers;
private Integer status;
private Object body;
private BodyType bodyType;
}

public enum BodyType {
JSON,
RAW
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ public class TargetRequest<T> {
private String url;
private T body;
private Map<String, String> headers;
private Map<String, String> queryParameters;
}
47 changes: 34 additions & 13 deletions src/main/java/io/github/osvaldjr/gateways/TargetGateway.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,41 +32,62 @@ public TargetGateway(Decoder decoder, Encoder encoder, ErrorDecoder errorDecoder
.client(new OkHttpClient());
}

public ResponseEntity<Object> get(String host, String uri, Map<String, String> headers) {
return buildClient(host).get(uri, headers);
public ResponseEntity<Object> get(
String host, String uri, Map<String, String> headers, Map<String, String> queryParameters) {
return buildClient(host).get(uri, headers, queryParameters);
}

public <T> ResponseEntity<Object> post(
String host, String uri, T body, Map<String, String> headers) {
return buildClient(host).post(uri, body, headers);
String host,
String uri,
T body,
Map<String, String> headers,
Map<String, String> queryParameters) {
return buildClient(host).post(uri, body, headers, queryParameters);
}

public <T> ResponseEntity<Object> delete(
String host, String uri, T body, Map<String, String> headers) {
return requestDelete(host, uri, body, headers);
String host,
String uri,
T body,
Map<String, String> headers,
Map<String, String> queryParameters) {
return requestDelete(host, uri, body, headers, queryParameters);
}

private <T> ResponseEntity<Object> requestDelete(
String host, String uri, T body, Map<String, String> headers) {
String host,
String uri,
T body,
Map<String, String> headers,
Map<String, String> queryParameters) {
TargetClient targetClient = buildClient(host);
ResponseEntity<Object> delete;

if (ofNullable(body).isPresent()) {
delete = targetClient.delete(uri, body, headers);
delete = targetClient.delete(uri, body, headers, queryParameters);
} else {
delete = targetClient.delete(uri, headers);
delete = targetClient.delete(uri, headers, queryParameters);
}
return delete;
}

public <T> ResponseEntity<Object> put(
String host, String uri, T body, Map<String, String> headers) {
return buildClient(host).put(uri, body, headers);
String host,
String uri,
T body,
Map<String, String> headers,
Map<String, String> queryParameters) {
return buildClient(host).put(uri, body, headers, queryParameters);
}

public <T> ResponseEntity<Object> patch(
String host, String uri, T body, Map<String, String> headers) {
return buildClient(host).patch(uri, body, headers);
String host,
String uri,
T body,
Map<String, String> headers,
Map<String, String> queryParameters) {
return buildClient(host).patch(uri, body, headers, queryParameters);
}

private TargetClient buildClient(String host) {
Expand Down
21 changes: 15 additions & 6 deletions src/main/java/io/github/osvaldjr/gateways/feign/TargetClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,48 @@
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(value = "target-client")
public interface TargetClient {

@GetMapping(path = "{url}")
ResponseEntity<Object> get(
@PathVariable("url") String url, @RequestHeader Map<String, String> headers);
@PathVariable("url") String url,
@RequestHeader Map<String, String> headers,
@RequestParam Map<String, String> queryParameters);

@PostMapping(path = "{url}")
<R> ResponseEntity<Object> post(
@PathVariable("url") String uri,
@RequestBody R body,
@RequestHeader Map<String, String> headers);
@RequestHeader Map<String, String> headers,
@RequestParam Map<String, String> queryParameters);

@DeleteMapping(path = "{url}")
<R> ResponseEntity<Object> delete(
@PathVariable("url") String uri,
@RequestBody R body,
@RequestHeader Map<String, String> headers);
@RequestHeader Map<String, String> headers,
@RequestParam Map<String, String> queryParameters);

@DeleteMapping(path = "{url}")
ResponseEntity<Object> delete(
@PathVariable("url") String uri, @RequestHeader Map<String, String> headers);
@PathVariable("url") String uri,
@RequestHeader Map<String, String> headers,
@RequestParam Map<String, String> queryParameters);

@PutMapping(path = "{url}")
<R> ResponseEntity<Object> put(
@PathVariable("url") String uri,
@RequestBody R body,
@RequestHeader Map<String, String> headers);
@RequestHeader Map<String, String> headers,
@RequestParam Map<String, String> queryParameters);

@PatchMapping(path = "{url}")
<R> ResponseEntity<Object> patch(
@PathVariable("url") String uri,
@RequestBody R body,
@RequestHeader Map<String, String> headers);
@RequestHeader Map<String, String> headers,
@RequestParam Map<String, String> queryParameters);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,36 @@ public Expectation assemble(

HttpResponse httpResponse =
HttpResponse.response()
.withBody(gson.toJson(response.getBody()))
.withBody(getResponseBody(response))
.withStatusCode(response.getStatus())
.withHeaders(getHeaders(response.getHeaders()));

return new Expectation(httpRequest, exactly(maxHits), unlimited()).thenRespond(httpResponse);
}

private String getResponseBody(StubbyRequest.ResponseBody response) {
return response.getBodyType() == StubbyRequest.BodyType.RAW
? response.getBody().toString()
: gson.toJson(response.getBody());
}

private HttpRequest getHttpRequest(StubbyRequest.RequestBody request) {
HttpRequest httpRequest = new HttpRequest();
httpRequest
.withMethod(request.getMethod())
.withPath("/" + request.getUrl())
.withHeaders(getHeaders(request.getHeaders()))
.withQueryStringParameters(getQueryParameters(request.getQueryParams()))
.withBody(gson.toJson(request.getBody()));
.withBody(getRequestBody(request));
return httpRequest;
}

private String getRequestBody(StubbyRequest.RequestBody request) {
return request.getBodyType() == StubbyRequest.BodyType.RAW
? request.getBody().toString()
: gson.toJson(request.getBody());
}

private List<Parameter> getQueryParameters(Map<String, String> queryParams) {
List<Parameter> parameters = new ArrayList<>();
queryParams.forEach((key, value) -> parameters.add(new Parameter(key, value)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,51 @@ public ResponseEntity execute(TargetRequest request) {
assertNotNull("host cannot be null in make request", request.getHost());

ResponseEntity response;
Map<String, String> headersMap = getHeaders(request.getHeaders());
Map<String, String> headersMap = getMapOfNullable(request.getHeaders());
Map<String, String> queryParametersMap = getMapOfNullable(request.getQueryParameters());

HttpMethod httpMethod = HttpMethod.valueOf(request.getMethod());

switch (httpMethod) {
case GET:
response = targetGateway.get(request.getHost(), request.getUrl(), headersMap);
response =
targetGateway.get(request.getHost(), request.getUrl(), headersMap, queryParametersMap);
break;
case POST:
response =
targetGateway.post(request.getHost(), request.getUrl(), request.getBody(), headersMap);
targetGateway.post(
request.getHost(),
request.getUrl(),
request.getBody(),
headersMap,
queryParametersMap);
break;
case PUT:
response =
targetGateway.put(request.getHost(), request.getUrl(), request.getBody(), headersMap);
targetGateway.put(
request.getHost(),
request.getUrl(),
request.getBody(),
headersMap,
queryParametersMap);
break;
case DELETE:
response =
targetGateway.delete(
request.getHost(), request.getUrl(), request.getBody(), headersMap);
request.getHost(),
request.getUrl(),
request.getBody(),
headersMap,
queryParametersMap);
break;
case PATCH:
response =
targetGateway.patch(request.getHost(), request.getUrl(), request.getBody(), headersMap);
targetGateway.patch(
request.getHost(),
request.getUrl(),
request.getBody(),
headersMap,
queryParametersMap);
break;
default:
throw new MethodNotAllowedException(
Expand All @@ -74,7 +96,7 @@ public ResponseEntity execute(TargetRequest request) {
return response;
}

private Map<String, String> getHeaders(Map<String, String> headers) {
private Map<String, String> getMapOfNullable(Map<String, String> headers) {
return ofNullable(headers).orElseGet(HashMap::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ void shouldGet(
@Random String host,
@Random String uri,
@Random Map<String, String> headers,
@Random Map<String, String> queryParams,
@Random ResponseEntity responseEntity) {

when(feignClient.get(uri, headers)).thenReturn(responseEntity);
ResponseEntity response = targetGateway.get(host, uri, headers);
when(feignClient.get(uri, headers, queryParams)).thenReturn(responseEntity);
ResponseEntity response = targetGateway.get(host, uri, headers, queryParams);

verify(feignClient, times(1)).get(uri, headers);
verify(feignClient, times(1)).get(uri, headers, queryParams);
verify(feignBuilder, times(1)).target(TargetClient.class, host);
assertThat(response, equalTo(responseEntity));
}
Expand All @@ -66,12 +67,13 @@ void shouldPost(
@Random String uri,
@Random Object body,
@Random Map<String, String> headers,
@Random Map<String, String> queryParams,
@Random ResponseEntity responseEntity) {
when(feignClient.post(uri, body, headers)).thenReturn(responseEntity);
when(feignClient.post(uri, body, headers, queryParams)).thenReturn(responseEntity);

ResponseEntity response = targetGateway.post(host, uri, body, headers);
ResponseEntity response = targetGateway.post(host, uri, body, headers, queryParams);

verify(feignClient, times(1)).post(uri, body, headers);
verify(feignClient, times(1)).post(uri, body, headers, queryParams);
verify(feignBuilder, times(1)).target(TargetClient.class, host);
assertThat(response, equalTo(responseEntity));
}
Expand All @@ -82,13 +84,14 @@ void shouldDelete(
@Random String uri,
@Random Object body,
@Random Map<String, String> headers,
@Random Map<String, String> queryParams,
@Random ResponseEntity responseEntity) {
when(feignClient.delete(uri, body, headers)).thenReturn(responseEntity);
when(feignClient.delete(uri, body, headers, queryParams)).thenReturn(responseEntity);

ResponseEntity response = targetGateway.delete(host, uri, body, headers);
ResponseEntity response = targetGateway.delete(host, uri, body, headers, queryParams);

verify(feignClient, times(1)).delete(uri, body, headers);
verify(feignClient, never()).delete(any(), any());
verify(feignClient, times(1)).delete(uri, body, headers, queryParams);
verify(feignClient, never()).delete(any(), any(), any());
verify(feignBuilder, times(1)).target(TargetClient.class, host);
assertThat(response, equalTo(responseEntity));
}
Expand All @@ -98,13 +101,14 @@ void shouldDeleteWithNullBody(
@Random String host,
@Random String uri,
@Random Map<String, String> headers,
@Random Map<String, String> queryParams,
@Random ResponseEntity responseEntity) {
when(feignClient.delete(uri, headers)).thenReturn(responseEntity);
when(feignClient.delete(uri, headers, queryParams)).thenReturn(responseEntity);

ResponseEntity response = targetGateway.delete(host, uri, null, headers);
ResponseEntity response = targetGateway.delete(host, uri, null, headers, queryParams);

verify(feignClient, times(1)).delete(uri, headers);
verify(feignClient, never()).delete(any(), any(), any());
verify(feignClient, times(1)).delete(uri, headers, queryParams);
verify(feignClient, never()).delete(any(), any(), any(), any());
verify(feignBuilder, times(1)).target(TargetClient.class, host);
assertThat(response, equalTo(responseEntity));
}
Expand All @@ -115,12 +119,13 @@ void shouldPut(
@Random String uri,
@Random Object body,
@Random Map<String, String> headers,
@Random Map<String, String> queryParams,
@Random ResponseEntity responseEntity) {
when(feignClient.put(uri, body, headers)).thenReturn(responseEntity);
when(feignClient.put(uri, body, headers, queryParams)).thenReturn(responseEntity);

ResponseEntity response = targetGateway.put(host, uri, body, headers);
ResponseEntity response = targetGateway.put(host, uri, body, headers, queryParams);

verify(feignClient, times(1)).put(uri, body, headers);
verify(feignClient, times(1)).put(uri, body, headers, queryParams);
verify(feignBuilder, times(1)).target(TargetClient.class, host);
assertThat(response, equalTo(responseEntity));
}
Expand All @@ -131,12 +136,13 @@ void shouldPatch(
@Random String uri,
@Random Object body,
@Random Map<String, String> headers,
@Random Map<String, String> queryParams,
@Random ResponseEntity responseEntity) {
when(feignClient.patch(uri, body, headers)).thenReturn(responseEntity);
when(feignClient.patch(uri, body, headers, queryParams)).thenReturn(responseEntity);

ResponseEntity response = targetGateway.patch(host, uri, body, headers);
ResponseEntity response = targetGateway.patch(host, uri, body, headers, queryParams);

verify(feignClient, times(1)).patch(uri, body, headers);
verify(feignClient, times(1)).patch(uri, body, headers, queryParams);
verify(feignBuilder, times(1)).target(TargetClient.class, host);
assertThat(response, equalTo(responseEntity));
}
Expand Down
Loading

0 comments on commit 5ea1615

Please sign in to comment.