Fixed consumer sample to accept bare string

This commit is contained in:
Dave Syer
2017-05-26 12:42:25 +01:00
parent 2a4c46f57b
commit 9bf3601143
12 changed files with 59 additions and 6 deletions

View File

@@ -0,0 +1,48 @@
/*
* Copyright 2016-2017 the original author or 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 org.springframework.cloud.function.compiler;
import java.util.function.Consumer;
import org.junit.Test;
import org.springframework.cloud.function.support.FunctionUtils;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Dave Syer
*
*/
public class ConsumerCompilerTests {
@Test
public void consumesFluxString() {
CompiledFunctionFactory<Consumer<String>> compiled = new ConsumerCompiler<String>(
String.class.getName()).compile("foos",
"flux -> flux.subscribe(System.out::println)", "Flux<String>");
assertThat(FunctionUtils.isFluxConsumer(compiled.getFactoryMethod())).isTrue();
}
@Test
public void consumesString() {
CompiledFunctionFactory<Consumer<String>> compiled = new ConsumerCompiler<String>(
String.class.getName()).compile("foos", "System.out::println", "String");
assertThat(FunctionUtils.isFluxConsumer(compiled.getFactoryMethod())).isFalse();
}
}

View File

@@ -0,0 +1,49 @@
/*
* Copyright 2016-2017 the original author or 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 org.springframework.cloud.function.compiler;
import java.util.function.Function;
import org.junit.Test;
import org.springframework.cloud.function.support.FunctionUtils;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Dave Syer
*
*/
public class FunctionCompilerTests {
@Test
public void transformsFluxString() {
CompiledFunctionFactory<Function<String, String>> compiled = new FunctionCompiler<String, String>(
String.class.getName()).compile("foos",
"flux -> flux.map(v -> v.toUpperCase())", "Flux<String>", "Flux<String>");
assertThat(FunctionUtils.isFluxFunction(compiled.getFactoryMethod())).isTrue();
}
@Test
public void transformsString() {
CompiledFunctionFactory<Function<String, String>> compiled = new FunctionCompiler<String, String>(
String.class.getName()).compile("foos", "v -> v.toUpperCase()", "String", "String");
assertThat(FunctionUtils.isFluxFunction(compiled.getFactoryMethod())).isFalse();
assertThat(compiled.getResult().apply("hello")).isEqualTo("HELLO");
}
}

View File

@@ -0,0 +1,50 @@
/*
* Copyright 2016-2017 the original author or 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 org.springframework.cloud.function.compiler;
import java.util.function.Supplier;
import org.junit.Test;
import org.springframework.cloud.function.support.FunctionUtils;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Dave Syer
*
*/
public class SupplierCompilerTests {
@Test
public void supppliesFluxString() {
CompiledFunctionFactory<Supplier<String>> compiled = new SupplierCompiler<String>(
String.class.getName()).compile("foos",
"() -> Flux.just(\"foo\", \"bar\")", "Flux<String>");
assertThat(FunctionUtils.isFluxSupplier(compiled.getFactoryMethod())).isTrue();
}
@Test
public void supppliesString() {
CompiledFunctionFactory<Supplier<String>> compiled = new SupplierCompiler<String>(
String.class.getName()).compile("foos",
"() -> \"foo\"", "String");
assertThat(FunctionUtils.isFluxSupplier(compiled.getFactoryMethod())).isFalse();
assertThat(compiled.getResult().get()).isEqualTo("foo");
}
}

View File

@@ -0,0 +1,111 @@
/*
* Copyright 2016-2017 the original author or 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 org.springframework.cloud.function.compiler.proxy;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import org.junit.Test;
import org.springframework.cloud.function.compiler.CompiledFunctionFactory;
import org.springframework.cloud.function.compiler.ConsumerCompiler;
import org.springframework.cloud.function.compiler.FunctionCompiler;
import org.springframework.cloud.function.compiler.SupplierCompiler;
import org.springframework.cloud.function.support.FunctionFactoryMetadata;
import org.springframework.cloud.function.support.FunctionUtils;
import org.springframework.core.io.ByteArrayResource;
import static org.assertj.core.api.Assertions.assertThat;
import reactor.core.publisher.Flux;
/**
* @author Dave Syer
*
*/
public class ByteCodeLoadingFunctionTests {
@Test
public void compileConsumer() throws Exception {
CompiledFunctionFactory<Consumer<String>> compiled = new ConsumerCompiler<String>(
String.class.getName()).compile("foos", "System.out::println", "String");
ByteArrayResource resource = new ByteArrayResource(compiled.getGeneratedClassBytes(), "foos") {
@Override
public String getFilename() {
return "foos.fun";
}
};
ByteCodeLoadingConsumer<String> consumer = new ByteCodeLoadingConsumer<>(resource);
consumer.afterPropertiesSet();
assertThat(consumer instanceof FunctionFactoryMetadata);
assertThat(FunctionUtils.isFluxConsumer(consumer.getFactoryMethod())).isFalse();
consumer.accept("foo");
}
@Test
public void compileSupplier() throws Exception {
CompiledFunctionFactory<Supplier<String>> compiled = new SupplierCompiler<String>(
String.class.getName()).compile("foos", "() -> \"foo\"", "String");
ByteArrayResource resource = new ByteArrayResource(compiled.getGeneratedClassBytes(), "foos") {
@Override
public String getFilename() {
return "foos.fun";
}
};
ByteCodeLoadingSupplier<String> supplier = new ByteCodeLoadingSupplier<>(resource);
supplier.afterPropertiesSet();
assertThat(supplier instanceof FunctionFactoryMetadata);
assertThat(FunctionUtils.isFluxSupplier(supplier.getFactoryMethod())).isFalse();
assertThat(supplier.get()).isEqualTo("foo");
}
@Test
public void compileFunction() throws Exception {
CompiledFunctionFactory<Function<String, String>> compiled = new FunctionCompiler<String, String>(
String.class.getName()).compile("foos", "v -> v.toUpperCase()", "String", "String");
ByteArrayResource resource = new ByteArrayResource(compiled.getGeneratedClassBytes(), "foos") {
@Override
public String getFilename() {
return "foos.fun";
}
};
ByteCodeLoadingFunction<String, String> function = new ByteCodeLoadingFunction<>(resource);
function.afterPropertiesSet();
assertThat(function instanceof FunctionFactoryMetadata);
assertThat(FunctionUtils.isFluxFunction(function.getFactoryMethod())).isFalse();
assertThat(function.apply("foo")).isEqualTo("FOO");
}
@Test
public void compileFluxFunction() throws Exception {
CompiledFunctionFactory<Function<Flux<String>, Flux<String>>> compiled = new FunctionCompiler<Flux<String>, Flux<String>>(
String.class.getName()).compile("foos", "flux -> flux.map(v -> v.toUpperCase())", "Flux<String>", "Flux<String>");
ByteArrayResource resource = new ByteArrayResource(compiled.getGeneratedClassBytes(), "foos") {
@Override
public String getFilename() {
return "foos.fun";
}
};
ByteCodeLoadingFunction<Flux<String>, Flux<String>> function = new ByteCodeLoadingFunction<>(resource);
function.afterPropertiesSet();
assertThat(function instanceof FunctionFactoryMetadata);
assertThat(FunctionUtils.isFluxFunction(function.getFactoryMethod())).isTrue();
assertThat(function.apply(Flux.just("foo")).blockFirst()).isEqualTo("FOO");
}
}