Skip to content

Commit

Permalink
Simplify integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Serhii Nahornyi committed Jul 6, 2021
1 parent e008d1a commit f3222de
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 344 deletions.
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
20 changes: 6 additions & 14 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,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 All @@ -13,10 +12,10 @@
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,15 +24,13 @@ 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]*$"))
.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"))));
Expand All @@ -44,17 +41,12 @@ public void openrtb2AuctionShouldRespondWithBidsFromEmxdigital() throws IOExcept
.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");

// 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());
}
}

15 changes: 14 additions & 1 deletion src/test/java/org/prebid/server/it/IntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.prebid.server.it.util.BidCacheRequestPattern;
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,6 +36,7 @@

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;
Expand Down Expand Up @@ -124,7 +126,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 +189,17 @@ static CustomComparator openrtbCacheDebugComparator() {
new Customization("ext.debug.httpcalls.cache", arrayValueMatcher));
}

static void assertJSONEquals(String file, String bidder, String response, 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(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
11 changes: 1 addition & 10 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,6 @@
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.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;
Expand All @@ -15,7 +13,6 @@
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 @@ -33,16 +30,10 @@ public void openrtb2AuctionShouldRespondWithBidsFromLoopme() throws IOException,
.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");

// 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",
"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

0 comments on commit f3222de

Please sign in to comment.