Added unit tests
Signed-off-by: Ruben Romero Montes <rromerom@redhat.com>
This commit is contained in:
parent
1cff239ab4
commit
3df3b29e09
|
@ -147,13 +147,29 @@ public final class CloudEventBuilder<T> implements
|
||||||
* @param <T> the type of 'data'
|
* @param <T> the type of 'data'
|
||||||
* @param data the value of data
|
* @param data the value of data
|
||||||
* @param attributes the context attributes
|
* @param attributes the context attributes
|
||||||
|
* @param extensions the extension attributes
|
||||||
* @return An new {@link CloudEventImpl} immutable instance
|
* @return An new {@link CloudEventImpl} immutable instance
|
||||||
* @throws IllegalStateException When there are specification constraints
|
* @throws IllegalStateException When there are specification constraints
|
||||||
* violations
|
* violations
|
||||||
*/
|
*/
|
||||||
public static <T> CloudEventImpl<T> of(T data, AttributesImpl attributes,
|
public static <T> CloudEventImpl<T> of(T data, AttributesImpl attributes,
|
||||||
Collection<ExtensionFormat> extensions) {
|
Collection<ExtensionFormat> extensions) {
|
||||||
CloudEventBuilder<T> builder = CloudEventBuilder.<T>builder()
|
return of(data, attributes, extensions, null);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Build an event from data and attributes
|
||||||
|
* @param <T> the type of 'data'
|
||||||
|
* @param data the value of data
|
||||||
|
* @param attributes the context attributes
|
||||||
|
* @param extensions the extension attributes
|
||||||
|
* @param validator existing instance of a validator
|
||||||
|
* @return An new {@link CloudEventImpl} immutable instance
|
||||||
|
* @throws IllegalStateException When there are specification constraints
|
||||||
|
* violations
|
||||||
|
*/
|
||||||
|
public static <T> CloudEventImpl<T> of(T data, AttributesImpl attributes,
|
||||||
|
Collection<ExtensionFormat> extensions, Validator validator) {
|
||||||
|
CloudEventBuilder<T> builder = CloudEventBuilder.<T>builder(validator)
|
||||||
.withId(attributes.getId())
|
.withId(attributes.getId())
|
||||||
.withSource(attributes.getSource())
|
.withSource(attributes.getSource())
|
||||||
.withType(attributes.getType());
|
.withType(attributes.getType());
|
||||||
|
@ -191,7 +207,7 @@ public final class CloudEventBuilder<T> implements
|
||||||
@Override
|
@Override
|
||||||
public CloudEvent<AttributesImpl, T> build(T data, AttributesImpl attributes,
|
public CloudEvent<AttributesImpl, T> build(T data, AttributesImpl attributes,
|
||||||
Collection<ExtensionFormat> extensions){
|
Collection<ExtensionFormat> extensions){
|
||||||
return CloudEventBuilder.<T>of(data, attributes, extensions);
|
return CloudEventBuilder.<T>of(data, attributes, extensions, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -15,11 +15,23 @@
|
||||||
*/
|
*/
|
||||||
package io.cloudevents.v03;
|
package io.cloudevents.v03;
|
||||||
|
|
||||||
|
import static io.cloudevents.v03.CloudEventBuilder.builder;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import javax.validation.ConstraintViolation;
|
||||||
|
import javax.validation.Validation;
|
||||||
|
import javax.validation.Validator;
|
||||||
|
import javax.validation.ValidatorFactory;
|
||||||
|
import javax.validation.executable.ExecutableValidator;
|
||||||
|
import javax.validation.metadata.BeanDescriptor;
|
||||||
|
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
@ -31,88 +43,88 @@ import io.cloudevents.extensions.ExtensionFormat;
|
||||||
import io.cloudevents.extensions.InMemoryFormat;
|
import io.cloudevents.extensions.InMemoryFormat;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author fabiojose
|
* @author fabiojose
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class CloudEventBuilderTest {
|
public class CloudEventBuilderTest {
|
||||||
|
|
||||||
@Rule
|
@Rule
|
||||||
public ExpectedException expectedEx = ExpectedException.none();
|
public ExpectedException expectedEx = ExpectedException.none();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void error_when_null_id() {
|
public void error_when_null_id() {
|
||||||
// setup
|
// setup
|
||||||
expectedEx.expect(IllegalStateException.class);
|
expectedEx.expect(IllegalStateException.class);
|
||||||
expectedEx.expectMessage("invalid payload: 'id' must not be blank");
|
expectedEx.expectMessage("invalid payload: 'id' must not be blank");
|
||||||
|
|
||||||
// act
|
// act
|
||||||
CloudEventBuilder.builder()
|
builder()
|
||||||
.withSource(URI.create("/test"))
|
.withSource(URI.create("/test"))
|
||||||
.withType("type")
|
.withType("type")
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void error_when_empty_id() {
|
public void error_when_empty_id() {
|
||||||
// setup
|
// setup
|
||||||
expectedEx.expect(IllegalStateException.class);
|
expectedEx.expect(IllegalStateException.class);
|
||||||
expectedEx.expectMessage("invalid payload: 'id' must not be blank");
|
expectedEx.expectMessage("invalid payload: 'id' must not be blank");
|
||||||
|
|
||||||
// act
|
// act
|
||||||
CloudEventBuilder.builder()
|
builder()
|
||||||
.withId("")
|
.withId("")
|
||||||
.withSource(URI.create("/test"))
|
.withSource(URI.create("/test"))
|
||||||
.withType("type")
|
.withType("type")
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void error_when_null_type() {
|
public void error_when_null_type() {
|
||||||
// setup
|
// setup
|
||||||
expectedEx.expect(IllegalStateException.class);
|
expectedEx.expect(IllegalStateException.class);
|
||||||
expectedEx.expectMessage("invalid payload: 'type' must not be blank");
|
expectedEx.expectMessage("invalid payload: 'type' must not be blank");
|
||||||
|
|
||||||
// act
|
// act
|
||||||
CloudEventBuilder.builder()
|
builder()
|
||||||
.withId("id")
|
.withId("id")
|
||||||
.withSource(URI.create("/test"))
|
.withSource(URI.create("/test"))
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void error_when_empty_type() {
|
public void error_when_empty_type() {
|
||||||
// setup
|
// setup
|
||||||
expectedEx.expect(IllegalStateException.class);
|
expectedEx.expect(IllegalStateException.class);
|
||||||
expectedEx.expectMessage("invalid payload: 'type' must not be blank");
|
expectedEx.expectMessage("invalid payload: 'type' must not be blank");
|
||||||
|
|
||||||
// act
|
// act
|
||||||
CloudEventBuilder.builder()
|
builder()
|
||||||
.withId("id")
|
.withId("id")
|
||||||
.withSource(URI.create("/test"))
|
.withSource(URI.create("/test"))
|
||||||
.withType("")
|
.withType("")
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void error_when_null_source() {
|
public void error_when_null_source() {
|
||||||
// setup
|
// setup
|
||||||
expectedEx.expect(IllegalStateException.class);
|
expectedEx.expect(IllegalStateException.class);
|
||||||
expectedEx.expectMessage("invalid payload: 'source' must not be null");
|
expectedEx.expectMessage("invalid payload: 'source' must not be null");
|
||||||
|
|
||||||
// act
|
// act
|
||||||
CloudEventBuilder.builder()
|
builder()
|
||||||
.withId("id")
|
.withId("id")
|
||||||
.withType("type")
|
.withType("type")
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void error_when_empty_subject() {
|
public void error_when_empty_subject() {
|
||||||
// setup
|
// setup
|
||||||
expectedEx.expect(IllegalStateException.class);
|
expectedEx.expect(IllegalStateException.class);
|
||||||
expectedEx.expectMessage("invalid payload: 'subject' size must be between 1 and 2147483647");
|
expectedEx.expectMessage("invalid payload: 'subject' size must be between 1 and 2147483647");
|
||||||
|
|
||||||
// act
|
// act
|
||||||
CloudEventBuilder.<Object>builder()
|
CloudEventBuilder.<Object>builder()
|
||||||
.withId("id")
|
.withId("id")
|
||||||
|
@ -121,13 +133,13 @@ public class CloudEventBuilderTest {
|
||||||
.withSubject("")
|
.withSubject("")
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void error_when_invalid_encoding() {
|
public void error_when_invalid_encoding() {
|
||||||
// setup
|
// setup
|
||||||
expectedEx.expect(IllegalStateException.class);
|
expectedEx.expect(IllegalStateException.class);
|
||||||
expectedEx.expectMessage("invalid payload: 'datacontentencoding' must match \"base64\"");
|
expectedEx.expectMessage("invalid payload: 'datacontentencoding' must match \"base64\"");
|
||||||
|
|
||||||
// act
|
// act
|
||||||
CloudEventBuilder.<Object>builder()
|
CloudEventBuilder.<Object>builder()
|
||||||
.withId("id")
|
.withId("id")
|
||||||
|
@ -137,18 +149,18 @@ public class CloudEventBuilderTest {
|
||||||
.withDatacontentencoding("binary")
|
.withDatacontentencoding("binary")
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void should_have_subject() {
|
public void should_have_subject() {
|
||||||
// act
|
// act
|
||||||
CloudEvent<AttributesImpl, Object> ce =
|
CloudEvent<AttributesImpl, Object> ce =
|
||||||
CloudEventBuilder.<Object>builder()
|
CloudEventBuilder.<Object>builder()
|
||||||
.withId("id")
|
.withId("id")
|
||||||
.withSource(URI.create("/source"))
|
.withSource(URI.create("/source"))
|
||||||
.withType("type")
|
.withType("type")
|
||||||
.withSubject("subject")
|
.withSubject("subject")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
// assert
|
// assert
|
||||||
assertTrue(ce.getAttributes().getSubject().isPresent());
|
assertTrue(ce.getAttributes().getSubject().isPresent());
|
||||||
assertEquals("subject", ce.getAttributes().getSubject().get());
|
assertEquals("subject", ce.getAttributes().getSubject().get());
|
||||||
|
@ -160,48 +172,133 @@ public class CloudEventBuilderTest {
|
||||||
final DistributedTracingExtension dt = new DistributedTracingExtension();
|
final DistributedTracingExtension dt = new DistributedTracingExtension();
|
||||||
dt.setTraceparent("0");
|
dt.setTraceparent("0");
|
||||||
dt.setTracestate("congo=4");
|
dt.setTracestate("congo=4");
|
||||||
|
|
||||||
final ExtensionFormat tracing = new DistributedTracingExtension.Format(dt);
|
final ExtensionFormat tracing = new DistributedTracingExtension.Format(dt);
|
||||||
|
|
||||||
// act
|
// act
|
||||||
CloudEventImpl<Object> ce =
|
CloudEventImpl<Object> ce =
|
||||||
CloudEventBuilder.builder()
|
builder()
|
||||||
.withId("id")
|
.withId("id")
|
||||||
.withSource(URI.create("/source"))
|
.withSource(URI.create("/source"))
|
||||||
.withType("type")
|
.withType("type")
|
||||||
.withExtension(tracing)
|
.withExtension(tracing)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Object actual = ce.getExtensions()
|
Object actual = ce.getExtensions()
|
||||||
.get(DistributedTracingExtension.Format.IN_MEMORY_KEY);
|
.get(DistributedTracingExtension.Format.IN_MEMORY_KEY);
|
||||||
|
|
||||||
// assert
|
// assert
|
||||||
assertNotNull(actual);
|
assertNotNull(actual);
|
||||||
assertTrue(actual instanceof DistributedTracingExtension);
|
assertTrue(actual instanceof DistributedTracingExtension);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void should_have_custom_extension() {
|
public void should_have_custom_extension() {
|
||||||
String myExtKey = "comexampleextension1";
|
String myExtKey = "comexampleextension1";
|
||||||
String myExtVal = "value";
|
String myExtVal = "value";
|
||||||
|
|
||||||
ExtensionFormat custom = ExtensionFormat
|
ExtensionFormat custom = ExtensionFormat
|
||||||
.of(InMemoryFormat.of(myExtKey, myExtKey, String.class),
|
.of(InMemoryFormat.of(myExtKey, myExtKey, String.class),
|
||||||
myExtKey, myExtVal);
|
myExtKey, myExtVal);
|
||||||
|
|
||||||
// act
|
// act
|
||||||
CloudEventImpl<Object> ce =
|
CloudEventImpl<Object> ce =
|
||||||
CloudEventBuilder.builder()
|
builder()
|
||||||
.withId("id")
|
.withId("id")
|
||||||
.withSource(URI.create("/source"))
|
.withSource(URI.create("/source"))
|
||||||
.withType("type")
|
.withType("type")
|
||||||
.withExtension(custom)
|
.withExtension(custom)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Object actual = ce.getExtensions()
|
Object actual = ce.getExtensions()
|
||||||
.get(myExtKey);
|
.get(myExtKey);
|
||||||
|
|
||||||
assertNotNull(actual);
|
assertNotNull(actual);
|
||||||
assertTrue(actual instanceof String);
|
assertTrue(actual instanceof String);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void should_copy_event_using_custom_validator() {
|
||||||
|
Validator validator = new MockValidator();
|
||||||
|
String expected = "test";
|
||||||
|
CloudEvent<AttributesImpl, String> event = CloudEventBuilder.<String>of(
|
||||||
|
expected,
|
||||||
|
new AttributesImpl(null, null, null, null, null, null, null, null, null),
|
||||||
|
Collections.emptyList(),
|
||||||
|
validator
|
||||||
|
);
|
||||||
|
|
||||||
|
try {
|
||||||
|
CloudEventBuilder.<String>builder().build();
|
||||||
|
fail("Expected validation error");
|
||||||
|
} catch (IllegalStateException e) {
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void should_build_event_using_custom_validator() {
|
||||||
|
Validator validator = new MockValidator();
|
||||||
|
String expected = "test";
|
||||||
|
|
||||||
|
CloudEventImpl<String> event = CloudEventBuilder
|
||||||
|
.<String>builder(validator)
|
||||||
|
.withData(expected)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
assertNotNull(event);
|
||||||
|
assertEquals(expected, event.getData().get());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void should_build_event_from_event_using_custom_validator() {
|
||||||
|
Validator validator = new MockValidator();
|
||||||
|
String expected = "test";
|
||||||
|
CloudEvent<AttributesImpl, String> event = CloudEventBuilder.<String>of(
|
||||||
|
expected,
|
||||||
|
new AttributesImpl(null, null, null, null, null, null, null, null, null),
|
||||||
|
Collections.emptyList(),
|
||||||
|
validator
|
||||||
|
);
|
||||||
|
|
||||||
|
CloudEvent<AttributesImpl, String> result = CloudEventBuilder
|
||||||
|
.<String>builder(event, validator)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals(expected, result.getData().get());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class MockValidator implements Validator {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> Set<ConstraintViolation<T>> validate(T object, Class<?>... groups) {
|
||||||
|
return new HashSet<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> Set<ConstraintViolation<T>> validateProperty(T object, String propertyName, Class<?>... groups) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> Set<ConstraintViolation<T>> validateValue(Class<T> beanType, String propertyName, Object value, Class<?>... groups) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BeanDescriptor getConstraintsForClass(Class<?> clazz) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> T unwrap(Class<T> type) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ExecutableValidator forExecutables() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue