-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PP-9001: add webhook message model for serialised webhook message body (
#44) * add webhook message model for serialised webhook message body Addresses TODO to serialise response in expected format add missing field 'resource_type' serialised from SQS message and added as top level field to message Serialises/Deserialises timestamp from SQS message as Instant * use naming strategy to set field names * get date and event type from WebhookMessageEntity * add fields to WebhookEntity and use to serialise webhook message body * restore comment (as resource still needs more transformation) rename record to make more specific to Webhook message body. * assorted changes based on PR comments
- Loading branch information
Showing
14 changed files
with
217 additions
and
19 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
src/main/java/uk/gov/pay/webhooks/message/WebhookMessageBody.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package uk.gov.pay.webhooks.message; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.PropertyNamingStrategies; | ||
import com.fasterxml.jackson.databind.annotation.JsonNaming; | ||
import com.fasterxml.jackson.databind.annotation.JsonSerialize; | ||
import uk.gov.pay.webhooks.eventtype.EventTypeName; | ||
import uk.gov.pay.webhooks.message.dao.entity.WebhookMessageEntity; | ||
import uk.gov.service.payments.commons.api.json.ApiResponseInstantSerializer; | ||
|
||
import java.time.Instant; | ||
|
||
|
||
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) | ||
public record WebhookMessageBody(String id, | ||
@JsonSerialize(using = ApiResponseInstantSerializer.class) Instant createdDate, | ||
String resourceId, | ||
Integer apiVersion, | ||
String resourceType, | ||
EventTypeName eventTypeName, | ||
JsonNode resource) { | ||
|
||
public static final int API_VERSION = 1; | ||
|
||
public static WebhookMessageBody from(WebhookMessageEntity webhookMessage) { | ||
return new WebhookMessageBody(webhookMessage.getExternalId(), | ||
webhookMessage.getEventDate().toInstant(), | ||
webhookMessage.getResourceExternalId(), | ||
API_VERSION, | ||
webhookMessage.getResourceType(), | ||
webhookMessage.getEventType().getName(), | ||
webhookMessage.getResource() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/main/java/uk/gov/pay/webhooks/util/InstantDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package uk.gov.pay.webhooks.util; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
|
||
import java.io.IOException; | ||
import java.time.Instant; | ||
|
||
public class InstantDeserializer extends JsonDeserializer<Instant> { | ||
|
||
@Override | ||
public Instant deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { | ||
return Instant.parse(p.getText()); | ||
} | ||
} | ||
|
27 changes: 27 additions & 0 deletions
27
src/main/java/uk/gov/pay/webhooks/util/MicrosecondPrecisionInstantSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package uk.gov.pay.webhooks.util; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.JsonSerializer; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import com.fasterxml.jackson.databind.ser.std.StdSerializer; | ||
|
||
import java.io.IOException; | ||
import java.time.Instant; | ||
import java.time.ZoneOffset; | ||
import java.time.format.DateTimeFormatter; | ||
import java.time.format.DateTimeFormatterBuilder; | ||
import java.util.Locale; | ||
|
||
public class MicrosecondPrecisionInstantSerializer extends JsonSerializer<Instant> { | ||
|
||
public static final DateTimeFormatter MICROSECOND_FORMATTER = | ||
new DateTimeFormatterBuilder() | ||
.appendInstant(6) | ||
.toFormatter(Locale.ENGLISH); | ||
|
||
@Override | ||
public void serialize(Instant value, JsonGenerator gen, SerializerProvider provider) throws IOException { | ||
gen.writeString(MICROSECOND_FORMATTER.format(value)); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/test/java/uk/gov/pay/webhooks/message/WebhookMessageBodyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package uk.gov.pay.webhooks.message; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.dropwizard.testing.junit5.DropwizardExtensionsSupport; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import uk.gov.pay.webhooks.eventtype.EventTypeName; | ||
import uk.gov.pay.webhooks.eventtype.dao.EventTypeEntity; | ||
import uk.gov.pay.webhooks.message.dao.entity.WebhookMessageEntity; | ||
|
||
import java.time.Instant; | ||
import java.time.InstantSource; | ||
import java.util.Date; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
@ExtendWith(DropwizardExtensionsSupport.class) | ||
class WebhookMessageBodyTest { | ||
|
||
private ObjectMapper objectMapper; | ||
private InstantSource instantSource; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
instantSource = InstantSource.fixed(Instant.parse("2019-10-01T08:25:24.00Z")); | ||
objectMapper = new ObjectMapper(); | ||
} | ||
|
||
@Test | ||
void serialisesWebhookMessageBody() throws JsonProcessingException { | ||
String resource = """ | ||
{ | ||
"json": "and", | ||
"the": "argonauts" | ||
} | ||
"""; | ||
var webhookMessageEntity = new WebhookMessageEntity(); | ||
webhookMessageEntity.setExternalId("externalId"); | ||
webhookMessageEntity.setEventDate(Date.from(instantSource.instant())); | ||
EventTypeEntity eventTypeEntity = new EventTypeEntity(EventTypeName.CARD_PAYMENT_CAPTURED); | ||
webhookMessageEntity.setEventType(eventTypeEntity); | ||
webhookMessageEntity.setResourceType("payment"); | ||
webhookMessageEntity.setResourceExternalId("resource-external-id"); | ||
webhookMessageEntity.setResource(objectMapper.readTree(resource)); | ||
|
||
var body = WebhookMessageBody.from(webhookMessageEntity);; | ||
var expectedJson = """ | ||
{ | ||
"id": "externalId", | ||
"created_date": "2019-10-01T08:25:24.000Z", | ||
"resource_id": "resource-external-id", | ||
"api_version": 1, | ||
"resource_type": "payment", | ||
"event_type_name": "card_payment_captured", | ||
"resource": { | ||
"json": "and", | ||
"the": "argonauts" | ||
} | ||
} | ||
"""; | ||
|
||
assertThat(objectMapper.readTree(expectedJson), equalTo(objectMapper.valueToTree(body))); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters