/**
 * 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.xslt;

import org.apache.camel.ContextTestSupport;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;

/**
 * Unit test hot reloading of XSL files (CAMEL-2737).
 */
public class XsltContentCacheTest extends ContextTestSupport {
    
    private static final String ORIGINAL_XSL = 
        "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">"
            + "<xsl:template match=\"/\"><goodbye><xsl:value-of select=\"/hello\"/></goodbye></xsl:template>"
            + "</xsl:stylesheet>";

    private static final String NEW_XSL = 
        "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">"
            + "<xsl:template match=\"/\"><goodnight><xsl:value-of select=\"/hello\"/></goodnight></xsl:template>"
            + "</xsl:stylesheet>";
    
    @Override
    public void setUp() throws Exception {
        super.setUp();
        
        // create file with original XSL transformation
        template.sendBodyAndHeader("file://target/test-classes/org/apache/camel/component/xslt?fileExist=Override", ORIGINAL_XSL, Exchange.FILE_NAME, "hello.xsl");
        
        // start the context AFTER we've created the hello.xsl file, otherwise the xslt routes will fail
        super.startCamelContext();
    }
    
    @Override
    protected void startCamelContext() throws Exception {
        // Override so we can start the context ourself in the setUp
    }

    public void testNotCached() throws Exception {
        MockEndpoint mock = getMockEndpoint("mock:result");
        mock.expectedBodiesReceived("<?xml version=\"1.0\" encoding=\"UTF-8\"?><goodbye>world!</goodbye>");

        template.sendBody("direct:a", "<hello>world!</hello>");
        mock.assertIsSatisfied();

        // now replace the file with a new XSL transformation
        template.sendBodyAndHeader("file://target/test-classes/org/apache/camel/component/xslt?fileExist=Override", NEW_XSL, Exchange.FILE_NAME, "hello.xsl");

        mock.reset();
        // we expect the new output as the cache is not enabled, so it's "goodnight" and not "goodbye"
        mock.expectedBodiesReceived("<?xml version=\"1.0\" encoding=\"UTF-8\"?><goodnight>world!</goodnight>");

        template.sendBody("direct:a", "<hello>world!</hello>");
        mock.assertIsSatisfied();
    }

    public void testCached() throws Exception {
        MockEndpoint mock = getMockEndpoint("mock:result");
        mock.expectedBodiesReceived("<?xml version=\"1.0\" encoding=\"UTF-8\"?><goodbye>world!</goodbye>");

        template.sendBody("direct:b", "<hello>world!</hello>");
        mock.assertIsSatisfied();

        // now replace the file with a new XSL transformation
        template.sendBodyAndHeader("file://target/test-classes/org/apache/camel/component/xslt?fileExist=Override", NEW_XSL, Exchange.FILE_NAME, "hello.xsl");

        mock.reset();
        // we expect the original output as the cache is enabled, so it's "goodbye" and not "goodnight"
        mock.expectedBodiesReceived("<?xml version=\"1.0\" encoding=\"UTF-8\"?><goodbye>world!</goodbye>");

        template.sendBody("direct:b", "<hello>world!</hello>");
        mock.assertIsSatisfied();
    }

    public void testCachedIsDefault() throws Exception {
        MockEndpoint mock = getMockEndpoint("mock:result");
        mock.expectedBodiesReceived("<?xml version=\"1.0\" encoding=\"UTF-8\"?><goodbye>world!</goodbye>");

        template.sendBody("direct:c", "<hello>world!</hello>");
        mock.assertIsSatisfied();

        // now replace the file with a new XSL transformation
        template.sendBodyAndHeader("file://target/test-classes/org/apache/camel/component/xslt?fileExist=Override", NEW_XSL, Exchange.FILE_NAME, "hello.xsl");

        mock.reset();
        // we expect the original output as the cache is enabled, so it's "goodbye" and not "goodnight"
        mock.expectedBodiesReceived("<?xml version=\"1.0\" encoding=\"UTF-8\"?><goodbye>world!</goodbye>");

        template.sendBody("direct:c", "<hello>world!</hello>");
        mock.assertIsSatisfied();
    }

    protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            public void configure() throws Exception {
                from("direct:a").to("xslt://org/apache/camel/component/xslt/hello.xsl?output=string&contentCache=false").to("mock:result");

                from("direct:b").to("xslt://org/apache/camel/component/xslt/hello.xsl?output=string&contentCache=true").to("mock:result");

                from("direct:c").to("xslt://org/apache/camel/component/xslt/hello.xsl?output=string").to("mock:result");
            }
        };
    }
}
