Skip to content

Commit 24b0aec

Browse files
committed
Add PojoToPojo and MapToMap tests
Related to GH-422 and GH-606
1 parent 7ddbbe5 commit 24b0aec

File tree

3 files changed

+63
-43
lines changed

3 files changed

+63
-43
lines changed

spring-cloud-function-samples/function-sample-cloudevent/src/main/java/io/spring/cloudevent/CloudeventDemoApplication.java

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package io.spring.cloudevent;
1818

19+
import java.text.ParseException;
20+
import java.text.SimpleDateFormat;
1921
import java.util.Collections;
2022
import java.util.Map;
2123
import java.util.UUID;
@@ -102,19 +104,9 @@ public Function<Message<SpringReleaseEvent>, Message<SpringReleaseEvent>> consum
102104
};
103105
}
104106

105-
@Bean
106-
public Function<Message<SpringReleaseEvent>, SpringReleaseEvent> consumeAndProduceCloudEventPojo() {
107-
return ceMessage -> {
108-
SpringReleaseEvent data = ceMessage.getPayload();
109-
data.setVersion("2.0");
110-
data.setReleaseDateAsString("01-10-2006");
111-
112-
return data;
113-
};
114-
}
115107

116108
@Bean
117-
public Function<Map<String, Object>, Map<String, Object>> consumeAndProduceCloudEventAsPojoToPojo() {
109+
public Function<Map<String, Object>, Map<String, Object>> consumeAndProduceCloudEventAsMapToMap() {
118110
return ceMessage -> {
119111
ceMessage.put("version", "10.0");
120112
ceMessage.put("releaseDate", "01-10-2050");
@@ -123,22 +115,10 @@ public Function<Map<String, Object>, Map<String, Object>> consumeAndProduceCloud
123115
}
124116

125117
@Bean
126-
public CloudEventAttributesProvider cloudEventAttributesProvider() {
127-
return new CustomCloudEventAtttributesProvider();
128-
}
129-
130-
public static class CustomCloudEventAtttributesProvider extends DefaultCloudEventAttributesProvider {
131-
132-
@Override
133-
public Map<String, Object> generateDefaultCloudEventHeaders(Message<?> inputMessage, Object result) {
134-
if (inputMessage.getHeaders().containsKey(CloudEventMessageUtils.CE_ID)) { // input is a cloud event
135-
String applicationName = "http://spring.io/fooBar";
136-
return this.get(inputMessage.getHeaders())
137-
.setId(UUID.randomUUID().toString())
138-
.setType(result.getClass().getName())
139-
.setSource(applicationName);
140-
}
141-
return Collections.emptyMap();
142-
}
118+
public Function<SpringReleaseEvent, SpringReleaseEvent> consumeAndProduceCloudEventAsPojoToPojo() {
119+
return event -> {
120+
event.setVersion("2.0");
121+
return event;
122+
};
143123
}
144124
}

spring-cloud-function-samples/function-sample-cloudevent/src/test/java/io/spring/cloudevent/CloudeventDemoApplicationFunctionTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public void demoPureFunctionProduceConsumeCloudEventAsPojo() {
9999
* is (see `asPOJOMessage` and `asPOJO` specifically). Type conversion will happen
100100
* inside spring-cloud-function.
101101
*/
102-
Function<Message<String>, Message<String>> asPojoMessage = catalog.lookup("consumeAndProduceCloudEventPojo");
102+
Function<Message<String>, Message<String>> asPojoMessage = catalog.lookup("consumeAndProduceCloudEventAsPojoToPojo");
103103
System.out.println(asPojoMessage.apply(binaryCloudEventMessage));
104104
}
105105
}

spring-cloud-function-samples/function-sample-cloudevent/src/test/java/io/spring/cloudevent/CloudeventDemoApplicationRESTTests.java

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,20 @@
2020

2121
import java.net.URI;
2222
import java.text.SimpleDateFormat;
23+
import java.util.Collections;
24+
import java.util.LinkedHashMap;
25+
import java.util.Map;
2326
import java.util.UUID;
2427

2528
import org.junit.jupiter.api.BeforeEach;
2629
import org.junit.jupiter.api.Test;
2730

2831
import org.springframework.boot.SpringApplication;
2932
import org.springframework.boot.test.web.client.TestRestTemplate;
33+
import org.springframework.cloud.function.cloudevent.CloudEventAttributesProvider;
3034
import org.springframework.cloud.function.cloudevent.CloudEventJsonMessageConverter;
35+
import org.springframework.cloud.function.cloudevent.CloudEventMessageUtils;
36+
import org.springframework.cloud.function.cloudevent.DefaultCloudEventAttributesProvider;
3137
import org.springframework.cloud.function.json.JsonMapper;
3238
import org.springframework.context.annotation.Bean;
3339
import org.springframework.context.annotation.Configuration;
@@ -42,6 +48,7 @@
4248
import org.springframework.messaging.converter.AbstractMessageConverter;
4349
import org.springframework.messaging.converter.MessageConverter;
4450
import org.springframework.util.MimeType;
51+
import org.springframework.util.MimeTypeUtils;
4552
import org.springframework.util.SocketUtils;
4653

4754
/**
@@ -200,6 +207,53 @@ public void testAsStracturalFormatToString() throws Exception {
200207
assertThat(response.getBody()).isEqualTo("{\"version\":\"1.0\",\"releaseName\":\"Spring Framework\",\"releaseDate\":\"24-03-2004\"}");
201208
}
202209

210+
@Test
211+
public void testAsBinaryMapToMap() throws Exception {
212+
SpringApplication.run(new Class[] {CloudeventDemoApplication.class}, new String[] {});
213+
214+
HttpHeaders headers = this.buildHeaders(MediaType.APPLICATION_JSON);
215+
String payload = "{\"releaseDate\":\"24-03-2004\", \"releaseName\":\"Spring Framework\", \"version\":\"1.0\"}";
216+
217+
RequestEntity<String> re = new RequestEntity<>(payload, headers, HttpMethod.POST, this.constructURI("/consumeAndProduceCloudEventAsMapToMap"));
218+
ResponseEntity<String> response = testRestTemplate.exchange(re, String.class);
219+
220+
assertThat(response.getBody()).isEqualTo("{\"releaseDate\":\"01-10-2050\",\"releaseName\":\"Spring Framework\",\"version\":\"10.0\"}");
221+
assertThat(response.getHeaders().get(CloudEventMessageUtils.CE_SOURCE))
222+
.isEqualTo(Collections.singletonList("http://spring.io/application-application"));
223+
assertThat(response.getHeaders().get(CloudEventMessageUtils.CE_TYPE))
224+
.isEqualTo(Collections.singletonList(LinkedHashMap.class.getName()));
225+
}
226+
227+
@Test
228+
public void testAsBinaryPojoToPojo() throws Exception {
229+
SpringApplication.run(new Class[] {CloudeventDemoApplication.class}, new String[] {});
230+
231+
HttpHeaders headers = this.buildHeaders(MediaType.APPLICATION_JSON);
232+
String payload = "{\"releaseDate\":\"01-10-2006\", \"releaseName\":\"Spring Framework\", \"version\":\"1.0\"}";
233+
234+
RequestEntity<String> re = new RequestEntity<>(payload, headers, HttpMethod.POST, this.constructURI("/consumeAndProduceCloudEventAsPojoToPojo"));
235+
ResponseEntity<String> response = testRestTemplate.exchange(re, String.class);
236+
237+
assertThat(response.getBody()).isEqualTo("{\"releaseDate\":\"01-10-2006\",\"releaseName\":\"Spring Framework\",\"version\":\"2.0\"}");
238+
assertThat(response.getHeaders().get(CloudEventMessageUtils.CE_SOURCE))
239+
.isEqualTo(Collections.singletonList("http://spring.io/application-application"));
240+
assertThat(response.getHeaders().get(CloudEventMessageUtils.CE_TYPE))
241+
.isEqualTo(Collections.singletonList(SpringReleaseEvent.class.getName()));
242+
}
243+
244+
private URI constructURI(String path) throws Exception {
245+
return new URI("http://localhost:" + System.getProperty("server.port") + path);
246+
}
247+
248+
private HttpHeaders buildHeaders(MediaType contentType) {
249+
HttpHeaders headers = new HttpHeaders();
250+
headers.setContentType(contentType);
251+
headers.set(CloudEventMessageUtils.CE_ID, UUID.randomUUID().toString());
252+
headers.set(CloudEventMessageUtils.CE_SOURCE, "https://spring.io/");
253+
headers.set(CloudEventMessageUtils.CE_SPECVERSION, "1.0");
254+
headers.set(CloudEventMessageUtils.CE_TYPE, "org.springframework");
255+
return headers;
256+
}
203257

204258
@Configuration
205259
public static class FooBarConverterConfiguration {
@@ -270,18 +324,4 @@ protected Object convertToInternal(Object payload, @Nullable MessageHeaders head
270324
}
271325
}
272326

273-
private URI constructURI(String path) throws Exception {
274-
return new URI("http://localhost:" + System.getProperty("server.port") + path);
275-
}
276-
277-
private HttpHeaders buildHeaders(MediaType contentType) {
278-
HttpHeaders headers = new HttpHeaders();
279-
headers.setContentType(contentType);
280-
headers.set("id", UUID.randomUUID().toString());
281-
headers.set("source", "https://spring.io/");
282-
headers.set("specversion", "1.0");
283-
headers.set("type", "org.springframework");
284-
return headers;
285-
}
286-
287327
}

0 commit comments

Comments
 (0)