Skip to content

Commit

Permalink
Fixed #239 Support header child elements in Java DSL
Browse files Browse the repository at this point in the history
  • Loading branch information
christophd committed Jun 30, 2017
1 parent bb27b36 commit 3623f48
Show file tree
Hide file tree
Showing 5 changed files with 403 additions and 1 deletion.
Expand Up @@ -69,6 +69,6 @@ public void plainTextValidation() {
.receive()
.response()
.messageType(MessageType.PLAINTEXT)
.header("HTTP/1.1"));
.version("HTTP/1.1"));
}
}
Expand Up @@ -318,6 +318,87 @@ public T header(String data) {
return self;
}

/**
* Expect this message header data as model object which is marshalled to a character sequence using the default object to xml mapper that
* is available in Spring bean application context.
*
* @param model
* @return
*/
public T headerFragment(Object model) {
Assert.notNull(applicationContext, "Citrus application context is not initialized!");

if (!CollectionUtils.isEmpty(applicationContext.getBeansOfType(Marshaller.class))) {
return headerFragment(model, applicationContext.getBean(Marshaller.class));
} else if (!CollectionUtils.isEmpty(applicationContext.getBeansOfType(ObjectMapper.class))) {
return headerFragment(model, applicationContext.getBean(ObjectMapper.class));
}

throw new CitrusRuntimeException("Unable to find default object mapper or marshaller in application context");
}

/**
* Expect this message header data as model object which is marshalled to a character sequence using the given object to xml mapper that
* is accessed by its bean name in Spring bean application context.
*
* @param model
* @param mapperName
* @return
*/
public T headerFragment(Object model, String mapperName) {
Assert.notNull(applicationContext, "Citrus application context is not initialized!");

if (applicationContext.containsBean(mapperName)) {
Object mapper = applicationContext.getBean(mapperName);

if (Marshaller.class.isAssignableFrom(mapper.getClass())) {
return headerFragment(model, (Marshaller) mapper);
} else if (ObjectMapper.class.isAssignableFrom(mapper.getClass())) {
return headerFragment(model, (ObjectMapper) mapper);
} else {
throw new CitrusRuntimeException(String.format("Invalid bean type for mapper '%s' expected ObjectMapper or Marshaller but was '%s'", mapperName, mapper.getClass()));
}
}

throw new CitrusRuntimeException("Unable to find default object mapper or marshaller in application context");
}

/**
* Expect this message header data as model object which is marshalled to a character sequence
* using the default object to xml mapper before validation is performed.
* @param model
* @param marshaller
* @return
*/
public T headerFragment(Object model, Marshaller marshaller) {
StringResult result = new StringResult();

try {
marshaller.marshal(model, result);
} catch (XmlMappingException e) {
throw new CitrusRuntimeException("Failed to marshal object graph for message header data", e);
} catch (IOException e) {
throw new CitrusRuntimeException("Failed to marshal object graph for message header data", e);
}

return header(result.toString());
}

/**
* Expect this message header data as model object which is mapped to a character sequence
* using the default object to json mapper before validation is performed.
* @param model
* @param objectMapper
* @return
*/
public T headerFragment(Object model, ObjectMapper objectMapper) {
try {
return header(objectMapper.writer().writeValueAsString(model));
} catch (JsonProcessingException e) {
throw new CitrusRuntimeException("Failed to map object graph for message header data", e);
}
}

/**
* Expect this message header data in received message from file resource. Message header data is used in
* SOAP messages as XML fragment for instance.
Expand Down
Expand Up @@ -325,6 +325,85 @@ public T header(Resource resource, Charset charset) {
return self;
}

/**
* Sets header data POJO object which is marshalled to a character sequence using the given object to xml mapper.
* @param model
* @param marshaller
* @return
*/
public T headerFragment(Object model, Marshaller marshaller) {
StringResult result = new StringResult();

try {
marshaller.marshal(model, result);
} catch (XmlMappingException e) {
throw new CitrusRuntimeException("Failed to marshal object graph for message header data", e);
} catch (IOException e) {
throw new CitrusRuntimeException("Failed to marshal object graph for message header data", e);
}

return header(result.toString());
}

/**
* Sets header data POJO object which is mapped to a character sequence using the given object to json mapper.
* @param model
* @param objectMapper
* @return
*/
public T headerFragment(Object model, ObjectMapper objectMapper) {
try {
return header(objectMapper.writer().writeValueAsString(model));
} catch (JsonProcessingException e) {
throw new CitrusRuntimeException("Failed to map object graph for message header data", e);
}
}

