/*
 * 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.karaf.decanter.collector.camel;

import java.io.StringReader;
import java.util.Map;

import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;

import org.apache.camel.Exchange;
import org.apache.camel.LoggingLevel;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.processor.interceptor.TraceEventHandler;
import org.apache.camel.processor.interceptor.Tracer;
import org.apache.karaf.decanter.marshaller.json.JsonMarshaller;
import org.junit.Assert;
import org.junit.Test;
import org.osgi.service.event.Event;

public class DecanterTraceEventHandlerTest {

    @Test
    public void testTracer() throws Exception {
        JsonMarshaller marshaller = new JsonMarshaller();
        MockEventAdmin eventAdmin = new MockEventAdmin();
        DecanterTraceEventHandler handler = new DecanterTraceEventHandler();
        handler.setEventAdmin(eventAdmin);
        DefaultCamelContext camelContext = createCamelContext(handler);

        ProducerTemplate template = camelContext.createProducerTemplate();
        template.sendBodyAndHeader("direct:start", "TEST", "header", "test");

        for (Event event : eventAdmin.getPostEvents()) {
            String jsonString = marshaller.marshal(event);
            JsonReader jsonReader = Json.createReader(new StringReader(jsonString));
            JsonObject rootObject = jsonReader.readObject();
            Assert.assertEquals("InOnly", rootObject.getString("exchangePattern"));
            Assert.assertEquals("camelTracer", rootObject.getString("type"));
            Assert.assertEquals("test-route", rootObject.getString("routeId"));
            Assert.assertEquals("log:foo", rootObject.getString("to2_label"));
            Assert.assertEquals("test-context", rootObject.getString("camelContextName"));
            JsonObject headersObject = rootObject.getJsonObject("inHeaders");
            Assert.assertEquals("test", headersObject.getString("header"));
            Assert.assertEquals("direct://start", rootObject.getString("fromEndpointUri"));
            Assert.assertEquals("TEST", rootObject.getString("inBody"));
            JsonObject propertiesObject = rootObject.getJsonObject("properties");
            Assert.assertEquals("log://foo", propertiesObject.getString("CamelToEndpoint"));
            Assert.assertEquals("String", rootObject.getString("inBodyType"));
            Assert.assertEquals("decanter/collect/camel/tracer", rootObject.getString("event_topics"));
            System.out.println(marshaller.marshal(event));
        }
    }

    @Test
    public void testTracerWithExtender() throws Exception {
        MockEventAdmin eventAdmin = new MockEventAdmin();
        TestExtender extender = new TestExtender();
        DecanterTraceEventHandler handler = new DecanterTraceEventHandler();
        handler.setExtender(extender);
        handler.setEventAdmin(eventAdmin);
        DefaultCamelContext camelContext = createCamelContext(handler);

        ProducerTemplate template = camelContext.createProducerTemplate();
        template.sendBodyAndHeader("direct:start", "TEST", "header", "test");

        Assert.assertEquals(2, eventAdmin.getPostEvents().size());

        Assert.assertEquals("test", eventAdmin.getPostEvents().get(0).getProperty("extender-test"));
        Assert.assertEquals("test", eventAdmin.getPostEvents().get(1).getProperty("extender-test"));
    }

    private DefaultCamelContext createCamelContext(TraceEventHandler handler) throws Exception {
        RouteBuilder builder = new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("direct:start").routeId("test-route").to("log:foo");
            }
        };
        DefaultCamelContext camelContext = new DefaultCamelContext();
        camelContext.setName("test-context");
        camelContext.addRoutes(builder);
        Tracer tracer = new Tracer();
        tracer.setEnabled(true);
        tracer.setTraceOutExchanges(true);
        tracer.setLogLevel(LoggingLevel.OFF);
        tracer.addTraceHandler(handler);
        camelContext.setTracing(true);
        camelContext.setDefaultTracer(tracer);
        camelContext.start();
        return camelContext;
    }

    private class TestExtender implements DecanterCamelEventExtender {

        @Override
        public void extend(Map<String, Object> decanterData, Exchange camelExchange) {
            decanterData.put("extender-test", "test");
        }

    }

}
