Skip to content

Commit

Permalink
Add support to mock with embedded mockserver
Browse files Browse the repository at this point in the history
  • Loading branch information
osvaldjr committed Jul 22, 2019
1 parent 663f572 commit dedaa8d
Show file tree
Hide file tree
Showing 28 changed files with 399 additions and 161 deletions.
30 changes: 28 additions & 2 deletions 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.6</version>
<version>0.0.7-SNAPSHOT</version>
<name>io.github.osvaldjr:easy-cucumber</name>
<description>Easy Cucumber JVM Testing</description>

Expand All @@ -36,11 +36,12 @@
<awaitility.version>3.1.6</awaitility.version>
<javax.interceptor-api.version>1.2</javax.interceptor-api.version>
<postgresql.version>9.4-1200-jdbc41</postgresql.version>
<mockserver.version>5.6.0</mockserver.version>
<mockserver-netty.version>5.6.0</mockserver-netty.version>

<sonar.coverage.exclusions>
**/stepdefinitions/**,**/confs/**,**/domains/**,**/jsons/**,**/*ApplicationConfiguration*,**/*EasyCucumberRunner*
</sonar.coverage.exclusions>
<mockserver-netty.version>5.6.0</mockserver-netty.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -379,6 +380,31 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-maven-plugin</artifactId>
<version>${mockserver.version}</version>
<configuration>
<serverPort>1080</serverPort>
<logLevel>DEBUG</logLevel>
</configuration>
<executions>
<execution>
<id>process-test-classes</id>
<phase>process-test-classes</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>verify</id>
<phase>verify</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
23 changes: 23 additions & 0 deletions src/main/java/io/github/osvaldjr/confs/MockServerConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.github.osvaldjr.confs;

import org.mockserver.client.MockServerClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnProperty("dependencies.mockserver.port")
public class MockServerConfig {

@Value("${dependencies.mockserver.port:}")
Integer mockServerPort;

@Value("${dependencies.mockserver.host:localhost}")
String mockServerHost;

@Bean
public MockServerClient mockServerClient() {
return new MockServerClient(mockServerHost, mockServerPort);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

import feign.Param;
import feign.RequestLine;
import io.github.osvaldjr.gateways.stubby.jsons.StubbyJsonRequest;
import io.github.osvaldjr.gateways.stubby.jsons.StubbyJsonResponse;
import io.github.osvaldjr.gateways.mock.stubby.jsons.StubbyJsonRequest;
import io.github.osvaldjr.gateways.mock.stubby.jsons.StubbyJsonResponse;

@FeignClient(value = "stubby-client", url = "${dependencies.stubby.url:}")
public interface StubbyClient {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/io/github/osvaldjr/gateways/mock/MockGateway.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.github.osvaldjr.gateways.mock;

import io.github.osvaldjr.domains.StubbyRequest;

public interface MockGateway {
Object createStubbyRequest(
StubbyRequest.RequestBody request, StubbyRequest.ResponseBody response);

void deleteAllServices();

Integer getMockHits(Object id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package io.github.osvaldjr.gateways.mock.mockserver;

import org.mockserver.client.MockServerClient;
import org.mockserver.mock.Expectation;
import org.mockserver.model.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import io.github.osvaldjr.domains.StubbyRequest;
import io.github.osvaldjr.gateways.mock.MockGateway;
import io.github.osvaldjr.gateways.mock.mockserver.assemblers.ExpectationRequestAssembler;

@Component
@ConditionalOnProperty("dependencies.mockserver.port")
public class MockServerMockGatewayImpl implements MockGateway {
private static final Integer MAX_HITS = Integer.MAX_VALUE;

private final MockServerClient mockServerClient;
private final ExpectationRequestAssembler expectationRequestAssembler;

@Autowired
public MockServerMockGatewayImpl(
MockServerClient mockServerClient, ExpectationRequestAssembler expectationRequestAssembler) {
this.mockServerClient = mockServerClient;
this.expectationRequestAssembler = expectationRequestAssembler;
}

@Override
public HttpRequest createStubbyRequest(
StubbyRequest.RequestBody request, StubbyRequest.ResponseBody response) {
Expectation expectation = expectationRequestAssembler.assemble(request, response, MAX_HITS);
mockServerClient.sendExpectation(expectation);
return expectation.getHttpRequest();
}

@Override
public void deleteAllServices() {
mockServerClient.reset();
}

@Override
public Integer getMockHits(Object requestIdentifier) {
Expectation[] expectations =
mockServerClient.retrieveActiveExpectations((HttpRequest) requestIdentifier);

int remainingTimes = 0;
if (expectations.length > 0) {
remainingTimes = expectations[0].getTimes().getRemainingTimes();
}
return MAX_HITS - remainingTimes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.github.osvaldjr.gateways.mock.mockserver.assemblers;

import static org.mockserver.matchers.TimeToLive.unlimited;
import static org.mockserver.matchers.Times.exactly;

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

import org.mockserver.mock.Expectation;
import org.mockserver.model.Header;
import org.mockserver.model.HttpRequest;
import org.mockserver.model.HttpResponse;
import org.springframework.stereotype.Component;

import gherkin.deps.com.google.gson.Gson;
import io.github.osvaldjr.domains.StubbyRequest;

@Component
public class ExpectationRequestAssembler {

private static final Gson gson = new Gson();

public Expectation assemble(
StubbyRequest.RequestBody request, StubbyRequest.ResponseBody response, int maxHits) {
HttpRequest httpRequest = getHttpRequest(request);

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

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

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

private List<Header> getHeaders(Map<String, String> headersMap) {
List<Header> headers = new ArrayList<>();
headersMap.forEach((key, value) -> headers.add(new Header(key, value)));
return headers;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.osvaldjr.gateways.stubby;
package io.github.osvaldjr.gateways.mock.stubby;

import static io.github.osvaldjr.domains.StubbyRequest.RequestBody;
import static io.github.osvaldjr.domains.StubbyRequest.ResponseBody;
Expand All @@ -9,31 +9,29 @@

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import io.github.osvaldjr.domains.StubbyResponse;
import io.github.osvaldjr.gateways.feign.StubbyClient;
import io.github.osvaldjr.gateways.stubby.assemblers.StubbyRequestAssembler;
import io.github.osvaldjr.gateways.stubby.assemblers.StubbyResponseAssembler;
import io.github.osvaldjr.gateways.stubby.jsons.StubbyJsonRequest;
import io.github.osvaldjr.gateways.stubby.jsons.StubbyJsonResponse;
import io.github.osvaldjr.gateways.mock.MockGateway;
import io.github.osvaldjr.gateways.mock.mockserver.MockServerMockGatewayImpl;
import io.github.osvaldjr.gateways.mock.stubby.assemblers.StubbyRequestAssembler;
import io.github.osvaldjr.gateways.mock.stubby.jsons.StubbyJsonRequest;
import io.github.osvaldjr.gateways.mock.stubby.jsons.StubbyJsonResponse;

@Component
public class StubbyGateway implements MockGateway {
@ConditionalOnMissingBean(MockServerMockGatewayImpl.class)
public class StubbyMockGatewayImpl implements MockGateway {

private StubbyClient stubbyClient;
private StubbyRequestAssembler stubbyRequestAssembler;
private StubbyResponseAssembler stubbyResponseAssembler;

@Autowired
public StubbyGateway(
StubbyClient stubbyClient,
StubbyRequestAssembler stubbyRequestAssembler,
StubbyResponseAssembler stubbyResponseAssembler) {
public StubbyMockGatewayImpl(
StubbyClient stubbyClient, StubbyRequestAssembler stubbyRequestAssembler) {
this.stubbyClient = stubbyClient;
this.stubbyRequestAssembler = stubbyRequestAssembler;
this.stubbyResponseAssembler = stubbyResponseAssembler;
}

@Override
Expand All @@ -51,8 +49,8 @@ public void deleteAllServices() {
}

@Override
public StubbyResponse getStubbyResponse(String id) {
return stubbyResponseAssembler.assemble(stubbyClient.getService(Integer.valueOf(id)));
public Integer getMockHits(Object id) {
return stubbyClient.getService(Integer.valueOf(id.toString())).getHits();
}

private static String getStubbyId(ResponseEntity response) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package io.github.osvaldjr.gateways.stubby.assemblers;
package io.github.osvaldjr.gateways.mock.stubby.assemblers;

import static io.github.osvaldjr.domains.StubbyRequest.RequestBody;
import static io.github.osvaldjr.domains.StubbyRequest.ResponseBody;

import org.springframework.stereotype.Component;

import gherkin.deps.com.google.gson.Gson;
import io.github.osvaldjr.gateways.stubby.jsons.StubbyJsonRequest;
import io.github.osvaldjr.gateways.stubby.jsons.StubbyRequestBody;
import io.github.osvaldjr.gateways.stubby.jsons.StubbyResponseBody;
import io.github.osvaldjr.gateways.mock.stubby.jsons.StubbyJsonRequest;
import io.github.osvaldjr.gateways.mock.stubby.jsons.StubbyRequestBody;
import io.github.osvaldjr.gateways.mock.stubby.jsons.StubbyResponseBody;

@Component
public class StubbyRequestAssembler {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.osvaldjr.gateways.stubby.jsons;
package io.github.osvaldjr.gateways.mock.stubby.jsons;

import java.io.Serializable;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.osvaldjr.gateways.stubby.jsons;
package io.github.osvaldjr.gateways.mock.stubby.jsons;

import java.io.Serializable;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.osvaldjr.gateways.stubby.jsons;
package io.github.osvaldjr.gateways.mock.stubby.jsons;

import java.io.Serializable;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.osvaldjr.gateways.stubby.jsons;
package io.github.osvaldjr.gateways.mock.stubby.jsons;

import java.io.Serializable;
import java.util.Map;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class DefaultSteps extends Steps {
private final CreateStubbyUseCase createStubbyUsecase;
private final HitsMatcherUseCase hitsMatcherUsecase;
private FeignException httpException;
private Map<String, String> stubbyIdMap;
private Map<String, Object> stubbyIdMap;

@Autowired
public DefaultSteps(
Expand Down Expand Up @@ -94,15 +94,15 @@ public void iExpectAsResponse(String responsePayload) throws IOException, JSONEx

@Then("A have a mock ([^\"]*) for dependency ([^\"]*)")
public void aHaveAMockForDependency(String mockName, String serviceName) throws IOException {
String stubbyId = createStubbyUsecase.execute(scenarioName, serviceName, mockName);
Object stubbyId = createStubbyUsecase.execute(scenarioName, serviceName, mockName);
stubbyIdMap.put(getStubbyKey(scenarioName, serviceName, mockName), stubbyId);
}

@Then("I expect mock ([^\"]*) for dependency ([^\"]*) to have been called (\\d+) times")
public void iExpectMockForDependencyToHaveBeenCalledTimes(
String mockName, String serviceName, int times) {
String mapKey = getStubbyKey(scenarioName, serviceName, mockName);
String stubbyId = stubbyIdMap.get(mapKey);
Object stubbyId = stubbyIdMap.get(mapKey);

assertTrue(hitsMatcherUsecase.execute(stubbyId, times));
}
Expand Down
Loading

0 comments on commit dedaa8d

Please sign in to comment.