-
Notifications
You must be signed in to change notification settings - Fork 226
Test: Replaceing mock server with wiremock #4390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
45578cc
d6a5c09
eedeb47
2d99ddb
a6a8efe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package org.prebid.server.functional.model | ||
|
|
||
| enum HttpStatusCode { | ||
|
|
||
| PROCESSING_102(102), | ||
| OK_200(200), | ||
| NO_CONTENT_204(204), | ||
| BAD_REQUEST_400(400), | ||
| NOT_FOUNT_404(404), | ||
| INTERNAL_SERVER_ERROR_500(500), | ||
| SERVICE_UNAVAILABLE_503(503) | ||
|
|
||
| Integer code | ||
|
|
||
| HttpStatusCode(Integer code){ | ||
| this.code = code | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| package org.prebid.server.functional.model | ||
|
|
||
| /** | ||
| * This marker interface should limit the possible values used by the MockServerClientWrapper. | ||
| * This marker interface should limit the possible values used by the WireMockClientWrapper. | ||
| */ | ||
| interface ResponseModel {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,50 +1,77 @@ | ||
| package org.prebid.server.functional.testcontainers.scaffolding | ||
|
|
||
| import org.mockserver.matchers.TimeToLive | ||
| import org.mockserver.matchers.Times | ||
| import org.mockserver.model.HttpRequest | ||
| import org.mockserver.model.HttpResponse | ||
| import com.github.tomakehurst.wiremock.matching.RequestPattern | ||
| import com.github.tomakehurst.wiremock.matching.RequestPatternBuilder | ||
| import org.prebid.server.functional.model.bidderspecific.BidderRequest | ||
| import org.prebid.server.functional.model.request.auction.Banner | ||
| import org.prebid.server.functional.model.request.auction.BidRequest | ||
| import org.prebid.server.functional.model.request.auction.Format | ||
| import org.prebid.server.functional.model.request.auction.Imp | ||
| import org.prebid.server.functional.model.response.auction.BidResponse | ||
| import org.testcontainers.containers.MockServerContainer | ||
| import org.prebid.server.functional.testcontainers.container.NetworkServiceContainer | ||
|
|
||
| import static org.mockserver.model.HttpRequest.request | ||
| import static org.mockserver.model.HttpResponse.response | ||
| import static org.mockserver.model.HttpStatusCode.OK_200 | ||
| import static org.mockserver.model.JsonPathBody.jsonPath | ||
| import static com.github.tomakehurst.wiremock.client.WireMock.post | ||
| import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor | ||
| import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching | ||
| import static com.github.tomakehurst.wiremock.client.WireMock.equalTo | ||
| import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo | ||
| import static com.github.tomakehurst.wiremock.client.WireMock.matchingJsonPath | ||
| import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo | ||
| import static com.github.tomakehurst.wiremock.client.WireMock.aResponse | ||
| import static org.prebid.server.functional.model.HttpStatusCode.OK_200 | ||
|
|
||
| class Bidder extends NetworkScaffolding { | ||
|
|
||
| Bidder(MockServerContainer mockServerContainer, String endpoint = "/auction") { | ||
| super(mockServerContainer, endpoint) | ||
| private static final String DEFAULT_BODY_RESPONSE = | ||
| ''' | ||
| { | ||
| "id": "{{jsonPath request.body '$.id'}}", | ||
| "seatbid": [ | ||
| { | ||
| "bid": [ | ||
| {{#each (jsonPath request.body '$.imp')}} | ||
| { | ||
| "id": "bid-{{randomInt}}", | ||
| "impid": "{{this.id}}", | ||
| "price": 10.0, | ||
| {{#if this.banner}} | ||
| "w": {{this.banner.format.[0].w}}, | ||
| "h": {{this.banner.format.[0].h}}, | ||
| {{/if}} | ||
| "crid": "creative-{{@index}}" | ||
| }{{#unless @last}},{{/unless}} | ||
| {{/each}} | ||
| ], | ||
| "seat": "generic" | ||
| } | ||
| ] | ||
| } | ||
| ''' | ||
|
|
||
| Bidder(NetworkServiceContainer wireMockContainer, String endpoint = "/auction") { | ||
| super(wireMockContainer, endpoint) | ||
| } | ||
|
|
||
| @Override | ||
| protected HttpRequest getRequest(String bidRequestId) { | ||
| request().withPath(endpoint) | ||
| .withBody(jsonPath("\$[?(@.id == '$bidRequestId')]")) | ||
| protected RequestPattern getRequest() { | ||
| postRequestedFor(urlEqualTo(endpoint)) | ||
| .build() | ||
| } | ||
|
|
||
| @Override | ||
| protected HttpRequest getRequest() { | ||
| request().withPath(endpoint) | ||
| protected RequestPatternBuilder getRequest(String bidRequestId) { | ||
| postRequestedFor(urlMatching("^$endpoint(\\?.*)?\$")) | ||
| .withRequestBody(matchingJsonPath("\$.id", equalTo(bidRequestId))) | ||
| } | ||
|
|
||
| HttpRequest getRequest(String bidRequestId, String requestMatchPath) { | ||
| request().withPath(endpoint) | ||
| .withBody(jsonPath("\$[?(@.$requestMatchPath == '$bidRequestId')]")) | ||
| RequestPattern getRequest(String bidRequestId, String requestMatchPath) { | ||
| postRequestedFor(urlMatching("^$endpoint(\\?.*)?\$")) | ||
| .withRequestBody(matchingJsonPath("\$[?(@." + requestMatchPath + " == '" + bidRequestId + "')]")) | ||
| .build() | ||
|
Comment on lines
+61
to
+64
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can be simplified: |
||
| } | ||
|
|
||
| @Override | ||
| void setResponse() { | ||
| mockServerClient.when(request().withPath(endpoint), Times.unlimited(), TimeToLive.unlimited(), -10) | ||
| .respond {request -> request.withPath(endpoint) | ||
| ? response().withStatusCode(OK_200.code()).withBody(getBodyByRequest(request)) | ||
| : HttpResponse.notFoundResponse()} | ||
| wireMockClient.register(post(urlPathEqualTo(endpoint)) | ||
| .atPriority(Integer.MAX_VALUE) | ||
| .willReturn(aResponse() | ||
| .withTransformers("response-template") | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can add |
||
| .withStatus(OK_200.code) | ||
| .withBody(DEFAULT_BODY_RESPONSE))) | ||
| } | ||
|
|
||
| List<BidderRequest> getBidderRequests(String bidRequestId) { | ||
|
|
@@ -65,20 +92,4 @@ class Bidder extends NetworkScaffolding { | |
| Map<String, List<String>> getLastRecordedBidderRequestHeaders(String bidRequestId) { | ||
| return getLastRecordedRequestHeaders(bidRequestId) | ||
| } | ||
|
|
||
| private String getBodyByRequest(HttpRequest request) { | ||
| def requestString = request.bodyAsString | ||
| def jsonNode = toJsonNode(requestString) | ||
| def id = jsonNode.get("id").asText() | ||
| def impNode = jsonNode.get("imp") | ||
| def imps = impNode.collect { | ||
| def formatNode = it.get("banner") != null ? it.get("banner").get("format") : null | ||
| new Imp(id: it.get("id").asText(), | ||
| banner: formatNode != null | ||
| ? new Banner(format: [new Format(width: formatNode.first().get("w").asInt(), height: formatNode.first().get("h").asInt())]) | ||
| : null)} | ||
| def bidRequest = new BidRequest(id: id, imp: imps) | ||
| def response = BidResponse.getDefaultBidResponse(bidRequest) | ||
| encode(response) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,42 +1,37 @@ | ||
| package org.prebid.server.functional.testcontainers.scaffolding | ||
|
|
||
| import org.mockserver.model.HttpRequest | ||
| import com.github.tomakehurst.wiremock.matching.RequestPattern | ||
| import com.github.tomakehurst.wiremock.matching.RequestPatternBuilder | ||
| import org.prebid.server.functional.model.mock.services.currencyconversion.CurrencyConversionRatesResponse | ||
| import org.testcontainers.containers.MockServerContainer | ||
| import org.prebid.server.functional.testcontainers.container.NetworkServiceContainer | ||
|
|
||
| import static org.mockserver.model.HttpRequest.request | ||
| import static org.mockserver.model.HttpResponse.response | ||
| import static org.mockserver.model.HttpStatusCode.OK_200 | ||
| import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor | ||
| import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo | ||
| import static org.prebid.server.functional.util.CurrencyUtil.DEFAULT_CURRENCY_RATES | ||
|
|
||
| class CurrencyConversion extends NetworkScaffolding { | ||
|
|
||
| static final String CURRENCY_ENDPOINT_PATH = "/currency" | ||
| private static final CurrencyConversionRatesResponse DEFAULT_RATES_RESPONSE = CurrencyConversionRatesResponse.getDefaultCurrencyConversionRatesResponse(DEFAULT_CURRENCY_RATES) | ||
|
|
||
| CurrencyConversion(MockServerContainer mockServerContainer) { | ||
| super(mockServerContainer, CURRENCY_ENDPOINT_PATH) | ||
| CurrencyConversion(NetworkServiceContainer wireMockContainer) { | ||
| super(wireMockContainer, CURRENCY_ENDPOINT_PATH) | ||
| } | ||
|
|
||
| void setCurrencyConversionRatesResponse(CurrencyConversionRatesResponse conversionRatesResponse = DEFAULT_RATES_RESPONSE) { | ||
| setResponse(request, conversionRatesResponse) | ||
| setResponse(getRequest(), conversionRatesResponse) | ||
| } | ||
|
|
||
| @Override | ||
| void setResponse() { | ||
| mockServerClient.when(request().withPath(endpoint)) | ||
| .respond(response().withStatusCode(OK_200.code())) | ||
| } | ||
| void setResponse() {} | ||
|
|
||
| @Override | ||
| protected HttpRequest getRequest(String ignored) { | ||
| request().withMethod("GET") | ||
| .withPath(CURRENCY_ENDPOINT_PATH) | ||
| protected RequestPattern getRequest() { | ||
| getRequestedFor(urlEqualTo(CURRENCY_ENDPOINT_PATH)).build() | ||
| } | ||
|
|
||
| @Override | ||
| protected HttpRequest getRequest() { | ||
| request().withMethod("GET") | ||
| .withPath(CURRENCY_ENDPOINT_PATH) | ||
| protected RequestPatternBuilder getRequest(String value) { | ||
| return null | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use
org.apache.http.HttpStatusinstead of our own codes