Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#113 Add formatting message interceptors
- Loading branch information
1 parent
4858af9
commit d303c50
Showing
6 changed files
with
236 additions
and
3 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
modules/citrus-core/src/main/java/com/consol/citrus/xml/XmlFormattingMessageInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
.../citrus-core/src/test/java/com/consol/citrus/xml/XmlFormattingMessageInterceptorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...itrus-ws/src/main/java/com/consol/citrus/ws/message/SoapFormattingMessageInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
...s-ws/src/test/java/com/consol/citrus/ws/message/SoapFormattingMessageInterceptorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters