Skip to content
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

Simplify integration tests #1356

Merged
merged 7 commits into from
Jul 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
<artifactId>wiremock-jre8</artifactId>
<version>${wiremock.version}</version>
<scope>test</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
import org.prebid.server.json.DecodeException;
import org.prebid.server.json.JacksonMapper;
import org.prebid.server.proto.openrtb.ext.ExtPrebid;
import org.prebid.server.proto.openrtb.ext.request.ExtRequest;
import org.prebid.server.proto.openrtb.ext.request.ExtRequestPrebid;
import org.prebid.server.proto.openrtb.ext.request.emxdigital.ExtImpEmxDigital;
import org.prebid.server.proto.openrtb.ext.response.BidType;
import org.prebid.server.util.HttpUtil;
Expand Down Expand Up @@ -241,28 +239,10 @@ private String makeUrl(BidRequest bidRequest) {
final Long tmax = bidRequest.getTmax();
final int urlTimeout = tmax == 0 ? 1000 : tmax.intValue();

if (isDebugEnabled(bidRequest)) {
// for passing validation tests
return String.format("%s?t=1000&ts=2060541160", endpointUrl);
}

return String.format("%s?t=%s&ts=%s&src=pbserver", endpointUrl, urlTimeout,
(int) Instant.now().getEpochSecond());
}

/**
* Determines debug flag from {@link BidRequest} or {@link ExtRequest}.
*/
private boolean isDebugEnabled(BidRequest bidRequest) {
if (Objects.equals(bidRequest.getTest(), 1)) {
return true;
}

final ExtRequest extRequest = bidRequest.getExt();
final ExtRequestPrebid extRequestPrebid = extRequest != null ? extRequest.getPrebid() : null;
return extRequestPrebid != null && Objects.equals(extRequestPrebid.getDebug(), 1);
}

@Override
public Result<List<BidderBid>> makeBids(HttpCall<BidRequest> httpCall, BidRequest bidRequest) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -496,43 +496,6 @@ public void makeHttpRequestsShouldSendRequestToModifiedUrlWithHeaders() {
tuple("Accept-Language", "fr"));
}

@Test
public void makeHttpRequestsShouldSendRequestToTestUrlWithHeadersWhenTestIsOne() {
// given
final BidRequest bidRequest = BidRequest.builder()
.imp(singletonList(Imp.builder()
.banner(Banner.builder().w(1).h(1).build())
.ext(mapper.valueToTree(ExtPrebid.of(null, ExtImpEmxDigital.of("1", "asd"))))
.build()))
.device(Device.builder().ip("ip").ua("Agent").language("fr").dnt(1).build())
.site(Site.builder().page("myPage").build())
.test(1)
.tmax(1000L)
.build();

// when
final Result<List<HttpRequest<BidRequest>>> result = emxDigitalBidder
.makeHttpRequests(bidRequest);

// then
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue()).hasSize(1)
.extracting(HttpRequest::getUri)
.allSatisfy(uri -> assertThat(uri).isEqualTo("https://test.endpoint.com?t=1000&ts=2060541160"));

assertThat(result.getValue()).hasSize(1)
.flatExtracting(r -> r.getHeaders().entries())
.extracting(Map.Entry::getKey, Map.Entry::getValue)
.containsOnly(
tuple("Content-Type", "application/json;charset=utf-8"),
tuple("Accept", "application/json"),
tuple("User-Agent", "Agent"),
tuple("X-Forwarded-For", "ip"),
tuple("Referer", "myPage"),
tuple("DNT", "1"),
tuple("Accept-Language", "fr"));
}

@Test
public void makeBidsShouldReturnErrorWhenResponseBodyCouldNotBeParsed() {
// given
Expand Down
19 changes: 5 additions & 14 deletions src/test/java/org/prebid/server/it/ApplicationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,8 @@ public void openrtb2AuctionShouldRespondWithBidsFromRubiconAndAppnexus() throws
.post("/openrtb2/auction");

// then
final String expectedAuctionResponse = openrtbAuctionResponseFrom(
"openrtb2/rubicon_appnexus/test-auction-rubicon-appnexus-response.json",
response, asList(RUBICON, APPNEXUS, APPNEXUS_ALIAS));

JSONAssert.assertEquals(expectedAuctionResponse, response.asString(), openrtbCacheDebugComparator());
assertJsonEquals("openrtb2/rubicon_appnexus/test-auction-rubicon-appnexus-response.json",
response, asList(RUBICON, APPNEXUS, APPNEXUS_ALIAS), openrtbCacheDebugCustomization());
}

