/*
 * The MIT License
 *
 *  Copyright (c) 2018, CloudBees, Inc.
 *
 *  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 com.cloudbees.plugins.credentials.casc;

import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.common.UsernamePasswordCredentials;
import com.cloudbees.plugins.credentials.domains.HostnameRequirement;
import hudson.security.ACL;
import io.jenkins.plugins.casc.ConfigurationContext;
import io.jenkins.plugins.casc.ConfiguratorRegistry;
import io.jenkins.plugins.casc.misc.ConfiguredWithCode;
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
import io.jenkins.plugins.casc.model.Mapping;
import io.jenkins.plugins.casc.model.Sequence;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import jenkins.model.Jenkins;
import org.junit.ClassRule;
import org.junit.Test;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;

public class SystemCredentialsTest {

    @ClassRule
    @ConfiguredWithCode("SystemCredentialsTest.yaml")
    public static JenkinsConfiguredWithCodeRule j = new JenkinsConfiguredWithCodeRule();

    @Test
    public void import_system_credentials() throws Exception {
        List<UsernamePasswordCredentials> ups = CredentialsProvider.lookupCredentials(
            UsernamePasswordCredentials.class, j.jenkins, ACL.SYSTEM,
            Collections.singletonList(new HostnameRequirement("api.test.com"))
        );
        assertThat(ups, hasSize(1));
        final UsernamePasswordCredentials up = ups.get(0);
        assertThat(up.getPassword().getPlainText(), equalTo("password"));
    }

    @Test
    public void export_system_credentials() throws Exception {
        CredentialsRootConfigurator root = Jenkins.getInstance()
            .getExtensionList(CredentialsRootConfigurator.class).get(0);

        ConfiguratorRegistry registry = ConfiguratorRegistry.get();
        ConfigurationContext context = new ConfigurationContext(registry);
        Mapping configNode = Objects
            .requireNonNull(root.describe(root.getTargetComponent(context), context)).asMapping();
        Mapping domainCredentials = configNode
            .get("system").asMapping().get("domainCredentials")
            .asSequence()
            .get(0).asMapping();
        Mapping domain = domainCredentials.get("domain").asMapping();
        Sequence credentials = domainCredentials.get("credentials").asSequence();
        Mapping usernamePassword = credentials.get(0).asMapping().get("usernamePassword")
            .asMapping();
        Mapping hostnameSpecification = domain.get("specifications").asSequence().get(0).asMapping()
            .get("hostnameSpecification").asMapping();

        assertThat(usernamePassword.getScalarValue("scope"), is("SYSTEM"));
        assertThat(usernamePassword.getScalarValue("username"), is("root"));
        assertThat(usernamePassword.getScalarValue("password"), is(not("password")));
        assertThat(domain.getScalarValue("name"), is("test.com"));
        assertThat(domain.getScalarValue("description"), is("test.com domain"));
        assertThat(hostnameSpecification.getScalarValue("includes"), is("*.test.com"));
    }
}

@@ -1442,16 +1442,43 @@
 ty.ACL;%0A
+import hudson.util.Secret;%0A
 import i
@@ -2123,16 +2123,62 @@
 rs.not;%0A
+import static org.junit.Assert.assertNotNull;%0A
 import s
@@ -3989,24 +3989,198 @@
 Mapping();%0A%0A
+        String password = usernamePassword.getScalarValue(%22password%22);%0A        Secret decryptedPassword = Secret.decrypt(password);%0A        assertNotNull(decryptedPassword);%0A
         asse
@@ -4343,60 +4343,96 @@
 hat(
-usernamePassword.getScalarValue(%22password%22
+password, is(not(%22password%22)));%0A        assertThat(decryptedPassword.getPlainText(
 ), is(
-not(
 %22pas
@@ -4431,33 +4431,32 @@
  is(%22password%22))
-)
 ;%0A        assert

test decrypted secret in configuration test