|
| 1 | +/* |
| 2 | + * Copyright 2020-2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.spring.cloudevent; |
| 18 | + |
| 19 | +import java.util.function.Function; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.springframework.boot.SpringApplication; |
| 23 | +import org.springframework.cloud.function.context.FunctionCatalog; |
| 24 | +import org.springframework.context.ApplicationContext; |
| 25 | +import org.springframework.messaging.Message; |
| 26 | +import org.springframework.messaging.support.MessageBuilder; |
| 27 | + |
| 28 | +/** |
| 29 | + * |
| 30 | + * @author Oleg Zhurakousky |
| 31 | + * |
| 32 | + */ |
| 33 | +public class CloudeventDemoApplicationFunctionTests { |
| 34 | + |
| 35 | + @Test |
| 36 | + public void demoPureFunctionInvocation() { |
| 37 | + ApplicationContext context = SpringApplication.run(CloudeventDemoApplication.class); |
| 38 | + FunctionCatalog catalog = context.getBean(FunctionCatalog.class); |
| 39 | + Message<String> binaryCloudEventMessage = MessageBuilder |
| 40 | + .withPayload("{\"releaseDate\":\"24-03-2004\", \"releaseName\":\"Spring Framework\", \"version\":\"1.0\"}") |
| 41 | + .setHeader("ce-specversion", "1.0") |
| 42 | + .setHeader("ce-type", "com.example.springevent") |
| 43 | + .setHeader("ce-source", "spring.io/spring-event") |
| 44 | + .setHeader("ce-id", "123-456-9876-09") |
| 45 | + .build(); |
| 46 | + |
| 47 | + /* |
| 48 | + * NOTE how it makes no difference what the actual function signature |
| 49 | + * is (see `asPOJOMessage` and `asPOJO` specifically). Type conversion will happen |
| 50 | + * inside spring-cloud-function. |
| 51 | + */ |
| 52 | + Function<Message<String>, String> asPojoMessage = catalog.lookup("asPOJOMessage"); |
| 53 | + System.out.println(asPojoMessage.apply(binaryCloudEventMessage)); |
| 54 | + |
| 55 | + Function<Message<String>, String> asPojo = catalog.lookup("asPOJO"); |
| 56 | + System.out.println(asPojo.apply(binaryCloudEventMessage)); |
| 57 | + |
| 58 | + Function<Message<String>, String> asString = catalog.lookup("asString"); |
| 59 | + System.out.println(asString.apply(binaryCloudEventMessage)); |
| 60 | + |
| 61 | + Function<Message<String>, String> asStringMessage = catalog.lookup("asStringMessage"); |
| 62 | + System.out.println(asStringMessage.apply(binaryCloudEventMessage)); |
| 63 | + } |
| 64 | +} |
0 commit comments