/*
 * Copyright (c) 2014 Spotify AB.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.spotify.helios.testing;


import com.spotify.helios.common.descriptors.Job;
import com.spotify.helios.common.descriptors.JobId;

import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

import static java.util.Arrays.asList;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.experimental.results.PrintableResult.testResult;
import static org.junit.experimental.results.ResultMatchers.isSuccessful;

public class HeliosSoloDeploymentTest {

  @Test
  public void heliosSoloDeploymentTest() {
    assertThat(testResult(HeliosSoloDeploymentTestImpl.class), isSuccessful());
  }


  public static class HeliosSoloDeploymentTestImpl {

    public static final String BUSYBOX = "busybox:latest";
    public static final List<String> IDLE_COMMAND = asList(
        "sh", "-c", "trap 'exit 0' SIGINT SIGTERM; while :; do sleep 1; done");

    private static final Logger log = LoggerFactory.getLogger(HeliosSoloDeploymentTestImpl.class);

    // TODO(negz): We want one deployment per test run, not one per test class.
    @ClassRule
    public static final HeliosDeploymentResource DEPLOYMENT = new HeliosDeploymentResource(
        HeliosSoloDeployment.fromEnv().build());

    @Rule
    public final TemporaryJobs temporaryJobs = TemporaryJobs.builder()
        .jobPrefix("HeliosSoloDeploymentTest")
        .client(DEPLOYMENT.client())
        .build();

    @Test
    public void testDeployToSolo() throws Exception {
      temporaryJobs.job()
          .command(IDLE_COMMAND)
          .deploy();

      final Map<JobId, Job> jobs = DEPLOYMENT.client().jobs().get(15, SECONDS);
      log.info("{} jobs deployed on helios-solo", jobs.size());
      for (Job job : jobs.values()) {
        log.info("job on helios-solo: {}", job);
      }

      assertEquals("wrong number of jobs running", 1, jobs.size());
      for (Job j : jobs.values()) {
        assertEquals("wrong job running", BUSYBOX, j.getImage());
      }
    }
  }
}

@@ -872,31 +872,8 @@
 y;%0A%0A
-import java.util.List;%0A
 impo
@@ -890,16 +890,16 @@
 il.Map;%0A
+
 %0Aimport 
@@ -1486,174 +1486,47 @@
 ing 
-BUSYBOX = %22busybox:latest%22;%0A    public static final List%3CString%3E IDLE_COMMAND = asList(%0A        %22sh%22, %22-c%22, %22trap 'exit 0' SIGINT SIGTERM; while :; do sleep 1; done%22)
+IMAGE_NAME = %22onescience/alpine:latest%22
 ;%0A%0A 
@@ -2158,20 +2158,166 @@
 and(
-IDLE_COMMAND
+asList(%22sh%22, %22-c%22, %22nc -l -v -p 4711 -e true%22))%0A          .image(IMAGE_NAME)%0A          .port(%22netcat%22, 4711)%0A          .registration(%22foobar%22, %22tcp%22, %22netcat%22
 )%0A  
@@ -2726,15 +2726,18 @@
 g%22, 
-BUSYBOX
+IMAGE_NAME
 , j.

address race condition in HeliosSoloDeploymentTest