/*
 * The MIT License
 *
 * Copyright (c) 2015, 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 org.jenkinsci.plugins.docker.commons.fingerprint;

import hudson.Util;
import hudson.model.Fingerprint;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.tasks.Fingerprinter;
import hudson.tasks.Shell;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

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

/**
 * @author <a href="mailto:tom.fennelly@gmail.com">tom.fennelly@gmail.com</a>
 */
public class DockerRunFingerprintFacetTest {
       
    @Rule
    public JenkinsRule rule = new JenkinsRule();
    
    @Test
    public void test_readResolve() throws Exception {
        FreeStyleProject p = createFreeStyleProjectWithFingerprints();
        FreeStyleBuild b = rule.assertBuildStatusSuccess(p.scheduleBuild2(0));
        
        Fingerprint f = rule.jenkins._getFingerprint(Util.getDigestOf(singleContents[0] + "\n"));
        DockerRunFingerprintFacet facet = new DockerRunFingerprintFacet(f, System.currentTimeMillis(), "xxxxx");

        ContainerRecord r = new ContainerRecord("192.168.1.10", "cid", null, "magic", System.currentTimeMillis(), Collections.<String, String>emptyMap());
        facet.add(r);

        Assert.assertNull(r.getImageId());
        facet.readResolve();
        Assert.assertEquals("xxxxx", r.getImageId());
        
        System.out.println();
    }


    // Lifted from FingerprinterTest in Jenkins core
    private FreeStyleProject createFreeStyleProjectWithFingerprints() throws IOException, Exception {
        FreeStyleProject project = rule.createFreeStyleProject();
        addFingerprinterToProject(project);
        return project;
    }
    private void addFingerprinterToProject(FreeStyleProject project) throws Exception {
        StringBuilder targets = new StringBuilder();
        for (int i = 0; i < singleContents.length; i++) {
            project.getBuildersList().add(new Shell("echo " + singleContents[i] + " > " + singleFiles[i]));
            targets.append(singleFiles[i]).append(',');
        }
        project.getPublishersList().add(new Fingerprinter(targets.toString(), false));
    }
    
    private static final String[] singleContents = {
        "abcdef"
    };
    private static final String[] singleFiles = {
        "test.txt"
    };
}

@@ -1199,28 +1199,8 @@
 t;%0A%0A
-import hudson.Util;%0A
 impo
@@ -1306,70 +1306,8 @@
 ct;%0A
-import hudson.tasks.Fingerprinter;%0Aimport hudson.tasks.Shell;%0A
 impo
@@ -1420,36 +1420,8 @@
 e;%0A%0A
-import java.io.IOException;%0A
 impo
@@ -1743,16 +1743,21 @@
 ect p = 
+rule.
 createFr
@@ -1770,33 +1770,23 @@
 eProject
-WithFingerprints(
+(%22test%22
 );%0A     
@@ -1880,1314 +1880,515 @@
     
-Fingerprint f = rule.jenkins._getFingerprint(Util.getDigestOf(singleContents%5B0%5D + %22%5Cn%22));%0A        DockerRunFingerprintFacet facet = new DockerRunFingerprintFacet(f, System.currentTimeMillis(), %22xxxxx%22);%0A%0A        ContainerRecord r = new ContainerRecord(%22192.168.1.10%22, %22cid%22, null, %22magic%22, System.currentTimeMillis(), Collections.%3CString, String%3EemptyMap());%0A        facet.add(r);%0A%0A        Assert.assertNull(r.getImageId());%0A        facet.readResolve();%0A        Assert.assertEquals(%22xxxxx%22, r.getImageId());%0A        %0A        System.out.println();%0A    %7D%0A%0A%0A    // Lifted from FingerprinterTest in Jenkins core%0A    private FreeStyleProject createFreeStyleProjectWithFingerprints() throws IOException, Exception %7B%0A        FreeStyleProject project = rule.createFreeStyleProject();%0A        addFingerprinterToProject(project);%0A        return project;%0A    %7D%0A    private void addFingerprinterToProject(FreeStyleProject project) throws Exception %7B%0A        StringBuilder targets = new StringBuilder
+ContainerRecord r = new ContainerRecord(%22192.168.1.10%22, %22cid%22, null, %22magic%22, System.currentTimeMillis(), Collections.%3CString, String%3EemptyMap());%0A        DockerFingerprints.addRunFacet(IMAGE_ID, r, b);%0A        Fingerprint fingerprint = DockerFingerprints.of(IMAGE_ID);%0A        DockerRunFingerprintFacet facet = (DockerRunFingerprintFacet) fingerprint.getFacets().iterator().next
 ();%0A
-  
+%0A
       
-for (int i = 0; i %3C singleContents.length; i++) %7B%0A            project.getBuildersList().add(new Shell(%22echo %22 + singleContents%5Bi%5D + %22 %3E %22 + singleFiles%5Bi%5D));%0A            targets.append(singleFiles%5Bi%5D).append(',');%0A        %7D%0A        project.getPublishersList().add(new Fingerprinter(targets.toString(), false
+  Assert.assertNull(r.getImageId());%0A        facet.readResolve();%0A        Assert.assertEquals(IMAGE_ID, r.getImageId(
 ));%0A
@@ -2393,20 +2393,16 @@
 ;%0A    %7D%0A
-    
 %0A    pri
@@ -2417,140 +2417,96 @@
 tic 
-final String%5B%5D singleContents = %7B%0A        %22abcdef%22%0A    %7D;%0A    private static final String%5B%5D singleFiles = %7B%0A        %22test.txt%22%0A
+String IMAGE_ID = %220409d3ebf4f571d7dd2cf4b00f9d897f8af1d6d8a0f1ff791d173ba9891fd72f%22;
     
-%7D;
 %0A%7D%0A

Simplify DockerRunFingerprintFacetTest.readResolve a bit