Skip to content

Commit

Permalink
Merge pull request quarkusio#14225 from matejvasek/expose-ce
Browse files Browse the repository at this point in the history
Funqy expose CloudEvents
  • Loading branch information
patriot1burke authored Jan 19, 2021
2 parents cd70a1a + 6f6691e commit 7302112
Show file tree
Hide file tree
Showing 14 changed files with 1,307 additions and 175 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class FunqyTest {
@Test
public void testCloudEvent() {
RestAssured.given().contentType("application/json")
.header("ce-specversion", "1.0")
.header("ce-id", UUID.randomUUID().toString())
.header("ce-type", "myCloudEventGreeting")
.header("ce-source", "test")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.quarkus.funqy.test;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import io.quarkus.funqy.Funq;
import io.quarkus.funqy.knative.events.CloudEventMapping;

public class BinaryPayload {

@Funq
@CloudEventMapping(trigger = "test-type")
public byte[] doubleInt32BE(byte[] data) {
int i = ByteBuffer.wrap(data).order(ByteOrder.BIG_ENDIAN).getInt();
byte[] result = new byte[4];
ByteBuffer.wrap(result).order(ByteOrder.BIG_ENDIAN).putInt(i * 2);
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package io.quarkus.funqy.test;

import static org.hamcrest.Matchers.equalTo;

import java.util.Base64;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;

public class BinaryPayloadTest {
@RegisterExtension
static QuarkusUnitTest test = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(BinaryPayload.class));

private static final byte[] IN = new byte[] { 0x12, 0x34, 0x56, 0x78 };
private static final String IN_AS_BASE_64 = Base64.getEncoder().encodeToString(IN);
private static final byte[] OUT = new byte[] { 0x24, 0x68, (byte) 0xAC, (byte) 0xf0 };
private static final String OUT_AS_BASE_64 = Base64.getEncoder().encodeToString(OUT);

@Test
void testBinaryEncoding() {

byte[] ba = RestAssured.given().contentType("application/octet-stream")
.header("ce-specversion", "1.0")
.header("ce-id", "test-id")
.header("ce-type", "test-type")
.header("ce-source", "test-source")
.body(IN)
.post("/")
.then()
.statusCode(200)
.extract().response().getBody().asByteArray();

Assertions.assertArrayEquals(OUT, ba);
}

@Test
void testStructuredEncodingV1() {
RestAssured.given().contentType("application/cloudevents+json")
.body(STRUCTURED_ENCODED_EVENT_V1_1_BODY)
.post("/")
.then()
.statusCode(200)
.body("data_base64", equalTo(OUT_AS_BASE_64));
}

@Test
void testStructuredEncodingV03() {
RestAssured.given().contentType("application/cloudevents+json")
.body(STRUCTURED_ENCODED_EVENT_V03_BODY)
.post("/")
.then()
.statusCode(200)
.body("datacontentencoding", equalTo("base64"))
.body("data", equalTo(OUT_AS_BASE_64));
}

private static final String STRUCTURED_ENCODED_EVENT_V1_1_BODY = "{ \"id\" : \"test-id\", " +
" \"specversion\": \"1.1\", " +
" \"source\": \"test-source\", " +
" \"type\": \"test-type\", " +
" \"data_base64\": \"" + IN_AS_BASE_64 + "\" " +
"}";

private static final String STRUCTURED_ENCODED_EVENT_V03_BODY = "{ \"id\" : \"test-id\", " +
" \"specversion\": \"0.3\", " +
" \"source\": \"test-source\", " +
" \"type\": \"test-type\", " +
" \"datacontentencoding\": \"base64\", " +
" \"data\": \"" + IN_AS_BASE_64 + "\" " +
"}";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
package io.quarkus.funqy.test;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;
import io.restassured.specification.RequestSpecification;

public class ExposedCloudEventTest {
@RegisterExtension
static QuarkusUnitTest test = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(ExposedCloudEvents.class));

@Test
public void testVanillaHttp() {
// when a function handles CloudEvent explicitly, vanilla HTTP is considered to be a bad request.
RestAssured.given().contentType("application/json")
.body("{}")
.post("/doubleIt")
.then()
.statusCode(400);
}

@Test
public void testCloudEventAttributeDefaultsForStructuredEncoding() {
String event = "{ \"id\" : \"test-id\", " +
" \"specversion\": \"1.0\", " +
" \"source\": \"test-source\", " +
" \"type\": \"test-defaults\" " +
"}";
RestAssured.given().contentType("application/cloudevents+json")
.body(event)
.post("/")
.then()
.statusCode(200)
.body("specversion", equalTo("1.0"))
.body("id", notNullValue())
.body("type", equalTo("default-type"))
.body("source", equalTo("default-source"));
}

@Test
public void testCloudEventAttributeDefaultsForBinaryEncoding() {
RestAssured.given()
.header("ce-id", "test-id")
.header("ce-specversion", "1.0")
.header("ce-type", "test-defaults")
.header("ce-source", "test-source")
.post()
.then()
.statusCode(204)
.header("ce-specversion", equalTo("1.0"))
.header("ce-id", notNullValue())
.header("ce-type", equalTo("default-type"))
.header("ce-source", equalTo("default-source"));
}

@Test
public void testGenericInput() {
RestAssured.given().contentType("application/json")
.header("ce-id", "test-id")
.header("ce-specversion", "1.0")
.header("ce-type", "test-generics")
.header("ce-source", "test-source")
.body("[{\"i\" : 1}, {\"i\" : 2}, {\"i\" : 3}]")
.then()
.statusCode(200)
.body(equalTo("6"));
}

@ParameterizedTest
@MethodSource("provideBinaryEncodingTestArgs")
public void testBinaryEncoding(Map<String, String> headers, String specversion, String dataSchemaHdrName) {

RequestSpecification req = RestAssured.given().contentType("application/json");

for (Map.Entry<String, String> h : headers.entrySet()) {
req = req.header(h.getKey(), h.getValue());
}

req.body(BINARY_ENCODED_EVENT_BODY)
.post("/")
.then()
.statusCode(200)
.header("ce-specversion", equalTo(specversion))
.header("ce-id", equalTo("double-it-id"))
.header("ce-type", equalTo("double-it-type"))
.header("ce-source", equalTo("/OfDoubleIt"))
.header(dataSchemaHdrName, equalTo("dataschema-server"))
.header("ce-extserver", equalTo("ext-server-val"))
.body("i", equalTo(42))
.body("s", equalTo("abcabc"));
}

@ParameterizedTest
@MethodSource("provideStructuredEncodingTestArgs")
public void testStructuredEncoding(String event, String specversion, String dataSchemaFieldName) {
RestAssured.given().contentType("application/cloudevents+json")
.body(event)
.post("/")
.then()
.statusCode(200)
.body("specversion", equalTo(specversion))
.body("id", equalTo("double-it-id"))
.body("type", equalTo("double-it-type"))
.body("source", equalTo("/OfDoubleIt"))
.body(dataSchemaFieldName, equalTo("dataschema-server"))
.body("extserver", equalTo("ext-server-val"))
.body("data.i", equalTo(42))
.body("data.s", equalTo("abcabc"));
}

static {
Map<String, String> common = new HashMap<>();
common.put("ce-id", "test-id");
common.put("ce-type", "test-type");
common.put("ce-source", "/OfTest");
common.put("ce-subject", "test-subj");
common.put("ce-time", "2018-04-05T17:31:00Z");
common.put("ce-extclient", "ext-client-val");

Map<String, String> v1 = new HashMap<>(common);
v1.put("ce-specversion", "1.0");
v1.put("ce-dataschema", "test-dataschema-client");
BINARY_ENCODED_EVENT_V1_HEADERS = Collections.unmodifiableMap(v1);

Map<String, String> v1_1 = new HashMap<>(common);
v1_1.put("ce-specversion", "1.1");
v1_1.put("ce-dataschema", "test-dataschema-client");
BINARY_ENCODED_EVENT_V1_1_HEADERS = Collections.unmodifiableMap(v1_1);

Map<String, String> v03 = new HashMap<>(common);
v03.put("ce-specversion", "0.3");
v03.put("ce-schemaurl", "test-dataschema-client");
BINARY_ENCODED_EVENT_V03_HEADERS = Collections.unmodifiableMap(v03);
}

public static final Map<String, String> BINARY_ENCODED_EVENT_V1_HEADERS;
public static final Map<String, String> BINARY_ENCODED_EVENT_V1_1_HEADERS;
public static final Map<String, String> BINARY_ENCODED_EVENT_V03_HEADERS;

private static Stream<Arguments> provideBinaryEncodingTestArgs() {
return Stream.<Arguments> builder()
.add(Arguments.arguments(BINARY_ENCODED_EVENT_V1_HEADERS, "1.0", "ce-dataschema"))
.add(Arguments.arguments(BINARY_ENCODED_EVENT_V1_1_HEADERS, "1.1", "ce-dataschema"))
.add(Arguments.arguments(BINARY_ENCODED_EVENT_V03_HEADERS, "0.3", "ce-schemaurl"))
.build();
}

public static final String BINARY_ENCODED_EVENT_BODY = " { \"i\" : 21, \"s\" : \"abc\" } ";

static final String STRUCTURED_ENCODED_EVENT_V1_BODY = "{ \"id\" : \"test-id\", " +
" \"specversion\": \"1.0\", " +
" \"source\": \"/OfTest\", " +
" \"subject\": \"test-subj\", " +
" \"time\": \"2018-04-05T17:31:00Z\", " +
" \"type\": \"test-type\", " +
" \"extclient\": \"ext-client-val\", " +
" \"dataschema\": \"test-dataschema-client\", " +
" \"datacontenttype\": \"application/json\", " +
" \"data\": { \"i\" : 21, \"s\" : \"abc\" } " +
"}";

static final String STRUCTURED_ENCODED_EVENT_V1_1_BODY = "{ \"id\" : \"test-id\", " +
" \"specversion\": \"1.1\", " +
" \"source\": \"/OfTest\", " +
" \"subject\": \"test-subj\", " +
" \"time\": \"2018-04-05T17:31:00Z\", " +
" \"type\": \"test-type\", " +
" \"extclient\": \"ext-client-val\", " +
" \"dataschema\": \"test-dataschema-client\", " +
" \"datacontenttype\": \"application/json\", " +
" \"data\": { \"i\" : 21, \"s\" : \"abc\" } " +
"}";

static final String STRUCTURED_ENCODED_EVENT_V03_BODY = "{ \"id\" : \"test-id\", " +
" \"specversion\": \"0.3\", " +
" \"source\": \"/OfTest\", " +
" \"subject\": \"test-subj\", " +
" \"time\": \"2018-04-05T17:31:00Z\", " +
" \"type\": \"test-type\", " +
" \"extclient\": \"ext-client-val\", " +
" \"schemaurl\": \"test-dataschema-client\", " +
" \"datacontenttype\": \"application/json\", " +
" \"data\": { \"i\" : 21, \"s\" : \"abc\" } " +
"}";

private static Stream<Arguments> provideStructuredEncodingTestArgs() {
return Stream.<Arguments> builder()
.add(Arguments.arguments(STRUCTURED_ENCODED_EVENT_V1_BODY, "1.0", "dataschema"))
.add(Arguments.arguments(STRUCTURED_ENCODED_EVENT_V1_1_BODY, "1.1", "dataschema"))
.add(Arguments.arguments(STRUCTURED_ENCODED_EVENT_V03_BODY, "0.3", "schemaurl"))
.build();
}
}
Loading

0 comments on commit 7302112

Please sign in to comment.