package ekoshkin.teamcity.clouds.kubernetes.podSpec;

import ekoshkin.teamcity.clouds.kubernetes.KubeCloudClientParameters;
import ekoshkin.teamcity.clouds.kubernetes.KubeCloudImage;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.client.KubernetesClientException;
import jetbrains.buildServer.BaseTestCase;
import jetbrains.buildServer.clouds.CloudInstanceUserData;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.Collections;

/**
 * Created by ekoshkin (koshkinev@gmail.com) on 17.06.17.
 */
public class CustomTemplatePodTemplateProviderTest extends BaseTestCase {
    private PodTemplateProvider myPodTemplateProvider;
    private Mockery m;

    @BeforeMethod
    @Override
    protected void setUp() throws Exception {
        super.setUp();
        m = new Mockery();
        myPodTemplateProvider = new CustomTemplatePodTemplateProvider();
    }

    @AfterMethod
    @Override
    protected void tearDown() throws Exception {
        m.assertIsSatisfied();
        super.tearDown();
    }

    @Test
    public void testGetPodTemplate_invalidJSON() throws Exception {
        CloudInstanceUserData instanceTag = createInstanceTag();
        KubeCloudClientParameters clientParams = m.mock(KubeCloudClientParameters.class);
        KubeCloudImage image = m.mock(KubeCloudImage.class);
        m.checking(new Expectations(){{
            allowing(image).getCustomPodTemplateSpec(); will(returnValue("foo"));
        }});
        assertExceptionThrown(() -> myPodTemplateProvider.getPodTemplate(instanceTag, image, clientParams), KubernetesClientException.class);
    }

    @Test
    public void testGetPodTemplate() throws Exception {
        CloudInstanceUserData instanceTag = createInstanceTag();
        KubeCloudClientParameters clientParams = m.mock(KubeCloudClientParameters.class);
        KubeCloudImage image = m.mock(KubeCloudImage.class);
        m.checking(new Expectations(){{
            allowing(image).getCustomPodTemplateSpec(); will(returnValue("{\n" +
                    "\t\t\"metadata\": {\n" +
                    "\t\t\t\"labels\": {\n" +
                    "\t\t\t\t\"app\": \"nginx\"\n" +
                    "\t\t\t}\n" +
                    "\t\t},\n" +
                    "\t\t\"spec\": {\n" +
                    "\t\t\t\"containers\": [\n" +
                    "\t\t\t\t{\n" +
                    "\t\t\t\t\t\"name\": \"nginx\",\n" +
                    "\t\t\t\t\t\"image\": \"nginx:1.7.9\",\n" +
                    "\t\t\t\t\t\"ports\": [\n" +
                    "\t\t\t\t\t\t{\n" +
                    "\t\t\t\t\t\t\t\"containerPort\": 80\n" +
                    "\t\t\t\t\t\t}\n" +
                    "\t\t\t\t\t]\n" +
                    "\t\t\t\t}\n" +
                    "\t\t\t]\n" +
                    "\t\t}\n" +
                    "}"));
        }});
        Pod podTemplate = myPodTemplateProvider.getPodTemplate(instanceTag, image, clientParams);
        assertNotNull(podTemplate);
        assertNotNull(podTemplate.getMetadata());
        assertNotNull(podTemplate.getSpec());
    }

    private CloudInstanceUserData createInstanceTag() {
        return new CloudInstanceUserData("foo", "foo", "foo", null, "foo", "foo", Collections.emptyMap());
    }
}
@@ -2060,32 +2060,192 @@
 xpectations()%7B%7B%0A
+            allowing(clientParams).getNamespace(); will(returnValue(%22custom namespace%22));%0A            allowing(image).getName(); will(returnValue(%22my image%22));%0A
             allo
@@ -3480,45 +3480,93 @@
 ta(%22
-foo%22, %22foo%22, %22foo%22, null, %22foo%22, %22foo
+agent name%22, %22auth token%22, %22server address%22, null, %22profile id%22, %22profile description
 %22, C

fix test