From a11b443a7691d34dd11fa575a903b0c83e52e55d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Fri, 30 Aug 2019 09:54:26 -0300 Subject: [PATCH] The extension format accessor tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- .../java/io/cloudevents/v02/AccessorTest.java | 134 ++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 api/src/test/java/io/cloudevents/v02/AccessorTest.java diff --git a/api/src/test/java/io/cloudevents/v02/AccessorTest.java b/api/src/test/java/io/cloudevents/v02/AccessorTest.java new file mode 100644 index 00000000..6b994bac --- /dev/null +++ b/api/src/test/java/io/cloudevents/v02/AccessorTest.java @@ -0,0 +1,134 @@ +/** + * 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.v02; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.net.URI; +import java.util.Collection; + +import org.junit.Test; + +import io.cloudevents.extensions.DistributedTracingExtension; +import io.cloudevents.extensions.ExtensionFormat; +import io.cloudevents.extensions.InMemoryFormat; + +/** + * + * @author fabiojose + * + */ +public class AccessorTest { + + @Test + public void should_empty_collection_when_no_extensions() { + // setup + CloudEventImpl ce = + CloudEventBuilder.builder() + .withId("x10") + .withSource(URI.create("/source")) + .withType("event-type") + .withSchemaurl(URI.create("/schema")) + .withContenttype("text/plain") + .withData("my-data") + .build(); + + // act + Collection actual = Accessor.extensionsOf(ce); + + // assert + assertTrue(actual.isEmpty()); + } + + @Test + public void should_return_the_tracing_extension() { + // setup + final DistributedTracingExtension dt = new DistributedTracingExtension(); + dt.setTraceparent("0"); + dt.setTracestate("congo=4"); + + final ExtensionFormat expected = new DistributedTracingExtension.Format(dt); + + CloudEventImpl ce = + CloudEventBuilder.builder() + .withId("x10") + .withSource(URI.create("/source")) + .withType("event-type") + .withSchemaurl(URI.create("/schema")) + .withContenttype("text/plain") + .withData("my-data") + .withExtension(expected) + .build(); + + // act + Collection extensions = + Accessor.extensionsOf(ce); + + // assert + assertFalse(extensions.isEmpty()); + ExtensionFormat actual = extensions.iterator().next(); + + assertEquals("0", actual.transport().get("traceparent")); + assertEquals("congo=4", actual.transport().get("tracestate")); + + assertEquals("0", + ((DistributedTracingExtension)actual.memory().getValue()).getTraceparent()); + + assertEquals("congo=4", + ((DistributedTracingExtension)actual.memory().getValue()).getTracestate()); + } + + @Test + public void should_return_the_custom_extension() { + // setup + String customExt = "comexampleextension1"; + String customVal = "my-ext-val"; + InMemoryFormat inMemory = + InMemoryFormat.of(customExt, customVal, String.class); + + ExtensionFormat expected = + ExtensionFormat.of(inMemory, customExt, customVal); + + CloudEventImpl ce = + CloudEventBuilder.builder() + .withId("x10") + .withSource(URI.create("/source")) + .withType("event-type") + .withSchemaurl(URI.create("/schema")) + .withContenttype("text/plain") + .withData("my-data") + .withExtension(expected) + .build(); + + // act + Collection extensions = + Accessor.extensionsOf(ce); + + // assert + assertFalse(extensions.isEmpty()); + ExtensionFormat actual = extensions.iterator().next(); + + assertEquals(customVal, actual.transport().get(customExt)); + + assertEquals(String.class, actual.memory().getValueType()); + + assertEquals(customExt, actual.memory().getKey()); + + assertEquals(customVal, actual.memory().getValue()); + } +}