-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPromotionEventIntegrationTest.java
90 lines (76 loc) · 3.17 KB
/
PromotionEventIntegrationTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//------------------------------------------------------------------
// Copyright 2021 mobile.de GmbH.
// Author/Developer: Philipp Bartsch
//
// This code is licensed under MIT license (see LICENSE for details)
//------------------------------------------------------------------
package org.example.move.webhookreceiver.rest;
import static org.assertj.core.api.Assertions.assertThat;
import static org.example.move.webhookreceiver.rest.ListingEventIntegrationTest.readResourceFile;
import static org.springframework.http.HttpMethod.POST;
import static org.springframework.http.HttpStatus.OK;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import ecg.move.sellermodel.promotion.PublicPromotionLargeModel;
import ecg.move.sellermodel.webhook.ListingUrl;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.example.move.IntegrationTestBase;
import org.example.move.webhookreceiver.rest.hmac.HmacChecker;
import org.example.move.webhookreceiver.rest.listingurl.ListingUrlEventEnvelope;
import org.example.move.webhookreceiver.rest.promotion.PromotionEventEnvelope;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
@Slf4j
class PromotionEventIntegrationTest extends IntegrationTestBase {
@Autowired
HmacChecker signatureChecker;
@Autowired
ObjectMapper objectMapper;
@Test
void promotion_can_be_posted() throws JsonProcessingException {
// given
PublicPromotionLargeModel promotion = aPromotionEvent().getPayload();
//when
ResponseEntity<?> response = postPromotionEvent(promotion);
//then
assertThat(response.getStatusCode()).isEqualTo(OK);
}
private ResponseEntity<Void> postPromotionEvent(PublicPromotionLargeModel promotion)
throws JsonProcessingException {
PromotionEventEnvelope payload = PromotionEventEnvelope.builder()
.payload(promotion)
.eventType(WebhookEventType.PROMOTIONS)
.timestamp(new Date())
.build();
String actualHmac = signatureChecker.createSignatureAsBase64String(objectMapper.writeValueAsString(payload));
return rest.exchange(
"/webhook/promotion",
POST,
new HttpEntity<>(
payload,
CollectionUtils.toMultiValueMap(
Map.of("signature", List.of(actualHmac))
)
),
Void.class);
}
private PromotionEventEnvelope aPromotionEvent() {
try {
TypeReference<PromotionEventEnvelope> event = new TypeReference<>() {
};
return objectMapper
.readValue(readResourceFile("webhook/real-promotion-event.json"), event);
} catch (URISyntaxException | IOException e) {
throw new RuntimeException(e);
}
}
}