package tw.com.acceptance;

import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import com.amazonaws.services.ec2.AmazonEC2Client;
import com.amazonaws.services.ec2.model.DeleteKeyPairRequest;
import com.amazonaws.services.ec2.model.DescribeKeyPairsRequest;
import com.amazonaws.services.ec2.model.DescribeKeyPairsResult;
import com.amazonaws.services.ec2.model.KeyPairInfo;
import org.junit.BeforeClass;
import org.junit.Test;
import tw.com.CLIArgBuilder;
import tw.com.EnvironmentSetupForTests;
import tw.com.commandline.Main;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertEquals;

public class TestKeyPairCreationAndSave {

    private static AmazonEC2Client ec2Client;

    @BeforeClass
    public static void beforeAllTestsRun() {
        DefaultAWSCredentialsProviderChain credentialsProvider = new DefaultAWSCredentialsProviderChain();
        ec2Client = EnvironmentSetupForTests.createEC2Client(credentialsProvider);
    }

    @Test
    public void shouldCreateKeyPairWithFilename() throws IOException {

        String filename = "testFilenameForPem.tmp";
        Path path = Paths.get(filename);
        Files.deleteIfExists(path);

        String[] args = CLIArgBuilder.createKeyPair(filename);
        Main main = new Main(args);
        int commandResult = main.parse();

        String keypairName = "CfnAssist_Test_keypair";
        DescribeKeyPairsRequest query = new DescribeKeyPairsRequest().withKeyNames(keypairName);
        DescribeKeyPairsResult keysFound = ec2Client.describeKeyPairs(query);
        List<KeyPairInfo> keys = keysFound.getKeyPairs();

        // need to make sure always delete keys

        if (keys.size()>0) {
            DeleteKeyPairRequest deleteRequest = new DeleteKeyPairRequest().withKeyName(keypairName);
            ec2Client.deleteKeyPair(deleteRequest);
        }

        // now do the asserts
        assertEquals(0, commandResult);
        assertEquals(1, keys.size());
        assertEquals(keypairName, keys.get(0).getKeyName());

        assertTrue(Files.exists(path));

        Files.deleteIfExists(path);
    }
}

@@ -21,16 +21,61 @@
 tance;%0A%0A
+import com.amazonaws.AmazonServiceException;%0A
 import c
@@ -710,16 +710,45 @@
 .Paths;%0A
+import java.util.LinkedList;%0A
 import j
@@ -1289,16 +1289,100 @@
 eption %7B
+%0A        String keypairName = %22CfnAssist_Test%22;%0A%0A        deleteKeyPair(keypairName);
 %0A%0A      
@@ -1659,55 +1659,432 @@
     
-String keypairName = %22CfnAssist_Test_keypair%22;%0A
+List%3CKeyPairInfo%3E keys = deleteKeyPair(keypairName);%0A%0A        // now do the asserts%0A        assertEquals(0, commandResult);%0A        assertEquals(1, keys.size());%0A        assertEquals(keypairName, keys.get(0).getKeyName());%0A%0A        assertTrue(Files.exists(path));%0A%0A        Files.deleteIfExists(path);%0A    %7D%0A%0A    private List%3CKeyPairInfo%3E deleteKeyPair(String keypairName) %7B%0A        List%3CKeyPairInfo%3E keys;%0A        try %7B%0A    
     
@@ -2176,32 +2176,36 @@
 rName);%0A        
+    
 DescribeKeyPairs
@@ -2266,33 +2266,19 @@
         
-List%3CKeyPairInfo%3E
+   
  keys = 
@@ -2302,25 +2302,24 @@
 airs();%0A
-%0A
         
 // need 
@@ -2314,48 +2314,110 @@
     
-// need to make sure always delete keys%0A
+%7D catch (AmazonServiceException exception) %7B%0A            keys = new LinkedList%3C%3E();%0A        %7D%0A        
 %0A   
@@ -2436,17 +2436,19 @@
 s.size()
-%3E
+ %3E 
 0) %7B%0A   
@@ -2608,25 +2608,24 @@
       %7D%0A
-%0A
         
 // now d
@@ -2620,245 +2620,19 @@
     
-// now do the asserts%0A        assertEquals(0, commandResult);%0A        assertEquals(1, keys.size());%0A        assertEquals(keypairName, keys.get(0).getKeyName());%0A%0A        assertTrue(Files.exists(path));%0A%0A        Files.deleteIfExists(path)
+return keys
 ;%0A  

Make sure acceptance test copes with keypair already existing