Prune dependencies and rename samples
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright 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 com.example;
|
||||
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.function.compiler.FunctionCompiler;
|
||||
import org.springframework.cloud.function.compiler.proxy.LambdaCompilingFunction;
|
||||
import org.springframework.cloud.function.context.FunctionScan;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
@FunctionScan
|
||||
@SpringBootApplication
|
||||
public class SampleApplication {
|
||||
|
||||
@Bean
|
||||
public Function<String, String> uppercase() {
|
||||
return value -> value.toUpperCase();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Function<Flux<String>, Flux<String>> lowercase() {
|
||||
return flux -> flux.map(value -> value.toLowerCase());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Supplier<String> hello() {
|
||||
return () -> "hello";
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Supplier<Flux<String>> words() {
|
||||
return () -> Flux.fromArray(new String[] { "foo", "bar" });
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Function<String, String> compiledUppercase(FunctionCompiler<String, String> compiler) {
|
||||
String lambda = "s -> s.toUpperCase()";
|
||||
LambdaCompilingFunction<String, String> function = new LambdaCompilingFunction<>(new ByteArrayResource(lambda.getBytes()), compiler);
|
||||
function.setTypeParameterizations("String", "String");
|
||||
return function;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Function<Flux<String>, Flux<String>> compiledLowercase(FunctionCompiler<Flux<String>, Flux<String>> compiler) {
|
||||
String lambda = "f->f.map(o->o.toString().toLowerCase())";
|
||||
return new LambdaCompilingFunction<>(new ByteArrayResource(lambda.getBytes()), compiler);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public <T, R> FunctionCompiler<T, R> compiler() {
|
||||
return new FunctionCompiler<>();
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
SpringApplication.run(SampleApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright 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 com.example.functions;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class CharCounter implements Function<String, Integer> {
|
||||
|
||||
@Override
|
||||
public Integer apply(String word) {
|
||||
return word.length();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright 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 com.example.functions;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class Exclaimer implements Function<Flux<String>, Flux<String>> {
|
||||
|
||||
@Override
|
||||
public Flux<String> apply(Flux<String> words) {
|
||||
return words.map(word->word+"!!!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright 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 com.example.functions;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class Greeter implements Function<String, String> {
|
||||
|
||||
@Override
|
||||
public String apply(String name) {
|
||||
return "Hello " + name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
boms.spring-cloud-dependencies: org.springframework.cloud:spring-cloud-dependencies:Dalston.RELEASE
|
||||
dependencies.spring-cloud-function-stream: org.springframework.cloud:spring-cloud-function-stream:1.0.0.BUILD-SNAPSHOT
|
||||
dependencies.spring-cloud-stream-rabbit: org.springframework.cloud:spring-cloud-starter-stream-rabbit
|
||||
@@ -0,0 +1,3 @@
|
||||
spring.cloud.stream.bindings.input.destination: words
|
||||
spring.cloud.stream.bindings.output.destination: uppercaseWords
|
||||
spring.cloud.function.stream.endpoint: uppercase
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright 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 com.example;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.example.functions.CharCounter;
|
||||
import com.example.functions.Exclaimer;
|
||||
import com.example.functions.Greeter;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
public class FunctionTests {
|
||||
|
||||
private final SampleApplication functions = new SampleApplication();
|
||||
|
||||
@Test
|
||||
public void testUppercase() {
|
||||
String output = functions.uppercase().apply("foobar");
|
||||
assertEquals("FOOBAR", output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLowercase() {
|
||||
Flux<String> output = functions.lowercase().apply(Flux.just("FOO", "BAR"));
|
||||
List<String> results = output.collectList().block();
|
||||
assertEquals(2, results.size());
|
||||
assertEquals("foo", results.get(0));
|
||||
assertEquals("bar", results.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHello() {
|
||||
String output = functions.hello().get();
|
||||
assertEquals("hello", output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWords() {
|
||||
Flux<String> output = functions.words().get();
|
||||
List<String> results = output.collectList().block();
|
||||
assertEquals(2, results.size());
|
||||
assertEquals("foo", results.get(0));
|
||||
assertEquals("bar", results.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGreeter() {
|
||||
assertEquals("Hello World", new Greeter().apply("World"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExclaimer() {
|
||||
Flux<String> input = Flux.just("foo", "bar");
|
||||
Flux<String> output = new Exclaimer().apply(input);
|
||||
List<String> results = output.collectList().block();
|
||||
assertEquals(2, results.size());
|
||||
assertEquals("foo!!!", results.get(0));
|
||||
assertEquals("bar!!!", results.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCharCounter() {
|
||||
assertEquals((Integer) 21, new CharCounter().apply("this is 21 chars long"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
/*
|
||||
* 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 com.example;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(properties = "spring.cloud.function.scan.packages=com.example.functions")
|
||||
public class SampleApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private Function<String, String> uppercase;
|
||||
|
||||
@Test
|
||||
public void testUppercase() {
|
||||
String output = this.uppercase.apply("foobar");
|
||||
assertEquals("FOOBAR", output);
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private Function<Flux<String>, Flux<String>> lowercase;
|
||||
|
||||
@Test
|
||||
public void testLowercase() {
|
||||
Flux<String> output = this.lowercase.apply(Flux.just("FOO", "BAR"));
|
||||
List<String> results = output.collectList().block();
|
||||
assertEquals(2, results.size());
|
||||
assertEquals("foo", results.get(0));
|
||||
assertEquals("bar", results.get(1));
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private Supplier<String> hello;
|
||||
|
||||
@Test
|
||||
public void testHello() {
|
||||
String output = this.hello.get();
|
||||
assertEquals("hello", output);
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private Supplier<Flux<String>> words;
|
||||
|
||||
@Test
|
||||
public void testWords() {
|
||||
Flux<String> output = this.words.get();
|
||||
List<String> results = output.collectList().block();
|
||||
assertEquals(2, results.size());
|
||||
assertEquals("foo", results.get(0));
|
||||
assertEquals("bar", results.get(1));
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private Function<String, String> compiledUppercase;
|
||||
|
||||
@Test
|
||||
public void testCompiledUppercase() {
|
||||
String output = this.compiledUppercase.apply("foobar");
|
||||
assertEquals("FOOBAR", output);
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private Function<Flux<String>, Flux<String>> compiledLowercase;
|
||||
|
||||
@Test
|
||||
public void testCompiledLowercase() {
|
||||
Flux<String> input = Flux.just("FOO", "BAR");
|
||||
Flux<String> output = this.compiledLowercase.apply(input);
|
||||
List<String> results = output.collectList().block();
|
||||
assertEquals(2, results.size());
|
||||
assertEquals("foo", results.get(0));
|
||||
assertEquals("bar", results.get(1));
|
||||
}
|
||||
|
||||
// the following are contributed via @FunctionScan:
|
||||
|
||||
@Autowired
|
||||
private Function<String, String> greeter;
|
||||
|
||||
@Test
|
||||
public void testGreeter() {
|
||||
String greeting = this.greeter.apply("World");
|
||||
assertEquals("Hello World", greeting);
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private Function<Flux<String>, Flux<String>> exclaimer;
|
||||
|
||||
@Test
|
||||
public void testExclaimer() {
|
||||
Flux<String> input = Flux.just("foo", "bar");
|
||||
Flux<String> output = this.exclaimer.apply(input);
|
||||
List<String> results = output.collectList().block();
|
||||
assertEquals(2, results.size());
|
||||
assertEquals("foo!!!", results.get(0));
|
||||
assertEquals("bar!!!", results.get(1));
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private Function<String, Integer> charCounter;
|
||||
|
||||
@Test
|
||||
public void testCharCounter() {
|
||||
Integer length = this.charCounter.apply("the quick brown fox");
|
||||
assertEquals(new Integer(19), length);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user