/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.camel.component.hl7;

import ca.uhn.hl7v2.DefaultHapiContext;
import ca.uhn.hl7v2.HapiContext;
import ca.uhn.hl7v2.Version;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.v24.message.ADT_A01;
import ca.uhn.hl7v2.model.v24.segment.PID;
import ca.uhn.hl7v2.validation.ValidationContext;
import ca.uhn.hl7v2.validation.builder.ValidationRuleBuilder;
import ca.uhn.hl7v2.validation.impl.ValidationContextFactory;
import org.apache.camel.CamelExecutionException;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Ignore;
import org.junit.Test;

import static org.apache.camel.component.hl7.HL7.messageConforms;
import static org.apache.camel.component.hl7.HL7.messageConformsTo;

public class MessageValidatorTest extends CamelTestSupport {

    private ValidationContext defaultValidationContext;
    private ValidationContext customValidationContext;
    private HapiContext defaultContext;
    private HapiContext customContext;

    @Override
    protected void doPreSetup() throws Exception {
        defaultValidationContext = ValidationContextFactory.defaultValidation();
        defaultContext = new DefaultHapiContext(defaultValidationContext);

        ValidationRuleBuilder builder = new ValidationRuleBuilder() {
            @Override
            protected void configure() {
                forVersion(Version.V24)
                        .message("ADT", "A01")
                        .terser("PID-8", not(empty()));
            }
        };
        customValidationContext = ValidationContextFactory.fromBuilder(builder);
        customContext = new DefaultHapiContext(customValidationContext);
    }

    @Test
    public void testDefaultHapiContext() throws Exception {
        MockEndpoint mock = getMockEndpoint("mock:test4");
        mock.expectedMessageCount(1);
        Message msg = createADT01Message();
        template.sendBody("direct:test4", msg);
        assertMockEndpointsSatisfied();
    }

    @Test(expected = CamelExecutionException.class)
    @Ignore
    public void testCustomHapiContext() throws Exception {
        MockEndpoint mock = getMockEndpoint("mock:test5");
        mock.expectedMessageCount(0);
        Message msg = createADT01Message();
        template.sendBody("direct:test5", msg);
        assertMockEndpointsSatisfied();
    }


    @Test(expected = CamelExecutionException.class)
    @Ignore
    public void testCustomValidationContext() throws Exception {
        MockEndpoint mock = getMockEndpoint("mock:test2");
        mock.expectedMessageCount(0);
        Message msg = createADT01Message();
        template.sendBody("direct:test2", msg);
        assertMockEndpointsSatisfied();
    }

    @Test
    public void testDynamicCustomValidationContext() throws Exception {
        MockEndpoint mock = getMockEndpoint("mock:test3");
        mock.expectedMessageCount(1);
        Message msg = createADT01Message();
        template.sendBodyAndHeader("direct:test3", msg, "validator", defaultValidationContext);
        assertMockEndpointsSatisfied();
    }

    @Test
    public void testDynamicDefaultHapiContext() throws Exception {
        MockEndpoint mock = getMockEndpoint("mock:test6");
        mock.expectedMessageCount(1);
        Message msg = createADT01Message();
        msg.setParser(defaultContext.getPipeParser());
        template.sendBody("direct:test6", msg);
        assertMockEndpointsSatisfied();
    }

    @Test(expected = CamelExecutionException.class)
    @Ignore
    public void testDynamicCustomHapiContext() throws Exception {
        MockEndpoint mock = getMockEndpoint("mock:test6");
        mock.expectedMessageCount(1);
        Message msg = createADT01Message();
        msg.setParser(customContext.getPipeParser());
        template.sendBody("direct:test6", msg);
        assertMockEndpointsSatisfied();
    }

    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            public void configure() throws Exception {
                from("direct:test1").validate(messageConformsTo(defaultValidationContext)).to("mock:test1");
                from("direct:test2").validate(messageConformsTo(customValidationContext)).to("mock:test2");
                from("direct:test3").validate(messageConformsTo(header("validator"))).to("mock:test3");
                from("direct:test4").validate(messageConformsTo(defaultContext)).to("mock:test4");
                from("direct:test5").validate(messageConformsTo(customContext)).to("mock:test5");
                from("direct:test6").validate(messageConforms()).to("mock:test6");
            }
        };
    }

    private static Message createADT01Message() throws Exception {
        ADT_A01 adt = new ADT_A01();
        adt.initQuickstart("ADT", "A01", "P");

        // Populate the PID Segment
        PID pid = adt.getPID();
        pid.getPatientName(0).getFamilyName().getSurname().setValue("Doe");
        pid.getPatientName(0).getGivenName().setValue("John");
        pid.getPatientIdentifierList(0).getID().setValue("123456");

        return adt;
    }
}

@@ -2916,36 +2916,24 @@
 tion.class)%0A
-    @Ignore%0A
     public v
@@ -3215,16 +3215,327 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void testDefaultValidationContext() throws Exception %7B%0A        MockEndpoint mock = getMockEndpoint(%22mock:test1%22);%0A        mock.expectedMessageCount(1);%0A        Message msg = createADT01Message();%0A        template.sendBody(%22direct:test1%22, msg);%0A        assertMockEndpointsSatisfied();%0A    %7D%0A
 %0A    @Te
@@ -3575,36 +3575,24 @@
 tion.class)%0A
-    @Ignore%0A
     public v

test update to camel-hl7