Skip to content

Commit

Permalink
Fixed comnpatibility with camel 2.21.x
Browse files Browse the repository at this point in the history
  • Loading branch information
valdar authored and lburgazzoli committed Apr 5, 2019
1 parent 45d4703 commit b77ea7e
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public final class Knative {
public static final String CONTENT_TYPE = "content.type";
public static final String MIME_STRUCTURED_CONTENT_MODE = "application/cloudevents+json";

public static final String SERVICE_META_HOST = "service.host";
public static final String SERVICE_META_ZONE = "service.zone";
public static final String SERVICE_META_PATH = "service.path";

private Knative() {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
*/
package org.apache.camel.component.knative;

import java.util.HashMap;
import java.util.Map;

import org.apache.camel.CamelContext;
import org.apache.camel.Consumer;
import org.apache.camel.DelegateEndpoint;
Expand All @@ -38,6 +35,9 @@
import org.apache.camel.util.StringHelper;
import org.apache.camel.util.URISupport;

import java.util.HashMap;
import java.util.Map;


@UriEndpoint(
firstVersion = "3.0.0",
Expand Down Expand Up @@ -154,7 +154,7 @@ private static Endpoint http(CamelContext context, ServiceDefinition definition)

if (ObjectHelper.isEmpty(host)) {
String name = definition.getName();
String zone = definition.getMetadata().get(ServiceDefinition.SERVICE_META_ZONE);
String zone = definition.getMetadata().get(Knative.SERVICE_META_ZONE);

if (ObjectHelper.isNotEmpty(zone)) {
try {
Expand All @@ -170,15 +170,15 @@ private static Endpoint http(CamelContext context, ServiceDefinition definition)
host = name;
}

ObjectHelper.notNull(host, ServiceDefinition.SERVICE_META_HOST);
ObjectHelper.notNull(host, Knative.SERVICE_META_HOST);
ObjectHelper.notNull(protocol, Knative.KNATIVE_PROTOCOL);

String uri = String.format("%s:%s://%s", scheme, protocol, host);
if (port != -1) {
uri = uri + ":" + port;
}

String path = definition.getMetadata().get(ServiceDefinition.SERVICE_META_PATH);
String path = definition.getMetadata().get(Knative.SERVICE_META_PATH);
if (path != null) {
if (!path.startsWith("/")) {
uri += "/";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
*/
package org.apache.camel.component.knative;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.camel.CamelContext;
import org.apache.camel.impl.cloud.DefaultServiceDefinition;
import org.apache.camel.k.adapter.Resources;
import org.apache.camel.util.CollectionHelper;
import org.apache.camel.util.StringHelper;

import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
Expand All @@ -29,15 +37,6 @@
import java.util.UUID;
import java.util.stream.Stream;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.camel.CamelContext;
import org.apache.camel.cloud.ServiceDefinition;
import org.apache.camel.impl.cloud.DefaultServiceDefinition;
import org.apache.camel.k.adapter.Resources;
import org.apache.camel.util.CollectionHelper;
import org.apache.camel.util.StringHelper;

/*
* Assuming it is loaded from a json for now
*/
Expand Down Expand Up @@ -104,7 +103,7 @@ private Optional<KnativeServiceDefinition> lookup(Knative.Type type, String name
definition.getPort(),
KnativeSupport.mergeMaps(
definition.getMetadata(),
Collections.singletonMap(ServiceDefinition.SERVICE_META_PATH, "/" + contextPath)
Collections.singletonMap(Knative.SERVICE_META_PATH, "/" + contextPath)
)
);
}
Expand All @@ -125,7 +124,7 @@ public static KnativeServiceDefinition computeServiceDefinition(Knative.Type typ

}
if (contextPath != null) {
meta.put(ServiceDefinition.SERVICE_META_PATH, "/" + contextPath);
meta.put(Knative.SERVICE_META_PATH, "/" + contextPath);
}

return new KnativeEnvironment.KnativeServiceDefinition(
Expand Down Expand Up @@ -190,13 +189,12 @@ public KnativeServiceDefinition(
@JsonProperty(value = "metadata", required = false) Map<String, String> metadata) {

super(
UUID.randomUUID().toString(),
name,
host,
port,
KnativeSupport.mergeMaps(
metadata,
CollectionHelper.mapOf(
KnativeSupport.mapOf(
Knative.KNATIVE_TYPE, type.name(),
Knative.KNATIVE_PROTOCOL, protocol.name())
)
Expand All @@ -212,7 +210,7 @@ public Knative.Protocol getProtocol() {
}

public String getPath() {
return getMetadata().get(ServiceDefinition.SERVICE_META_PATH);
return getMetadata().get(Knative.SERVICE_META_PATH);
}

public String getEventType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,18 @@ public static <K, V> Map<K, V> mergeMaps(Map<K, V> map, Map<K, V>... maps) {

return answer;
}

public static <K, V> Map<K, V> mapOf(K key, V value, Object... keyVals) {
Map<K, V> map = new HashMap<>();
map.put(key, value);

for (int i = 0; i < keyVals.length; i += 2) {
map.put(
(K) keyVals[i],
(V) keyVals[i + 1]
);
}

return map;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,10 @@
*/
package org.apache.camel.component.knative;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.cloud.ServiceDefinition;
import org.apache.camel.component.knative.ce.CloudEventsProcessors;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.impl.DefaultCamelContext;
Expand All @@ -33,7 +28,9 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.apache.camel.util.CollectionHelper.mapOf;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;

public class CloudEventsV01Test {

Expand Down Expand Up @@ -74,8 +71,8 @@ void testInvokeEndpoint() throws Exception {
"myEndpoint",
"localhost",
port,
mapOf(
ServiceDefinition.SERVICE_META_PATH, "/a/path",
KnativeSupport.mapOf(
Knative.SERVICE_META_PATH, "/a/path",
Knative.KNATIVE_EVENT_TYPE, "org.apache.camel.event",
Knative.CONTENT_TYPE, "text/plain"
))
Expand Down Expand Up @@ -129,8 +126,8 @@ void testConsumeStructuredContent() throws Exception {
"myEndpoint",
"localhost",
port,
mapOf(
ServiceDefinition.SERVICE_META_PATH, "/a/path",
KnativeSupport.mapOf(
Knative.SERVICE_META_PATH, "/a/path",
Knative.KNATIVE_EVENT_TYPE, "org.apache.camel.event",
Knative.CONTENT_TYPE, "text/plain"
))
Expand Down Expand Up @@ -167,7 +164,7 @@ public void configure() throws Exception {
"direct:source",
e -> {
e.getIn().setHeader(Exchange.CONTENT_TYPE, Knative.MIME_STRUCTURED_CONTENT_MODE);
e.getIn().setBody(new ObjectMapper().writeValueAsString(mapOf(
e.getIn().setBody(new ObjectMapper().writeValueAsString(KnativeSupport.mapOf(
"cloudEventsVersion", CloudEventsProcessors.v01.getVersion(),
"eventType", "org.apache.camel.event",
"eventID", "myEventID",
Expand All @@ -192,8 +189,8 @@ void testConsumeContent() throws Exception {
"myEndpoint",
"localhost",
port,
mapOf(
ServiceDefinition.SERVICE_META_PATH, "/a/path",
KnativeSupport.mapOf(
Knative.SERVICE_META_PATH, "/a/path",
Knative.KNATIVE_EVENT_TYPE, "org.apache.camel.event",
Knative.CONTENT_TYPE, "text/plain"
))
Expand Down Expand Up @@ -253,7 +250,7 @@ void testConsumeContentWithFilter() throws Exception {
"ep1",
"localhost",
port,
mapOf(
KnativeSupport.mapOf(
Knative.KNATIVE_EVENT_TYPE, "org.apache.camel.event",
Knative.CONTENT_TYPE, "text/plain",
Knative.FILTER_HEADER_NAME, "CE-Source",
Expand All @@ -265,7 +262,7 @@ void testConsumeContentWithFilter() throws Exception {
"ep2",
"localhost",
port,
mapOf(
KnativeSupport.mapOf(
Knative.KNATIVE_EVENT_TYPE, "org.apache.camel.event",
Knative.CONTENT_TYPE, "text/plain",
Knative.FILTER_HEADER_NAME, "CE-Source",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,10 @@
*/
package org.apache.camel.component.knative;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.cloud.ServiceDefinition;
import org.apache.camel.component.knative.ce.CloudEventsProcessors;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.impl.DefaultCamelContext;
Expand All @@ -33,7 +28,9 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.apache.camel.util.CollectionHelper.mapOf;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;

public class CloudEventsV02Test {

Expand Down Expand Up @@ -74,8 +71,8 @@ void testInvokeEndpoint() throws Exception {
"myEndpoint",
"localhost",
port,
mapOf(
ServiceDefinition.SERVICE_META_PATH, "/a/path",
KnativeSupport.mapOf(
Knative.SERVICE_META_PATH, "/a/path",
Knative.KNATIVE_EVENT_TYPE, "org.apache.camel.event",
Knative.CONTENT_TYPE, "text/plain"
))
Expand Down Expand Up @@ -129,8 +126,8 @@ void testConsumeStructuredContent() throws Exception {
"myEndpoint",
"localhost",
port,
mapOf(
ServiceDefinition.SERVICE_META_PATH, "/a/path",
KnativeSupport.mapOf(
Knative.SERVICE_META_PATH, "/a/path",
Knative.KNATIVE_EVENT_TYPE, "org.apache.camel.event",
Knative.CONTENT_TYPE, "text/plain"
))
Expand Down Expand Up @@ -167,7 +164,7 @@ public void configure() throws Exception {
"direct:source",
e -> {
e.getIn().setHeader(Exchange.CONTENT_TYPE, Knative.MIME_STRUCTURED_CONTENT_MODE);
e.getIn().setBody(new ObjectMapper().writeValueAsString(mapOf(
e.getIn().setBody(new ObjectMapper().writeValueAsString(KnativeSupport.mapOf(
"specversion", CloudEventsProcessors.v02.getVersion(),
"type", "org.apache.camel.event",
"id", "myEventID",
Expand All @@ -192,8 +189,8 @@ void testConsumeContent() throws Exception {
"myEndpoint",
"localhost",
port,
mapOf(
ServiceDefinition.SERVICE_META_PATH, "/a/path",
KnativeSupport.mapOf(
Knative.SERVICE_META_PATH, "/a/path",
Knative.KNATIVE_EVENT_TYPE, "org.apache.camel.event",
Knative.CONTENT_TYPE, "text/plain"
))
Expand Down Expand Up @@ -253,7 +250,7 @@ void testConsumeContentWithFilter() throws Exception {
"ep1",
"localhost",
port,
mapOf(
KnativeSupport.mapOf(
Knative.KNATIVE_EVENT_TYPE, "org.apache.camel.event",
Knative.CONTENT_TYPE, "text/plain",
Knative.FILTER_HEADER_NAME, "ce-source",
Expand All @@ -265,7 +262,7 @@ void testConsumeContentWithFilter() throws Exception {
"ep2",
"localhost",
port,
mapOf(
KnativeSupport.mapOf(
Knative.KNATIVE_EVENT_TYPE, "org.apache.camel.event",
Knative.CONTENT_TYPE, "text/plain",
Knative.FILTER_HEADER_NAME, "ce-source",
Expand Down
Loading

0 comments on commit b77ea7e

Please sign in to comment.