private void addRecipeToContext(Long id) {
   IntegrationTestContext itContext = getItContext();
   Set<Long> recipeIds =
       itContext.getContextParam(CloudbreakITContextConstants.RECIPE_ID, Set.class);
   recipeIds = recipeIds == null ? new HashSet<>() : recipeIds;
   recipeIds.add(id);
   itContext.putContextParam(CloudbreakITContextConstants.RECIPE_ID, recipeIds);
 }
 @BeforeMethod
 public void setContextParams() {
   IntegrationTestContext itContext = getItContext();
   Assert.assertNotNull(
       itContext.getContextParam(CloudbreakITContextConstants.TEMPLATE_ID, List.class),
       "Template id is mandatory.");
   Assert.assertNotNull(
       itContext.getContextParam(CloudbreakITContextConstants.CREDENTIAL_ID),
       "Credential id is mandatory.");
   Assert.assertNotNull(
       itContext.getContextParam(CloudbreakITContextConstants.NETWORK_ID),
       "Network id is mandatory.");
   Assert.assertNotNull(
       itContext.getContextParam(CloudbreakITContextConstants.SECURITY_GROUP_ID),
       "Security group id is mandatory.");
 }
 private void putStackToContextIfExist(StackEndpoint endpoint, String stackName) throws Exception {
   if (StringUtils.hasLength(stackName)) {
     String resourceId = endpoint.getPublic(stackName).getId().toString();
     if (resourceId != null) {
       itContext.putContextParam(CloudbreakITContextConstants.STACK_ID, resourceId);
     }
   }
 }
 @Test
 @Parameters({
   "stackName",
   "region",
   "onFailureAction",
   "threshold",
   "adjustmentType",
   "variant",
   "availabilityZone"
 })
 public void testStackCreation(
     @Optional("testing1") String stackName,
     @Optional("europe-west1") String region,
     @Optional("DO_NOTHING") String onFailureAction,
     @Optional("4") Long threshold,
     @Optional("EXACT") String adjustmentType,
     @Optional("") String variant,
     @Optional() String availabilityZone)
     throws Exception {
   // GIVEN
   IntegrationTestContext itContext = getItContext();
   List<InstanceGroup> instanceGroups =
       itContext.getContextParam(CloudbreakITContextConstants.TEMPLATE_ID, List.class);
   Map<String, Object> igMap = new HashMap<>();
   for (InstanceGroup ig : instanceGroups) {
     igMap.put(ig.getName(), ig);
   }
   String credentialId = itContext.getContextParam(CloudbreakITContextConstants.CREDENTIAL_ID);
   String networkId = itContext.getContextParam(CloudbreakITContextConstants.NETWORK_ID);
   String securityGroupId =
       itContext.getContextParam(CloudbreakITContextConstants.SECURITY_GROUP_ID);
   // WHEN
   String stackId =
       getClient()
           .postStack(
               stackName,
               credentialId,
               region,
               false,
               igMap,
               onFailureAction,
               threshold,
               adjustmentType,
               null,
               networkId,
               securityGroupId,
               null,
               null,
               variant,
               availabilityZone);
   // THEN
   Assert.assertNotNull(stackId);
   itContext.putCleanUpParam(CloudbreakITContextConstants.STACK_ID, stackId);
   CloudbreakUtil.waitForStackStatus(itContext, stackId, "AVAILABLE");
   itContext.putContextParam(CloudbreakITContextConstants.STACK_ID, stackId);
 }
 private void putBlueprintToContextIfExist(BlueprintEndpoint endpoint, String blueprintName)
     throws Exception {
   endpoint.getPublics();
   if (StringUtils.isEmpty(blueprintName)) {
     blueprintName = defaultBlueprintName;
   }
   if (StringUtils.hasLength(blueprintName)) {
     String resourceId = endpoint.getPublic(blueprintName).getId().toString();
     if (resourceId != null) {
       itContext.putContextParam(CloudbreakITContextConstants.BLUEPRINT_ID, resourceId);
     }
   }
 }
 private void putNetworkToContext(
     NetworkEndpoint endpoint, String cloudProvider, String networkName) throws Exception {
   endpoint.getPublics();
   if (StringUtils.isEmpty(networkName)) {
     String defaultNetworkName = itProps.getDefaultNetwork(cloudProvider);
     networkName = defaultNetworkName;
   }
   if (StringUtils.hasLength(networkName)) {
     String resourceId = endpoint.getPublic(networkName).getId();
     if (resourceId != null) {
       itContext.putContextParam(CloudbreakITContextConstants.NETWORK_ID, resourceId);
     }
   }
 }
 private void putCredentialToContext(
     CredentialEndpoint endpoint, String cloudProvider, String credentialName) throws Exception {
   if (StringUtils.isEmpty(credentialName)) {
     String defaultCredentialName = itProps.getCredentialName(cloudProvider);
     if (!"__ignored__".equals(defaultCredentialName)) {
       credentialName = defaultCredentialName;
     }
   }
   if (StringUtils.hasLength(credentialName)) {
     String resourceId = endpoint.getPublic(credentialName).getId().toString();
     if (resourceId != null) {
       itContext.putContextParam(CloudbreakITContextConstants.CREDENTIAL_ID, resourceId);
     }
   }
 }
 private void putSecurityGroupToContext(SecurityGroupEndpoint endpoint, String securityGroupName)
     throws Exception {
   endpoint.getPublics();
   if (StringUtils.isEmpty(securityGroupName)) {
     String defaultSecurityGroupName = itProps.getDefaultSecurityGroup();
     securityGroupName = defaultSecurityGroupName;
   }
   if (StringUtils.hasLength(securityGroupName)) {
     try {
       String resourceId = endpoint.getPublic(securityGroupName).getId().toString();
       if (resourceId != null) {
         itContext.putContextParam(CloudbreakITContextConstants.SECURITY_GROUP_ID, resourceId);
       }
     } catch (Exception e) {
       LOG.warn("Could not set security group id", e);
     }
   }
 }
 @AfterSuite(alwaysRun = true)
 @Parameters("cleanUp")
 public void cleanUp(@Optional("true") boolean cleanUp) throws Exception {
   if (isCleanUpNeeded(cleanUp)) {
     CloudbreakClient cloudbreakClient =
         itContext.getContextParam(
             CloudbreakITContextConstants.CLOUDBREAK_CLIENT, CloudbreakClient.class);
     String stackId = itContext.getCleanUpParameter(CloudbreakITContextConstants.STACK_ID);
     cleanUpService.deleteStackAndWait(cloudbreakClient, stackId);
     List<InstanceGroup> instanceGroups =
         itContext.getCleanUpParameter(CloudbreakITContextConstants.TEMPLATE_ID, List.class);
     if (instanceGroups != null && !instanceGroups.isEmpty()) {
       Set<String> deletedTemplates = new HashSet<>();
       for (InstanceGroup ig : instanceGroups) {
         if (!deletedTemplates.contains(ig.getTemplateId())) {
           cleanUpService.deleteTemplate(cloudbreakClient, ig.getTemplateId());
           deletedTemplates.add(ig.getTemplateId());
         }
       }
     }
     Set<Long> recipeIds =
         itContext.getContextParam(CloudbreakITContextConstants.RECIPE_ID, Set.class);
     if (recipeIds != null) {
       for (Long recipeId : recipeIds) {
         cleanUpService.deleteRecipe(cloudbreakClient, recipeId);
       }
     }
     cleanUpService.deleteCredential(
         cloudbreakClient,
         itContext.getCleanUpParameter(CloudbreakITContextConstants.CREDENTIAL_ID));
     cleanUpService.deleteBlueprint(
         cloudbreakClient,
         itContext.getCleanUpParameter(CloudbreakITContextConstants.BLUEPRINT_ID));
     cleanUpService.deleteNetwork(
         cloudbreakClient, itContext.getCleanUpParameter(CloudbreakITContextConstants.NETWORK_ID));
     cleanUpService.deleteSecurityGroup(
         cloudbreakClient,
         itContext.getCleanUpParameter(CloudbreakITContextConstants.SECURITY_GROUP_ID));
   }
 }
  @BeforeSuite(dependsOnMethods = "initContext")
  @Parameters({
    "cloudbreakServer",
    "cloudProvider",
    "credentialName",
    "instanceGroups",
    "hostGroups",
    "blueprintName",
    "stackName",
    "networkName",
    "securityGroupName"
  })
  public void initCloudbreakSuite(
      @Optional("") String cloudbreakServer,
      @Optional("") String cloudProvider,
      @Optional("") String credentialName,
      @Optional("") String instanceGroups,
      @Optional("") String hostGroups,
      @Optional("") String blueprintName,
      @Optional("") String stackName,
      @Optional("") String networkName,
      @Optional("") String securityGroupName)
      throws Exception {
    cloudbreakServer =
        StringUtils.hasLength(cloudbreakServer) ? cloudbreakServer : defaultCloudbreakServer;
    itContext.putContextParam(
        CloudbreakITContextConstants.SKIP_REMAINING_SUITETEST_AFTER_ONE_FAILED,
        skipRemainingSuiteTestsAfterOneFailed);
    itContext.putContextParam(CloudbreakITContextConstants.CLOUDBREAK_SERVER, cloudbreakServer);
    String identity = itContext.getContextParam(IntegrationTestContext.IDENTITY_URL);
    String user = itContext.getContextParam(IntegrationTestContext.AUTH_USER);
    String password = itContext.getContextParam(IntegrationTestContext.AUTH_PASSWORD);

    CloudbreakClient cloudbreakClient =
        new CloudbreakClient.CloudbreakClientBuilder(cloudbreakServer, identity, "cloudbreak_shell")
            .withCertificateValidation(false)
            .withDebug(true)
            .withCredential(user, password)
            .build();
    itContext.putContextParam(CloudbreakITContextConstants.CLOUDBREAK_CLIENT, cloudbreakClient);
    if (cleanUpBeforeStart) {
      cleanUpService.deleteTestStacksAndResources(cloudbreakClient);
    }
    putBlueprintToContextIfExist(
        itContext
            .getContextParam(CloudbreakITContextConstants.CLOUDBREAK_CLIENT, CloudbreakClient.class)
            .blueprintEndpoint(),
        blueprintName);
    putNetworkToContext(
        itContext
            .getContextParam(CloudbreakITContextConstants.CLOUDBREAK_CLIENT, CloudbreakClient.class)
            .networkEndpoint(),
        cloudProvider,
        networkName);
    putSecurityGroupToContext(
        itContext
            .getContextParam(CloudbreakITContextConstants.CLOUDBREAK_CLIENT, CloudbreakClient.class)
            .securityGroupEndpoint(),
        securityGroupName);
    putCredentialToContext(
        itContext
            .getContextParam(CloudbreakITContextConstants.CLOUDBREAK_CLIENT, CloudbreakClient.class)
            .credentialEndpoint(),
        cloudProvider,
        credentialName);
    putStackToContextIfExist(
        itContext
            .getContextParam(CloudbreakITContextConstants.CLOUDBREAK_CLIENT, CloudbreakClient.class)
            .stackEndpoint(),
        stackName);
    if (StringUtils.hasLength(instanceGroups)) {
      List<String[]> instanceGroupStrings =
          templateAdditionHelper.parseCommaSeparatedRows(instanceGroups);
      itContext.putContextParam(
          CloudbreakITContextConstants.TEMPLATE_ID,
          createInstanceGroups(
              itContext
                  .getContextParam(
                      CloudbreakITContextConstants.CLOUDBREAK_CLIENT, CloudbreakClient.class)
                  .templateEndpoint(),
              instanceGroupStrings));
    }
    if (StringUtils.hasLength(hostGroups)) {
      List<String[]> hostGroupStrings = templateAdditionHelper.parseCommaSeparatedRows(hostGroups);
      itContext.putContextParam(
          CloudbreakITContextConstants.HOSTGROUP_ID, createHostGroups(hostGroupStrings));
    }
  }
 private boolean isCleanUpNeeded(boolean cleanUp) {
   boolean noTestsFailed =
       CollectionUtils.isEmpty(
           itContext.getContextParam(CloudbreakITContextConstants.FAILED_TESTS, List.class));
   return cleanUp && (cleanUpOnFailure || (!cleanUpOnFailure && noTestsFailed));
 }