/**
 * Copyright (c) 2002-2012 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.neo4j.server.webadmin.rest;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.core.Response;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.neo4j.kernel.Config;
import org.neo4j.server.database.Database;
import org.neo4j.server.rest.repr.OutputFormat;
import org.neo4j.server.rest.repr.formats.JsonFormat;
import org.neo4j.server.webadmin.console.ScriptSession;
import org.neo4j.test.ImpermanentGraphDatabase;

public class Neo4jShellConsoleSessionTest implements SessionFactory
{
    private ConsoleService consoleService;
    private Database database;
    private final URI uri = URI.create( "http://peteriscool.com:6666/" );

    @Before
    public void setUp() throws Exception
    {
        Map<String, String> params = new HashMap<String, String>();
        params.put(Config.ENABLE_REMOTE_SHELL, "true");
        this.database = new Database( new ImpermanentGraphDatabase(params) );
        this.consoleService = new ConsoleService( this, database, new OutputFormat( new JsonFormat(), uri, null ) );
    }

    @After
    public void shutdownDatabase()
    {
        this.database.shutdown();
    }

    @Override
    public ScriptSession createSession( String engineName, Database database )
    {
        return new ShellSession( database.graph );
    }

    @Test
    public void doesntMangleNewlines() throws UnsupportedEncodingException
    {
        Response response = consoleService.exec( new JsonFormat(),
                "{ \"command\" : \"start n=node(0) return n\", \"engine\":\"shell\" }" );

     
        assertEquals( 200, response.getStatus() );
        String result = decode( response );

        // Awful hack: Result contains a timestamp for how
        // long the query took, remove that timestamp to get 
        // a deterministic test.
        result = result.replaceAll("\\d+ ms", "");
        
        String expected = "[ \"+-----------+\\n| n         |\\n+-----------+\\n| Node[0]{} |\\n+-----------+\\n1 rows, \\n\\n\", \"neo4j-sh (0)$ \" ]";

        assertThat( result, is(expected) );
    }
    
    private String decode( final Response response ) throws UnsupportedEncodingException
    {
        return new String( (byte[]) response.getEntity(), "UTF-8" );
    }
}

@@ -1062,16 +1062,39 @@
 ashMap;%0A
+import java.util.List;%0A
 import j
@@ -1290,16 +1290,120 @@
 tabase;%0A
+import org.neo4j.server.rest.domain.JsonHelper;%0Aimport org.neo4j.server.rest.domain.JsonParseException;%0A
 import o
@@ -1671,16 +1671,96 @@
 ctory%0A%7B%0A
+    %0A    private static final String LN = System.getProperty(%22line.separator%22);%0A
     priv
@@ -2591,35 +2591,16 @@
  throws 
-UnsupportedEncoding
 Exceptio
@@ -2863,16 +2863,23 @@
 esponse 
+).get(0
 );%0A%0A    
@@ -3118,12 +3118,8 @@
 d = 
-%22%5B %5C
 %22+--
@@ -3132,116 +3132,274 @@
 ---+
-%5C%5Cn%7C n         %7C%5C%5Cn+-----------+%5C%5Cn%7C Node%5B0%5D%7B%7D %7C%5C%5Cn+-----------+%5C%5Cn1 rows, %5C%5Cn%5C%5Cn%5C%22, %5C%22neo4j-sh (0)$ %5C%22 %5D%22;%0A
+%22+LN%0A                         +%22%7C n         %7C%22+LN%0A                         +%22+-----------+%22+LN%0A                         +%22%7C Node%5B0%5D%7B%7D %7C%22+LN%0A                         +%22+-----------+%22+LN%0A                         +%221 rows, %22+LN%0A                         +%22%22+LN;%0A        
 %0A   
@@ -3462,22 +3462,28 @@
 private 
+List%3C
 String
+%3E
  decode(
@@ -3536,32 +3536,52 @@
 ncodingException
+, JsonParseException
 %0A    %7B%0A        r
@@ -3582,24 +3582,58 @@
      return 
+(List%3CString%3E)JsonHelper.readJson(
 new String( 
@@ -3671,16 +3671,17 @@
 %22UTF-8%22 
+)
 );%0A    %7D

Fix to make failing newline test pass on windows as well.