@Test
Expand Down Expand Up @@ -214,11 +211,8 @@ public void openrtb2MultiBidAuctionShouldRespondWithBidsFromRubiconAndAppnexus()
.post("/openrtb2/auction");

// then
final String expectedAuctionResponse = openrtbAuctionResponseFrom(
"openrtb2/rubicon_appnexus_multi_bid/test-auction-rubicon-appnexus-response.json",
assertJsonEquals("openrtb2/rubicon_appnexus_multi_bid/test-auction-rubicon-appnexus-response.json",
response, asList(RUBICON, APPNEXUS, APPNEXUS_ALIAS));

JSONAssert.assertEquals(expectedAuctionResponse, response.asString(), openrtbCacheDebugComparator());
}

@Test
Expand All @@ -240,11 +234,8 @@ public void openrtb2AuctionShouldRespondWithStoredBidResponse() throws IOExcepti
.post("/openrtb2/auction");

// then
final String expectedAuctionResponse = openrtbAuctionResponseFrom(
"openrtb2/storedresponse/test-auction-response.json",
response, singletonList(RUBICON));

JSONAssert.assertEquals(expectedAuctionResponse, response.asString(), openrtbCacheDebugComparator());
assertJsonEquals("openrtb2/storedresponse/test-auction-response.json",
response, singletonList(RUBICON), openrtbCacheDebugCustomization());
}

@Test
Expand Down
6 changes: 1 addition & 5 deletions src/test/java/org/prebid/server/it/AvocetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.json.JSONException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.skyscreamer.jsonassert.JSONAssert;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;
Expand Down Expand Up @@ -38,9 +37,6 @@ public void openrtb2AuctionShouldRespondWithBidsFromAvocet() throws IOException,
.post("/openrtb2/auction");

// then
final String expectedAuctionResponse = openrtbAuctionResponseFrom(
"openrtb2/avocet/test-auction-avocet-response.json", response, singletonList("avocet"));

JSONAssert.assertEquals(expectedAuctionResponse, response.asString(), openrtbCacheDebugComparator());
assertJsonEquals("openrtb2/avocet/test-auction-avocet-response.json", response, singletonList("avocet"));
}
}
7 changes: 1 addition & 6 deletions src/test/java/org/prebid/server/it/BeintooTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.json.JSONException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.skyscreamer.jsonassert.JSONAssert;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;
Expand Down Expand Up @@ -48,10 +47,6 @@ public void openrtb2AuctionShouldRespondWithBidsFromBeintoo() throws IOException
.post("/openrtb2/auction");

// then
final String expectedAuctionResponse = openrtbAuctionResponseFrom(
"openrtb2/beintoo/test-auction-beintoo-response.json",
response, singletonList("beintoo"));

JSONAssert.assertEquals(expectedAuctionResponse, response.asString(), openrtbCacheDebugComparator());
assertJsonEquals("openrtb2/beintoo/test-auction-beintoo-response.json", response, singletonList("beintoo"));
}
}
6 changes: 1 addition & 5 deletions src/test/java/org/prebid/server/it/DatablocksTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.json.JSONException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.skyscreamer.jsonassert.JSONAssert;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;
Expand Down Expand Up @@ -45,10 +44,7 @@ public void openrtb2AuctionShouldRespondWithBidsFromDatablocks() throws IOExcept
.post("/openrtb2/auction");

// then
final String expectedAuctionResponse = openrtbAuctionResponseFrom(
"openrtb2/datablocks/test-auction-datablocks-response.json",
assertJsonEquals("openrtb2/datablocks/test-auction-datablocks-response.json",
response, singletonList("datablocks"));

JSONAssert.assertEquals(expectedAuctionResponse, response.asString(), openrtbCacheDebugComparator());
}
}
30 changes: 9 additions & 21 deletions src/test/java/org/prebid/server/it/EmxdigitalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.json.JSONException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.skyscreamer.jsonassert.JSONAssert;
import org.prebid.server.model.Endpoint;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;
Expand All @@ -13,9 +13,9 @@
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.equalToIgnoreCase;
import static com.github.tomakehurst.wiremock.client.WireMock.equalToJson;
import static com.github.tomakehurst.wiremock.client.WireMock.matching;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
import static io.restassured.RestAssured.given;
import static java.util.Collections.singletonList;

