Search in sources :

Example 11 with PersonType

use of org.apache.camel.foo.bar.PersonType in project camel by apache.

the class CamelJaxbNoNamespaceSchemaTest method testMarshalWithNoNamespaceSchemaLocation.

@Test
public void testMarshalWithNoNamespaceSchemaLocation() throws Exception {
    PersonType person = new PersonType();
    person.setFirstName("foo");
    person.setLastName("bar");
    MockEndpoint resultEndpoint = resolveMandatoryEndpoint("mock:result", MockEndpoint.class);
    resultEndpoint.expectedMessageCount(1);
    template.sendBody("direct:marshal", person);
    resultEndpoint.assertIsSatisfied();
    String body = resultEndpoint.getReceivedExchanges().get(0).getIn().getBody(String.class);
    assertTrue("We should get the noNamespaceSchemaLocation here", body.contains("noNamespaceSchemaLocation=\"person-no-namespace.xsd\""));
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) PersonType(org.apache.camel.foo.bar.PersonType) Test(org.junit.Test)

Example 12 with PersonType

use of org.apache.camel.foo.bar.PersonType in project camel by apache.

the class CamelJaxbTest method testUnmarshal.

@Test
public void testUnmarshal() throws Exception {
    final String xml = "<Person><firstName>FOO</firstName><lastName>BAR</lastName></Person>";
    PersonType expected = new PersonType();
    expected.setFirstName("FOO");
    expected.setLastName("BAR");
    MockEndpoint resultEndpoint = resolveMandatoryEndpoint("mock:result", MockEndpoint.class);
    resultEndpoint.expectedBodiesReceived(expected);
    resultEndpoint.expectedHeaderReceived("foo", "bar");
    template.sendBodyAndHeader("direct:getJAXBElementValue", xml, "foo", "bar");
    resultEndpoint.assertIsSatisfied();
    resultEndpoint.reset();
    resultEndpoint.expectedMessageCount(1);
    template.sendBody("direct:getJAXBElement", xml);
    resultEndpoint.assertIsSatisfied();
    assertTrue("We should get the JAXBElement here", resultEndpoint.getExchanges().get(0).getIn().getBody() instanceof JAXBElement);
    resultEndpoint.reset();
    resultEndpoint.expectedMessageCount(1);
    resultEndpoint.expectedBodiesReceived(expected);
    template.sendBody("direct:unmarshall", xml);
    resultEndpoint.assertIsSatisfied();
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) PersonType(org.apache.camel.foo.bar.PersonType) JAXBElement(javax.xml.bind.JAXBElement) Test(org.junit.Test)

Example 13 with PersonType

use of org.apache.camel.foo.bar.PersonType in project camel by apache.

the class CamelJaxbTest method testMarshalBadCharsNoFiltering.

@Test
public void testMarshalBadCharsNoFiltering() throws Exception {
    PersonType person = new PersonType();
    person.setFirstName("foo");
    person.setLastName("bar");
    MockEndpoint resultEndpoint = resolveMandatoryEndpoint("mock:result", MockEndpoint.class);
    resultEndpoint.expectedMessageCount(1);
    resultEndpoint.expectedHeaderReceived(Exchange.CONTENT_TYPE, "application/xml");
    template.sendBody("direct:marshal", person);
    resultEndpoint.assertIsSatisfied();
    String body = resultEndpoint.getReceivedExchanges().get(0).getIn().getBody(String.class);
    assertTrue("Non-xml character unexpectedly did not get into marshalled contents", body.contains(""));
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) PersonType(org.apache.camel.foo.bar.PersonType) Test(org.junit.Test)

Example 14 with PersonType

use of org.apache.camel.foo.bar.PersonType in project camel by apache.

the class CamelJaxbFallbackConverterTest method testConverter.

@Test
public void testConverter() throws Exception {
    TypeConverter converter = context.getTypeConverter();
    PersonType person = converter.convertTo(PersonType.class, "<Person><firstName>FOO</firstName><lastName>BAR</lastName></Person>");
    assertNotNull("Person should not be null ", person);
    assertEquals("Get the wrong first name ", "FOO", person.getFirstName());
    assertEquals("Get the wrong second name ", "BAR", person.getLastName());
    Exchange exchange = new DefaultExchange(context);
    exchange.setProperty(Exchange.CHARSET_NAME, "UTF-8");
    String value = converter.convertTo(String.class, exchange, person);
    assertTrue("Should get a right marshalled string", value.indexOf("<lastName>BAR</lastName>") > 0);
    byte[] buffers = "<Person><firstName>FOO</firstName><lastName>BAR</lastName></Person>".getBytes("UTF-8");
    InputStream is = new ByteArrayInputStream(buffers);
    try {
        converter.convertTo(PersonType.class, exchange, is);
        fail("Should have thrown exception");
    } catch (TypeConversionException e) {
    // expected
    }
}
Also used : TypeConverter(org.apache.camel.TypeConverter) DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) ByteArrayInputStream(java.io.ByteArrayInputStream) TypeConversionException(org.apache.camel.TypeConversionException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PersonType(org.apache.camel.foo.bar.PersonType) Test(org.junit.Test)

Aggregations

PersonType (org.apache.camel.foo.bar.PersonType)14 Test (org.junit.Test)12 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)10 Exchange (org.apache.camel.Exchange)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 TypeConverter (org.apache.camel.TypeConverter)2 DefaultExchange (org.apache.camel.impl.DefaultExchange)2 JAXBElement (javax.xml.bind.JAXBElement)1 TypeConversionException (org.apache.camel.TypeConversionException)1