Skip to content
Permalink
Browse files
fix(core): default SourceType as source
Setting any empty SourceType as a source.

Closes #658
Closes apache/camel-k#2223
  • Loading branch information
squakez authored and lburgazzoli committed Jun 10, 2021
1 parent b2a462c commit db0c960d2eeaf6d2d12438c872d8e54d584e51d1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
@@ -28,7 +28,8 @@ public class SourceDefinition implements IdAware {
private String language;
private String loader;
private List<String> interceptors;
private SourceType type;
// Default as source type
private SourceType type = SourceType.source;
private List<String> propertyNames;
private String location;
private byte[] content;
@@ -17,6 +17,7 @@
package org.apache.camel.k.listener;

import java.util.Arrays;
import java.util.Comparator;

import org.apache.camel.k.Runtime;
import org.apache.camel.k.SourceDefinition;
@@ -100,19 +101,7 @@ static void sortSources(SourceDefinition[] sources) {
return;
}
// We must ensure the source order as defined in SourceType enum
Arrays.sort(sources,
(a, b) -> {
if (a.getType() == null && b.getType() == null) {
return 0;
}
if (a.getType() == null) {
return SourceType.source.compareTo(b.getType());
} else if (b.getType() == null) {
return a.getType().compareTo(SourceType.source);
} else {
return a.getType().compareTo(b.getType());
}
});
Arrays.sort(sources, Comparator.comparingInt(a -> a.getType().ordinal()));
}

}
@@ -18,26 +18,26 @@

import org.apache.camel.CamelContext;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.k.SourceType;
import org.apache.camel.k.support.PropertiesSupport;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.apache.camel.k.test.CamelKTestSupport.asProperties;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class SourceConfigurerTest {
@Test
public void shouldLoadMultipleSources() {
CamelContext context = new DefaultCamelContext();
context.getPropertiesComponent().setInitialProperties(asProperties(
"camel.k.sources[0].name", "sourceName0",
"camel.k.sources[0].location", "classpath:MyTemplate1.java",
"camel.k.sources[0].name", "templateName",
"camel.k.sources[0].location", "classpath:MyTemplate.java",
"camel.k.sources[0].type", "template",
"camel.k.sources[1].name", "err1",
"camel.k.sources[1].location", "classpath:MyTemplate2.java",
"camel.k.sources[2].name", "err2",
"camel.k.sources[2].location", "classpath:Err2.java",
"camel.k.sources[1].name", "src",
"camel.k.sources[1].location", "classpath:MySrc.java",
"camel.k.sources[2].name", "err",
"camel.k.sources[2].location", "classpath:Err.java",
"camel.k.sources[2].type", "errorHandler"
));

@@ -56,7 +56,7 @@ public void shouldLoadMultipleSources() {
public void shouldFailOnMultipleErrorHandlers() {
CamelContext context = new DefaultCamelContext();
context.getPropertiesComponent().setInitialProperties(asProperties(
"camel.k.sources[0].name", "sourceName0",
"camel.k.sources[0].name", "templateName0",
"camel.k.sources[0].location", "classpath:MyTemplate1.java",
"camel.k.sources[0].type", "template",
"camel.k.sources[1].name", "err1",
@@ -81,13 +81,38 @@ public void shouldFailOnMultipleErrorHandlers() {
}, "java.lang.IllegalArgumentException: Expected only one error handler source type, got 2");
}

@Test
public void shouldDefaultSourcesWithEmptyType() {
CamelContext context = new DefaultCamelContext();
context.getPropertiesComponent().setInitialProperties(asProperties(
"camel.k.sources[0].name", "source0",
"camel.k.sources[1].name", "source1",
"camel.k.sources[2].name", "source2",
"camel.k.sources[2].type", "source"
));

SourcesConfigurer configuration = new SourcesConfigurer();

PropertiesSupport.bindProperties(
context,
configuration,
k -> k.startsWith(SourcesConfigurer.CAMEL_K_SOURCES_PREFIX),
SourcesConfigurer.CAMEL_K_PREFIX);

assertThat(configuration.getSources().length).isEqualTo(3);
assertThat(configuration.getSources()[0].getType()).isEqualTo(SourceType.source);
assertThat(configuration.getSources()[1].getType()).isEqualTo(SourceType.source);
assertThat(configuration.getSources()[2].getType()).isEqualTo(SourceType.source);
}

@Test
public void shouldOrderSourcesByType() {
CamelContext context = new DefaultCamelContext();
context.getPropertiesComponent().setInitialProperties(asProperties(
"camel.k.sources[0].name", "template1",
"camel.k.sources[0].type", "template",
"camel.k.sources[1].name", "source1",
"camel.k.sources[1].type", "source",
"camel.k.sources[2].name", "source2",
"camel.k.sources[3].name", "errorHandler1",
"camel.k.sources[3].type", "errorHandler"
@@ -104,10 +129,14 @@ public void shouldOrderSourcesByType() {

assertThat(configuration.getSources().length).isEqualTo(4);
assertThat(configuration.getSources()[0].getName()).isEqualTo("errorHandler1");
assertThat(configuration.getSources()[0].getType()).isEqualTo(SourceType.errorHandler);
assertThat(configuration.getSources()[1].getName()).isEqualTo("template1");
assertThat(configuration.getSources()[1].getType()).isEqualTo(SourceType.template);
// Order for the same type does not matter
assertThat(configuration.getSources()[2].getName()).contains("source");
assertThat(configuration.getSources()[2].getType()).isEqualTo(SourceType.source);
assertThat(configuration.getSources()[3].getName()).contains("source");
assertThat(configuration.getSources()[3].getType()).isEqualTo(SourceType.source);
}

@Test

0 comments on commit db0c960

Please sign in to comment.