Search in sources :

Example 1 with TestPerson

use of org.springframework.integration.json.TestPerson in project spring-integration by spring-projects.

the class ParentContextTests method testSpelBeanReferencesInChildAndParent.

/**
 * Verifies that beans in hierarchical contexts get an evaluation context that has the proper
 * BeanResolver. Verifies that the two Foos in the parent context get an evaluation context
 * with the same bean resolver. Verifies that the one Foo in the child context gets a different
 * bean resolver. Verifies that bean references in SpEL expressions to beans in the child
 * and parent contexts work. Verifies that PropertyAccessors are inherited in the child context
 * 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());
    Object parentEvaluationContextFactoryBean = parent.getBean(IntegrationEvaluationContextFactoryBean.class);
    Map<?, ?> parentFunctions = TestUtils.getPropertyValue(parentEvaluationContextFactoryBean, "functions", Map.class);
    assertEquals(4, parentFunctions.size());
    Object jsonPath = parentFunctions.get("jsonPath");
    assertNotNull(jsonPath);
    assertThat((Method) jsonPath, Matchers.isOneOf(JsonPathUtils.class.getMethods()));
    assertEquals(2, evalContexts.size());
    ClassPathXmlApplicationContext child = new ClassPathXmlApplicationContext(parent);
    child.setConfigLocation("org/springframework/integration/expression/ChildContext-context.xml");
    child.refresh();
    Object childEvaluationContextFactoryBean = child.getBean(IntegrationEvaluationContextFactoryBean.class);
    Map<?, ?> childFunctions = TestUtils.getPropertyValue(childEvaluationContextFactoryBean, "functions", Map.class);
    assertEquals(5, childFunctions.size());
    assertTrue(childFunctions.containsKey("barParent"));
    assertTrue(childFunctions.containsKey("fooFunc"));
    jsonPath = childFunctions.get("jsonPath");
    assertNotNull(jsonPath);
    assertThat((Method) jsonPath, Matchers.not(Matchers.isOneOf(JsonPathUtils.class.getMethods())));
    assertEquals(3, evalContexts.size());
    assertSame(evalContexts.get(0).getBeanResolver(), evalContexts.get(1).getBeanResolver());
    List<PropertyAccessor> propertyAccessors = evalContexts.get(0).getPropertyAccessors();
    assertEquals(4, propertyAccessors.size());
    PropertyAccessor parentPropertyAccessorOverride = parent.getBean("jsonPropertyAccessor", PropertyAccessor.class);
    PropertyAccessor parentPropertyAccessor = parent.getBean("parentJsonPropertyAccessor", PropertyAccessor.class);
    assertTrue(propertyAccessors.contains(parentPropertyAccessorOverride));
    assertTrue(propertyAccessors.contains(parentPropertyAccessor));
    assertTrue(propertyAccessors.indexOf(parentPropertyAccessorOverride) > propertyAccessors.indexOf(parentPropertyAccessor));
    Map<String, Object> variables = (Map<String, Object>) TestUtils.getPropertyValue(evalContexts.get(0), "variables");
    assertEquals(4, variables.size());
    assertTrue(variables.containsKey("bar"));
    assertTrue(variables.containsKey("barParent"));
    assertTrue(variables.containsKey("fooFunc"));
    assertTrue(variables.containsKey("jsonPath"));
    assertNotSame(evalContexts.get(1).getBeanResolver(), evalContexts.get(2).getBeanResolver());
    propertyAccessors = evalContexts.get(1).getPropertyAccessors();
    assertEquals(4, propertyAccessors.size());
    assertTrue(propertyAccessors.contains(parentPropertyAccessorOverride));
    variables = (Map<String, Object>) TestUtils.getPropertyValue(evalContexts.get(1), "variables");
    assertEquals(4, variables.size());
    assertTrue(variables.containsKey("bar"));
    assertTrue(variables.containsKey("barParent"));
    assertTrue(variables.containsKey("fooFunc"));
    assertTrue(variables.containsKey("jsonPath"));
    propertyAccessors = evalContexts.get(2).getPropertyAccessors();
    assertEquals(4, propertyAccessors.size());
    PropertyAccessor childPropertyAccessor = child.getBean("jsonPropertyAccessor", PropertyAccessor.class);
    assertTrue(propertyAccessors.contains(childPropertyAccessor));
    assertTrue(propertyAccessors.contains(parentPropertyAccessor));
    assertFalse(propertyAccessors.contains(parentPropertyAccessorOverride));
    assertTrue(propertyAccessors.indexOf(childPropertyAccessor) < propertyAccessors.indexOf(parentPropertyAccessor));
    variables = (Map<String, Object>) TestUtils.getPropertyValue(evalContexts.get(2), "variables");
    assertEquals(5, variables.size());
    assertTrue(variables.containsKey("bar"));
    assertTrue(variables.containsKey("barParent"));
    assertTrue(variables.containsKey("fooFunc"));
    assertTrue(variables.containsKey("barChild"));
    assertTrue(variables.containsKey("jsonPath"));
    // Test transformer expressions
    child.getBean("input", MessageChannel.class).send(new GenericMessage<String>("baz"));
    Message<?> out = child.getBean("output", QueueChannel.class).receive(0);
    assertNotNull(out);
    assertEquals("foobar", out.getPayload());
    child.getBean("parentIn", MessageChannel.class).send(MutableMessageBuilder.withPayload("bar").build());
    out = child.getBean("parentOut", QueueChannel.class).receive(0);
    assertNotNull(out);
    assertThat(out, instanceOf(GenericMessage.class));
    assertEquals("foo", out.getPayload());
    IntegrationEvaluationContextFactoryBean evaluationContextFactoryBean = child.getBean("&" + IntegrationContextUtils.INTEGRATION_EVALUATION_CONTEXT_BEAN_NAME, IntegrationEvaluationContextFactoryBean.class);
    try {
        evaluationContextFactoryBean.setPropertyAccessors(Collections.<String, PropertyAccessor>emptyMap());
        fail("IllegalArgumentException expected.");
    } catch (Exception e) {
        assertThat(e, Matchers.instanceOf(IllegalArgumentException.class));
    }
    parent.getBean("fromParentToChild", MessageChannel.class).send(new GenericMessage<String>("foo"));
    out = child.getBean("output", QueueChannel.class).receive(0);
    assertNotNull(out);
    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();
}
Also used : PropertyAccessor(org.springframework.expression.PropertyAccessor) QueueChannel(org.springframework.integration.channel.QueueChannel) JsonPathUtils(org.springframework.integration.json.JsonPathUtils) BeansException(org.springframework.beans.BeansException) GenericMessage(org.springframework.messaging.support.GenericMessage) AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) MessageChannel(org.springframework.messaging.MessageChannel) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) PollableChannel(org.springframework.messaging.PollableChannel) TestPerson(org.springframework.integration.json.TestPerson) Map(java.util.Map) IntegrationEvaluationContextFactoryBean(org.springframework.integration.config.IntegrationEvaluationContextFactoryBean) Test(org.junit.Test)

Aggregations

Map (java.util.Map)1 Test (org.junit.Test)1 BeansException (org.springframework.beans.BeansException)1 AbstractApplicationContext (org.springframework.context.support.AbstractApplicationContext)1 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)1 PropertyAccessor (org.springframework.expression.PropertyAccessor)1 QueueChannel (org.springframework.integration.channel.QueueChannel)1 IntegrationEvaluationContextFactoryBean (org.springframework.integration.config.IntegrationEvaluationContextFactoryBean)1 JsonPathUtils (org.springframework.integration.json.JsonPathUtils)1 TestPerson (org.springframework.integration.json.TestPerson)1 MessageChannel (org.springframework.messaging.MessageChannel)1 PollableChannel (org.springframework.messaging.PollableChannel)1 GenericMessage (org.springframework.messaging.support.GenericMessage)1