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 2 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
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
31 changes: 9 additions & 22 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,10 +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)
public class EmxdigitalTest extends IntegrationTest {
Expand All @@ -25,36 +24,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))
.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",
response, singletonList("emx_digital"));

JSONAssert.assertEquals(expectedAuctionResponse, response.asString(), openrtbCacheDebugComparator());
assertJSONEquals("openrtb2/emxdigital/test-auction-emxdigital-response.json",
"emx_digital", response.asString());
}
}

27 changes: 26 additions & 1 deletion 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 Down Expand Up @@ -81,6 +85,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 Down Expand Up @@ -124,7 +138,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 @@ -187,6 +201,17 @@ static CustomComparator openrtbCacheDebugComparator() {
new Customization("ext.debug.httpcalls.cache", arrayValueMatcher));
}

protected static void assertJSONEquals(String file, String bidder, String response, Customization... customizations)
rpanchyk marked this conversation as resolved.
Show resolved Hide resolved
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(String.format("ext.responsetimemillis.%s", bidder), (o1, o2) -> true));

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

static BidCacheRequestPattern equalToBidCacheRequest(String json) {
return new BidCacheRequestPattern(json);
}
Expand Down
22 changes: 4 additions & 18 deletions src/test/java/org/prebid/server/it/LoopmeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
import org.json.JSONException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.prebid.server.model.Endpoint;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;
Expand All @@ -14,8 +13,6 @@
import static com.github.tomakehurst.wiremock.client.WireMock.equalToJson;
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)
public class LoopmeTest extends IntegrationTest {
Expand All @@ -28,21 +25,10 @@ public void openrtb2AuctionShouldRespondWithBidsFromLoopme() throws IOException,
.willReturn(aResponse().withBody(jsonFrom("openrtb2/loopme/test-loopme-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":{"loopme":"LPM-UID"}}
.cookie("uids", "eyJ1aWRzIjp7Imxvb3BtZSI6IkxQTS1VSUQifX0=")
.body(jsonFrom("openrtb2/loopme/test-auction-loopme-request.json"))
.post("/openrtb2/auction");
final Response response =
responseFor("openrtb2/loopme/test-auction-loopme-request.json", Endpoint.openrtb2_auction);

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

JSONAssert.assertEquals(expectedAuctionResponse, response.asString(), JSONCompareMode.NON_EXTENSIBLE);
assertJSONEquals("openrtb2/loopme/test-auction-loopme-response.json", "loopme", response.asString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@
{
"id": "uuid",
rpanchyk marked this conversation as resolved.
Show resolved Hide resolved
"banner": {
"format": [
{
"w": 300,
"h": 250
}
]
"w": 300,
"h": 250
},
"ext": {
"prebid": {
Expand All @@ -22,54 +18,6 @@
}
}
],
"device": {
"pxratio": 4.2,
"dnt": 2,
"language": "en",
"ifa": "ifaId",
"ua": "Android Chrome/60",
"ip": "127.0.0.1"
},
"site": {
"page": "http://www.example.com",
"publisher": {
"id": "publisherId"
},
"ext": {
"amp": 0
}
},
"at": 1,
"tmax": 5000,
"cur": [
"USD"
],
"source": {
"fd": 1,
"tid": "tid"
},
"ext": {
"prebid": {
"debug": 1,
"targeting": {
"pricegranularity": {
"precision": 2,
"ranges": [
{
"max": 20,
"increment": 0.1
}
]
}
},
"auctiontimestamp": 1000
}
},
"user": {
"ext": {
"consent": "consentValue"
}
},
"regs": {
"ext": {
"gdpr": 0
Expand Down
Loading