/**
* Sets header data POJO object which is marshalled to a character sequence using the default object to xml or object
* to json mapper that is available in Spring bean application context.
*
* @param model
* @return
*/
public T headerFragment(Object model) {
Assert.notNull(applicationContext, "Citrus application context is not initialized!");

if (!CollectionUtils.isEmpty(applicationContext.getBeansOfType(Marshaller.class))) {
return headerFragment(model, applicationContext.getBean(Marshaller.class));
} else if (!CollectionUtils.isEmpty(applicationContext.getBeansOfType(ObjectMapper.class))) {
return headerFragment(model, applicationContext.getBean(ObjectMapper.class));
}

throw new CitrusRuntimeException("Unable to find default object mapper or marshaller in application context");
}

/**
* Sets header data POJO object which is marshalled to a character sequence using the given object to xml mapper that
* is accessed by its bean name in Spring bean application context.
*
* @param model
* @param mapperName
* @return
*/
public T headerFragment(Object model, String mapperName) {
Assert.notNull(applicationContext, "Citrus application context is not initialized!");

if (applicationContext.containsBean(mapperName)) {
Object mapper = applicationContext.getBean(mapperName);

if (Marshaller.class.isAssignableFrom(mapper.getClass())) {
return headerFragment(model, (Marshaller) mapper);
} else if (ObjectMapper.class.isAssignableFrom(mapper.getClass())) {
return headerFragment(model, (ObjectMapper) mapper);
} else {
throw new CitrusRuntimeException(String.format("Invalid bean type for mapper '%s' expected ObjectMapper or Marshaller but was '%s'", mapperName, mapper.getClass()));
}
}

throw new CitrusRuntimeException("Unable to find default object mapper or marshaller in application context");
}

