package org.csanchez.jenkins.plugins.kubernetes;

import hudson.model.Label;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

import static org.csanchez.jenkins.plugins.kubernetes.PodTemplate.LABEL_DIGEST_FUNCTION;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.junit.Assert.assertEquals;

public class PodTemplateJenkinsTest {
    @Rule
    public JenkinsRule j = new JenkinsRule();

    @Test
    @Issue({"JENKINS-59690", "JENKINS-60537"})
    public void singleLabel() {
        PodTemplate podTemplate = new PodTemplate();
        podTemplate.setLabel("foo");
        Map<String, String> labelsMap = podTemplate.getLabelsMap();
        assertEquals("foo" , labelsMap.get("jenkins/label"));
        LABEL_DIGEST_FUNCTION.update("foo".getBytes(StandardCharsets.UTF_8));
        assertEquals(Arrays.toString(LABEL_DIGEST_FUNCTION.digest()), labelsMap.get("jenkins/label-digest"));
    }

    @Test
    @Issue({"JENKINS-59690", "JENKINS-60537"})
    public void multiLabel() {
        PodTemplate podTemplate = new PodTemplate();
        podTemplate.setLabel("foo bar");
        Map<String, String> labelsMap = podTemplate.getLabelsMap();
        assertEquals("foo_bar", labelsMap.get("jenkins/label"));
        LABEL_DIGEST_FUNCTION.update("foo bar".getBytes(StandardCharsets.UTF_8));
        assertEquals(Arrays.toString(LABEL_DIGEST_FUNCTION.digest()), labelsMap.get("jenkins/label-digest"));
    }
    
    @Test
    @Issue({"JENKINS-59690", "JENKINS-60537"})
    public void defaultLabel() {
        PodTemplate podTemplate = new PodTemplate();
        podTemplate.setLabel(null);
        Map<String, String> labelsMap = podTemplate.getLabelsMap();
        assertEquals("slave-default", labelsMap.get("jenkins/label"));
        assertEquals("0", labelsMap.get("jenkins/label-digest"));
    }

    @Test
    public void jenkinsLabels() {
        KubernetesCloud kubernetesCloud = new KubernetesCloud("kubernetes");
        j.jenkins.clouds.add(kubernetesCloud);
        PodTemplate podTemplate = new PodTemplate();
        kubernetesCloud.addTemplate(podTemplate);
        podTemplate.setLabel("foo bar");
        assertThat(j.jenkins.getLabels().stream()
                .map(Label::getName)
                .collect(Collectors.toSet()),
                containsInAnyOrder("master", "foo", "bar")
        );
    }
}

@@ -195,16 +195,45 @@
 sRule;%0A%0A
+import java.math.BigInteger;%0A
 import j
@@ -1097,32 +1097,57 @@
 tEquals(
-Arrays.toString(
+String.format(%22%25040x%22, new BigInteger(1, 
 LABEL_DI
@@ -1161,32 +1161,33 @@
 NCTION.digest())
+)
 , labelsMap.get(
@@ -1641,24 +1641,49 @@
 als(
-Arrays.toString(
+String.format(%22%25040x%22, new BigInteger(1, 
 LABE
@@ -1709,16 +1709,17 @@
 igest())
+)
 , labels

fix digest label tests