From 0d2e1d75797b2850edfafa46bf171eadf304e55d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Mon, 26 Aug 2019 21:32:05 -0300 Subject: [PATCH] License and tests with new way to play with extensions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- .../v03/CloudEventBuilderTest.java | 69 +++++++++++++++++++ .../v03/CloudEventJacksonTest.java | 21 +++++- 2 files changed, 87 insertions(+), 3 deletions(-) diff --git a/api/src/test/java/io/cloudevents/v03/CloudEventBuilderTest.java b/api/src/test/java/io/cloudevents/v03/CloudEventBuilderTest.java index 448d8960..6b6cc683 100644 --- a/api/src/test/java/io/cloudevents/v03/CloudEventBuilderTest.java +++ b/api/src/test/java/io/cloudevents/v03/CloudEventBuilderTest.java @@ -1,6 +1,22 @@ +/** + * Copyright 2019 The CloudEvents Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package io.cloudevents.v03; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.net.URI; @@ -10,6 +26,9 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import io.cloudevents.CloudEvent; +import io.cloudevents.extensions.DistributedTracingExtension; +import io.cloudevents.extensions.ExtensionFormat; +import io.cloudevents.extensions.InMemoryFormat; /** * @@ -135,4 +154,54 @@ public class CloudEventBuilderTest { assertEquals("subject", ce.getAttributes().getSubject().get()); } + @Test + public void should_have_dte() { + // setup + final DistributedTracingExtension dt = new DistributedTracingExtension(); + dt.setTraceparent("0"); + dt.setTracestate("congo=4"); + + final ExtensionFormat tracing = new DistributedTracingExtension.Format(dt); + + // act + CloudEventImpl ce = + CloudEventBuilder.builder() + .withId("id") + .withSource(URI.create("/source")) + .withType("type") + .withExtension(tracing) + .build(); + + Object actual = ce.getExtensions() + .get(DistributedTracingExtension.Format.IN_MEMORY_KEY); + + // assert + assertNotNull(actual); + assertTrue(actual instanceof DistributedTracingExtension); + } + + @Test + public void should_have_custom_extension() { + String myExtKey = "comexampleextension1"; + String myExtVal = "value"; + + ExtensionFormat custom = ExtensionFormat + .of(InMemoryFormat.of(myExtKey, myExtKey, String.class), + myExtKey, myExtVal); + + // act + CloudEventImpl ce = + CloudEventBuilder.builder() + .withId("id") + .withSource(URI.create("/source")) + .withType("type") + .withExtension(custom) + .build(); + + Object actual = ce.getExtensions() + .get(myExtKey); + + assertNotNull(actual); + assertTrue(actual instanceof String); + } } diff --git a/api/src/test/java/io/cloudevents/v03/CloudEventJacksonTest.java b/api/src/test/java/io/cloudevents/v03/CloudEventJacksonTest.java index 353ce42d..2fbb7514 100644 --- a/api/src/test/java/io/cloudevents/v03/CloudEventJacksonTest.java +++ b/api/src/test/java/io/cloudevents/v03/CloudEventJacksonTest.java @@ -1,3 +1,18 @@ +/** + * Copyright 2019 The CloudEvents Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package io.cloudevents.v03; import static org.junit.Assert.assertEquals; @@ -15,8 +30,8 @@ import org.junit.rules.ExpectedException; import com.fasterxml.jackson.core.type.TypeReference; import io.cloudevents.CloudEvent; -import io.cloudevents.ExtensionFormat; import io.cloudevents.extensions.DistributedTracingExtension; +import io.cloudevents.extensions.ExtensionFormat; import io.cloudevents.json.Json; import io.cloudevents.json.types.Much; @@ -99,7 +114,7 @@ public class CloudEventJacksonTest { dt.setTraceparent("0"); dt.setTracestate("congo=4"); - final ExtensionFormat tracing = new DistributedTracingExtension.InMemory(dt); + final ExtensionFormat tracing = new DistributedTracingExtension.Format(dt); CloudEvent ce = CloudEventBuilder.builder() @@ -231,7 +246,7 @@ public class CloudEventJacksonTest { // assert assertNotNull(ce.getExtensions() - .get(DistributedTracingExtension.InMemory.IN_MEMORY_KEY)); + .get(DistributedTracingExtension.Format.IN_MEMORY_KEY)); } @Test