INT-3427: Add JsonNodeToStringConverter

JIRA: https://jira.spring.io/browse/INT-3427

INT-3427: Workaround for String -> File converter

INT-3427: PR comments
This commit is contained in:
Artem Bilan
2014-07-04 18:33:50 +03:00
committed by Gary Russell
parent f0ab3b03bd
commit 85866a2ea8
8 changed files with 201 additions and 54 deletions

View File

@@ -34,4 +34,17 @@
<bean id="parentJsonPropertyAccessor" class="org.springframework.integration.json.JsonPropertyAccessor"/>
<bean id="barService" class="org.springframework.integration.expression.ParentContextTests$Bar" />
<int:chain input-channel="testJsonNodeToStringConverterInputChannel"
output-channel="testJsonNodeToStringConverterOutputChannel">
<int:object-to-json-transformer result-type="NODE"/>
<int:transformer expression="payload.firstName"/>
<int:service-activator ref="barService" method="testJsonNodeToStringConverter"/>
</int:chain>
<int:channel id="testJsonNodeToStringConverterOutputChannel">
<int:queue/>
</int:channel>
</beans>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013 the original author or authors.
* Copyright 2013-2014 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.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.expression;
import static org.hamcrest.Matchers.instanceOf;
@@ -30,6 +31,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.hamcrest.Matchers;
import org.junit.Test;
@@ -42,10 +44,12 @@ import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.config.IntegrationEvaluationContextFactoryBean;
import org.springframework.integration.context.IntegrationContextUtils;
import org.springframework.integration.json.JsonPathUtils;
import org.springframework.integration.json.TestPerson;
import org.springframework.integration.support.MutableMessageBuilder;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.PollableChannel;
import org.springframework.messaging.support.GenericMessage;
/**
@@ -66,14 +70,19 @@ public class ParentContextTests {
* and the parent's ones are last in the propertyAccessors list of EvaluationContext.
* Verifies that SpEL functions are inherited from parent context and overridden with the same 'id'.
* Verifies that child and parent contexts can have different message builders.
* <p>
* Only single test method is allowed for 'ParentContext-context.xml',
* since it relies on static 'evalContexts' variable.
*/
@Test
@SuppressWarnings("unchecked")
public void testSpelBeanReferencesInChildAndParent() throws Exception {
AbstractApplicationContext parent = new ClassPathXmlApplicationContext("ParentContext-context.xml", this.getClass());
AbstractApplicationContext parent = new ClassPathXmlApplicationContext("ParentContext-context.xml",
this.getClass());
Object parentEvaluationContextFactoryBean = parent.getBean(IntegrationEvaluationContextFactoryBean.class);
Map<?, ?> parentFunctions = TestUtils.getPropertyValue(parentEvaluationContextFactoryBean, "functions", Map.class);
Map<?, ?> parentFunctions = TestUtils.getPropertyValue(parentEvaluationContextFactoryBean, "functions",
Map.class);
assertEquals(3, parentFunctions.size());
Object jsonPath = parentFunctions.get("jsonPath");
assertNotNull(jsonPath);
@@ -84,7 +93,8 @@ public class ParentContextTests {
child.refresh();
Object childEvaluationContextFactoryBean = child.getBean(IntegrationEvaluationContextFactoryBean.class);
Map<?, ?> childFunctions = TestUtils.getPropertyValue(childEvaluationContextFactoryBean, "functions", Map.class);
Map<?, ?> childFunctions = TestUtils.getPropertyValue(childEvaluationContextFactoryBean, "functions",
Map.class);
assertEquals(4, childFunctions.size());
assertTrue(childFunctions.containsKey("barParent"));
jsonPath = childFunctions.get("jsonPath");
@@ -101,7 +111,8 @@ public class ParentContextTests {
assertTrue(propertyAccessors.contains(parentPropertyAccessor));
assertTrue(propertyAccessors.indexOf(parentPropertyAccessorOverride) > propertyAccessors.indexOf(parentPropertyAccessor));
Map<String, Object> variables = (Map<String, Object>) TestUtils.getPropertyValue(evalContexts.get(0), "variables");
Map<String, Object> variables = (Map<String, Object>) TestUtils.getPropertyValue(evalContexts.get(0),
"variables");
assertEquals(3, variables.size());
assertTrue(variables.containsKey("bar"));
assertTrue(variables.containsKey("barParent"));
@@ -161,6 +172,37 @@ public class ParentContextTests {
assertEquals("org.springframework.integration.support.MutableMessage", out.getClass().getName());
assertEquals("FOO", out.getPayload());
assertTrue(parent
.containsBean(IntegrationContextUtils.TO_STRING_FRIENDLY_JSON_NODE_TO_STRING_CONVERTER_BEAN_NAME));
assertTrue(child
.containsBean(IntegrationContextUtils.TO_STRING_FRIENDLY_JSON_NODE_TO_STRING_CONVERTER_BEAN_NAME));
Object converterRegistrar = parent.getBean(IntegrationContextUtils.CONVERTER_REGISTRAR_BEAN_NAME);
assertNotNull(converterRegistrar);
Set<?> converters = TestUtils.getPropertyValue(converterRegistrar, "converters", Set.class);
boolean toStringFriendlyJsonNodeToStringConverterPresent = false;
for (Object converter : converters) {
if ("ToStringFriendlyJsonNodeToStringConverter".equals(converter.getClass().getSimpleName())) {
toStringFriendlyJsonNodeToStringConverterPresent = true;
break;
}
}
assertTrue(toStringFriendlyJsonNodeToStringConverterPresent);
MessageChannel input = parent.getBean("testJsonNodeToStringConverterInputChannel", MessageChannel.class);
PollableChannel output = parent.getBean("testJsonNodeToStringConverterOutputChannel", PollableChannel.class);
TestPerson person = new TestPerson();
person.setFirstName("John");
input.send(new GenericMessage<Object>(person));
Message<?> result = output.receive(1000);
assertNotNull(result);
assertEquals("JOHN", result.getPayload());
child.close();
parent.close();
}
@@ -180,5 +222,10 @@ public class ParentContextTests {
return o;
}
public String testJsonNodeToStringConverter(String payload) {
return payload.toUpperCase();
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013 the original author or authors.
* Copyright 2013-2014 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.
@@ -20,7 +20,7 @@ package org.springframework.integration.json;
* @author Mark Fisher
* @since 2.0
*/
class TestPerson {
public class TestPerson {
private volatile String firstName;
@@ -30,7 +30,7 @@ class TestPerson {
private volatile TestAddress address;
TestPerson() {
public TestPerson() {
}
public TestPerson(String firstName, String lastName, int age) {