@RunWith(SpringRunner.class)
Expand All @@ -25,36 +25,24 @@ public class EmxdigitalTest extends IntegrationTest {
public void openrtb2AuctionShouldRespondWithBidsFromEmxdigital() throws IOException, JSONException {
// given
WIRE_MOCK_RULE.stubFor(post(urlPathEqualTo("/emx_digital-exchange"))
.withQueryParam("t", equalTo("1000"))
.withQueryParam("ts", equalTo("2060541160"))
.withQueryParam("t", matching("^[0-9]*$"))
rpanchyk marked this conversation as resolved.
Show resolved Hide resolved
.withQueryParam("ts", matching("^[0-9]*$"))
.withHeader("Accept", equalTo("application/json"))
.withHeader("Content-Type", equalToIgnoreCase("application/json;charset=utf-8"))
.withHeader("User-Agent", equalTo("Android Chrome/60"))
.withHeader("User-Agent", equalTo("userAgent"))
.withHeader("X-Forwarded-For", equalTo("193.168.244.1"))
.withHeader("Referer", equalTo("http://www.example.com"))
.withHeader("DNT", equalTo("2"))
.withHeader("Accept-Language", equalTo("en"))
.withRequestBody(equalToJson(jsonFrom("openrtb2/emxdigital/test-emxdigital-bid-request.json"),
true, true))
true, false))
.willReturn(aResponse().withBody(jsonFrom("openrtb2/emxdigital/test-emxdigital-bid-response.json"))));

// when
final Response response = given(SPEC)
.header("Referer", "http://www.example.com")
.header("X-Forwarded-For", "193.168.244.1")
.header("User-Agent", "userAgent")
.header("Origin", "http://www.example.com")
// this uids cookie value stands for {"uids":{"emxdigital":"STR-UID"}}
.cookie("uids", "eyJ1aWRzIjp7ImVteGRpZ2l0YWwiOiJTVFItVUlEIn19")
.body(jsonFrom("openrtb2/emxdigital/test-auction-emxdigital-request.json"))
.post("/openrtb2/auction");
final Response response =
responseFor("openrtb2/emxdigital/test-auction-emxdigital-request.json", Endpoint.openrtb2_auction);

// then
final String expectedAuctionResponse = openrtbAuctionResponseFrom(
"openrtb2/emxdigital/test-auction-emxdigital-response.json",
assertJsonEquals("openrtb2/emxdigital/test-auction-emxdigital-response.json",
response, singletonList("emx_digital"));

JSONAssert.assertEquals(expectedAuctionResponse, response.asString(), openrtbCacheDebugComparator());
}
}

