/**
 * 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.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.writeModelToFile(outFilePath, model, outFileFormat);

        assertThat(this.outStream.toString()).startsWith(testString);
    }
}

@@ -603,16 +603,56 @@
 source;%0A
+import com.hp.hpl.jena.vocabulary.RDFS;%0A
 import o
@@ -3459,32 +3459,192 @@
 %22'%22%0A        );%0A%0A
+        RDFService.writeModelToFile(%22%22, model, outFileFormat);%0A        assertThat(this.outStream.toString()).startsWith(%22%5BERROR%5D Could not open output file%22);%0A%0A
         RDFServi
@@ -3695,25 +3695,24 @@
 ileFormat);%0A
-%0A
         asse
@@ -3749,29 +3749,851 @@
 ()).
-startsWith(testString
+contains(testString);%0A    %7D%0A%0A    /**%0A     * Test that an empty and a non empty RDF file can be opened.%0A     * @throws Exception%0A     */%0A    @Test%0A    public void testOpenModelFromFile() throws Exception %7B%0A        final File currEmptyTestFile = this.testFileFolder.resolve(%22testEmpty.ttl%22).toFile();%0A        FileUtils.write(currEmptyTestFile, %22%22);%0A%0A        Model emptyModel = RDFService.openModelFromFile(currEmptyTestFile.toString());%0A        assertThat(emptyModel.isEmpty()).isTrue();%0A%0A        final String miniTTL = %22@prefix foaf: %3Chttp://xmlns.com/foaf/0.1/%3E .%5Cn%5Cn_:a foaf:name%5Ct%5C%22TestName%5C%22%22;%0A        final File currTestFile = this.testFileFolder.resolve(%22test.ttl%22).toFile();%0A        FileUtils.write(currTestFile, miniTTL);%0A%0A        Model m = RDFService.openModelFromFile(currTestFile.toString());%0A        assertThat(m.isEmpty()).isFalse(
 );%0A 

update RDFService tests