package com.spotify.spydra.submitter.api;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.google.common.collect.ImmutableList;
import com.spotify.spydra.api.DataprocAPI;
import com.spotify.spydra.api.model.Cluster;
import com.spotify.spydra.model.SpydraArgument;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Optional;
import org.junit.Before;
import org.junit.Test;

/**
 * Created by TwN on 2017-04-20.
 */
public class DynamicSubmitterTest {

  private static final String spydraClusterName = "spydra-uuid";

  DynamicSubmitter dynamicSubmitter;
  DataprocAPI dataprocAPI;
  SpydraArgument arguments;

  @Before
  public void before() {
    dynamicSubmitter = new DynamicSubmitter();
    dataprocAPI = mock(DataprocAPI.class);
    arguments = new SpydraArgument();
  }

  static Cluster perfectCluster() {
    Cluster cluster = new Cluster();
    cluster.clusterName = spydraClusterName;
    Cluster.Status status = new Cluster.Status();
    status.state = Cluster.Status.RUNNING;
    status.stateStartTime = ZonedDateTime.now(ZoneOffset.UTC);
    cluster.status = status;
    cluster.config.gceClusterConfig.metadata.heartbeat =
            Optional.of(cluster.status.stateStartTime.plusMinutes(60));
    return cluster;
  }

  static Cluster errorCluster() {
    Cluster cluster = perfectCluster();
    cluster.status.state = Cluster.Status.ERROR;
    return cluster;
  }

  @Test
  public void randomizeZoneIfAbsent() throws Exception {
    String expected = "default_zone";
    arguments.defaultZones = ImmutableList.of(expected);

    dynamicSubmitter.randomizeZoneIfAbsent(arguments);
    assertEquals(expected, arguments.cluster.getOptions().get("zone"));
  }

  @Test
  public void releaseErrorCluster() throws Exception {
    arguments.cluster.setName(spydraClusterName);
    when(dataprocAPI.listClusters(arguments)).thenReturn(ImmutableList.of(errorCluster()));
    when(dataprocAPI.deleteCluster(arguments)).thenReturn(true);
    assertTrue("A broken cluster should be collected without problems.", dynamicSubmitter.releaseCluster(arguments, dataprocAPI));
    verify(dataprocAPI).deleteCluster(arguments);
  }
}

@@ -155,19 +155,141 @@
 to.M
-ockito.mock
+atchers.anyMap;%0Aimport static org.mockito.Matchers.eq;%0Aimport static org.mockito.Mockito.mock;%0Aimport static org.mockito.Mockito.when
 ;%0Aim
@@ -331,48 +331,8 @@
 ify;
-%0Aimport static org.mockito.Mockito.when;
 %0A%0Aim
@@ -517,96 +517,8 @@
 nt;%0A
-import java.time.ZoneOffset;%0Aimport java.time.ZonedDateTime;%0Aimport java.util.Optional;%0A
 impo
@@ -797,16 +797,35 @@
 guments;
+%0A  String clientId;
 %0A%0A  @Bef
@@ -987,614 +987,41 @@
 ;%0A  
-%7D%0A%0A  static Cluster perfectCluster() %7B%0A    Cluster cluster = new Cluster();%0A    cluster.clusterName = spydraClusterName;%0A    Cluster.Status status = new Cluster.Status();%0A    status.state = Cluster.Status.RUNNING;%0A    status.stateStartTime = ZonedDateTime.now(ZoneOffset.UTC);%0A    cluster.status = status;%0A    cluster.config.gceClusterConfig.metadata.heartbeat =%0A            Optional.of(cluster.status.stateStartTime.plusMinutes(60));%0A    return cluster;%0A  %7D%0A%0A  static Cluster errorCluster() %7B%0A    Cluster cluster = perfectCluster();%0A    cluster.status.state = Cluster.Status.ERROR;%0A    return cluster;%0A  %7D
+  clientId = %22my-client-id%22;%0A  %7D%0A
 %0A%0A  
@@ -1370,24 +1370,304 @@
 Exception %7B%0A
+    ImmutableList%3CCluster%3E clusters =%0A            ImmutableList.of(PoolingTest.errorCluster(clientId), PoolingTest.errorCluster(clientId));%0A%0A    SpydraArgument.Pooling pooling = new SpydraArgument.Pooling();%0A    arguments.setPooling(pooling);%0A    arguments.setClientId(clientId);%0A
     argument
@@ -1700,24 +1700,25 @@
 usterName);%0A
+%0A
     when(dat
@@ -1739,16 +1739,19 @@
 lusters(
+eq(
 argument
@@ -1756,53 +1756,53 @@
 nts)
-).thenReturn(ImmutableList.of(errorC
+, anyMap()))%0A            .thenReturn(c
 luster
-())
+s
 );%0A 

Fix DynamicSubmitterTest after merging PR #55