/*
 * Copyright 2015 Collective, Inc.
 *
 * 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.collective.celos.server;

import com.collective.celos.Constants;
import com.collective.celos.database.FileSystemStateDatabase;
import com.collective.celos.database.JDBCStateDatabase;
import com.collective.celos.database.StateDatabase;
import junit.framework.Assert;
import org.junit.Test;

import java.io.File;

public class ServerCommandLineParserTest {
    
    @Test(expected=IllegalArgumentException.class)
    public void testPortIsRequired() throws Exception {
        new ServerCommandLineParser().parse(new String[] {});
    }
    
    @Test(expected=IllegalArgumentException.class)
    public void testPortIsRequiredEvenIfOtherArgsAreSupplied() throws Exception {
        new ServerCommandLineParser().parse(new String[] {"--defaults", "/foo"});
    }
    
    @Test
    public void testDefaults() throws Exception {
        ServerCommandLine cmdLine = new ServerCommandLineParser().parse(new String[] {"--port", "1337"});
        Assert.assertEquals(1337, cmdLine.getPort());
        Assert.assertEquals(new File(Constants.DEFAULT_WORKFLOWS_DIR), cmdLine.getWorkflowsDir());
        Assert.assertEquals(new File(Constants.DEFAULT_DEFAULTS_DIR), cmdLine.getDefaultsDir());
        Assert.assertEquals(StateDatabase.DatabaseType.FILESYSTEM, cmdLine.getDatabase().getDatabaseType());
        FileSystemStateDatabase db = (FileSystemStateDatabase) cmdLine.getDatabase();
        Assert.assertEquals(new File(Constants.DEFAULT_DB_DIR), db.getDir());
    }

    @Test
    public void testOverrideDefaults() throws Exception {
        ServerCommandLine cmdLine = new ServerCommandLineParser().parse(new String[] {"--port", "1337", "--workflows", "/wf", "--db", "/db", "--defaults", "/defaults"});
        Assert.assertEquals(1337, cmdLine.getPort());
        Assert.assertEquals(new File("/wf"), cmdLine.getWorkflowsDir());
        Assert.assertEquals(new File("/defaults"), cmdLine.getDefaultsDir());
        Assert.assertEquals(StateDatabase.DatabaseType.FILESYSTEM, cmdLine.getDatabase().getDatabaseType());
        FileSystemStateDatabase db = (FileSystemStateDatabase) cmdLine.getDatabase();
        Assert.assertEquals(new File("/db"), db.getDir());
    }

    @Test (expected = IllegalArgumentException.class)
    public void testJdbcConnectionParamsNoUrl() throws Exception {
        new ServerCommandLineParser().parse(new String[]{"--port", "1337", "--workflows", "/wf", "--dbType", "jdbc", "--defaults", "/defaults"});
    }

    @Test (expected = IllegalArgumentException.class)
    public void testJdbcConnectionParamsNoUsername() throws Exception {
        new ServerCommandLineParser().parse(new String[] {"--port", "1337", "--workflows", "/wf", "--dbType", "jdbc" , "--jdbcUrl", "url", "--jdbcName", "uname", "--defaults", "/defaults"});
    }

    @Test (expected = IllegalArgumentException.class)
    public void testJdbcConnectionParamsNoPassword() throws Exception {
        ServerCommandLine cmdLine = new ServerCommandLineParser().parse(new String[]{"--port", "1337", "--workflows", "/wf", "--dbType", "jdbc", "--jdbcUrl", "url", "--jdbcName", "uname", "--defaults", "/defaults"});
        Assert.assertEquals(1337, cmdLine.getPort());
        Assert.assertEquals(new File("/wf"), cmdLine.getWorkflowsDir());
        Assert.assertEquals(new File("/defaults"), cmdLine.getDefaultsDir());
        Assert.assertEquals(StateDatabase.DatabaseType.FILESYSTEM, cmdLine.getDatabase().getDatabaseType());
        FileSystemStateDatabase config = (FileSystemStateDatabase) cmdLine.getDatabase();
        Assert.assertEquals(new File("/db"), config.getDir());

    }

    @Test
    public void testJdbcConnectionParams() throws Exception {
        ServerCommandLine cmdLine = new ServerCommandLineParser().parse(new String[]{"--port", "1337", "--workflows", "/wf", "--dbType", "jdbc", "--jdbcUrl", "url", "--jdbcName", "uname", "--jdbcPassword", "pwd", "--defaults", "/defaults"});
        Assert.assertEquals(1337, cmdLine.getPort());
        Assert.assertEquals(new File("/wf"), cmdLine.getWorkflowsDir());
        Assert.assertEquals(new File("/defaults"), cmdLine.getDefaultsDir());
        Assert.assertEquals(StateDatabase.DatabaseType.JDBC, cmdLine.getDatabase().getDatabaseType());
        JDBCStateDatabase config = (JDBCStateDatabase) cmdLine.getDatabase();
        Assert.assertEquals("url", config.getUrl());
        Assert.assertEquals("uname", config.getName());
        Assert.assertEquals("pwd", config.getPassword());

    }

}

@@ -891,34 +891,150 @@
 nit.
-Test;%0A%0Aimport java.io.File
+Before;%0Aimport org.junit.Rule;%0Aimport org.junit.Test;%0Aimport org.junit.rules.TemporaryFolder;%0A%0Aimport java.io.File;%0Aimport java.io.IOException
 ;%0A%0Ap
@@ -1075,20 +1075,213 @@
 rTest %7B%0A
+%0A
     
+@Rule%0A    public TemporaryFolder folder = new TemporaryFolder();%0A%0A    private File tmpDir;%0A%0A    @Before%0A    public void setUp() throws IOException %7B%0A        tmpDir = folder.newFolder();%0A    %7D%0A
 %0A    @Te
@@ -1666,36 +1666,213 @@
 %22/foo%22%7D);%0A    %7D%0A
+%0A
     
+@Test (expected = IOException.class)%0A    public void testDefaultsNoDb() throws Exception %7B%0A        new ServerCommandLineParser().parse(new String%5B%5D %7B%22--port%22, %221337%22 %7D);%0A    %7D%0A
 %0A    @Test%0A    p
@@ -2018,16 +2018,51 @@
 , %221337%22
+, %22--db%22, tmpDir.getAbsolutePath() 
 %7D);%0A    
@@ -2534,44 +2534,8 @@
 als(
-new File(Constants.DEFAULT_DB_DIR), 
 db.g
@@ -2533,32 +2533,40 @@
 uals(db.getDir()
+, tmpDir
 );%0A    %7D%0A%0A    @T
@@ -2761,21 +2761,40 @@
 %22--db%22, 
-%22/db%22
+tmpDir.getAbsolutePath()
 , %22--def
@@ -3244,31 +3244,22 @@
 tEquals(
-new File(%22/db%22)
+tmpDir
 , db.get

fix tests