/*
* Copyright 2006-2010 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.validation.xhtml;
import com.consol.citrus.actions.ReceiveMessageAction;
import com.consol.citrus.context.TestContext;
import com.consol.citrus.endpoint.Endpoint;
import com.consol.citrus.endpoint.EndpointConfiguration;
import com.consol.citrus.message.DefaultMessage;
import com.consol.citrus.message.Message;
import com.consol.citrus.messaging.Consumer;
import com.consol.citrus.testng.AbstractTestNGUnitTest;
import com.consol.citrus.validation.builder.PayloadTemplateMessageBuilder;
import com.consol.citrus.validation.context.ValidationContext;
import com.consol.citrus.validation.xml.XmlMessageValidationContext;
import org.easymock.EasyMock;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.ArrayList;
import java.util.List;
import static org.easymock.EasyMock.*;
/**
* @author Christoph Deppisch
*/
public class XhtmlMessageValidatorTest extends AbstractTestNGUnitTest {
private Endpoint endpoint = EasyMock.createMock(Endpoint.class);
private Consumer consumer = EasyMock.createMock(Consumer.class);
private EndpointConfiguration endpointConfiguration = EasyMock.createMock(EndpointConfiguration.class);
private ReceiveMessageAction receiveMessageBean;
private XhtmlMessageValidator xhtmlMessageValidator = new XhtmlMessageValidator();
@Override
@BeforeMethod
public void prepareTest() {
super.prepareTest();
receiveMessageBean = new ReceiveMessageAction();
receiveMessageBean.setEndpoint(endpoint);
try {
xhtmlMessageValidator.afterPropertiesSet();
} catch (Exception e) {
throw new RuntimeException(e);
}
receiveMessageBean.setValidator(xhtmlMessageValidator);
}
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void testXhtmlConversion() throws Exception {
reset(endpoint, consumer, endpointConfiguration);
expect(endpoint.createConsumer()).andReturn(consumer).anyTimes();
expect(endpoint.getEndpointConfiguration()).andReturn(endpointConfiguration).anyTimes();
expect(endpointConfiguration.getTimeout()).andReturn(5000L).anyTimes();
Message message = new DefaultMessage("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">"
+ "<html>"
+ "<body>"
+ "<p>Hello TestFramework!</p>"
+ "</body>"
+ "</html>");
expect(consumer.receive(anyObject(TestContext.class), anyLong())).andReturn(message).once();
expect(endpoint.getActor()).andReturn(null).anyTimes();
replay(endpoint, consumer, endpointConfiguration);
PayloadTemplateMessageBuilder controlMessageBuilder = new PayloadTemplateMessageBuilder();
XmlMessageValidationContext validationContext = new XmlMessageValidationContext();
validationContext.setMessageBuilder(controlMessageBuilder);
controlMessageBuilder.setPayloadData("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"org/w3/xhtml/xhtml1-strict.dtd\">"
+ "<html xmlns=\"http://www.w3.org/1999/xhtml\">"
+ "<head>"
+ "<title/>"
+ "</head>"
+ "<body>"
+ "<p>Hello TestFramework!</p>"
+ "</body>"
+ "</html>");
List<ValidationContext> validationContexts = new ArrayList<ValidationContext>();
validationContexts.add(validationContext);
receiveMessageBean.setValidationContexts(validationContexts);
receiveMessageBean.execute(context);
}
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void testXhtmlValidation() throws Exception {
reset(endpoint, consumer, endpointConfiguration);
expect(endpoint.createConsumer()).andReturn(consumer).anyTimes();
expect(endpoint.getEndpointConfiguration()).andReturn(endpointConfiguration).anyTimes();
expect(endpointConfiguration.getTimeout()).andReturn(5000L).anyTimes();
Message message = new DefaultMessage("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"org/w3/xhtml/xhtml1-strict.dtd\">"
+ "<html xmlns=\"http://www.w3.org/1999/xhtml\">"
+ "<head>"
+ "<title>Sample XHTML content</title>"
+ "</head>"
+ "<body>"
+ "<p>Hello TestFramework!</p>"
+ "</body>"
+ "</html>");
expect(consumer.receive(anyObject(TestContext.class), anyLong())).andReturn(message).once();
expect(endpoint.getActor()).andReturn(null).anyTimes();
replay(endpoint, consumer, endpointConfiguration);
PayloadTemplateMessageBuilder controlMessageBuilder = new PayloadTemplateMessageBuilder();
XmlMessageValidationContext validationContext = new XmlMessageValidationContext();
validationContext.setMessageBuilder(controlMessageBuilder);
controlMessageBuilder.setPayloadData("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"org/w3/xhtml/xhtml1-strict.dtd\">"
+ "<html xmlns=\"http://www.w3.org/1999/xhtml\">"
+ "<head>"
+ "<title>Sample XHTML content</title>"
+ "</head>"
+ "<body>"
+ "<p>Hello TestFramework!</p>"
+ "</body>"
+ "</html>");
List<ValidationContext> validationContexts = new ArrayList<ValidationContext>();
validationContexts.add(validationContext);
receiveMessageBean.setValidationContexts(validationContexts);
receiveMessageBean.execute(context);
}
}