package io.subutai.core.localpeer.impl.container;


import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import io.subutai.common.command.CommandException;
import io.subutai.common.command.CommandResult;
import io.subutai.common.command.CommandUtil;
import io.subutai.common.command.RequestBuilder;
import io.subutai.common.peer.ContainerHost;
import io.subutai.common.peer.ResourceHost;
import io.subutai.common.peer.ResourceHostException;
import io.subutai.common.protocol.TemplateKurjun;
import io.subutai.common.settings.Common;
import io.subutai.core.hostregistry.api.HostRegistry;

import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;


@RunWith( MockitoJUnitRunner.class )
public class CreateContainerTaskTest
{
    private static final String HOSTNAME = "hostname";
    private static final String IP = "127.0.0.1";
    private static final String CIDR = "192.168.1.0/24";
    private static final int VLAN = 100;
    private static final String ENV_ID = "123";
    private static final String TEMPLATE_NAME = "master";
    private static final int TIMEOUT = 30;
    private static final String OUT = "TEMPLATE\n" + "--------\n" + "master";

    @Mock
    ResourceHost resourceHost;
    @Mock
    TemplateKurjun template;
    @Mock
    CommandUtil commandUtil;
    @Mock
    CommandResult commandResult;
    @Mock
    ContainerHost containerHost;

    CreateContainerTask task;
    @Mock
    private HostRegistry hostRegistry;


    @Before
    public void setUp() throws Exception
    {
        task = new CreateContainerTask( hostRegistry, resourceHost, template, HOSTNAME, CIDR, VLAN,
                TIMEOUT/*, ENV_ID*/ );
        task.commandUtil = commandUtil;
        when( resourceHost.execute( any( RequestBuilder.class ) ) ).thenReturn( commandResult );
        when( commandResult.hasSucceeded() ).thenReturn( true );
        when( commandResult.getStdOut() ).thenReturn( OUT );
        when( template.getName() ).thenReturn( TEMPLATE_NAME );
        when( resourceHost.getContainerHostByName( HOSTNAME ) ).thenReturn( containerHost );
        when( containerHost.getIpByInterfaceName( Common.DEFAULT_CONTAINER_INTERFACE ) ).thenReturn( IP );
    }


    @Test( expected = ResourceHostException.class )
    public void testPrepareTemplate() throws Exception
    {
        when( commandResult.getStdOut() ).thenReturn( "" );
        task.prepareTemplate( template );
    }


    @Test( expected = ResourceHostException.class )
    public void testIsTemplateExists() throws Exception
    {
        assertTrue( task.templateExists( template ) );

        when( commandResult.getStdOut() ).thenReturn( "" );

        assertFalse( task.templateExists( template ) );

        doThrow( new CommandException( "" ) ).when( resourceHost ).execute( any( RequestBuilder.class ) );

        task.templateExists( template );
    }


    @Test( expected = ResourceHostException.class )
    public void testImportTemplate() throws Exception
    {
        task.importTemplate( template );

        verify( commandUtil ).execute( any( RequestBuilder.class ), eq( resourceHost ) );

        doThrow( new CommandException( "" ) ).when( commandUtil )
                                             .execute( any( RequestBuilder.class ), eq( resourceHost ) );

        task.importTemplate( template );
    }


    @Test
    @Ignore
    public void testCall() throws Exception
    {
        task.call();

        //        verify( containerHost ).setDefaultGateway( GATEWAY );

        CreateContainerTask task = new CreateContainerTask( hostRegistry, resourceHost, template, HOSTNAME, CIDR, VLAN,
                TIMEOUT/*, ENV_ID*/ );
        task.commandUtil = commandUtil;

        task.call();

        //        verify( containerHost, times( 2 ) ).setDefaultGateway( GATEWAY );
    }
}

@@ -228,59 +228,8 @@
 r;%0A%0A
-import io.subutai.common.command.CommandException;%0A
 impo
@@ -460,61 +460,8 @@
 st;%0A
-import io.subutai.common.peer.ResourceHostException;%0A
 impo
@@ -621,258 +621,31 @@
 tic 
-junit.framework.TestCase.assertFalse;%0Aimport static junit.framework.TestCase.assertTrue;%0Aimport static org.mockito.Matchers.any;%0Aimport static org.mockito.Matchers.eq;%0Aimport static org.mockito.Mockito.doThrow;%0Aimport static org.mockito.Mockito.verif
+org.mockito.Matchers.an
 y;%0Ai
@@ -1641,32 +1641,16 @@
 LAN,
-%0A               
  TIMEOUT
 /*, 
@@ -1641,34 +1641,32 @@
 LAN, TIMEOUT
-/*
 , ENV_ID
 */ );%0A      
@@ -1645,34 +1645,32 @@
  TIMEOUT, ENV_ID
-*/
  );%0A        task
@@ -2205,273 +2205,20 @@
 Test
-( expected = ResourceHostException.class )%0A    public void testPrepareTemplate() throws Exception%0A    %7B%0A        when( commandResult.getStdOut() ).thenReturn( %22%22 );%0A        task.prepareTemplate( template );%0A    %7D%0A%0A%0A    @Test( expected = ResourceHostException.class )
+%0A    @Ignore
 %0A   
@@ -2238,24 +2238,12 @@
 test
-IsTemplateExists
+Call
 () t
@@ -2276,891 +2276,56 @@
     
-assertTrue( task.templateExists( template ) );%0A%0A        when( commandResult.getStdOut() ).thenReturn( %22%22 );%0A%0A        assertFalse( task.templateExists( template ) );%0A%0A        doThrow( new CommandException( %22%22 ) ).when( resourceHost ).execute( any( RequestBuilder.class ) );%0A%0A        task.templateExists( template );%0A    %7D%0A%0A%0A    @Test( expected = ResourceHostException.class )%0A    public void testImportTemplate() throws Exception%0A    %7B%0A        task.importTemplate( template );%0A%0A        verify( commandUtil ).execute( any( RequestBuilder.class ), eq( resourceHost ) );%0A%0A        doThrow( new CommandException( %22%22 ) ).when( commandUtil )%0A                                             .execute( any( RequestBuilder.class ), eq( resourceHost ) );%0A%0A        task.importTemplate( template );%0A    %7D%0A%0A%0A    @Test%0A    @Ignore%0A    public void testCall() throws Exception%0A    %7B%0A        task.call();%0A
+task.call();%0A%0A        CreateContainerTask task =
 %0A   
@@ -2333,107 +2333,15 @@
     
-//
        
- verify( containerHost ).setDefaultGateway( GATEWAY );%0A%0A        CreateContainerTask task =
  new
@@ -2425,32 +2425,16 @@
 LAN,
-%0A               
  TIMEOUT
 /*, 
@@ -2433,18 +2433,16 @@
 EOUT
-/*
 , ENV_ID
 */ )
@@ -2441,10 +2441,8 @@
 V_ID
-*/
  );%0A
@@ -2506,93 +2506,8 @@
 l();
-%0A%0A        //        verify( containerHost, times( 2 ) ).setDefaultGateway( GATEWAY );
 %0A   

update clone task SS-3083