Add PojoToPojo and MapToMap tests

Related to GH-422 and GH-606
This commit is contained in:
Oleg Zhurakousky
2020-11-13 18:44:01 +01:00
parent 7ddbbe52cd
commit 24b0aec2b2
3 changed files with 63 additions and 43 deletions

View File

@@ -16,6 +16,8 @@
package io.spring.cloudevent;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Map;
import java.util.UUID;
@@ -102,19 +104,9 @@ public class CloudeventDemoApplication {
};
}
@Bean
public Function<Message<SpringReleaseEvent>, SpringReleaseEvent> consumeAndProduceCloudEventPojo() {
return ceMessage -> {
SpringReleaseEvent data = ceMessage.getPayload();
data.setVersion("2.0");
data.setReleaseDateAsString("01-10-2006");
return data;
};
}
@Bean
public Function<Map<String, Object>, Map<String, Object>> consumeAndProduceCloudEventAsPojoToPojo() {
public Function<Map<String, Object>, Map<String, Object>> consumeAndProduceCloudEventAsMapToMap() {
return ceMessage -> {
ceMessage.put("version", "10.0");
ceMessage.put("releaseDate", "01-10-2050");
@@ -123,22 +115,10 @@ public class CloudeventDemoApplication {
}
@Bean
public CloudEventAttributesProvider cloudEventAttributesProvider() {
return new CustomCloudEventAtttributesProvider();
}
public static class CustomCloudEventAtttributesProvider extends DefaultCloudEventAttributesProvider {
@Override
public Map<String, Object> generateDefaultCloudEventHeaders(Message<?> inputMessage, Object result) {
if (inputMessage.getHeaders().containsKey(CloudEventMessageUtils.CE_ID)) { // input is a cloud event
String applicationName = "http://spring.io/fooBar";
return this.get(inputMessage.getHeaders())
.setId(UUID.randomUUID().toString())
.setType(result.getClass().getName())
.setSource(applicationName);
}
return Collections.emptyMap();
}
public Function<SpringReleaseEvent, SpringReleaseEvent> consumeAndProduceCloudEventAsPojoToPojo() {
return event -> {
event.setVersion("2.0");
return event;
};
}
}

View File

@@ -99,7 +99,7 @@ public class CloudeventDemoApplicationFunctionTests {
* is (see `asPOJOMessage` and `asPOJO` specifically). Type conversion will happen
* inside spring-cloud-function.
*/
Function<Message<String>, Message<String>> asPojoMessage = catalog.lookup("consumeAndProduceCloudEventPojo");
Function<Message<String>, Message<String>> asPojoMessage = catalog.lookup("consumeAndProduceCloudEventAsPojoToPojo");
System.out.println(asPojoMessage.apply(binaryCloudEventMessage));
}
}

View File

@@ -20,6 +20,9 @@ import static org.assertj.core.api.Assertions.assertThat;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;
import org.junit.jupiter.api.BeforeEach;
@@ -27,7 +30,10 @@ import org.junit.jupiter.api.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.cloud.function.cloudevent.CloudEventAttributesProvider;
import org.springframework.cloud.function.cloudevent.CloudEventJsonMessageConverter;
import org.springframework.cloud.function.cloudevent.CloudEventMessageUtils;
import org.springframework.cloud.function.cloudevent.DefaultCloudEventAttributesProvider;
import org.springframework.cloud.function.json.JsonMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -42,6 +48,7 @@ import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.converter.AbstractMessageConverter;
import org.springframework.messaging.converter.MessageConverter;
import org.springframework.util.MimeType;
import org.springframework.util.MimeTypeUtils;
import org.springframework.util.SocketUtils;
/**
@@ -200,6 +207,53 @@ public class CloudeventDemoApplicationRESTTests {
assertThat(response.getBody()).isEqualTo("{\"version\":\"1.0\",\"releaseName\":\"Spring Framework\",\"releaseDate\":\"24-03-2004\"}");
}
@Test
public void testAsBinaryMapToMap() throws Exception {
SpringApplication.run(new Class[] {CloudeventDemoApplication.class}, new String[] {});
HttpHeaders headers = this.buildHeaders(MediaType.APPLICATION_JSON);
String payload = "{\"releaseDate\":\"24-03-2004\", \"releaseName\":\"Spring Framework\", \"version\":\"1.0\"}";
RequestEntity<String> re = new RequestEntity<>(payload, headers, HttpMethod.POST, this.constructURI("/consumeAndProduceCloudEventAsMapToMap"));
ResponseEntity<String> response = testRestTemplate.exchange(re, String.class);
assertThat(response.getBody()).isEqualTo("{\"releaseDate\":\"01-10-2050\",\"releaseName\":\"Spring Framework\",\"version\":\"10.0\"}");
assertThat(response.getHeaders().get(CloudEventMessageUtils.CE_SOURCE))
.isEqualTo(Collections.singletonList("http://spring.io/application-application"));
assertThat(response.getHeaders().get(CloudEventMessageUtils.CE_TYPE))
.isEqualTo(Collections.singletonList(LinkedHashMap.class.getName()));
}
@Test
public void testAsBinaryPojoToPojo() throws Exception {
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\"}";
RequestEntity<String> re = new RequestEntity<>(payload, headers, HttpMethod.POST, this.constructURI("/consumeAndProduceCloudEventAsPojoToPojo"));
ResponseEntity<String> response = testRestTemplate.exchange(re, String.class);
assertThat(response.getBody()).isEqualTo("{\"releaseDate\":\"01-10-2006\",\"releaseName\":\"Spring Framework\",\"version\":\"2.0\"}");
assertThat(response.getHeaders().get(CloudEventMessageUtils.CE_SOURCE))
.isEqualTo(Collections.singletonList("http://spring.io/application-application"));
assertThat(response.getHeaders().get(CloudEventMessageUtils.CE_TYPE))
.isEqualTo(Collections.singletonList(SpringReleaseEvent.class.getName()));
}
private URI constructURI(String path) throws Exception {
return new URI("http://localhost:" + System.getProperty("server.port") + path);
}
private HttpHeaders buildHeaders(MediaType contentType) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(contentType);
headers.set(CloudEventMessageUtils.CE_ID, UUID.randomUUID().toString());
headers.set(CloudEventMessageUtils.CE_SOURCE, "https://spring.io/");
headers.set(CloudEventMessageUtils.CE_SPECVERSION, "1.0");
headers.set(CloudEventMessageUtils.CE_TYPE, "org.springframework");
return headers;
}
@Configuration
public static class FooBarConverterConfiguration {
@@ -270,18 +324,4 @@ public class CloudeventDemoApplicationRESTTests {
}
}
private URI constructURI(String path) throws Exception {
return new URI("http://localhost:" + System.getProperty("server.port") + path);
}
private HttpHeaders buildHeaders(MediaType contentType) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(contentType);
headers.set("id", UUID.randomUUID().toString());
headers.set("source", "https://spring.io/");
headers.set("specversion", "1.0");
headers.set("type", "org.springframework");
return headers;
}
}