GH-422 Add initial set of Cloud Event samples

This commit adds initial set of samples and tests demonstrating various ways of how cloud event can be consumed (i.e., structured, bindary, pojo etc)
This commit is contained in:
Oleg Zhurakousky
2020-11-10 16:29:30 +01:00
parent 8a032e7ed9
commit 2a88b52ca1
13 changed files with 1149 additions and 8 deletions

View File

@@ -0,0 +1,101 @@
/*
* Copyright 2020-2020 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
*
* https://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 io.spring.cloudevent;
import java.util.function.Function;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.messaging.Message;
/**
* Sample application that demonstrates how user functions can be triggered by cloud event.
* Events can come from anywhere (e.g., HTTP, Messaging, RSocket etc).
* Given that this particular sample comes already with spring-cloud-function-web support each
* function is a valid REST endpoint where function name signifies URL path (e.g., http://localhost:8080/asPOJOMessage).
*
* Simply start the application and post cloud event to individual function - (see individual 'curl' command at each function).
*
* You can also run CloudeventDemoApplicationTests.
*
* @author Oleg Zhurakousky
*
*/
@SpringBootApplication
public class CloudeventDemoApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(CloudeventDemoApplication.class, args);
}
/*
* curl -w'\n' localhost:8080/asStringMessage \
* -H "Ce-Specversion: 1.0" \
* -H "Ce-Type: com.example.springevent" \
* -H "Ce-Source: spring.io/spring-event" \
* -H "Content-Type: application/json" \
* -H "Ce-Id: 0001" \
* -d '{"releaseDate":"2004-03-24", "releaseName":"Spring Framework", "version":"1.0"}'
*/
@Bean
public Function<Message<String>, String> asStringMessage() {
return v -> v.getPayload().toString();
}
/*
* curl -w'\n' localhost:8080/asString \
* -H "Ce-Specversion: 1.0" \
* -H "Ce-Type: com.example.springevent" \
* -H "Ce-Source: spring.io/spring-event" \
* -H "Content-Type: application/json" \
* -H "Ce-Id: 0001" \
* -d '{"releaseDate":"2004-03-24", "releaseName":"Spring Framework", "version":"1.0"}'
*/
@Bean
public Function<String, String> asString() {
return v -> v;
}
/*
* curl -w'\n' localhost:8080/asPOJOMessage \
* -H "Ce-Specversion: 1.0" \
* -H "Ce-Type: com.example.springevent" \
* -H "Ce-Source: spring.io/spring-event" \
* -H "Content-Type: application/json" \
* -H "Ce-Id: 0001" \
* -d '{"releaseDate":"2004-03-24", "releaseName":"Spring Framework", "version":"1.0"}'
*/
@Bean
public Function<Message<SpringReleaseEvent>, String> asPOJOMessage() {
return v -> v.getPayload().toString();
}
/*
* curl -w'\n' localhost:8080/asPOJO \
* -H "Ce-Specversion: 1.0" \
* -H "Ce-Type: com.example.springevent" \
* -H "Ce-Source: spring.io/spring-event" \
* -H "Content-Type: application/json" \
* -H "Ce-Id: 0001" \
* -d '{"releaseDate":"2004-03-24", "releaseName":"Spring Framework", "version":"1.0"}'
*/
@Bean
public Function<SpringReleaseEvent, String> asPOJO() {
return v -> v.toString();
}
}

View File

@@ -0,0 +1,67 @@
/*
* Copyright 2020-2020 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
*
* https://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 io.spring.cloudevent;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
/**
* An example POJO that represents cloud event data
*
* @author Oleg Zhurakousky
*
*/
public class SpringReleaseEvent {
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
private Date releaseDate;
private String releaseName;
private String version;
public Date getReleaseDate() {
return releaseDate;
}
public void setReleaseDate(Date releaseDate) {
this.releaseDate = releaseDate;
}
public String getReleaseName() {
return releaseName;
}
public void setReleaseName(String releaseName) {
this.releaseName = releaseName;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
@Override
public String toString() {
return "releaseDate:" + new SimpleDateFormat("dd-MM-yyyy").format(releaseDate) + "; releaseName:" + releaseName + "; version:" + version;
}
}