/*
 * The MIT License
 *
 * Copyright (c) 2004-, Kohsuke Kawaguchi, Sun Microsystems, Inc., and a number of other of contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package hudson.plugins.ec2;

import com.amazonaws.services.ec2.AmazonEC2;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.mockito.Mockito;
import org.xml.sax.SAXException;

import hudson.util.VersionNumber;
import jenkins.model.Jenkins;

import java.io.IOException;
import java.util.Collections;

import static org.junit.Assert.assertEquals;

/**
 * @author Kohsuke Kawaguchi
 */
public class AmazonEC2CloudTest {

    @Rule
    public JenkinsRule r = new JenkinsRule();

    private AmazonEC2Cloud cloud;

    @Before
    public void setUp() throws Exception {
        cloud = new AmazonEC2Cloud("us-east-1", true, "abc", "us-east-1", null, "ghi", "3", Collections.emptyList(), "roleArn", "roleSessionName");
        r.jenkins.clouds.add(cloud);
    }

    @Test
    public void testConfigRoundtrip() throws Exception {
        r.submit(getConfigForm());
        r.assertEqualBeans(cloud, r.jenkins.clouds.get(AmazonEC2Cloud.class), "cloudName,region,useInstanceProfileForCredentials,privateKey,instanceCap,roleArn,roleSessionName");
    }

    @Test
    public void testAmazonEC2FactoryGetInstance() throws Exception {
        r.configRoundtrip();
        AmazonEC2Cloud cloud = r.jenkins.clouds.get(AmazonEC2Cloud.class);
        AmazonEC2 connection = cloud.connect();
        Assert.assertNotNull(connection);
        Assert.assertTrue(Mockito.mockingDetails(connection).isMock());
    }

    @Test
    public void testSshKeysCredentialsIdRemainsUnchangedAfterUpdatingOtherFields() throws Exception {
        HtmlForm form = getConfigForm();
        HtmlTextInput input = form.getInputByName("_.cloudName");

        input.setText("test-cloud-2");
        r.submit(form);
        AmazonEC2Cloud actual = r.jenkins.clouds.get(AmazonEC2Cloud.class);
        assertEquals("test-cloud-2", actual.getCloudName());
        r.assertEqualBeans(cloud, actual, "region,useInstanceProfileForCredentials,sshKeysCredentialsId,instanceCap,roleArn,roleSessionName");
    }

    private HtmlForm getConfigForm() throws IOException, SAXException {
        if (Jenkins.getVersion().isNewerThanOrEqualTo(new VersionNumber("2.205"))) {
            return r.createWebClient().goTo("configureClouds").getFormByName("config");
        } else {
            return r.createWebClient().goTo("configure").getFormByName("config");
        }
    }

}

@@ -1276,16 +1276,402 @@
 zonEC2;%0A
+import com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey;%0Aimport com.cloudbees.plugins.credentials.CredentialsProvider;%0Aimport com.cloudbees.plugins.credentials.CredentialsScope;%0Aimport com.cloudbees.plugins.credentials.CredentialsStore;%0Aimport com.cloudbees.plugins.credentials.SystemCredentialsProvider;%0Aimport com.cloudbees.plugins.credentials.domains.Domain;%0A
 import c
@@ -1771,16 +1771,49 @@
 tInput;%0A
+import hudson.util.ListBoxModel;%0A
 import o
@@ -2150,33 +2150,171 @@
 org.
-junit.Assert.assertEquals
+hamcrest.MatcherAssert.assertThat;%0Aimport static org.hamcrest.Matchers.*;%0Aimport static org.junit.Assert.assertEquals;%0Aimport static org.junit.Assert.assertNotNull
 ;%0A%0A/
@@ -3929,24 +3929,1192 @@
 e%22);%0A    %7D%0A%0A
+    @Test%0A    public void testSshCredentials() throws IOException %7B%0A        AmazonEC2Cloud actual = r.jenkins.clouds.get(AmazonEC2Cloud.class);%0A        AmazonEC2Cloud.DescriptorImpl descriptor = (AmazonEC2Cloud.DescriptorImpl) actual.getDescriptor();%0A        assertNotNull(descriptor);%0A        ListBoxModel m = descriptor.doFillSshKeysCredentialsIdItems(%22%22);%0A        assertThat(m.size(), is(1));%0A        BasicSSHUserPrivateKey sshKeyCredentials = new BasicSSHUserPrivateKey(CredentialsScope.SYSTEM, %22ghi%22, %22key%22,%0A                new BasicSSHUserPrivateKey.DirectEntryPrivateKeySource(%22somekey%22), %22%22, %22%22);%0A        for (CredentialsStore credentialsStore: CredentialsProvider.lookupStores(r.jenkins)) %7B%0A            if (credentialsStore instanceof  SystemCredentialsProvider.StoreImpl) %7B%0A                    credentialsStore.addCredentials(Domain.global(), sshKeyCredentials);%0A            %7D%0A        %7D%0A        //Ensure added credential is displayed%0A        m = descriptor.doFillSshKeysCredentialsIdItems(%22%22);%0A        assertThat(m.size(), is(2));%0A        //Ensure that the cloud can resolve the new key%0A        assertThat(actual.resolvePrivateKey(), notNullValue());%0A    %7D%0A%0A
     private 

Add an ssh credential test (#523)