package com.herokuapp.directto.client;

import com.herokuapp.directto.client.models.Pipeline;
import org.junit.Test;

import java.io.File;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.*;

/**
 * @author Ryan Brainard
 */
public class DirectToHerokuClientTest {

    private final String apiKey = System.getProperty("heroku.apiKey");
    private final String appName = System.getProperty("heroku.appName");
    private final String warFilePath = System.getProperty("heroku.warFile");
    private final DirectToHerokuClient client = new DirectToHerokuClient("http", "direct-to.herokuapp.com", 80, apiKey);

    @Test
    public void testGetPipelineNames() throws Exception {
        final Collection<String> pipelineNames = client.getPipelineNames();
        assertTrue(pipelineNames.contains("war"));
        assertTrue(pipelineNames.contains("fatjar"));
    }

    @Test
    public void testGetPipeline() throws Exception {
        final Pipeline pipeline = client.getPipeline("war");
        assertEquals("war", pipeline.getName());
        assertEquals("POST", pipeline.getCall().getMethod());
        assertEquals("/direct/<your app>/war", pipeline.getCall().getUrl());
        assertEquals("Directly deploy a war that will be executed with tomcat runner", pipeline.getManifest().getDescription());
        assertEquals("The war file to be deployed alongside tomcat runner", pipeline.getManifest().getRequiredFileInfo().get("war"));
    }

    @Test
    public void testDeploy() throws Exception {
        final File warFile = new File(warFilePath);
        assertTrue("Precondition", warFile.exists());

        final Map<String, File> files = new HashMap<String, File>(1);
        files.put("war", warFile);

        assertEquals("success", client.deploy("war", appName, files).get("status"));
    }

    @Test
    public void testAsyncDeploy() throws Exception {
        final File warFile = new File(warFilePath);
        assertTrue("Precondition", warFile.exists());

        final Map<String, File> files = new HashMap<String, File>(1);
        files.put("war", warFile);

        assertEquals("success", client.deployAsync("war", appName, files).get().get("status"));
    }

    @Test
    public void testVerify_Pass() throws Exception {
        final File warFile = new File(warFilePath);
        assertTrue("Precondition", warFile.exists());

        final Map<String, File> files = new HashMap<String, File>(1);
        files.put("war", warFile);

        client.verify("war", appName, files);
    }

    @Test
    public void testVerify_InvalidPipelineName() throws Exception {
        try {
            client.verify("BLAH", "anApp", null);
            fail();
        } catch (VerificationException e) {
            assertEquals("[Invalid pipeline name: BLAH]", e.getMessage());
        }
    }

    @Test
    public void testVerify_InvalidMisc() throws Exception {
        final HashMap<String, File> files = new HashMap<String, File>();
        files.put("meaningless", new File("i'm not really here"));
        try {
            client.verify("fatjar", "", files);
            fail();
        } catch (VerificationException e) {
            assertEquals("[App name must be populated, " +
                    "Required file not specified: jar (the fat jar), " +
                    "Required file not specified: procfile (The Procfile), " +
                    "File not found for: meaningless (i'm not really here)]",
                    e.getMessage());
        }
    }
}


@@ -87,16 +87,41 @@
 peline;%0A
+import org.junit.Before;%0A
 import o
@@ -344,16 +344,129 @@
 Test %7B%0A%0A
+    public static final String WAR_PIPELINE = %22war%22;%0A    public static final String FATJAR_PIPELINE = %22fatjar%22;%0A%0A
     priv
@@ -796,24 +796,70 @@
 , apiKey);%0A%0A
+    @Before%0A    public void setUp() %7B%0A%0A    %7D%0A%0A
     @Test%0A  
@@ -1032,21 +1032,28 @@
 ontains(
-%22war%22
+WAR_PIPELINE
 ));%0A    
@@ -1090,24 +1090,31 @@
 ontains(
-%22fatjar%22
+FATJAR_PIPELINE
 ));%0A    
@@ -1232,21 +1232,28 @@
 ipeline(
-%22war%22
+WAR_PIPELINE
 );%0A     
@@ -1268,21 +1268,28 @@
 tEquals(
-%22war%22
+WAR_PIPELINE
 , pipeli
@@ -1782,580 +1782,173 @@
     
-final File warFile = new File(warFilePath);%0A        assertTrue(%22Precondition%22, warFile.exists());%0A%0A        final Map%3CString, File%3E files = new HashMap%3CString, File%3E(1);%0A        files.put(%22war%22, warFile);%0A%0A        assertEquals(%22success%22, client.deploy(%22war%22, appName, files).get(%22status%22));%0A    %7D%0A%0A    @Test%0A    public void testAsyncDeploy() throws Exception %7B%0A        final File warFile = new File(warFilePath);%0A        assertTrue(%22Precondition%22, warFile.exists());%0A%0A        final Map%3CString, File%3E files = new HashMap%3CString, File%3E(1);%0A        files.put(%22war%22, warFile);%0A
+assertEquals(%22success%22, client.deploy(WAR_PIPELINE, appName, createWarBundle()).get(%22status%22));%0A    %7D%0A%0A    @Test%0A    public void testAsyncDeploy() throws Exception %7B
 %0A   
@@ -1995,37 +1995,56 @@
 oyAsync(
-%22war%22, appName, files
+WAR_PIPELINE, appName, createWarBundle()
 ).get().
@@ -2141,256 +2141,62 @@
     
-final File warFile = new File(warFilePath);%0A        assertTrue(%22Precondition%22, warFile.exists());%0A%0A        final Map%3CString, File%3E files = new HashMap%3CString, File%3E(1);%0A        files.put(%22war%22, warFile);%0A%0A        client.verify(%22war%22, appName, files
+client.verify(WAR_PIPELINE, appName, createWarBundle()
 );%0A 
@@ -3176,12 +3176,303 @@
  %7D%0A    %7D
+%0A%0A    private Map%3CString, File%3E createWarBundle() %7B%0A        final File warFile = new File(warFilePath);%0A        assertTrue(%22Precondition%22, warFile.exists());%0A%0A        final Map%3CString, File%3E files = new HashMap%3CString, File%3E(1);%0A        files.put(%22war%22, warFile);%0A        return files;%0A    %7D
 %0A%7D%0A%0A

extract out some duplication in tests