-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support to mock with embedded mockserver
- Loading branch information
Showing
28 changed files
with
399 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/main/java/io/github/osvaldjr/confs/MockServerConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/main/java/io/github/osvaldjr/gateways/mock/MockGateway.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/io/github/osvaldjr/gateways/mock/mockserver/MockServerMockGatewayImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...a/io/github/osvaldjr/gateways/mock/mockserver/assemblers/ExpectationRequestAssembler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
...by/assemblers/StubbyRequestAssembler.java → ...by/assemblers/StubbyRequestAssembler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...eways/stubby/jsons/StubbyJsonRequest.java → .../mock/stubby/jsons/StubbyJsonRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ways/stubby/jsons/StubbyJsonResponse.java → ...mock/stubby/jsons/StubbyJsonResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...eways/stubby/jsons/StubbyRequestBody.java → .../mock/stubby/jsons/StubbyRequestBody.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ways/stubby/jsons/StubbyResponseBody.java → ...mock/stubby/jsons/StubbyResponseBody.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 0 additions & 16 deletions
16
src/main/java/io/github/osvaldjr/gateways/stubby/assemblers/StubbyResponseAssembler.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.