diff --git a/pom.xml b/pom.xml
index 4f64a57..a48415d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -12,7 +12,7 @@
io.github.osvaldjr
easy-cucumber
jar
- 0.0.6
+ 0.0.7-SNAPSHOT
io.github.osvaldjr:easy-cucumber
Easy Cucumber JVM Testing
@@ -36,11 +36,12 @@
3.1.6
1.2
9.4-1200-jdbc41
+ 5.6.0
+ 5.6.0
**/stepdefinitions/**,**/confs/**,**/domains/**,**/jsons/**,**/*ApplicationConfiguration*,**/*EasyCucumberRunner*
- 5.6.0
@@ -379,6 +380,31 @@
+
+ org.mock-server
+ mockserver-maven-plugin
+ ${mockserver.version}
+
+ 1080
+ DEBUG
+
+
+
+ process-test-classes
+ process-test-classes
+
+ start
+
+
+
+ verify
+ verify
+
+ stop
+
+
+
+
diff --git a/src/main/java/io/github/osvaldjr/confs/MockServerConfig.java b/src/main/java/io/github/osvaldjr/confs/MockServerConfig.java
new file mode 100644
index 0000000..95ea4ad
--- /dev/null
+++ b/src/main/java/io/github/osvaldjr/confs/MockServerConfig.java
@@ -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);
+ }
+}
diff --git a/src/main/java/io/github/osvaldjr/gateways/feign/StubbyClient.java b/src/main/java/io/github/osvaldjr/gateways/feign/StubbyClient.java
index 3ae6b03..9005b26 100644
--- a/src/main/java/io/github/osvaldjr/gateways/feign/StubbyClient.java
+++ b/src/main/java/io/github/osvaldjr/gateways/feign/StubbyClient.java
@@ -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 {
diff --git a/src/main/java/io/github/osvaldjr/gateways/mock/MockGateway.java b/src/main/java/io/github/osvaldjr/gateways/mock/MockGateway.java
new file mode 100644
index 0000000..e7e269e
--- /dev/null
+++ b/src/main/java/io/github/osvaldjr/gateways/mock/MockGateway.java
@@ -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);
+}
diff --git a/src/main/java/io/github/osvaldjr/gateways/mock/mockserver/MockServerMockGatewayImpl.java b/src/main/java/io/github/osvaldjr/gateways/mock/mockserver/MockServerMockGatewayImpl.java
new file mode 100644
index 0000000..acb6329
--- /dev/null
+++ b/src/main/java/io/github/osvaldjr/gateways/mock/mockserver/MockServerMockGatewayImpl.java
@@ -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;
+ }
+}
diff --git a/src/main/java/io/github/osvaldjr/gateways/mock/mockserver/assemblers/ExpectationRequestAssembler.java b/src/main/java/io/github/osvaldjr/gateways/mock/mockserver/assemblers/ExpectationRequestAssembler.java
new file mode 100644
index 0000000..87b6255
--- /dev/null
+++ b/src/main/java/io/github/osvaldjr/gateways/mock/mockserver/assemblers/ExpectationRequestAssembler.java
@@ -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 getHeaders(Map headersMap) {
+ List headers = new ArrayList<>();
+ headersMap.forEach((key, value) -> headers.add(new Header(key, value)));
+ return headers;
+ }
+}
diff --git a/src/main/java/io/github/osvaldjr/gateways/stubby/StubbyGateway.java b/src/main/java/io/github/osvaldjr/gateways/mock/stubby/StubbyMockGatewayImpl.java
similarity index 63%
rename from src/main/java/io/github/osvaldjr/gateways/stubby/StubbyGateway.java
rename to src/main/java/io/github/osvaldjr/gateways/mock/stubby/StubbyMockGatewayImpl.java
index f687f42..72526a5 100644
--- a/src/main/java/io/github/osvaldjr/gateways/stubby/StubbyGateway.java
+++ b/src/main/java/io/github/osvaldjr/gateways/mock/stubby/StubbyMockGatewayImpl.java
@@ -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;
@@ -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
@@ -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) {
diff --git a/src/main/java/io/github/osvaldjr/gateways/stubby/assemblers/StubbyRequestAssembler.java b/src/main/java/io/github/osvaldjr/gateways/mock/stubby/assemblers/StubbyRequestAssembler.java
similarity index 82%
rename from src/main/java/io/github/osvaldjr/gateways/stubby/assemblers/StubbyRequestAssembler.java
rename to src/main/java/io/github/osvaldjr/gateways/mock/stubby/assemblers/StubbyRequestAssembler.java
index e314584..df35353 100644
--- a/src/main/java/io/github/osvaldjr/gateways/stubby/assemblers/StubbyRequestAssembler.java
+++ b/src/main/java/io/github/osvaldjr/gateways/mock/stubby/assemblers/StubbyRequestAssembler.java
@@ -1,4 +1,4 @@
-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;
@@ -6,9 +6,9 @@
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 {
diff --git a/src/main/java/io/github/osvaldjr/gateways/stubby/jsons/StubbyJsonRequest.java b/src/main/java/io/github/osvaldjr/gateways/mock/stubby/jsons/StubbyJsonRequest.java
similarity index 85%
rename from src/main/java/io/github/osvaldjr/gateways/stubby/jsons/StubbyJsonRequest.java
rename to src/main/java/io/github/osvaldjr/gateways/mock/stubby/jsons/StubbyJsonRequest.java
index 8ca5714..4e002b9 100644
--- a/src/main/java/io/github/osvaldjr/gateways/stubby/jsons/StubbyJsonRequest.java
+++ b/src/main/java/io/github/osvaldjr/gateways/mock/stubby/jsons/StubbyJsonRequest.java
@@ -1,4 +1,4 @@
-package io.github.osvaldjr.gateways.stubby.jsons;
+package io.github.osvaldjr.gateways.mock.stubby.jsons;
import java.io.Serializable;
diff --git a/src/main/java/io/github/osvaldjr/gateways/stubby/jsons/StubbyJsonResponse.java b/src/main/java/io/github/osvaldjr/gateways/mock/stubby/jsons/StubbyJsonResponse.java
similarity index 83%
rename from src/main/java/io/github/osvaldjr/gateways/stubby/jsons/StubbyJsonResponse.java
rename to src/main/java/io/github/osvaldjr/gateways/mock/stubby/jsons/StubbyJsonResponse.java
index a6637c2..45ffac8 100644
--- a/src/main/java/io/github/osvaldjr/gateways/stubby/jsons/StubbyJsonResponse.java
+++ b/src/main/java/io/github/osvaldjr/gateways/mock/stubby/jsons/StubbyJsonResponse.java
@@ -1,4 +1,4 @@
-package io.github.osvaldjr.gateways.stubby.jsons;
+package io.github.osvaldjr.gateways.mock.stubby.jsons;
import java.io.Serializable;
diff --git a/src/main/java/io/github/osvaldjr/gateways/stubby/jsons/StubbyRequestBody.java b/src/main/java/io/github/osvaldjr/gateways/mock/stubby/jsons/StubbyRequestBody.java
similarity index 89%
rename from src/main/java/io/github/osvaldjr/gateways/stubby/jsons/StubbyRequestBody.java
rename to src/main/java/io/github/osvaldjr/gateways/mock/stubby/jsons/StubbyRequestBody.java
index ae3d828..c8e2501 100644
--- a/src/main/java/io/github/osvaldjr/gateways/stubby/jsons/StubbyRequestBody.java
+++ b/src/main/java/io/github/osvaldjr/gateways/mock/stubby/jsons/StubbyRequestBody.java
@@ -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;
diff --git a/src/main/java/io/github/osvaldjr/gateways/stubby/jsons/StubbyResponseBody.java b/src/main/java/io/github/osvaldjr/gateways/mock/stubby/jsons/StubbyResponseBody.java
similarity index 86%
rename from src/main/java/io/github/osvaldjr/gateways/stubby/jsons/StubbyResponseBody.java
rename to src/main/java/io/github/osvaldjr/gateways/mock/stubby/jsons/StubbyResponseBody.java
index 6e6d0b4..bfb084c 100644
--- a/src/main/java/io/github/osvaldjr/gateways/stubby/jsons/StubbyResponseBody.java
+++ b/src/main/java/io/github/osvaldjr/gateways/mock/stubby/jsons/StubbyResponseBody.java
@@ -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;
diff --git a/src/main/java/io/github/osvaldjr/gateways/stubby/assemblers/StubbyResponseAssembler.java b/src/main/java/io/github/osvaldjr/gateways/stubby/assemblers/StubbyResponseAssembler.java
deleted file mode 100644
index 9e0cb57..0000000
--- a/src/main/java/io/github/osvaldjr/gateways/stubby/assemblers/StubbyResponseAssembler.java
+++ /dev/null
@@ -1,16 +0,0 @@
-package io.github.osvaldjr.gateways.stubby.assemblers;
-
-import org.springframework.stereotype.Component;
-
-import io.github.osvaldjr.domains.StubbyResponse;
-import io.github.osvaldjr.gateways.stubby.jsons.StubbyJsonResponse;
-
-@Component
-public class StubbyResponseAssembler {
- public StubbyResponse assemble(StubbyJsonResponse stubbyJsonResponse) {
- return StubbyResponse.builder()
- .hits(stubbyJsonResponse.getHits())
- .id(stubbyJsonResponse.getId())
- .build();
- }
-}
diff --git a/src/main/java/io/github/osvaldjr/stepdefinitions/steps/DefaultSteps.java b/src/main/java/io/github/osvaldjr/stepdefinitions/steps/DefaultSteps.java
index 7624ef6..d27c114 100644
--- a/src/main/java/io/github/osvaldjr/stepdefinitions/steps/DefaultSteps.java
+++ b/src/main/java/io/github/osvaldjr/stepdefinitions/steps/DefaultSteps.java
@@ -43,7 +43,7 @@ public class DefaultSteps extends Steps {
private final CreateStubbyUseCase createStubbyUsecase;
private final HitsMatcherUseCase hitsMatcherUsecase;
private FeignException httpException;
- private Map stubbyIdMap;
+ private Map stubbyIdMap;
@Autowired
public DefaultSteps(
@@ -94,7 +94,7 @@ 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);
}
@@ -102,7 +102,7 @@ public void aHaveAMockForDependency(String mockName, String serviceName) throws
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));
}
diff --git a/src/main/java/io/github/osvaldjr/stepdefinitions/steps/Hooks.java b/src/main/java/io/github/osvaldjr/stepdefinitions/steps/Hooks.java
index 6f216d8..d2ba075 100644
--- a/src/main/java/io/github/osvaldjr/stepdefinitions/steps/Hooks.java
+++ b/src/main/java/io/github/osvaldjr/stepdefinitions/steps/Hooks.java
@@ -3,40 +3,33 @@
import java.util.ArrayList;
import java.util.Optional;
-import org.mockserver.integration.ClientAndServer;
import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.factory.annotation.Value;
-import cucumber.api.java.After;
import cucumber.api.java.Before;
import io.github.osvaldjr.domains.properties.FeaturesProperties;
import io.github.osvaldjr.domains.properties.QueueProperties;
import io.github.osvaldjr.gateways.FeatureGateway;
-import io.github.osvaldjr.gateways.stubby.StubbyGateway;
+import io.github.osvaldjr.gateways.mock.MockGateway;
import io.github.osvaldjr.usecases.CleanQueueUseCase;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class Hooks {
- private final StubbyGateway stubbyGateway;
+ private final MockGateway mockGateway;
private final FeatureGateway featureGateway;
private final CleanQueueUseCase cleanQueueUseCase;
private final QueueProperties queueProperties;
private final FeaturesProperties featuresProperties;
- private ClientAndServer mockServer;
-
- @Value("${dependencies.mockserver.port:}")
- private Integer mockServerPort;
@Autowired
public Hooks(
- StubbyGateway stubbyGateway,
+ MockGateway mockGateway,
FeatureGateway featureGateway,
CleanQueueUseCase cleanQueueUseCase,
QueueProperties queueProperties,
FeaturesProperties featuresProperties) {
- this.stubbyGateway = stubbyGateway;
+ this.mockGateway = mockGateway;
this.featureGateway = featureGateway;
this.cleanQueueUseCase = cleanQueueUseCase;
this.queueProperties = queueProperties;
@@ -45,7 +38,7 @@ public Hooks(
@Before("@CleanStubby")
public void cleanupStubby() {
- stubbyGateway.deleteAllServices();
+ mockGateway.deleteAllServices();
}
@Before("@EnableFeatures")
@@ -64,26 +57,4 @@ public void disableFeatures() {
public void cleanupQueues() {
queueProperties.getNames().forEach(cleanQueueUseCase::execute);
}
-
- @Before
- public void startMockServer() {
- if (mockServerPort != null) {
- log.info("Starting mockserver");
- mockServer = ClientAndServer.startClientAndServer(mockServerPort);
- log.info(
- "Mockserver is running on "
- + mockServer.getRemoteAddress()
- + ":"
- + mockServer.getLocalPort());
- }
- }
-
- @After
- public void stopMockServer() {
- if (mockServer != null) {
- log.info("Stoping mockserver");
- mockServer.stop();
- log.info("Mockserver stoped");
- }
- }
}
diff --git a/src/main/java/io/github/osvaldjr/usecases/CreateStubbyUseCase.java b/src/main/java/io/github/osvaldjr/usecases/CreateStubbyUseCase.java
index 406e9dc..a195df5 100644
--- a/src/main/java/io/github/osvaldjr/usecases/CreateStubbyUseCase.java
+++ b/src/main/java/io/github/osvaldjr/usecases/CreateStubbyUseCase.java
@@ -10,25 +10,21 @@
import org.springframework.stereotype.Component;
import io.github.osvaldjr.gateways.FileGateway;
-import io.github.osvaldjr.gateways.stubby.MockServerGateway;
-import io.github.osvaldjr.gateways.stubby.StubbyGateway;
+import io.github.osvaldjr.gateways.mock.MockGateway;
@Component
public class CreateStubbyUseCase {
private FileGateway fileGateway;
- private StubbyGateway stubbyGateway;
- // private MockServerGateway mockServerGateway;
+ private MockGateway mockGateway;
@Autowired
- public CreateStubbyUseCase(
- FileGateway fileGateway, StubbyGateway stubbyGateway, MockServerGateway mockServerGateway) {
+ public CreateStubbyUseCase(FileGateway fileGateway, MockGateway mockGateway) {
this.fileGateway = fileGateway;
- this.stubbyGateway = stubbyGateway;
- // this.mockServerGateway = mockServerGateway;
+ this.mockGateway = mockGateway;
}
- public String execute(String scenario, String serviceName, String mockName) throws IOException {
+ public Object execute(String scenario, String serviceName, String mockName) throws IOException {
String mockRequestFile = "mocks/" + mockName + "-request.json";
String mockResponseFile = "mocks/" + mockName + "-response.json";
@@ -41,7 +37,7 @@ public String execute(String scenario, String serviceName, String mockName) thro
assertNotNull("method cannot be null in create mock", stubbyRequestBody.getMethod());
stubbyRequestBody.setUrl(getUrl(serviceName, stubbyRequestBody));
- return stubbyGateway.createStubbyRequest(stubbyRequestBody, stubbyResponseBody);
+ return mockGateway.createStubbyRequest(stubbyRequestBody, stubbyResponseBody);
}
private String getUrl(String serviceName, RequestBody stubbyRequestBody) {
diff --git a/src/main/java/io/github/osvaldjr/usecases/HitsMatcherUseCase.java b/src/main/java/io/github/osvaldjr/usecases/HitsMatcherUseCase.java
index cefe349..e87df7a 100644
--- a/src/main/java/io/github/osvaldjr/usecases/HitsMatcherUseCase.java
+++ b/src/main/java/io/github/osvaldjr/usecases/HitsMatcherUseCase.java
@@ -3,19 +3,19 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
-import io.github.osvaldjr.gateways.stubby.StubbyGateway;
+import io.github.osvaldjr.gateways.mock.MockGateway;
@Component
public class HitsMatcherUseCase {
- private StubbyGateway stubbyGateway;
+ private MockGateway mockGateway;
@Autowired
- public HitsMatcherUseCase(StubbyGateway stubbyGateway) {
- this.stubbyGateway = stubbyGateway;
+ public HitsMatcherUseCase(MockGateway mockGateway) {
+ this.mockGateway = mockGateway;
}
- public boolean execute(String id, Integer hits) {
- return stubbyGateway.getStubbyResponse(id).getHits().equals(hits);
+ public boolean execute(T id, Integer hits) {
+ return mockGateway.getMockHits(id).equals(hits);
}
}
diff --git a/src/test/java/io/github/osvaldjr/unit/gateways/mock/mockserver/MockServerMockGatewayImplTest.java b/src/test/java/io/github/osvaldjr/unit/gateways/mock/mockserver/MockServerMockGatewayImplTest.java
new file mode 100644
index 0000000..39aa98e
--- /dev/null
+++ b/src/test/java/io/github/osvaldjr/unit/gateways/mock/mockserver/MockServerMockGatewayImplTest.java
@@ -0,0 +1,82 @@
+package io.github.osvaldjr.unit.gateways.mock.mockserver;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import org.junit.jupiter.api.Test;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockserver.client.MockServerClient;
+import org.mockserver.matchers.TimeToLive;
+import org.mockserver.matchers.Times;
+import org.mockserver.mock.Expectation;
+import org.mockserver.model.HttpRequest;
+
+import io.github.glytching.junit.extension.random.Random;
+import io.github.osvaldjr.domains.StubbyRequest;
+import io.github.osvaldjr.gateways.mock.mockserver.MockServerMockGatewayImpl;
+import io.github.osvaldjr.gateways.mock.mockserver.assemblers.ExpectationRequestAssembler;
+import io.github.osvaldjr.unit.UnitTest;
+
+class MockServerMockGatewayImplTest extends UnitTest {
+
+ @Mock MockServerClient mockServerClient;
+ @Mock ExpectationRequestAssembler expectationRequestAssembler;
+ @InjectMocks MockServerMockGatewayImpl mockServerMockGateway;
+
+ @Test
+ void shouldCreateStubbyRequest(
+ @Random StubbyRequest.RequestBody request, @Random StubbyRequest.ResponseBody response) {
+
+ HttpRequest httpRequest = new HttpRequest();
+ Expectation expectation =
+ new Expectation(httpRequest, Times.exactly(10), TimeToLive.unlimited());
+
+ when(expectationRequestAssembler.assemble(any(), any(), anyInt())).thenReturn(expectation);
+
+ HttpRequest stubbyRequest = mockServerMockGateway.createStubbyRequest(request, response);
+
+ assertThat(stubbyRequest, notNullValue());
+ assertThat(stubbyRequest, equalTo(expectation.getHttpRequest()));
+ verify(expectationRequestAssembler, times(1)).assemble(any(), any(), anyInt());
+ verify(mockServerClient, times(1)).sendExpectation(expectation);
+ }
+
+ @Test
+ void shouldDeleteAllServices() {
+ mockServerMockGateway.deleteAllServices();
+ verify(mockServerClient, times(1)).reset();
+ }
+
+ @Test
+ void shouldReturnMockHitsCorrectly() {
+ HttpRequest httpRequest = new HttpRequest();
+ Expectation[] expectations = {
+ new Expectation(httpRequest, Times.exactly(10), TimeToLive.unlimited())
+ };
+ when(mockServerClient.retrieveActiveExpectations(any(HttpRequest.class)))
+ .thenReturn(expectations);
+
+ Integer mockHits = mockServerMockGateway.getMockHits(httpRequest);
+ assertThat(mockHits, notNullValue());
+ assertThat(mockHits, equalTo(Integer.MAX_VALUE - 10));
+ }
+
+ @Test
+ void shouldReturnMockHitsCorrectlyWhenMockServerDoesNotReturnExpectation() {
+ HttpRequest httpRequest = new HttpRequest();
+ Expectation[] expectations = {};
+ when(mockServerClient.retrieveActiveExpectations(any(HttpRequest.class)))
+ .thenReturn(expectations);
+
+ Integer mockHits = mockServerMockGateway.getMockHits(httpRequest);
+ assertThat(mockHits, notNullValue());
+ assertThat(mockHits, equalTo(Integer.MAX_VALUE));
+ }
+}
diff --git a/src/test/java/io/github/osvaldjr/unit/gateways/mock/mockserver/assemblers/ExpectationRequestAssemblerTest.java b/src/test/java/io/github/osvaldjr/unit/gateways/mock/mockserver/assemblers/ExpectationRequestAssemblerTest.java
new file mode 100644
index 0000000..a73724b
--- /dev/null
+++ b/src/test/java/io/github/osvaldjr/unit/gateways/mock/mockserver/assemblers/ExpectationRequestAssemblerTest.java
@@ -0,0 +1,67 @@
+package io.github.osvaldjr.unit.gateways.mock.mockserver.assemblers;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.junit.Assert.assertTrue;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.jupiter.api.Test;
+import org.mockito.InjectMocks;
+import org.mockserver.mock.Expectation;
+import org.mockserver.model.Headers;
+import org.mockserver.model.HttpRequest;
+import org.mockserver.model.HttpResponse;
+
+import gherkin.deps.com.google.gson.Gson;
+import io.github.glytching.junit.extension.random.Random;
+import io.github.osvaldjr.domains.StubbyRequest;
+import io.github.osvaldjr.gateways.mock.mockserver.assemblers.ExpectationRequestAssembler;
+import io.github.osvaldjr.unit.UnitTest;
+
+class ExpectationRequestAssemblerTest extends UnitTest {
+
+ @InjectMocks private ExpectationRequestAssembler expectationRequestAssembler;
+
+ @Test
+ void shouldAssembleCorrectly(
+ @Random StubbyRequest.RequestBody requestBody,
+ @Random StubbyRequest.ResponseBody responseBody,
+ @Random int maxHits) {
+ Gson gson = new Gson();
+ Map map = new HashMap<>();
+ map.put("key", "value");
+ requestBody.setBody(map);
+ requestBody.setHeaders(map);
+
+ Expectation expectation =
+ expectationRequestAssembler.assemble(requestBody, responseBody, maxHits);
+
+ assertThat(expectation, notNullValue());
+ HttpRequest request = expectation.getHttpRequest();
+ assertThat(request, notNullValue());
+
+ assertHeaders(requestBody.getHeaders(), request.getHeaders());
+ assertThat(request.getMethod(), equalTo(requestBody.getMethod()));
+ assertThat(request.getBodyAsString(), equalTo(gson.toJson(requestBody.getBody())));
+ assertThat(request.getPath(), equalTo("/" + requestBody.getUrl()));
+
+ HttpResponse response = expectation.getHttpResponse();
+ assertThat(response, notNullValue());
+
+ assertHeaders(responseBody.getHeaders(), response.getHeaders());
+ assertThat(response.getStatusCode(), equalTo(responseBody.getStatus()));
+ assertThat(response.getBodyAsString(), equalTo(gson.toJson(responseBody.getBody())));
+ }
+
+ private void assertHeaders(Map headersMap, Headers headers) {
+
+ headersMap.forEach(
+ (k, v) -> {
+ assertTrue(headers.containsEntry(k));
+ assertThat(headers.getFirstValue(k), equalTo(v));
+ });
+ }
+}
diff --git a/src/test/java/io/github/osvaldjr/unit/gateways/stubby/StubbyGatewayTest.java b/src/test/java/io/github/osvaldjr/unit/gateways/mock/stubby/StubbyMockGatewayImplTest.java
similarity index 82%
rename from src/test/java/io/github/osvaldjr/unit/gateways/stubby/StubbyGatewayTest.java
rename to src/test/java/io/github/osvaldjr/unit/gateways/mock/stubby/StubbyMockGatewayImplTest.java
index ca46717..e7705a1 100644
--- a/src/test/java/io/github/osvaldjr/unit/gateways/stubby/StubbyGatewayTest.java
+++ b/src/test/java/io/github/osvaldjr/unit/gateways/mock/stubby/StubbyMockGatewayImplTest.java
@@ -1,4 +1,4 @@
-package io.github.osvaldjr.unit.gateways.stubby;
+package io.github.osvaldjr.unit.gateways.mock.stubby;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.nullValue;
@@ -23,35 +23,31 @@
import io.github.osvaldjr.domains.StubbyRequest;
import io.github.osvaldjr.domains.StubbyResponse;
import io.github.osvaldjr.gateways.feign.StubbyClient;
-import io.github.osvaldjr.gateways.stubby.StubbyGateway;
-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.stubby.StubbyMockGatewayImpl;
+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;
import io.github.osvaldjr.unit.UnitTest;
-public class StubbyGatewayTest extends UnitTest {
+class StubbyMockGatewayImplTest extends UnitTest {
@Mock StubbyClient stubbyClient;
- @Mock StubbyResponseAssembler stubbyResponseAssembler;
@Mock StubbyRequestAssembler stubbyRequestAssembler;
- @InjectMocks StubbyGateway stubbyGateway;
+ @InjectMocks StubbyMockGatewayImpl stubbyGateway;
@Captor private ArgumentCaptor integerArgumentCaptor;
@Captor private ArgumentCaptor stubbyRequestArgumentCaptor;
@Test
- void shouldGetStubbyResponse(
+ void shouldGetMockHits(
@Random Integer id,
@Random StubbyJsonResponse stubbyJsonResponseMock,
@Random StubbyResponse stubbyResponseMock) {
when(stubbyClient.getService(id)).thenReturn(stubbyJsonResponseMock);
- when(stubbyResponseAssembler.assemble(stubbyJsonResponseMock)).thenReturn(stubbyResponseMock);
- StubbyResponse stubbyResponseReturned = stubbyGateway.getStubbyResponse(String.valueOf(id));
+ Integer mockHits = stubbyGateway.getMockHits(id);
- assertThat(stubbyResponseReturned, equalTo(stubbyResponseMock));
+ assertThat(mockHits, equalTo(stubbyJsonResponseMock.getHits()));
verify(stubbyClient, times(1)).getService(id);
- verify(stubbyResponseAssembler, times(1)).assemble(stubbyJsonResponseMock);
}
@Test
diff --git a/src/test/java/io/github/osvaldjr/unit/gateways/stubby/assemblers/StubbyRequestAssemblerTest.java b/src/test/java/io/github/osvaldjr/unit/gateways/mock/stubby/assemblers/StubbyRequestAssemblerTest.java
similarity index 82%
rename from src/test/java/io/github/osvaldjr/unit/gateways/stubby/assemblers/StubbyRequestAssemblerTest.java
rename to src/test/java/io/github/osvaldjr/unit/gateways/mock/stubby/assemblers/StubbyRequestAssemblerTest.java
index 6007935..c901400 100644
--- a/src/test/java/io/github/osvaldjr/unit/gateways/stubby/assemblers/StubbyRequestAssemblerTest.java
+++ b/src/test/java/io/github/osvaldjr/unit/gateways/mock/stubby/assemblers/StubbyRequestAssemblerTest.java
@@ -1,4 +1,4 @@
-package io.github.osvaldjr.unit.gateways.stubby.assemblers;
+package io.github.osvaldjr.unit.gateways.mock.stubby.assemblers;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
@@ -11,10 +11,10 @@
import gherkin.deps.com.google.gson.Gson;
import io.github.glytching.junit.extension.random.Random;
import io.github.osvaldjr.domains.StubbyRequest;
-import io.github.osvaldjr.gateways.stubby.assemblers.StubbyRequestAssembler;
-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.assemblers.StubbyRequestAssembler;
+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;
import io.github.osvaldjr.unit.UnitTest;
class StubbyRequestAssemblerTest extends UnitTest {
diff --git a/src/test/java/io/github/osvaldjr/unit/gateways/stubby/assemblers/StubbyResponseAssemblerTest.java b/src/test/java/io/github/osvaldjr/unit/gateways/stubby/assemblers/StubbyResponseAssemblerTest.java
deleted file mode 100644
index f14f5e5..0000000
--- a/src/test/java/io/github/osvaldjr/unit/gateways/stubby/assemblers/StubbyResponseAssemblerTest.java
+++ /dev/null
@@ -1,28 +0,0 @@
-package io.github.osvaldjr.unit.gateways.stubby.assemblers;
-
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.equalTo;
-import static org.hamcrest.Matchers.notNullValue;
-
-import org.junit.jupiter.api.Test;
-import org.mockito.InjectMocks;
-
-import io.github.glytching.junit.extension.random.Random;
-import io.github.osvaldjr.domains.StubbyResponse;
-import io.github.osvaldjr.gateways.stubby.assemblers.StubbyResponseAssembler;
-import io.github.osvaldjr.gateways.stubby.jsons.StubbyJsonResponse;
-import io.github.osvaldjr.unit.UnitTest;
-
-class StubbyResponseAssemblerTest extends UnitTest {
-
- @InjectMocks private StubbyResponseAssembler stubbyResponseAssembler;
-
- @Test
- void shouldAssembleResponseCorrectly(@Random StubbyJsonResponse stubbyJsonResponse) {
- StubbyResponse stubbyResponse = stubbyResponseAssembler.assemble(stubbyJsonResponse);
-
- assertThat(stubbyResponse, notNullValue());
- assertThat(stubbyResponse.getHits(), equalTo(stubbyJsonResponse.getHits()));
- assertThat(stubbyResponse.getId(), equalTo(stubbyJsonResponse.getId()));
- }
-}
diff --git a/src/test/java/io/github/osvaldjr/unit/usecases/CreateStubbyUseCaseTest.java b/src/test/java/io/github/osvaldjr/unit/usecases/CreateStubbyUseCaseTest.java
index 0facc6e..21d5a49 100644
--- a/src/test/java/io/github/osvaldjr/unit/usecases/CreateStubbyUseCaseTest.java
+++ b/src/test/java/io/github/osvaldjr/unit/usecases/CreateStubbyUseCaseTest.java
@@ -20,14 +20,14 @@
import io.github.glytching.junit.extension.random.Random;
import io.github.osvaldjr.domains.StubbyRequest;
import io.github.osvaldjr.gateways.FileGateway;
-import io.github.osvaldjr.gateways.stubby.StubbyGateway;
+import io.github.osvaldjr.gateways.mock.MockGateway;
import io.github.osvaldjr.unit.UnitTest;
import io.github.osvaldjr.usecases.CreateStubbyUseCase;
class CreateStubbyUseCaseTest extends UnitTest {
@Mock private FileGateway fileGateway;
- @Mock private StubbyGateway stubbyGateway;
+ @Mock private MockGateway mockGateway;
@InjectMocks private CreateStubbyUseCase createStubbyUsecase;
@Captor private ArgumentCaptor stubbyRequestBodyArgumentCaptor;
@Captor private ArgumentCaptor stubbyResponseBodyArgumentCaptor;
@@ -49,11 +49,11 @@ void shouldExecute(
when(fileGateway.getObjectFromFile(
scenario, mockResponseFile, StubbyRequest.ResponseBody.class))
.thenReturn(stubbyResponseBody);
- when(stubbyGateway.createStubbyRequest(
+ when(mockGateway.createStubbyRequest(
stubbyRequestBodyArgumentCaptor.capture(), stubbyResponseBodyArgumentCaptor.capture()))
.thenReturn(id);
- String stubbyId = createStubbyUsecase.execute(scenario, serviceName, mockName);
+ Object stubbyId = createStubbyUsecase.execute(scenario, serviceName, mockName);
assertThat(stubbyId, equalTo(id));
StubbyRequest.RequestBody requestBody = stubbyRequestBodyArgumentCaptor.getValue();
@@ -65,7 +65,7 @@ void shouldExecute(
StubbyRequest.ResponseBody responseBody = stubbyResponseBodyArgumentCaptor.getValue();
assertThat(responseBody, equalTo(stubbyResponseBody));
verify(fileGateway, times(2)).getObjectFromFile(anyString(), anyString(), any());
- verify(stubbyGateway, times(1))
+ verify(mockGateway, times(1))
.createStubbyRequest(
any(StubbyRequest.RequestBody.class), any(StubbyRequest.ResponseBody.class));
}
diff --git a/src/test/java/io/github/osvaldjr/unit/usecases/HitsMatcherUseCaseTest.java b/src/test/java/io/github/osvaldjr/unit/usecases/HitsMatcherUseCaseTest.java
index 78b9337..d1bf7f2 100644
--- a/src/test/java/io/github/osvaldjr/unit/usecases/HitsMatcherUseCaseTest.java
+++ b/src/test/java/io/github/osvaldjr/unit/usecases/HitsMatcherUseCaseTest.java
@@ -11,24 +11,23 @@
import org.mockito.Mock;
import io.github.glytching.junit.extension.random.Random;
-import io.github.osvaldjr.domains.StubbyResponse;
-import io.github.osvaldjr.gateways.stubby.StubbyGateway;
+import io.github.osvaldjr.gateways.mock.MockGateway;
import io.github.osvaldjr.unit.UnitTest;
import io.github.osvaldjr.usecases.HitsMatcherUseCase;
class HitsMatcherUseCaseTest extends UnitTest {
- @Mock private StubbyGateway stubbyGateway;
+ @Mock private MockGateway mockGateway;
@InjectMocks private HitsMatcherUseCase hitsMatcherUsecase;
@Test
- void shouldExecute(@Random String id, @Random StubbyResponse stubbyResponse) {
- stubbyResponse.setHits(10);
- when(stubbyGateway.getStubbyResponse(id)).thenReturn(stubbyResponse);
+ void shouldExecute(@Random String id) {
+ Integer hits = 10;
+ when(mockGateway.getMockHits(id)).thenReturn(hits);
- boolean match = hitsMatcherUsecase.execute(id, 10);
+ boolean match = hitsMatcherUsecase.execute(id, hits);
Assertions.assertTrue(match);
- verify(stubbyGateway, times(1)).getStubbyResponse(anyString());
+ verify(mockGateway, times(1)).getMockHits(anyString());
}
}
diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml
index b5002a8..021a807 100644
--- a/src/test/resources/application.yml
+++ b/src/test/resources/application.yml
@@ -1,7 +1,7 @@
server.port: 9000
logging:
level:
- io.github.osvaldjr.gateways.feign.IntegrationClient: DEBUG
+ io.github.osvaldjr.gateways.feign.IntegrationClient: FULL
feign:
client:
config:
@@ -21,7 +21,8 @@ dependencies:
server: localhost
port: 6379
# mockserver:
-# port: 9090
+# port: 1080
+# host: yourhost #optional - default localhost
spring:
datasource:
diff --git a/src/test/resources/data/disableFeatures/mocks/successful-request.json b/src/test/resources/data/disableFeatures/mocks/successful-request.json
index d39d839..60a1d57 100644
--- a/src/test/resources/data/disableFeatures/mocks/successful-request.json
+++ b/src/test/resources/data/disableFeatures/mocks/successful-request.json
@@ -1,7 +1,9 @@
{
"url": "/delete",
"method": "DELETE",
- "body": {},
+ "body": {
+ "name": "Linux"
+ },
"headers": {},
"queryParams": {}
}
\ No newline at end of file
diff --git a/src/test/resources/data/enableFeatures/mocks/successful-request.json b/src/test/resources/data/enableFeatures/mocks/successful-request.json
index d39d839..60a1d57 100644
--- a/src/test/resources/data/enableFeatures/mocks/successful-request.json
+++ b/src/test/resources/data/enableFeatures/mocks/successful-request.json
@@ -1,7 +1,9 @@
{
"url": "/delete",
"method": "DELETE",
- "body": {},
+ "body": {
+ "name": "Linux"
+ },
"headers": {},
"queryParams": {}
}
\ No newline at end of file
diff --git a/src/test/resources/data/integration/mocks/http_delete_successful-request.json b/src/test/resources/data/integration/mocks/http_delete_successful-request.json
index d39d839..60a1d57 100644
--- a/src/test/resources/data/integration/mocks/http_delete_successful-request.json
+++ b/src/test/resources/data/integration/mocks/http_delete_successful-request.json
@@ -1,7 +1,9 @@
{
"url": "/delete",
"method": "DELETE",
- "body": {},
+ "body": {
+ "name": "Linux"
+ },
"headers": {},
"queryParams": {}
}
\ No newline at end of file