Skip to content

Commit

Permalink
#113 Add formatting message interceptors
Browse files Browse the repository at this point in the history
  • Loading branch information
christophd committed Nov 4, 2016
1 parent 4858af9 commit d303c50
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 3 deletions.
@@ -0,0 +1,40 @@
/*
* Copyright 2006-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.consol.citrus.xml;

import com.consol.citrus.context.TestContext;
import com.consol.citrus.message.Message;
import com.consol.citrus.util.XMLUtils;
import com.consol.citrus.validation.interceptor.AbstractMessageConstructionInterceptor;

/**
* @author Christoph Deppisch
* @since 2.6.2
*/
public class XmlFormattingMessageInterceptor extends AbstractMessageConstructionInterceptor {

@Override
public Message interceptMessage(Message message, String messageType, TestContext context) {
message.setPayload(XMLUtils.prettyPrint(message.getPayload(String.class)));
return message;
}

@Override
public boolean supportsMessageType(String messageType) {
return messageType.equalsIgnoreCase("xml");
}
}
@@ -0,0 +1,52 @@
/*
* Copyright 2006-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.consol.citrus.xml;

import com.consol.citrus.message.*;
import com.consol.citrus.testng.AbstractTestNGUnitTest;
import org.testng.Assert;
import org.testng.annotations.Test;

/**
* @author Christoph Deppisch
* @since 2.6.2
*/
public class XmlFormattingMessageInterceptorTest extends AbstractTestNGUnitTest {

private XmlFormattingMessageInterceptor messageInterceptor = new XmlFormattingMessageInterceptor();

@Test
public void testInterceptMessage() throws Exception {
Message message = new DefaultMessage("<root>"
+ "<element attribute='attribute-value'>"
+ "<sub-element>text-value</sub-element>"
+ "</element>"
+ "</root>");

messageInterceptor.interceptMessageConstruction(message, MessageType.XML.name(), context);

Assert.assertTrue(message.getPayload(String.class).contains(System.lineSeparator()));
}

@Test
public void testInterceptNonXmlMessage() throws Exception {
Message message = new DefaultMessage("This is plaintext");
messageInterceptor.interceptMessageConstruction(message, MessageType.PLAINTEXT.name(), context);
Assert.assertEquals(message.getPayload(String.class), "This is plaintext");
}

}
Expand Up @@ -17,6 +17,7 @@
package com.consol.citrus.ws.message;

import com.consol.citrus.exceptions.CitrusRuntimeException;
import com.consol.citrus.message.Message;
import org.springframework.beans.propertyeditors.LocaleEditor;
import org.springframework.util.StringUtils;
import org.springframework.ws.soap.SoapFaultDetailElement;
Expand Down Expand Up @@ -53,6 +54,29 @@ public class SoapFault extends SoapMessage {
/** List of fault detail elements */
private List<String> faultDetails = new ArrayList<String>();

/**
* Default constructor.
*/
public SoapFault() {
super();
}

/**
* Default constructor using parent message.
* @param message
*/
public SoapFault(Message message) {
super(message);
}

/**
* Default constructor using payload.
* @param payload
*/
public SoapFault(Object payload) {
super(payload);
}

/**
* Returns fault code as qualified name.
* @return
Expand Down
@@ -0,0 +1,48 @@
/*
* Copyright 2006-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.consol.citrus.ws.message;

import com.consol.citrus.context.TestContext;
import com.consol.citrus.message.Message;
import com.consol.citrus.util.XMLUtils;
import com.consol.citrus.xml.XmlFormattingMessageInterceptor;

import java.util.ArrayList;
import java.util.List;

/**
* @author Christoph Deppisch
* @since 2.6.2
*/
public class SoapFormattingMessageInterceptor extends XmlFormattingMessageInterceptor {

@Override
public Message interceptMessage(Message message, String messageType, TestContext context) {
if (message instanceof SoapFault) {
List<String> faultDetailsFormat = new ArrayList<>();
for (String faultDetail : ((SoapFault) message).getFaultDetails()) {
faultDetailsFormat.add(XMLUtils.prettyPrint(faultDetail));
}

if (faultDetailsFormat.size() > 0) {
((SoapFault) message).faultDetails(faultDetailsFormat);
}
}

return super.interceptMessage(message, messageType, context);
}
}
@@ -0,0 +1,69 @@
/*
* Copyright 2006-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.consol.citrus.ws.message;

import com.consol.citrus.message.*;
import com.consol.citrus.testng.AbstractTestNGUnitTest;
import org.testng.Assert;
import org.testng.annotations.Test;

/**
* @author Christoph Deppisch
* @since 2.6
*/
public class SoapFormattingMessageInterceptorTest extends AbstractTestNGUnitTest {

private SoapFormattingMessageInterceptor messageInterceptor = new SoapFormattingMessageInterceptor();

@Test
public void testInterceptMessage() throws Exception {
SoapMessage message = new SoapMessage("<root>"
+ "<element attribute='attribute-value'>"
+ "<sub-element>text-value</sub-element>"
+ "</element>"
+ "</root>");

messageInterceptor.interceptMessageConstruction(message, MessageType.XML.name(), context);

Assert.assertTrue(message.getPayload(String.class).contains(System.lineSeparator()));
}

@Test
public void testInterceptSoapFault() throws Exception {
SoapFault message = new SoapFault("<root>"
+ "<element attribute='attribute-value'>"
+ "<sub-element>text-value</sub-element>"
+ "</element>"
+ "</root>");

message.addFaultDetail("<fault-detail><error>Something went wrong</error></fault-detail>");

messageInterceptor.interceptMessageConstruction(message, MessageType.XML.name(), context);

Assert.assertTrue(message.getPayload(String.class).contains(System.lineSeparator()));
Assert.assertEquals(message.getFaultDetails().size(), 1L);
Assert.assertTrue(message.getFaultDetails().get(0).contains(System.lineSeparator()));
}

@Test
public void testInterceptNonXmlMessage() throws Exception {
Message message = new DefaultMessage("This is plaintext");
messageInterceptor.interceptMessageConstruction(message, MessageType.PLAINTEXT.name(), context);
Assert.assertEquals(message.getPayload(String.class), "This is plaintext");
}

}
6 changes: 3 additions & 3 deletions src/manual/validation-xml.md
Expand Up @@ -133,13 +133,13 @@ a *XmlConfigurer* Spring bean to the Citrus application context.
<bean id="xmlConfigurer" class="com.consol.citrus.xml.XmlConfigurer">
<property name="parseSettings">
<map>
<entry key="validate-if-schema" value="false"/>
<entry key="validate-if-schema" value="false" value-type="java.lang.Boolean"/>
</map>
</property>
<property name="serializeSettings">
<map>
<entry key="comments" value="false"/>
<entry key="format-pretty-print" value="false"/>
<entry key="comments" value="false" value-type="java.lang.Boolean"/>
<entry key="format-pretty-print" value="false" value-type="java.lang.Boolean"/>
</map>
</property>
</bean>
Expand Down

0 comments on commit d303c50

Please sign in to comment.