/**
 * Copyright (C) 2014 Stratio (http://stratio.com)
 *
 * 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.stratio.ingestion.deserializer.xmlxpath;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.flume.Context;
import org.apache.flume.Event;
import org.apache.flume.serialization.EventDeserializer;
import org.apache.flume.serialization.ResettableFileInputStream;
import org.apache.flume.serialization.ResettableInputStream;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;

import com.stratio.ingestion.serialization.tracker.TransientPositionTracker;

@RunWith(JUnit4.class)
public class XmlXpathDeserializerTest {

    private ResettableInputStream getTestInputStream() throws IOException {
      return getTestInputStream("test.xml");
    }

    private ResettableInputStream getTestInputStream(final String path) throws IOException {
      return new ResettableFileInputStream(new File("src/test/resources/" + path), new TransientPositionTracker("dummy"));
    }

    @Test
    public void testReadsAndMark() throws IOException {
        Context context = new Context();
        context.put("expression", "/bookstore/book");
        EventDeserializer des = new XmlXpathDeserializer.Builder().build(context, getTestInputStream());
        validateReadAndMark(des);
    }

    @Test
    public void testReset() throws IOException {
        Context context = new Context();
        context.put("expression", "/bookstore/book/title/text()");
        EventDeserializer des = new XmlXpathDeserializer.Builder().build(context, getTestInputStream());
        validateReset(des);
    }

    @Test
    public void testHeader() throws IOException {
      Context context = new Context();
      context.put("expression", "/bookstore/book");
      context.put("outputHeader", "myHeader");
      context.put("outputBody", "false");
      EventDeserializer des = new XmlXpathDeserializer.Builder().build(context, getTestInputStream());
      validateReadAndMarkWithHeader(des);
    }

    @Test
    public void testDocument2String() throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = factory.newDocumentBuilder();
        InputSource is = new InputSource(new ResettableInputStreamInputStream(getTestInputStream()));
        Document doc = docBuilder.parse(is);

        Context context = new Context();
        context.put("expression", "/bookstore/book/title");
        XmlXpathDeserializer des = new XmlXpathDeserializer(context, getTestInputStream());

        Assert.assertNotNull(des.documentToString(doc));
        des.close();
    }

    @Test(expected = RuntimeException.class)
    public void testBadXML() throws IOException {
      EventDeserializer des = new XmlXpathDeserializer.Builder().build(new Context(), getTestInputStream("bad.xml"));
    }

    @Test()
    public void testXPathWithNS() throws IOException {
      Context context = new Context();
      context.put("expression", "/bookstore/book");
      EventDeserializer des = new XmlXpathDeserializer.Builder().build(context, getTestInputStream("ns.xml"));
      List<Event> events = des.readEvents(4);
      assertEquals(4, events.size());
      for (final Event event : events) {
        assertNotNull(event);
      }
    }

    @Test(expected = RuntimeException.class)
    public void testBadXPath() throws IOException {
      Context context = new Context();
      context.put("expression", "ñ/b\ngnklñ13");
      EventDeserializer des = new XmlXpathDeserializer.Builder().build(context, getTestInputStream());
    }

    @Test(expected = RuntimeException.class)
    public void testNoOutputNoHeader() throws IOException {
      Context context = new Context();
      context.put("outputBody", "false");
      EventDeserializer des = new XmlXpathDeserializer.Builder().build(context, getTestInputStream());
    }

    private void validateReadAndMark(EventDeserializer des) throws IOException {
        Event evt;

        evt = des.readEvent();
        assertTrue(new String(evt.getBody()).contains("Giada De Laurentiis"));
        des.mark();

        evt = des.readEvent();
        assertTrue(new String(evt.getBody()).contains("J K. Rowling"));
        des.mark(); // reset!

        List<Event> readEvents = des.readEvents(2);
        assertEquals(2, readEvents.size());

        evt = des.readEvent();
        assertNull("Event should be null because there are no more books " + "left to read", evt);

        des.mark();
        des.mark();
        des.close();
    }

    private void validateReadAndMarkWithHeader(EventDeserializer des) throws IOException {
        Event evt;

        evt = des.readEvent();
        System.out.println(evt.getHeaders().get("myHeader"));
        assertTrue(evt.getHeaders().get("myHeader").contains("Giada De Laurentiis"));
        des.mark();

        evt = des.readEvent();
        assertTrue(evt.getHeaders().get("myHeader").contains("J K. Rowling"));
        des.mark(); // reset!

        List<Event> readEvents = des.readEvents(2);
        assertEquals(2, readEvents.size());

        evt = des.readEvent();
        assertNull("Event should be null because there are no more books " + "left to read", evt);

        des.mark();
        des.mark();
        des.close();
    }

    private void validateReset(EventDeserializer des) throws IOException {
        Event evt = des.readEvent();
        assertEquals("Everyday Italian", new String(evt.getBody()));
        des.mark();

        List<Event> events = des.readEvents(3);
        assertEquals(3, events.size());
        assertEquals("Harry Potter", new String(events.get(0).getBody()));
        assertEquals("XQuery Kick Start", new String(events.get(1).getBody()));
        assertEquals("Learning XML", new String(events.get(2).getBody()));

        des.reset(); // reset!

        events = des.readEvents(3);
        assertEquals(3, events.size());
        assertEquals("Harry Potter", new String(events.get(0).getBody()));
        assertEquals("XQuery Kick Start", new String(events.get(1).getBody()));
        assertEquals("Learning XML", new String(events.get(2).getBody()));

        evt = des.readEvent();
        Assert.assertNull("Event should be null because there are no more books " + "left to read", evt);

    }

    private void validateHeaders(EventDeserializer des) throws IOException {
        List<Event> events = des.readEvents(4);
        Assert.assertTrue(events.size() == 4);

        for (Event evt : events) {
            Assert.assertEquals(evt.getHeaders().get("author"), "J K. Rowling");
        }
    }

}
