/**
 * Copyright (c) 2015, German Neuroinformatics Node (G-Node)
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted under the terms of the BSD License. See
 * LICENSE file in the root of the Project.
 */

package org.g_node.srv;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.g_node.micro.commons.RDFService;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;

/**
 * Unit tests for the {@link RDFService} class. Output and Error streams are redirected
 * from the console to a different PrintStream and reset after tests are finished
 * to avoid mixing tool error messages with actual test error messages.
 *
 * @author Michael Sonntag (sonntag@bio.lmu.de)
 */
public class RDFServiceTest {

    private final String tmpRoot = System.getProperty("java.io.tmpdir");
    private final String testFolderName = "tdfservicetest";
    private final String testFileName = "test.txt";
    private final Path testFileFolder = Paths.get(tmpRoot, testFolderName);

    private ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    private PrintStream stdout;

    /**
     * Redirect Out stream and create a test folder and the main
     * test file in the java temp directory.
     * @throws Exception
     */
    @Before
    public void setUp() throws Exception {

        this.stdout = System.out;
        this.outStream = new ByteArrayOutputStream();
        System.setOut(new PrintStream(this.outStream));

        Logger rootLogger = Logger.getRootLogger();
        rootLogger.setLevel(Level.INFO);
        rootLogger.addAppender(
                new ConsoleAppender(
                        new PatternLayout("[%-5p] %m%n")
                )
        );

        final File currTestFile = this.testFileFolder.resolve(this.testFileName).toFile();
        FileUtils.write(currTestFile, "This is a normal test file");
    }

    /**
     * Reset Out stream to the console and remove all created
     * folders and files after the tests are done.
     * @throws Exception
     */
    @After
    public void tearDown() throws Exception {

        System.setOut(this.stdout);

        if (Files.exists(this.testFileFolder)) {
            FileUtils.deleteDirectory(this.testFileFolder.toFile());
        }
    }

    /**
     * Check, that a Jena RDF model is written to file.
     * @throws Exception
     */
    @Test
    public void testWriteModelToFile() throws Exception {

        final Model model = ModelFactory.createDefaultModel();
        final Resource modelEntry = model.createResource();

        final String outFileName = "testrdf.ttl";
        final String outFilePath = this.testFileFolder.resolve(outFileName).toString();
        final String outFileFormat = "TTL";
        final String testString = String.join(
                "", "[INFO ] Writing data to RDF file '",
                outFilePath, "' using format '", outFileFormat, "'"
        );

        RDFService.saveModelToFile("", model, outFileFormat);
        assertThat(this.outStream.toString()).startsWith("[ERROR] Could not open output file");

        RDFService.saveModelToFile(outFilePath, model, outFileFormat);
        assertThat(this.outStream.toString()).contains(testString);
    }

    /**
     * Test that an empty and a non empty RDF file can be opened.
     * @throws Exception
     */
    @Test
    public void testOpenModelFromFile() throws Exception {
        final File currEmptyTestFile = this.testFileFolder.resolve("testEmpty.ttl").toFile();
        FileUtils.write(currEmptyTestFile, "");

        Model emptyModel = RDFService.openModelFromFile(currEmptyTestFile.toString());
        assertThat(emptyModel.isEmpty()).isTrue();

        final String miniTTL = "@prefix foaf: <http://xmlns.com/foaf/0.1/> .\n\n_:a foaf:name\t\"TestName\"";
        final File currTestFile = this.testFileFolder.resolve("test.ttl").toFile();
        FileUtils.write(currTestFile, miniTTL);

        Model m = RDFService.openModelFromFile(currTestFile.toString());
        assertThat(m.isEmpty()).isFalse();
    }
}

@@ -17,17 +17,17 @@
  (c) 201
-5
+6
 , German
@@ -303,16 +303,103 @@
 e.srv;%0A%0A
+import com.hp.hpl.jena.rdf.model.Model;%0Aimport com.hp.hpl.jena.rdf.model.ModelFactory;%0A
 import j
@@ -564,138 +564,8 @@
 hs;%0A
-import com.hp.hpl.jena.rdf.model.Model;%0Aimport com.hp.hpl.jena.rdf.model.ModelFactory;%0Aimport com.hp.hpl.jena.rdf.model.Resource;%0A
 impo
@@ -743,16 +743,74 @@
 Layout;%0A
+import static org.assertj.core.api.Assertions.assertThat;%0A
 import o
@@ -920,66 +920,8 @@
 est;
-%0Aimport static org.assertj.core.api.Assertions.assertThat;
 %0A%0A/*
@@ -1246,24 +1246,132 @@
 viceTest %7B%0A%0A
+    private ByteArrayOutputStream outStream = new ByteArrayOutputStream();%0A    private PrintStream stdout;%0A%0A
     private 
@@ -1478,73 +1478,21 @@
  = %22
-tdfs
+rdfS
 ervice
-test%22;%0A    private final String testFileName = %22test.tx
+Tes
 t%22;%0A
@@ -1572,116 +1572,8 @@
 );%0A%0A
-    private ByteArrayOutputStream outStream = new ByteArrayOutputStream();%0A    private PrintStream stdout;%0A%0A
     
@@ -2161,32 +2161,80 @@
   )%0A        );%0A%0A
+        final String testFileName = %22test.txt%22;%0A
         final Fi
@@ -2279,21 +2279,16 @@
 resolve(
-this.
 testFile
@@ -2890,20 +2890,19 @@
 oid test
-Writ
+Sav
 eModelTo
@@ -2932,132 +2932,8 @@
  %7B%0A%0A
-        final Model model = ModelFactory.createDefaultModel();%0A        final Resource modelEntry = model.createResource();%0A%0A
     
@@ -2968,17 +2968,17 @@
  = %22test
-r
+R
 df.ttl%22;
@@ -3287,32 +3287,96 @@
 %22'%22%0A        );%0A%0A
+        final Model model = ModelFactory.createDefaultModel();%0A%0A
         RDFServi
@@ -4487,10 +4487,11 @@
 ;%0A    %7D%0A
+%0A
 %7D%0A

Update RDFService tests