/*
 * Copyright (c) 2013-2016 Cinchapi 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.cinchapi.concourse.server;

import java.nio.file.Files;
import java.nio.file.Paths;
import com.cinchapi.concourse.Concourse;
import com.cinchapi.concourse.Timestamp;
import com.cinchapi.concourse.thrift.Operator;
import com.cinchapi.concourse.util.ConcourseCodebase;

import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;

import com.cinchapi.concourse.server.ManagedConcourseServer;
import com.cinchapi.concourse.server.ManagedConcourseServer.ReflectiveClient;

/**
 * Unit tests for {@link ManagedConcourseServer}.
 * 
 * @author jnelson
 */
public class ManagedConcourseServerTest {

    private ManagedConcourseServer server = null;

    @Rule
    public TestWatcher watcher = new TestWatcher() {

        @Override
        protected void starting(Description description) {
            ConcourseCodebase codebase = ConcourseCodebase.cloneFromGithub();
            String installer = codebase.buildInstaller();
            server = ManagedConcourseServer
                    .manageNewServer(Paths.get(installer).toFile());
        }

        @Override
        protected void finished(Description description) {
            server.destroy();
            server = null;
        }
    };

    @Test
    public void testStart() {
        server.start();
        Assert.assertTrue(server.isRunning());
    }

    @Test
    public void testStop() {
        server.start();
        server.stop();
        Assert.assertFalse(server.isRunning());
    }
    
    @Test
    public void testStopWithClient(){
        server.start();
        server.connect();
        server.stop();
        Assert.assertFalse(server.isRunning());
    }

    @Test
    public void testIsRunning() {
        Assert.assertFalse(server.isRunning());
        server.start();
        Assert.assertTrue(server.isRunning());
    }

    @Test
    public void testDestroy() {
        server.destroy();
        Assert.assertFalse(
                Files.exists(Paths.get(server.getInstallDirectory())));
    }

    @Test
    public void testConnectAndInteract() {
        server.start();
        Concourse concourse = server.connect();
        concourse.add("foo", "bar", 1);
        Assert.assertEquals("bar", concourse.get("foo", 1));
    }

    @Test
    public void testClientFind() {
        server.start();
        Concourse concourse = server.connect();
        concourse.add("foo", 1, 1);
        Assert.assertTrue(
                concourse.find("foo", Operator.EQUALS, 1).contains(1L));
    }

    @Test
    public void testClientFindWithTime() {
        server.start();
        Concourse concourse = server.connect();
        concourse.add("foo", 1, 1);
        Timestamp timestamp = Timestamp.now();
        concourse.add("foo", 1, 2);
        Assert.assertFalse(concourse.find("foo", Operator.EQUALS, 1, timestamp)
                .contains(2L));
    }

    @Test
    public void testAutoUnboxOnClientCall() {
        server.start();
        ReflectiveClient concourse = (ReflectiveClient) server.connect();
        concourse.add("name", "jeff", 1);
        long record = 1;
        String result = concourse.call("get", "name", record);
        Assert.assertEquals("jeff", result);
    }

}

@@ -694,16 +694,40 @@
 .Paths;%0A
+import java.util.List;%0A%0A
 import c
@@ -759,16 +759,16 @@
 course;%0A
-
 import c
@@ -2177,13 +2177,9 @@
   %7D%0A
-    
 %0A
+
     
@@ -2220,16 +2220,17 @@
 Client()
+ 
 %7B%0A      
@@ -3836,16 +3836,16 @@
 ecord);%0A
-
         
@@ -3888,10 +3888,402 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void testExecuteCli() %7B%0A        server.start();%0A        List%3CString%3E stdout = server.executeCli(%22users%22, %22--list-sessions%22);%0A        boolean passed = false;%0A        for (String line : stdout) %7B%0A            if(line.contains(%22Current User Sessions%22)) %7B%0A                passed = true;%0A                break;%0A            %7D%0A        %7D%0A        Assert.assertTrue(passed);%0A    %7D%0A%0A
 %7D%0A

add unit test managed server cli execution