Tests for http marshallers
Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
parent
a971933128
commit
ac8561003c
|
@ -0,0 +1,130 @@
|
|||
/**
|
||||
* 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.http;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import io.cloudevents.extensions.DistributedTracingExtension;
|
||||
import io.cloudevents.extensions.ExtensionFormat;
|
||||
import io.cloudevents.format.Wire;
|
||||
import io.cloudevents.json.types.Much;
|
||||
import io.cloudevents.v02.CloudEventBuilder;
|
||||
import io.cloudevents.v02.CloudEventImpl;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author fabiojose
|
||||
*
|
||||
*/
|
||||
public class HTTPBinaryMarshallerTest {
|
||||
|
||||
@Test
|
||||
public void should_marshal_data_as_json() {
|
||||
// setup
|
||||
String expected = "{\"wow\":\"yes!\"}";
|
||||
Much ceData = new Much();
|
||||
ceData.setWow("yes!");
|
||||
|
||||
CloudEventImpl<Much> ce =
|
||||
CloudEventBuilder.<Much>builder()
|
||||
.withId("x10")
|
||||
.withSource(URI.create("/source"))
|
||||
.withType("event-type")
|
||||
.withContenttype("application/json")
|
||||
.withData(ceData)
|
||||
.build();
|
||||
|
||||
// act
|
||||
Wire<String, String, String> actual =
|
||||
Marshallers.<Much>
|
||||
binary()
|
||||
.withEvent(() -> ce)
|
||||
.marshal();
|
||||
|
||||
// assert
|
||||
assertTrue(actual.getPayload().isPresent());
|
||||
assertEquals(expected, actual.getPayload().get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_marshal_attributes_as_headers() {
|
||||
// setup
|
||||
Much ceData = new Much();
|
||||
ceData.setWow("yes!");
|
||||
|
||||
CloudEventImpl<Much> ce =
|
||||
CloudEventBuilder.<Much>builder()
|
||||
.withId("x10")
|
||||
.withSource(URI.create("/source"))
|
||||
.withType("event-type")
|
||||
.withContenttype("application/json")
|
||||
.withData(ceData)
|
||||
.build();
|
||||
|
||||
// act
|
||||
Wire<String, String, String> actual =
|
||||
Marshallers.<Much>
|
||||
binary()
|
||||
.withEvent(() -> ce)
|
||||
.marshal();
|
||||
|
||||
// assert
|
||||
assertFalse(actual.getHeaders().isEmpty());
|
||||
assertEquals(ce.getAttributes().getId(), actual.getHeaders().get("ce-id"));
|
||||
assertEquals(ce.getAttributes().getSource(), URI.create(actual.getHeaders().get("ce-source")));
|
||||
assertEquals(ce.getAttributes().getType(), actual.getHeaders().get("ce-type"));
|
||||
assertEquals(ce.getAttributes().getContenttype().get(), actual.getHeaders().get("Content-Type"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void should_marshal_the_tracing_extension_as_header() {
|
||||
// setup
|
||||
final DistributedTracingExtension dt = new DistributedTracingExtension();
|
||||
dt.setTraceparent("0");
|
||||
dt.setTracestate("congo=4");
|
||||
|
||||
final ExtensionFormat tracing = new DistributedTracingExtension.Format(dt);
|
||||
|
||||
CloudEventImpl<String> ce =
|
||||
CloudEventBuilder.<String>builder()
|
||||
.withId("id")
|
||||
.withSource(URI.create("/source"))
|
||||
.withType("type")
|
||||
.withExtension(tracing)
|
||||
.build();
|
||||
|
||||
// act
|
||||
Wire<String, String, String> actual =
|
||||
Marshallers.<String>
|
||||
binary()
|
||||
.withEvent(() -> ce)
|
||||
.marshal();
|
||||
|
||||
assertFalse(actual.getHeaders().isEmpty());
|
||||
assertNotNull(actual.getHeaders().get(DistributedTracingExtension
|
||||
.Format.TRACE_PARENT_KEY));
|
||||
assertNotNull(actual.getHeaders().get(DistributedTracingExtension
|
||||
.Format.TRACE_STATE_KEY));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,75 @@
|
|||
/**
|
||||
* 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.http;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import io.cloudevents.CloudEvent;
|
||||
import io.cloudevents.json.types.Much;
|
||||
import io.cloudevents.v02.AttributesImpl;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author fabiojose
|
||||
*
|
||||
*/
|
||||
public class HTTPBinaryUnmarshallerTest {
|
||||
|
||||
@Test
|
||||
public void should_unmarshal_headers_and_json_payload() {
|
||||
// setup
|
||||
Much expected = new Much();
|
||||
expected.setWow("yes!");
|
||||
|
||||
Map<String, Object> myHeaders = new HashMap<>();
|
||||
myHeaders.put("ce-id", "0x11");
|
||||
myHeaders.put("ce-source", "/source");
|
||||
myHeaders.put("ce-specversion", "0.2");
|
||||
myHeaders.put("ce-type", "br.my");
|
||||
myHeaders.put("ce-time", "2019-09-16T20:49:00Z");
|
||||
myHeaders.put("ce-schemaurl", "http://my.br");
|
||||
myHeaders.put("Content-Type", "application/json");
|
||||
|
||||
String payload = "{\"wow\":\"yes!\"}";
|
||||
|
||||
// act
|
||||
CloudEvent<AttributesImpl, Much> actual =
|
||||
Unmarshallers.binary(Much.class)
|
||||
.withHeaders(() -> myHeaders)
|
||||
.withPayload(() -> payload)
|
||||
.unmarshal();
|
||||
|
||||
// assert
|
||||
assertEquals("0x11", actual.getAttributes().getId());
|
||||
assertEquals(URI.create("/source"), actual.getAttributes().getSource());
|
||||
assertEquals("0.2", actual.getAttributes().getSpecversion());
|
||||
assertEquals("br.my", actual.getAttributes().getType());
|
||||
assertTrue(actual.getAttributes().getTime().isPresent());
|
||||
assertTrue(actual.getAttributes().getSchemaurl().isPresent());
|
||||
assertEquals(URI.create("http://my.br"), actual.getAttributes().getSchemaurl().get());
|
||||
assertTrue(actual.getAttributes().getContenttype().isPresent());
|
||||
assertEquals("application/json", actual.getAttributes().getContenttype().get());
|
||||
assertTrue(actual.getData().isPresent());
|
||||
assertEquals(expected, actual.getData().get());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue