|
| 1 | +package org.prebid.server.bidder.zentotem; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 4 | +import com.iab.openrtb.request.BidRequest; |
| 5 | +import com.iab.openrtb.request.Imp; |
| 6 | +import com.iab.openrtb.response.Bid; |
| 7 | +import com.iab.openrtb.response.BidResponse; |
| 8 | +import com.iab.openrtb.response.SeatBid; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.prebid.server.VertxTest; |
| 11 | +import org.prebid.server.bidder.model.BidderBid; |
| 12 | +import org.prebid.server.bidder.model.BidderCall; |
| 13 | +import org.prebid.server.bidder.model.BidderError; |
| 14 | +import org.prebid.server.bidder.model.HttpRequest; |
| 15 | +import org.prebid.server.bidder.model.HttpResponse; |
| 16 | +import org.prebid.server.bidder.model.Result; |
| 17 | +import org.prebid.server.proto.openrtb.ext.response.BidType; |
| 18 | + |
| 19 | +import java.math.BigDecimal; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Set; |
| 23 | +import java.util.function.UnaryOperator; |
| 24 | + |
| 25 | +import static java.util.Collections.singletonList; |
| 26 | +import static org.assertj.core.api.Assertions.assertThat; |
| 27 | +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
| 28 | +import static org.prebid.server.bidder.model.BidderError.badServerResponse; |
| 29 | + |
| 30 | +public class ZentotemBidderTest extends VertxTest { |
| 31 | + |
| 32 | + private static final String ENDPOINT_URL = "https://test.endpoint.com"; |
| 33 | + |
| 34 | + private final ZentotemBidder target = new ZentotemBidder(ENDPOINT_URL, jacksonMapper); |
| 35 | + |
| 36 | + @Test |
| 37 | + public void creationShouldFailOnInvalidEndpointUrl() { |
| 38 | + assertThatIllegalArgumentException().isThrownBy(() -> new ZentotemBidder("invalid_url", jacksonMapper)); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void makeHttpRequestsShouldCreateSeparateRequestForEachImp() { |
| 43 | + // given |
| 44 | + final BidRequest bidRequest = givenBidRequest( |
| 45 | + imp -> imp.id("imp1"), |
| 46 | + imp -> imp.id("imp2")); |
| 47 | + |
| 48 | + // when |
| 49 | + final Result<List<HttpRequest<BidRequest>>> result = target.makeHttpRequests(bidRequest); |
| 50 | + |
| 51 | + // then |
| 52 | + assertThat(result.getErrors()).isEmpty(); |
| 53 | + assertThat(result.getValue()).hasSize(2) |
| 54 | + .extracting(HttpRequest::getImpIds) |
| 55 | + .containsExactly(Set.of("imp1"), Set.of("imp2")); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void makeHttpRequestsShouldSetCorrectUriAndBody() { |
| 60 | + // given |
| 61 | + final BidRequest bidRequest = givenBidRequest(imp -> imp.id("imp1")); |
| 62 | + |
| 63 | + // when |
| 64 | + final Result<List<HttpRequest<BidRequest>>> result = target.makeHttpRequests(bidRequest); |
| 65 | + |
| 66 | + // then |
| 67 | + final BidRequest expectedRequest = bidRequest.toBuilder() |
| 68 | + .imp(singletonList(bidRequest.getImp().getFirst())) |
| 69 | + .build(); |
| 70 | + |
| 71 | + assertThat(result.getErrors()).isEmpty(); |
| 72 | + assertThat(result.getValue()).hasSize(1).first() |
| 73 | + .satisfies(httpRequest -> { |
| 74 | + assertThat(httpRequest.getUri()).isEqualTo(ENDPOINT_URL); |
| 75 | + assertThat(httpRequest.getPayload()).isEqualTo(expectedRequest); |
| 76 | + assertThat(httpRequest.getBody()).isEqualTo(jacksonMapper.encodeToBytes(expectedRequest)); |
| 77 | + }); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void makeBidsShouldReturnErrorWhenResponseBodyCouldNotBeParsed() { |
| 82 | + // given |
| 83 | + final BidderCall<BidRequest> httpCall = givenHttpCall("invalid_json"); |
| 84 | + |
| 85 | + // when |
| 86 | + final Result<List<BidderBid>> result = target.makeBids(httpCall, null); |
| 87 | + |
| 88 | + // then |
| 89 | + assertThat(result.getErrors()).hasSize(1) |
| 90 | + .allSatisfy(error -> { |
| 91 | + assertThat(error.getType()).isEqualTo(BidderError.Type.bad_server_response); |
| 92 | + assertThat(error.getMessage()).startsWith("Failed to decode: Unrecognized token 'invalid_json'"); |
| 93 | + }); |
| 94 | + assertThat(result.getValue()).isEmpty(); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void makeBidsShouldReturnEmptyListWhenBidResponseOrSeatBidAreNull() throws JsonProcessingException { |
| 99 | + // given |
| 100 | + final BidResponse bidResponseWithNullSeatBid = BidResponse.builder().seatbid(null).build(); |
| 101 | + final BidderCall<BidRequest> httpCallWithNullSeatBid = |
| 102 | + givenHttpCall(mapper.writeValueAsString(bidResponseWithNullSeatBid)); |
| 103 | + |
| 104 | + // when |
| 105 | + final Result<List<BidderBid>> nullSeatBidResult = target.makeBids(httpCallWithNullSeatBid, null); |
| 106 | + |
| 107 | + // then |
| 108 | + assertThat(nullSeatBidResult.getErrors()).isEmpty(); |
| 109 | + assertThat(nullSeatBidResult.getValue()).isEmpty(); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void makeBidsShouldReturnBannerBid() throws JsonProcessingException { |
| 114 | + // given |
| 115 | + final Bid bannerBid = givenBid(1); |
| 116 | + final BidderCall<BidRequest> httpCall = givenHttpCall(givenBidResponse(bannerBid)); |
| 117 | + |
| 118 | + // when |
| 119 | + final Result<List<BidderBid>> result = target.makeBids(httpCall, null); |
| 120 | + |
| 121 | + // then |
| 122 | + assertThat(result.getErrors()).isEmpty(); |
| 123 | + assertThat(result.getValue()).containsOnly(BidderBid.of(bannerBid, BidType.banner, "USD")); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void makeBidsShouldReturnVideoBid() throws JsonProcessingException { |
| 128 | + // given |
| 129 | + final Bid videoBid = givenBid(2); |
| 130 | + final BidderCall<BidRequest> httpCall = givenHttpCall(givenBidResponse(videoBid)); |
| 131 | + |
| 132 | + // when |
| 133 | + final Result<List<BidderBid>> result = target.makeBids(httpCall, null); |
| 134 | + |
| 135 | + // then |
| 136 | + assertThat(result.getErrors()).isEmpty(); |
| 137 | + assertThat(result.getValue()).containsOnly(BidderBid.of(videoBid, BidType.video, "USD")); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + public void makeBidsShouldReturnNativeBid() throws JsonProcessingException { |
| 142 | + // given |
| 143 | + final Bid nativeBid = givenBid(4); |
| 144 | + final BidderCall<BidRequest> httpCall = givenHttpCall(givenBidResponse(nativeBid)); |
| 145 | + |
| 146 | + // when |
| 147 | + final Result<List<BidderBid>> result = target.makeBids(httpCall, null); |
| 148 | + |
| 149 | + // then |
| 150 | + assertThat(result.getErrors()).isEmpty(); |
| 151 | + assertThat(result.getValue()).containsOnly(BidderBid.of(nativeBid, BidType.xNative, "USD")); |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + public void makeBidsShouldReturnErrorIfMtypeIsUnsupported() throws JsonProcessingException { |
| 156 | + // given |
| 157 | + final Bid bidWithUnsupportedMtype = givenBid(3); |
| 158 | + final BidderCall<BidRequest> httpCall = givenHttpCall(givenBidResponse(bidWithUnsupportedMtype)); |
| 159 | + |
| 160 | + // when |
| 161 | + final Result<List<BidderBid>> result = target.makeBids(httpCall, null); |
| 162 | + |
| 163 | + // then |
| 164 | + assertThat(result.getValue()).isEmpty(); |
| 165 | + assertThat(result.getErrors()).hasSize(1) |
| 166 | + .containsExactly(badServerResponse("could not define media type for impression: impId")); |
| 167 | + } |
| 168 | + |
| 169 | + private static BidRequest givenBidRequest(UnaryOperator<Imp.ImpBuilder>... impCustomizers) { |
| 170 | + final List<Imp> imps = Arrays.stream(impCustomizers) |
| 171 | + .map(ZentotemBidderTest::givenImp) |
| 172 | + .toList(); |
| 173 | + return BidRequest.builder().imp(imps).build(); |
| 174 | + } |
| 175 | + |
| 176 | + private static Imp givenImp(UnaryOperator<Imp.ImpBuilder> impCustomizer) { |
| 177 | + return impCustomizer.apply(Imp.builder().id("impId")).build(); |
| 178 | + } |
| 179 | + |
| 180 | + private static Bid givenBid(Integer mtype) { |
| 181 | + return Bid.builder().id("bidId").impid("impId").price(BigDecimal.ONE).mtype(mtype).build(); |
| 182 | + } |
| 183 | + |
| 184 | + private static String givenBidResponse(Bid... bids) throws JsonProcessingException { |
| 185 | + return mapper.writeValueAsString(BidResponse.builder() |
| 186 | + .cur("USD") |
| 187 | + .seatbid(singletonList(SeatBid.builder().bid(List.of(bids)).build())) |
| 188 | + .build()); |
| 189 | + } |
| 190 | + |
| 191 | + private static BidderCall<BidRequest> givenHttpCall(String body) { |
| 192 | + return BidderCall.succeededHttp( |
| 193 | + HttpRequest.<BidRequest>builder().build(), |
| 194 | + HttpResponse.of(200, null, body), |
| 195 | + null); |
| 196 | + } |
| 197 | +} |
0 commit comments