GH-422, GH-606 Fis structure mode attribute generation
This commit is contained in:
@@ -22,12 +22,9 @@ import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
||||
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||
import org.springframework.cloud.function.cloudevent.CloudEventAttributes;
|
||||
import org.springframework.cloud.function.cloudevent.CloudEventAttributesProvider;
|
||||
import org.springframework.cloud.function.cloudevent.CloudEventMessageUtils;
|
||||
import org.springframework.cloud.function.web.util.HeaderUtils;
|
||||
@@ -37,7 +34,6 @@ import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageHeaders;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
/**
|
||||
* Sample application that demonstrates how user functions can be triggered by cloud event.
|
||||
@@ -55,6 +51,8 @@ import org.springframework.web.client.RestTemplate;
|
||||
@SpringBootApplication
|
||||
public class CloudeventDemoApplication {
|
||||
|
||||
boolean consumerSuccess;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
SpringApplication.run(CloudeventDemoApplication.class, args);
|
||||
}
|
||||
@@ -145,22 +143,8 @@ public class CloudeventDemoApplication {
|
||||
Assert.notEmpty(idHeader, "'id' must not be null");
|
||||
List<String> specversionHeader = entity.getHeaders().get("ce-specversion");
|
||||
Assert.notEmpty(specversionHeader, "'specversion' must not be null");
|
||||
this.consumerSuccess = true;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@Bean
|
||||
@ConditionalOnExpression("'${K_SINK:}'!=''")
|
||||
public Consumer<Message<Map<String, Object>>> sink(CloudEventAttributesProvider provider,
|
||||
RestTemplateBuilder builder, @Value("${K_SINK}") String url) {
|
||||
RestTemplate client = builder.build();
|
||||
return eventMessage -> {
|
||||
RequestEntity<Map<String, Object>> entity = RequestEntity.post(URI.create("http://foo.com"))
|
||||
.headers(HeaderUtils.fromMessage(
|
||||
new MessageHeaders(CloudEventMessageUtils.generateAttributes(eventMessage, provider, "io.spring"))))
|
||||
.body(eventMessage.getPayload());
|
||||
client.exchange(entity, byte[].class);
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -171,6 +171,11 @@ public class CloudeventDemoApplicationRESTTests {
|
||||
response = testRestTemplate.exchange(re, String.class);
|
||||
|
||||
assertThat(response.getBody()).isEqualTo("releaseDate:24-03-2004; releaseName:Spring Framework; version:1.0");
|
||||
assertThat(response.getHeaders().get(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.SOURCE))
|
||||
.isEqualTo(Collections.singletonList("https://interface21.com/"));
|
||||
assertThat(response.getHeaders().get(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.TYPE))
|
||||
.isEqualTo(Collections.singletonList("com.interface21"));
|
||||
assertThat(response.getHeaders().get(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.ID)).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -202,6 +207,11 @@ public class CloudeventDemoApplicationRESTTests {
|
||||
response = testRestTemplate.exchange(re, String.class);
|
||||
|
||||
assertThat(response.getBody()).isEqualTo("{\"version\":\"1.0\",\"releaseName\":\"Spring Framework\",\"releaseDate\":\"24-03-2004\"}");
|
||||
assertThat(response.getHeaders().get(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.SOURCE))
|
||||
.isEqualTo(Collections.singletonList("https://interface21.com/"));
|
||||
assertThat(response.getHeaders().get(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.TYPE))
|
||||
.isEqualTo(Collections.singletonList("com.interface21"));
|
||||
assertThat(response.getHeaders().get(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.ID)).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -271,7 +281,7 @@ public class CloudeventDemoApplicationRESTTests {
|
||||
|
||||
|
||||
@Test
|
||||
public void testAsStructuralPojoToPojo() throws Exception {
|
||||
public void testAsStructuralPojoToPojoDefaultDataContentType() throws Exception {
|
||||
ApplicationContext context = SpringApplication.run(CloudeventDemoApplication.class);
|
||||
JsonMapper mapper = context.getBean(JsonMapper.class);
|
||||
|
||||
@@ -280,7 +290,6 @@ public class CloudeventDemoApplicationRESTTests {
|
||||
" \"type\" : \"org.springframework\",\n" +
|
||||
" \"source\" : \"https://spring.io/\",\n" +
|
||||
" \"id\" : \"A234-1234-1234\",\n" +
|
||||
// " \"ce-datacontenttype\" : \"application/json\",\n" +
|
||||
" \"data\" : {\n" +
|
||||
" \"version\" : \"1.0\",\n" +
|
||||
" \"releaseName\" : \"Spring Framework\",\n" +
|
||||
@@ -288,7 +297,7 @@ public class CloudeventDemoApplicationRESTTests {
|
||||
" }\n" +
|
||||
"}";
|
||||
|
||||
System.out.println(payload);
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.valueOf("application/cloudevents+json;charset=utf-8"));
|
||||
|
||||
@@ -308,16 +317,16 @@ public class CloudeventDemoApplicationRESTTests {
|
||||
assertThat(springReleaseEvent.getReleaseName()).isEqualTo("Spring Framework");
|
||||
assertThat(springReleaseEvent.getVersion()).isEqualTo("10.0");
|
||||
|
||||
|
||||
// assertThat(response.getHeaders().get(CloudEventMessageUtils.CANONICAL_SOURCE))
|
||||
// .isEqualTo(Collections.singletonList("http://spring.io/application-application"));
|
||||
// assertThat(response.getHeaders().get(CloudEventMessageUtils.CANONICAL_TYPE))
|
||||
// .isEqualTo(Collections.singletonList(SpringReleaseEvent.class.getName()));
|
||||
assertThat(response.getHeaders().get(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.SOURCE))
|
||||
.isEqualTo(Collections.singletonList("https://interface21.com/"));
|
||||
assertThat(response.getHeaders().get(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.TYPE))
|
||||
.isEqualTo(Collections.singletonList("com.interface21"));
|
||||
assertThat(response.getHeaders().get(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.ID)).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPojoConsumer() throws Exception {
|
||||
SpringApplication.run(new Class[] {CloudeventDemoApplication.class}, new String[] {});
|
||||
ApplicationContext context = SpringApplication.run(new Class[] {CloudeventDemoApplication.class}, new String[] {});
|
||||
|
||||
HttpHeaders headers = this.buildHeaders(MediaType.APPLICATION_JSON);
|
||||
String payload = "{\"releaseDate\":\"01-10-2006\", \"releaseName\":\"Spring Framework\", \"version\":\"1.0\"}";
|
||||
@@ -325,6 +334,8 @@ public class CloudeventDemoApplicationRESTTests {
|
||||
RequestEntity<String> re = new RequestEntity<>(payload, headers, HttpMethod.POST, this.constructURI("/pojoConsumer"));
|
||||
ResponseEntity<String> response = testRestTemplate.exchange(re, String.class);
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.ACCEPTED);
|
||||
CloudeventDemoApplication application = context.getBean(CloudeventDemoApplication.class);
|
||||
assertThat(application.consumerSuccess).isTrue();
|
||||
}
|
||||
|
||||
private URI constructURI(String path) throws Exception {
|
||||
|
||||
Reference in New Issue
Block a user