75 changes: 58 additions & 17 deletions src/test/java/org/prebid/server/it/IntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
import org.prebid.server.cache.proto.response.CacheObject;
import org.prebid.server.it.hooks.TestHooksConfiguration;
import org.prebid.server.it.util.BidCacheRequestPattern;
import org.prebid.server.model.Endpoint;
import org.skyscreamer.jsonassert.ArrayValueMatcher;
import org.skyscreamer.jsonassert.Customization;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompare;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.skyscreamer.jsonassert.ValueMatcher;
Expand All @@ -35,12 +37,14 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
import static io.restassured.RestAssured.given;
import static java.lang.String.format;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
Expand All @@ -63,6 +67,9 @@ public abstract class IntegrationTest extends VertxTest {
public WireMockClassRule instanceRule = WIRE_MOCK_RULE;

protected static final RequestSpecification SPEC = spec(APP_PORT);
private static final String HOST_AND_PORT = "localhost:" + WIREMOCK_PORT;
private static final String CACHE_PATH = "/cache";
private static final String CACHE_ENDPOINT = "http://" + HOST_AND_PORT + CACHE_PATH;

@BeforeClass
public static void setUp() throws IOException {
Expand All @@ -81,6 +88,16 @@ static RequestSpecification spec(int port) {
.build();
}

protected static Response responseFor(String file, Endpoint endpoint) throws IOException {
return given(SPEC)
.header("Referer", "http://www.example.com")
.header("X-Forwarded-For", "193.168.244.1")
.header("User-Agent", "userAgent")
.header("Origin", "http://www.example.com")
.body(jsonFrom(file))
.post(endpoint.value());
}

protected static String jsonFrom(String file) throws IOException {
// workaround to clear formatting
return mapper.writeValueAsString(mapper.readTree(IntegrationTest.class.getResourceAsStream(file)));
Expand All @@ -94,20 +111,12 @@ protected static String openrtbAuctionResponseFrom(String templatePath, Response

private static String auctionResponseFrom(String templatePath, Response response, String responseTimePath,
List<String> bidders) throws IOException {
final String hostAndPort = "localhost:" + WIREMOCK_PORT;
final String cachePath = "/cache";
final String cacheEndpoint = "http://" + hostAndPort + cachePath;

String result = jsonFrom(templatePath)
.replaceAll("\\{\\{ cache.endpoint }}", cacheEndpoint)
.replaceAll("\\{\\{ cache.resource_url }}", cacheEndpoint + "?uuid=")
.replaceAll("\\{\\{ cache.host }}", hostAndPort)
.replaceAll("\\{\\{ cache.path }}", cachePath)
.replaceAll("\\{\\{ event.url }}", "http://localhost:8080/event?");

String result = replaceStaticInfo(jsonFrom(templatePath));

for (final String bidder : bidders) {
result = result.replaceAll("\\{\\{ " + bidder + "\\.exchange_uri }}",
"http://" + hostAndPort + "/" + bidder + "-exchange");
"http://" + HOST_AND_PORT + "/" + bidder + "-exchange");
result = setResponseTime(response, result, bidder, responseTimePath);
}

Expand All @@ -124,7 +133,7 @@ private static String setResponseTime(Response response, String expectedResponse
}

final Object cacheVal = response.path("ext.responsetimemillis.cache");
final Integer cacheResponseTime = val instanceof Integer ? (Integer) cacheVal : null;
final Integer cacheResponseTime = cacheVal instanceof Integer ? (Integer) cacheVal : null;
if (cacheResponseTime != null) {
expectedResponseJson = expectedResponseJson.replaceAll("\"\\{\\{ cache\\.response_time_ms }}\"",
cacheResponseTime.toString());
Expand Down Expand Up @@ -165,9 +174,9 @@ private static String resolvePriceForJsonMediaType(PutObject putItem) {

/**
* Cache debug fields "requestbody" and "responsebody" are escaped JSON strings.
* This comparator allows to compare them with actual values as usual JSON objects.
* This customization allows to compare them with actual values as usual JSON objects.
*/
static CustomComparator openrtbCacheDebugComparator() {
static Customization openrtbCacheDebugCustomization() {
final ValueMatcher<Object> jsonStringValueMatcher = (actual, expected) -> {
try {
return !JSONCompare.compareJSON(actual.toString(), expected.toString(), JSONCompareMode.NON_EXTENSIBLE)
Expand All @@ -182,9 +191,41 @@ static CustomComparator openrtbCacheDebugComparator() {
new Customization("ext.debug.httpcalls.cache[*].requestbody", jsonStringValueMatcher),
new Customization("ext.debug.httpcalls.cache[*].responsebody", jsonStringValueMatcher)));

return new CustomComparator(
JSONCompareMode.NON_EXTENSIBLE,
new Customization("ext.debug.httpcalls.cache", arrayValueMatcher));
return new Customization("ext.debug.httpcalls.cache", arrayValueMatcher);
}

protected static void assertJsonEquals(String file,
Response response,
List<String> bidders,
Customization... customizations) throws IOException, JSONException {
final List<Customization> fullCustomizations = new ArrayList<>(Arrays.asList(customizations));
fullCustomizations.add(new Customization("ext.prebid.auctiontimestamp", (o1, o2) -> true));
fullCustomizations.add(new Customization("ext.responsetimemillis.cache", (o1, o2) -> true));
String expectedRequest = replaceStaticInfo(jsonFrom(file));
for (String bidder : bidders) {
expectedRequest = replaceBidderRelatedStaticInfo(expectedRequest, bidder);
fullCustomizations.add(new Customization(
String.format("ext.responsetimemillis.%s", bidder), (o1, o2) -> true));
}

JSONAssert.assertEquals(expectedRequest, response.asString(),
new CustomComparator(JSONCompareMode.NON_EXTENSIBLE,
fullCustomizations.stream().toArray(Customization[]::new)));
}

private static String replaceStaticInfo(String json) {

return json.replaceAll("\\{\\{ cache.endpoint }}", CACHE_ENDPOINT)
.replaceAll("\\{\\{ cache.resource_url }}", CACHE_ENDPOINT + "?uuid=")
.replaceAll("\\{\\{ cache.host }}", HOST_AND_PORT)
.replaceAll("\\{\\{ cache.path }}", CACHE_PATH)
.replaceAll("\\{\\{ event.url }}", "http://localhost:8080/event?");
}

private static String replaceBidderRelatedStaticInfo(String json, String bidder) {

return json.replaceAll("\\{\\{ " + bidder + "\\.exchange_uri }}",
"http://" + HOST_AND_PORT + "/" + bidder + "-exchange");
}

static BidCacheRequestPattern equalToBidCacheRequest(String json) {
Expand Down
Loading