/**
* Sets a explicit message type for this receive action.
* @param messageType
Expand Down
Expand Up @@ -511,6 +511,116 @@ public void configure() {
Assert.assertEquals(((StaticMessageContentBuilder)action.getMessageBuilder()).getHeaderData().get(1), "<Header><Name>operation</Name><Value>foo2</Value></Header>");
Assert.assertEquals(((StaticMessageContentBuilder)action.getMessageBuilder()).getHeaderResources().size(), 0L);
}

@Test
public void testReceiveBuilderWithHeaderFragment() {
reset(applicationContextMock);
when(applicationContextMock.getBean(TestActionListeners.class)).thenReturn(new TestActionListeners());
when(applicationContextMock.getBeansOfType(SequenceBeforeTest.class)).thenReturn(new HashMap<String, SequenceBeforeTest>());
when(applicationContextMock.getBeansOfType(SequenceAfterTest.class)).thenReturn(new HashMap<String, SequenceAfterTest>());
when(applicationContextMock.getBeansOfType(Marshaller.class)).thenReturn(Collections.<String, Marshaller>singletonMap("marshaller", marshaller));
when(applicationContextMock.getBean(Marshaller.class)).thenReturn(marshaller);
MockTestDesigner builder = new MockTestDesigner(applicationContextMock, context) {
@Override
public void configure() {
receive(messageEndpoint)
.headerFragment(new TestRequest("Hello Citrus!"));
}
};

builder.configure();

TestCase test = builder.getTestCase();
Assert.assertEquals(test.getActionCount(), 1);
Assert.assertEquals(test.getActions().get(0).getClass(), DelegatingTestAction.class);
Assert.assertEquals(((DelegatingTestAction)test.getActions().get(0)).getDelegate().getClass(), ReceiveMessageAction.class);

ReceiveMessageAction action = (ReceiveMessageAction) ((DelegatingTestAction)test.getActions().get(0)).getDelegate();
Assert.assertEquals(action.getName(), "receive");

Assert.assertEquals(action.getMessageType(), MessageType.XML.name());
Assert.assertEquals(action.getEndpoint(), messageEndpoint);
Assert.assertEquals(action.getValidationContexts().size(), 3);
Assert.assertEquals(action.getValidationContexts().get(0).getClass(), DefaultValidationContext.class);
Assert.assertEquals(action.getValidationContexts().get(1).getClass(), XmlMessageValidationContext.class);
Assert.assertEquals(action.getValidationContexts().get(2).getClass(), JsonMessageValidationContext.class);

Assert.assertTrue(action.getMessageBuilder() instanceof PayloadTemplateMessageBuilder);
Assert.assertEquals(((PayloadTemplateMessageBuilder)action.getMessageBuilder()).getHeaderData().size(), 1L);
Assert.assertEquals(((PayloadTemplateMessageBuilder)action.getMessageBuilder()).getHeaderData().get(0), "<TestRequest><Message>Hello Citrus!</Message></TestRequest>");

}

@Test
public void testReceiveBuilderWithHeaderFragmentExplicitMarshaller() {
MockTestDesigner builder = new MockTestDesigner(applicationContext, context) {
@Override
public void configure() {
receive(messageEndpoint)
.headerFragment(new TestRequest("Hello Citrus!"), marshaller);
}
};

builder.configure();

TestCase test = builder.getTestCase();
Assert.assertEquals(test.getActionCount(), 1);
Assert.assertEquals(test.getActions().get(0).getClass(), DelegatingTestAction.class);
Assert.assertEquals(((DelegatingTestAction)test.getActions().get(0)).getDelegate().getClass(), ReceiveMessageAction.class);

ReceiveMessageAction action = (ReceiveMessageAction) ((DelegatingTestAction)test.getActions().get(0)).getDelegate();
Assert.assertEquals(action.getName(), "receive");

Assert.assertEquals(action.getMessageType(), MessageType.XML.name());
Assert.assertEquals(action.getEndpoint(), messageEndpoint);
Assert.assertEquals(action.getValidationContexts().size(), 3);
Assert.assertEquals(action.getValidationContexts().get(0).getClass(), DefaultValidationContext.class);
Assert.assertEquals(action.getValidationContexts().get(1).getClass(), XmlMessageValidationContext.class);
Assert.assertEquals(action.getValidationContexts().get(2).getClass(), JsonMessageValidationContext.class);

Assert.assertTrue(action.getMessageBuilder() instanceof PayloadTemplateMessageBuilder);
Assert.assertEquals(((PayloadTemplateMessageBuilder)action.getMessageBuilder()).getHeaderData().size(), 1L);
Assert.assertEquals(((PayloadTemplateMessageBuilder)action.getMessageBuilder()).getHeaderData().get(0), "<TestRequest><Message>Hello Citrus!</Message></TestRequest>");
}

@Test
public void testReceiveBuilderWithHeaderFragmentExplicitMarshallerName() {
reset(applicationContextMock);
when(applicationContextMock.getBean(TestActionListeners.class)).thenReturn(new TestActionListeners());
when(applicationContextMock.getBeansOfType(SequenceBeforeTest.class)).thenReturn(new HashMap<String, SequenceBeforeTest>());
when(applicationContextMock.getBeansOfType(SequenceAfterTest.class)).thenReturn(new HashMap<String, SequenceAfterTest>());
when(applicationContextMock.containsBean("myMarshaller")).thenReturn(true);
when(applicationContextMock.getBean("myMarshaller")).thenReturn(marshaller);
MockTestDesigner builder = new MockTestDesigner(applicationContextMock, context) {
@Override
public void configure() {
receive(messageEndpoint)
.headerFragment(new TestRequest("Hello Citrus!"), "myMarshaller");
}
};

builder.configure();

TestCase test = builder.getTestCase();
Assert.assertEquals(test.getActionCount(), 1);
Assert.assertEquals(test.getActions().get(0).getClass(), DelegatingTestAction.class);
Assert.assertEquals(((DelegatingTestAction)test.getActions().get(0)).getDelegate().getClass(), ReceiveMessageAction.class);

ReceiveMessageAction action = (ReceiveMessageAction) ((DelegatingTestAction)test.getActions().get(0)).getDelegate();
Assert.assertEquals(action.getName(), "receive");

Assert.assertEquals(action.getMessageType(), MessageType.XML.name());
Assert.assertEquals(action.getEndpoint(), messageEndpoint);
Assert.assertEquals(action.getValidationContexts().size(), 3);
Assert.assertEquals(action.getValidationContexts().get(0).getClass(), DefaultValidationContext.class);
Assert.assertEquals(action.getValidationContexts().get(1).getClass(), XmlMessageValidationContext.class);
Assert.assertEquals(action.getValidationContexts().get(2).getClass(), JsonMessageValidationContext.class);

Assert.assertTrue(action.getMessageBuilder() instanceof PayloadTemplateMessageBuilder);
Assert.assertEquals(((PayloadTemplateMessageBuilder)action.getMessageBuilder()).getHeaderData().size(), 1L);
Assert.assertEquals(((PayloadTemplateMessageBuilder)action.getMessageBuilder()).getHeaderData().get(0), "<TestRequest><Message>Hello Citrus!</Message></TestRequest>");

}

@Test
public void testReceiveBuilderWithHeaderResource() throws IOException {
Expand Down

0 comments on commit 3623f48

Please sign in to comment.