class NovaLauncher extends Launcher {

  @Override
  public void cleanup(JsonCluster definition, ClusterRuntime runtime) throws KaramelException {
    runtime.resolveFailures();
    List<GroupRuntime> groups = runtime.getGroups();
    Set<String> allNovaVms = new HashSet<>();
    Set<String> allNovaVmsIds = new HashSet<>();
    Map<String, String> groupRegion = new HashMap<>();
    for (GroupRuntime group : groups) {
      group.getCluster().resolveFailures();
      Provider provider = UserClusterDataExtractor.getGroupProvider(definition, group.getName());
      if (provider instanceof Nova) {
        for (MachineRuntime machine : group.getMachines()) {
          if (machine.getVmId() != null) {
            allNovaVmsIds.add(machine.getVmId());
          }
        }
        JsonGroup jg = UserClusterDataExtractor.findGroup(definition, group.getName());
        List<String> vmNames = NovaSetting.NOVA_UNIQUE_VM_NAMES(group.getCluster().getName(), group.getName(),
                jg.getSize());
        allNovaVms.addAll(vmNames);
        groupRegion.put(group.getName(), novaContext.getNovaCredentials().getRegion());
      }
    }
    cleanup(definition.getName(), allNovaVmsIds, allNovaVms, groupRegion);
  }

}

class NovaLauncherTest {

  private static final Set<String> ports;
  private NovaContext novaContext;
  private SshKeyPair sshKeyPair;
  private NovaCredentials novaCredentials;
  private ContextBuilder builder;
  private ComputeServiceContext serviceContext;
  private ComputeService novaComputeService;
  private NovaApi novaApi;
  private SecurityGroupApi securityGroupApi;
  private KeyPairApi keyPairApi;
  private Optional securityGroupApiOptional;
  private Optional keyPairApiOptional;
  private SecurityGroup securityGroupCreated;
  private String clusterName;
  private String groupName;
  private Nova nova;

  @Test
  public void cleanup() throws KaramelException{

    String uniqueGroup = NovaSetting.NOVA_UNIQUE_GROUP_NAME(clusterName, groupName);

    //mocking
    JsonCluster cluster = mock(JsonCluster.class);
    ClusterRuntime clusterRuntime = mock(ClusterRuntime.class);
    when(clusterRuntime.getName()).thenReturn(clusterName);

    List<JsonGroup> groups = new ArrayList<>();
    JsonGroup group = mock(JsonGroup.class);
    groups.add(group);
    when(cluster.getGroups()).thenReturn(groups);
    when(cluster.getProvider()).thenReturn(nova);
    when(cluster.getName()).thenReturn(clusterName);

    //mocking json group
    when(group.getName()).thenReturn(groupName);
    when(group.getProvider()).thenReturn(nova);
    when(group.getSize()).thenReturn(1);

    //mocking group runtime
    List<GroupRuntime> groupRuntimes = new ArrayList<>();
    GroupRuntime groupRuntime = mock(GroupRuntime.class);
    when(groupRuntime.getName()).thenReturn(groupName);
    when(groupRuntime.getId()).thenReturn("10");
    when(groupRuntime.getCluster()).thenReturn(clusterRuntime);
    groupRuntimes.add(groupRuntime);

    //mocking clusterRuntime
    when(clusterRuntime.getGroups()).thenReturn(groupRuntimes);

    //mocking securityGroups
    SecurityGroup securityGroup = mock(SecurityGroup.class);
    List<SecurityGroup> securityGroupList = new ArrayList<>();
    securityGroupList.add(securityGroup);
    FluentIterable<SecurityGroup> securityGroupFluentIterable = FluentIterable.from(securityGroupList);

    when(novaContext.getSecurityGroupApi()).thenReturn(securityGroupApi);
    when(securityGroupApi.list()).thenReturn(securityGroupFluentIterable);
    when(securityGroup.getName()).thenReturn(uniqueGroup);

    NovaLauncher novaLauncher = new NovaLauncher(novaContext, sshKeyPair);
    novaLauncher.cleanup(cluster, clusterRuntime);
  }

}