|
| 1 | +/* |
| 2 | + * Copyright 2019-2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.cloudevents.spring.amqp; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | + |
| 20 | +import java.net.URI; |
| 21 | +import java.nio.charset.StandardCharsets; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import io.cloudevents.CloudEvent; |
| 25 | +import io.cloudevents.SpecVersion; |
| 26 | +import io.cloudevents.core.builder.CloudEventBuilder; |
| 27 | +import io.cloudevents.rw.CloudEventRWException; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | +import org.springframework.amqp.core.Message; |
| 30 | +import org.springframework.amqp.core.MessageBuilder; |
| 31 | +import org.springframework.amqp.core.MessageProperties; |
| 32 | + |
| 33 | +/** |
| 34 | + * @author Lars Michele |
| 35 | + * @see io.cloudevents.spring.messaging.CloudEventMessageConverterTests used as stencil for the implementation |
| 36 | + */ |
| 37 | +class CloudEventMessageConverterTests { |
| 38 | + |
| 39 | + private static final String JSON = "{\"specversion\":\"1.0\"," // |
| 40 | + + "\"id\":\"12345\"," // |
| 41 | + + "\"source\":\"https://spring.io/events\"," // |
| 42 | + + "\"type\":\"io.spring.event\"," // |
| 43 | + + "\"datacontenttype\":\"application/json\"," // |
| 44 | + + "\"data\":{\"value\":\"Dave\"}" // |
| 45 | + + "}"; |
| 46 | + |
| 47 | + private final CloudEventMessageConverter converter = new CloudEventMessageConverter(); |
| 48 | + |
| 49 | + @Test |
| 50 | + void noSpecVersion() { |
| 51 | + Message message = MessageBuilder.withBody(new byte[0]).build(); |
| 52 | + assertThatExceptionOfType(CloudEventRWException.class).isThrownBy(() -> { |
| 53 | + assertThat(converter.fromMessage(message)).isNull(); |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void notValidCloudEvent() { |
| 59 | + Message message = MessageBuilder.withBody(new byte[0]).setHeader("cloudEvents_specversion", "1.0").build(); |
| 60 | + assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> { |
| 61 | + assertThat(converter.fromMessage(message)).isNull(); |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void validCloudEvent() { |
| 67 | + Message message = MessageBuilder.withBody(new byte[0]).setHeader("cloudEvents_specversion", "1.0") |
| 68 | + .setHeader("cloudEvents_id", "12345").setHeader("cloudEvents_source", "https://spring.io/events") |
| 69 | + .setHeader("cloudEvents_type", "io.spring.event").build(); |
| 70 | + CloudEvent event = converter.fromMessage(message); |
| 71 | + assertThat(event).isNotNull(); |
| 72 | + assertThat(event.getSpecVersion()).isEqualTo(SpecVersion.V1); |
| 73 | + assertThat(event.getId()).isEqualTo("12345"); |
| 74 | + assertThat(event.getSource()).isEqualTo(URI.create("https://spring.io/events")); |
| 75 | + assertThat(event.getType()).isEqualTo("io.spring.event"); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void structuredCloudEvent() { |
| 80 | + byte[] payload = JSON.getBytes(StandardCharsets.UTF_8); |
| 81 | + Message message = MessageBuilder.withBody(payload) |
| 82 | + .setContentType("application/cloudevents+json").build(); |
| 83 | + CloudEvent event = converter.fromMessage(message); |
| 84 | + assertThat(event).isNotNull(); |
| 85 | + assertThat(event.getSpecVersion()).isEqualTo(SpecVersion.V1); |
| 86 | + assertThat(event.getId()).isEqualTo("12345"); |
| 87 | + assertThat(event.getSource()).isEqualTo(URI.create("https://spring.io/events")); |
| 88 | + assertThat(event.getType()).isEqualTo("io.spring.event"); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + void fromCloudEvent() { |
| 93 | + CloudEvent attributes = CloudEventBuilder.v1().withId("A234-1234-1234") |
| 94 | + .withSource(URI.create("https://spring.io/")).withType("org.springframework") |
| 95 | + .withData("hello".getBytes(StandardCharsets.UTF_8)).build(); |
| 96 | + Message message = converter.toMessage(attributes, new MessageProperties()); |
| 97 | + Map<String, ?> headers = message.getMessageProperties().getHeaders(); |
| 98 | + assertThat(headers.get("cloudEvents_id")).isEqualTo("A234-1234-1234"); |
| 99 | + assertThat(headers.get("cloudEvents_specversion")).isEqualTo("1.0"); |
| 100 | + assertThat(headers.get("cloudEvents_source")).isEqualTo("https://spring.io/"); |
| 101 | + assertThat(headers.get("cloudEvents_type")).isEqualTo("org.springframework"); |
| 102 | + assertThat("hello".getBytes(StandardCharsets.UTF_8)).isEqualTo(message.getBody()); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void fromNonCloudEvent() { |
| 107 | + assertThat(converter.toMessage(new byte[0], new MessageProperties())).isNull(); |
| 108 | + } |
| 109 | +} |
0 commit comments