|
53 | 53 | @ExtendWith(MockitoExtension.class) |
54 | 54 | public class LiveintentAnalyticsReporterTest extends VertxTest { |
55 | 55 |
|
56 | | - @Mock |
57 | | - private HttpClient httpClient; |
58 | | - |
59 | | - @Captor |
60 | | - private ArgumentCaptor<String> jsonCaptor; |
61 | | - |
62 | | - private LiveIntentAnalyticsReporter target; |
63 | | - |
64 | | - private LiveIntentAnalyticsProperties properties; |
65 | | - |
66 | | - private static final TypeReference<List<PbsjBid>> PBJS_COLLECTION_TYPE = new TypeReference<>() { |
67 | | - }; |
68 | | - |
69 | | - @BeforeEach |
70 | | - public void setUp() { |
71 | | - |
72 | | - properties = LiveIntentAnalyticsProperties.builder() |
73 | | - .analyticsEndpoint("https://localhost:8080") |
74 | | - .partnerId("pbsj") |
75 | | - .timeoutMs(1000L) |
76 | | - .build(); |
77 | | - |
78 | | - target = new LiveIntentAnalyticsReporter( |
79 | | - properties, |
80 | | - httpClient, |
81 | | - jacksonMapper); |
82 | | - } |
83 | | - |
84 | | - @Test |
85 | | - public void shouldProcessNotificationEvent() { |
86 | | - // given |
87 | | - final HttpClientResponse mockResponse = mock(HttpClientResponse.class); |
88 | | - when(httpClient.get(anyString(), anyLong())).thenReturn(Future.succeededFuture(mockResponse)); |
89 | | - |
90 | | - // when |
91 | | - target.processEvent(NotificationEvent.builder().bidId("123").bidder("foo").build()); |
92 | | - |
93 | | - // then |
94 | | - // Verify that the HTTP client was called with the expected parameters |
95 | | - verify(httpClient).get(eq(properties.getAnalyticsEndpoint() + "/analytic-events/pbsj-winning-bid" |
96 | | - + "?b=foo&bidId=123"), eq(properties.getTimeoutMs())); |
97 | | - } |
98 | | - |
99 | | - @Test |
100 | | - public void shouldSendAllBidsToLiveIntent() { |
101 | | - // given |
102 | | - final HttpClientResponse mockResponse = mock(HttpClientResponse.class); |
103 | | - when(httpClient.post(anyString(), anyString(), anyLong())) |
104 | | - .thenReturn(Future.succeededFuture(mockResponse)); |
105 | | - |
106 | | - // when |
107 | | - target.processEvent(buildEvent(true)); |
108 | | - |
109 | | - // then |
110 | | - verify(httpClient).post( |
111 | | - eq(properties.getAnalyticsEndpoint() + "/analytic-events/pbsj-bids"), |
112 | | - jsonCaptor.capture(), |
113 | | - eq(properties.getTimeoutMs())); |
114 | | - |
115 | | - final String capturedJson = jsonCaptor.getValue(); |
116 | | - final List<PbsjBid> pbsjBids = jacksonMapper.decodeValue(capturedJson, PBJS_COLLECTION_TYPE); |
117 | | - assertThat(pbsjBids).isEqualTo(List.of( |
118 | | - PbsjBid.builder() |
119 | | - .bidId("bid-id") |
120 | | - .price(BigDecimal.ONE) |
121 | | - .adUnitId("ad-unit-id") |
122 | | - .enriched(true) |
123 | | - .currency("USD") |
124 | | - .treatmentRate(0.5f) |
125 | | - .timestamp(0L) |
126 | | - .partnerId("pbsj") |
127 | | - .build())); |
128 | | - } |
129 | | - |
130 | | - @Test |
131 | | - public void shouldSendAllBidsToLiveIntentNotEnriched() { |
132 | | - // given |
133 | | - final HttpClientResponse mockResponse = mock(HttpClientResponse.class); |
134 | | - when(httpClient.post(anyString(), anyString(), anyLong())) |
135 | | - .thenReturn(Future.succeededFuture(mockResponse)); |
136 | | - |
137 | | - // when |
138 | | - target.processEvent(buildEvent(false)); |
139 | | - |
140 | | - // then |
141 | | - verify(httpClient).post( |
142 | | - eq(properties.getAnalyticsEndpoint() + "/analytic-events/pbsj-bids"), |
143 | | - jsonCaptor.capture(), |
144 | | - eq(properties.getTimeoutMs())); |
145 | | - |
146 | | - final String capturedJson = jsonCaptor.getValue(); |
147 | | - final List<PbsjBid> pbsjBids = jacksonMapper.decodeValue(capturedJson, PBJS_COLLECTION_TYPE); |
148 | | - assertThat(pbsjBids).isEqualTo(List.of( |
149 | | - PbsjBid.builder() |
150 | | - .bidId("bid-id") |
151 | | - .price(BigDecimal.ONE) |
152 | | - .adUnitId("ad-unit-id") |
153 | | - .enriched(false) |
154 | | - .currency("USD") |
155 | | - .treatmentRate(0.5f) |
156 | | - .timestamp(0L) |
157 | | - .partnerId("pbsj") |
158 | | - .build())); |
159 | | - } |
160 | | - |
161 | | - @Test |
162 | | - public void shouldSendAllBidsToLiveIntentNoTreatmentRate() { |
163 | | - // given |
164 | | - final HttpClientResponse mockResponse = mock(HttpClientResponse.class); |
165 | | - when(httpClient.post(anyString(), anyString(), anyLong())) |
166 | | - .thenReturn(Future.succeededFuture(mockResponse)); |
167 | | - |
168 | | - // when |
169 | | - target.processEvent(buildEvent(false, false)); |
170 | | - |
171 | | - // then |
172 | | - verify(httpClient).post( |
173 | | - eq(properties.getAnalyticsEndpoint() + "/analytic-events/pbsj-bids"), |
174 | | - jsonCaptor.capture(), |
175 | | - eq(properties.getTimeoutMs())); |
176 | | - |
177 | | - final String capturedJson = jsonCaptor.getValue(); |
178 | | - final List<PbsjBid> pbsjBids = jacksonMapper.decodeValue(capturedJson, PBJS_COLLECTION_TYPE); |
179 | | - assertThat(pbsjBids).isEqualTo(List.of( |
180 | | - PbsjBid.builder() |
181 | | - .bidId("bid-id") |
182 | | - .price(BigDecimal.ONE) |
183 | | - .adUnitId("ad-unit-id") |
184 | | - .enriched(false) |
185 | | - .currency("USD") |
186 | | - .timestamp(0L) |
187 | | - .treatmentRate(null) |
188 | | - .partnerId("pbsj") |
189 | | - .build())); |
190 | | - } |
191 | | - |
192 | | - private AuctionEvent buildEvent(Boolean isEnriched) { |
193 | | - return buildEvent(isEnriched, true); |
194 | | - } |
195 | | - |
196 | | - private AuctionEvent buildEvent(Boolean isEnriched, Boolean withTags) { |
197 | | - final HookId hookId = HookId.of( |
198 | | - "liveintent-omni-channel-identity-enrichment-hook", |
199 | | - "liveintent-omni-channel-identity-enrichment-hook"); |
200 | | - |
201 | | - final ObjectNode treatmentRateNode = ObjectMapperProvider.mapper().createObjectNode() |
202 | | - .put("treatmentRate", 0.5f); |
203 | | - |
204 | | - final AppliedToImpl appliedTo = AppliedToImpl.builder().bidIds(List.of("bid-id")) |
205 | | - .impIds(List.of("imp-id")) |
206 | | - .bidders(List.of("pbsj")) |
207 | | - .request(true) |
208 | | - .response(true) |
209 | | - .build(); |
210 | | - |
211 | | - final ResultImpl result = ResultImpl.of("treatmentRate", treatmentRateNode, appliedTo); |
212 | | - |
213 | | - final ActivityImpl enrichmentRate = ActivityImpl.of("liveintent-treatment-rate", "0.5", |
214 | | - List.of(result)); |
215 | | - |
216 | | - final List<ActivityImpl> enriched = isEnriched |
217 | | - ? List.of(ActivityImpl.of("liveintent-enriched", "success", List.of())) |
218 | | - : List.of(); |
219 | | - |
220 | | - final HookExecutionOutcome hookExecutionOutcome = HookExecutionOutcome.builder() |
221 | | - .hookId(hookId) |
222 | | - .executionTime(100L) |
223 | | - .status(ExecutionStatus.success) |
224 | | - .analyticsTags(TagsImpl.of( |
225 | | - withTags |
226 | | - ? ListUtil.union(List.of(enrichmentRate), enriched) |
227 | | - : List.of())) |
228 | | - .action(null) |
229 | | - .build(); |
230 | | - |
231 | | - final StageExecutionOutcome stageExecutionOutcome = StageExecutionOutcome.of( |
232 | | - "auction-request", |
233 | | - List.of(GroupExecutionOutcome.of(List.of(hookExecutionOutcome)))); |
234 | | - |
235 | | - final EnumMap<Stage, List<StageExecutionOutcome>> stageOutcomes = new EnumMap<>(Stage.class); |
236 | | - stageOutcomes.put(Stage.processed_auction_request, List.of(stageExecutionOutcome)); |
237 | | - return AuctionEvent.builder() |
238 | | - .auctionContext( |
239 | | - AuctionContext.builder() |
240 | | - .bidRequest(BidRequest.builder() |
241 | | - .id("request-id") |
242 | | - .imp(List.of( |
243 | | - Imp.builder() |
244 | | - .id("imp-id") |
245 | | - .tagid("ad-unit-id") |
246 | | - .build())) |
247 | | - .build()) |
248 | | - .bidResponse(BidResponse.builder() |
249 | | - .bidid("bid-id") |
250 | | - .cur("USD") |
251 | | - .seatbid(List.of( |
252 | | - SeatBid.builder() |
253 | | - .bid(List.of( |
254 | | - Bid.builder() |
255 | | - .id("bid-id") |
256 | | - .impid("imp-id") |
257 | | - .price(BigDecimal.ONE) |
258 | | - .build())) |
259 | | - .build())) |
260 | | - .build()) |
261 | | - .hookExecutionContext(HookExecutionContext.of( |
262 | | - Endpoint.openrtb2_auction, |
263 | | - stageOutcomes)) |
264 | | - .build()) |
265 | | - .build(); |
266 | | - } |
| 56 | + @Mock |
| 57 | + private HttpClient httpClient; |
| 58 | + |
| 59 | + @Captor |
| 60 | + private ArgumentCaptor<String> jsonCaptor; |
| 61 | + |
| 62 | + private LiveIntentAnalyticsReporter target; |
| 63 | + |
| 64 | + private LiveIntentAnalyticsProperties properties; |
| 65 | + |
| 66 | + private static final TypeReference<List<PbsjBid>> PBJS_COLLECTION_TYPE = new TypeReference<>() { |
| 67 | + }; |
| 68 | + |
| 69 | + @BeforeEach |
| 70 | + public void setUp() { |
| 71 | + |
| 72 | + properties = LiveIntentAnalyticsProperties.builder().analyticsEndpoint("https://localhost:8080") |
| 73 | + .partnerId("pbsj").timeoutMs(1000L).build(); |
| 74 | + |
| 75 | + target = new LiveIntentAnalyticsReporter(properties, httpClient, jacksonMapper); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void shouldProcessNotificationEvent() { |
| 80 | + // given |
| 81 | + final HttpClientResponse mockResponse = mock(HttpClientResponse.class); |
| 82 | + when(httpClient.get(anyString(), anyLong())).thenReturn(Future.succeededFuture(mockResponse)); |
| 83 | + |
| 84 | + // when |
| 85 | + target.processEvent(NotificationEvent.builder().bidId("123").bidder("foo").build()); |
| 86 | + |
| 87 | + // then |
| 88 | + // Verify that the HTTP client was called with the expected parameters |
| 89 | + verify(httpClient).get( |
| 90 | + eq(properties.getAnalyticsEndpoint() + "/analytic-events/pbsj-winning-bid" + "?b=foo&bidId=123"), |
| 91 | + eq(properties.getTimeoutMs())); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void shouldSendAllBidsToLiveIntent() { |
| 96 | + // given |
| 97 | + final HttpClientResponse mockResponse = mock(HttpClientResponse.class); |
| 98 | + when(httpClient.post(anyString(), anyString(), anyLong())).thenReturn(Future.succeededFuture(mockResponse)); |
| 99 | + |
| 100 | + // when |
| 101 | + target.processEvent(buildEvent(true)); |
| 102 | + |
| 103 | + // then |
| 104 | + verify(httpClient).post(eq(properties.getAnalyticsEndpoint() + "/analytic-events/pbsj-bids"), |
| 105 | + jsonCaptor.capture(), eq(properties.getTimeoutMs())); |
| 106 | + |
| 107 | + final String capturedJson = jsonCaptor.getValue(); |
| 108 | + final List<PbsjBid> pbsjBids = jacksonMapper.decodeValue(capturedJson, PBJS_COLLECTION_TYPE); |
| 109 | + assertThat(pbsjBids) |
| 110 | + .isEqualTo(List.of(PbsjBid.builder().bidId("bid-id").price(BigDecimal.ONE).adUnitId("ad-unit-id") |
| 111 | + .enriched(true).currency("USD").treatmentRate(0.5f).timestamp(0L).partnerId("pbsj").build())); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void shouldSendAllBidsToLiveIntentNotEnriched() { |
| 116 | + // given |
| 117 | + final HttpClientResponse mockResponse = mock(HttpClientResponse.class); |
| 118 | + when(httpClient.post(anyString(), anyString(), anyLong())).thenReturn(Future.succeededFuture(mockResponse)); |
| 119 | + |
| 120 | + // when |
| 121 | + target.processEvent(buildEvent(false)); |
| 122 | + |
| 123 | + // then |
| 124 | + verify(httpClient).post(eq(properties.getAnalyticsEndpoint() + "/analytic-events/pbsj-bids"), |
| 125 | + jsonCaptor.capture(), eq(properties.getTimeoutMs())); |
| 126 | + |
| 127 | + final String capturedJson = jsonCaptor.getValue(); |
| 128 | + final List<PbsjBid> pbsjBids = jacksonMapper.decodeValue(capturedJson, PBJS_COLLECTION_TYPE); |
| 129 | + assertThat(pbsjBids) |
| 130 | + .isEqualTo(List.of(PbsjBid.builder().bidId("bid-id").price(BigDecimal.ONE).adUnitId("ad-unit-id") |
| 131 | + .enriched(false).currency("USD").treatmentRate(0.5f).timestamp(0L).partnerId("pbsj").build())); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void shouldSendAllBidsToLiveIntentNoTreatmentRate() { |
| 136 | + // given |
| 137 | + final HttpClientResponse mockResponse = mock(HttpClientResponse.class); |
| 138 | + when(httpClient.post(anyString(), anyString(), anyLong())).thenReturn(Future.succeededFuture(mockResponse)); |
| 139 | + |
| 140 | + // when |
| 141 | + target.processEvent(buildEvent(false, false)); |
| 142 | + |
| 143 | + // then |
| 144 | + verify(httpClient).post(eq(properties.getAnalyticsEndpoint() + "/analytic-events/pbsj-bids"), |
| 145 | + jsonCaptor.capture(), eq(properties.getTimeoutMs())); |
| 146 | + |
| 147 | + final String capturedJson = jsonCaptor.getValue(); |
| 148 | + final List<PbsjBid> pbsjBids = jacksonMapper.decodeValue(capturedJson, PBJS_COLLECTION_TYPE); |
| 149 | + assertThat(pbsjBids) |
| 150 | + .isEqualTo(List.of(PbsjBid.builder().bidId("bid-id").price(BigDecimal.ONE).adUnitId("ad-unit-id") |
| 151 | + .enriched(false).currency("USD").timestamp(0L).treatmentRate(null).partnerId("pbsj").build())); |
| 152 | + } |
| 153 | + |
| 154 | + private AuctionEvent buildEvent(Boolean isEnriched) { |
| 155 | + return buildEvent(isEnriched, true); |
| 156 | + } |
| 157 | + |
| 158 | + private AuctionEvent buildEvent(Boolean isEnriched, Boolean withTags) { |
| 159 | + final HookId hookId = HookId.of("liveintent-omni-channel-identity-enrichment-hook", |
| 160 | + "liveintent-omni-channel-identity-enrichment-hook"); |
| 161 | + |
| 162 | + final ObjectNode treatmentRateNode = ObjectMapperProvider.mapper().createObjectNode().put("treatmentRate", |
| 163 | + 0.5f); |
| 164 | + |
| 165 | + final AppliedToImpl appliedTo = AppliedToImpl.builder().bidIds(List.of("bid-id")).impIds(List.of("imp-id")) |
| 166 | + .bidders(List.of("pbsj")).request(true).response(true).build(); |
| 167 | + |
| 168 | + final ResultImpl result = ResultImpl.of("treatmentRate", treatmentRateNode, appliedTo); |
| 169 | + |
| 170 | + final ActivityImpl enrichmentRate = ActivityImpl.of("liveintent-treatment-rate", "0.5", List.of(result)); |
| 171 | + |
| 172 | + final List<ActivityImpl> enriched = isEnriched |
| 173 | + ? List.of(ActivityImpl.of("liveintent-enriched", "success", List.of())) |
| 174 | + : List.of(); |
| 175 | + |
| 176 | + final HookExecutionOutcome hookExecutionOutcome = HookExecutionOutcome.builder().hookId(hookId) |
| 177 | + .executionTime(100L).status(ExecutionStatus.success) |
| 178 | + .analyticsTags(TagsImpl.of(withTags ? ListUtil.union(List.of(enrichmentRate), enriched) : List.of())) |
| 179 | + .action(null).build(); |
| 180 | + |
| 181 | + final StageExecutionOutcome stageExecutionOutcome = StageExecutionOutcome.of("auction-request", |
| 182 | + List.of(GroupExecutionOutcome.of(List.of(hookExecutionOutcome)))); |
| 183 | + |
| 184 | + final EnumMap<Stage, List<StageExecutionOutcome>> stageOutcomes = new EnumMap<>(Stage.class); |
| 185 | + stageOutcomes.put(Stage.processed_auction_request, List.of(stageExecutionOutcome)); |
| 186 | + return AuctionEvent.builder().auctionContext(AuctionContext.builder() |
| 187 | + .bidRequest(BidRequest.builder().id("request-id") |
| 188 | + .imp(List.of(Imp.builder().id("imp-id").tagid("ad-unit-id").build())).build()) |
| 189 | + .bidResponse(BidResponse.builder().bidid("bid-id").cur("USD") |
| 190 | + .seatbid(List.of(SeatBid.builder() |
| 191 | + .bid(List.of(Bid.builder().id("bid-id").impid("imp-id").price(BigDecimal.ONE).build())) |
| 192 | + .build())) |
| 193 | + .build()) |
| 194 | + .hookExecutionContext(HookExecutionContext.of(Endpoint.openrtb2_auction, stageOutcomes)).build()) |
| 195 | + .build(); |
| 196 | + } |
267 | 197 | } |
0 commit comments