/*
 * Copyright 2017 flow.ci
 *
 * 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.flow.platform.plugin.test.service;

import com.flow.platform.plugin.domain.Plugin;
import com.flow.platform.plugin.exception.PluginException;
import com.flow.platform.plugin.service.PluginService;
import com.flow.platform.plugin.service.PluginServiceImpl;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
 * @author yh@firim
 */
public class PluginServiceTest {

    private final static String PLUGIN_SOURCE_URL = "https://raw.githubusercontent.com/yunheli/plugins/master/repository.json";
    private final static String ERROR_PLUGIN_SOURCE_URL = "https://raw.githubusercontent.com/yunheli/plugins/master/repository_error.json";

    private PluginService pluginService;

    @Before
    public void init() throws IOException {
        Path dest = Paths.get("/tmp/test");
        if(!dest.toFile().exists()){
            Files.createDirectories(dest);
        }

        pluginService = new PluginServiceImpl(
            PLUGIN_SOURCE_URL,
            dest,
            dest,
            dest,
            new ThreadPoolExecutor(5, 5, 5, TimeUnit.HOURS, new LinkedBlockingQueue<>(1000)));
    }

    @Test
    public void should_get_plugins_success() {

        // when: get plugins list
        List<Plugin> pluginList = pluginService.list();

        // then: pluginList not null
        Assert.assertNotNull(pluginList);

        // then: pluginList size is not 0
        Assert.assertNotEquals(0, pluginList.size());

        // then: pluginList size is 2
        Assert.assertEquals(2, pluginList.size());
    }

    @Test(expected = PluginException.class)
    public void should_get_plugins_throw_exceptions() {

//        pluginService = new PluginServiceImpl(ERROR_PLUGIN_SOURCE_URL);
//
//        //then: should throw PluginException
//        pluginService.list();
    }

    @Test
    public void should_install_success() {
//        pluginService = new PluginServiceImpl(PLUGIN_SOURCE_URL,
//            Paths.get("/tmp"), Paths.get("/tmp"),
//            new ThreadPoolExecutor(1, 1, 600, TimeUnit.SECONDS, new LinkedBlockingQueue<>()));
//
////        pluginService.doInstall("fircli");
//        File file = new File("/tmp/test/ecx");
//        try {
//            Git git = Git.init().setDirectory(file).call();
//        } catch (GitAPIException e) {
//            e.printStackTrace();
//        }
    }
}

@@ -679,16 +679,69 @@
 Plugin;%0A
+import com.flow.platform.plugin.domain.PluginStatus;%0A
 import c
@@ -1731,16 +1731,17 @@
       if
+ 
 (!dest.t
@@ -1757,16 +1757,17 @@
 xists())
+ 
 %7B%0A      
@@ -2570,19 +2570,60 @@
 ons() %7B%0A
-%0A//
+        Path dest = Paths.get(%22/tmp/test%22);%0A
         
@@ -2660,16 +2660,29 @@
 iceImpl(
+%0A            
 ERROR_PL
@@ -2696,24 +2696,168 @@
 URCE_URL
-);%0A//%0A//
+,%0A            dest,%0A            dest,%0A            dest,%0A            new ThreadPoolExecutor(5, 5, 5, TimeUnit.HOURS, new LinkedBlockingQueue%3C%3E(1000)));%0A%0A
         
@@ -2893,18 +2893,16 @@
 ception%0A
-//
         
@@ -2967,15 +2967,14 @@
 uld_
-install
+update
 _suc
@@ -2982,26 +2982,24 @@
 ess() %7B%0A
-//
         
 pluginSe
@@ -2994,221 +2994,104 @@
     
-p
+P
 lugin
-Service = new PluginServiceImpl(PLUGIN_SOURCE_URL,%0A//            Paths.get(%22/tmp%22), Paths.get(%22/tmp%22),%0A//            new ThreadPoolExecutor(1, 1, 600, TimeUnit.SECONDS, new LinkedBlockingQueue%3C%3E()));%0A//%0A////
+ plugin = pluginService.find(%22fircli%22);%0A        plugin.setStatus(PluginStatus.INSTALLED);%0A
     
@@ -3112,39 +3112,33 @@
 ice.
-doInstall(%22fircli%22);%0A//
+update(plugin);%0A%0A
         
 File
@@ -3137,57 +3137,63 @@
     
-File file = new File(%22/tmp/test/ecx%22
+plugin = pluginService.find(plugin.getName()
 );%0A
-//
         
 try 
@@ -3192,162 +3192,72 @@
     
-try %7B%0A//            Git git = Git.init().setDirectory(file).call();%0A//        %7D catch (GitAPIException e) %7B%0A//            e.printStackTrace();%0A//        %7D
+Assert.assertEquals(PluginStatus.INSTALLED, plugin.getStatus());
 %0A   

add update test