/*
 * Copyright 2021 Google LLC
 *
 * 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.example.spanner;

import com.google.cloud.spanner.DatabaseAdminClient;
import com.google.cloud.spanner.InstanceAdminClient;
import com.google.cloud.spanner.Spanner;
import com.google.cloud.spanner.SpannerOptions;
import org.junit.AfterClass;
import org.junit.BeforeClass;

/**
 * Base class for sample integration tests.
 */
public class SampleTestBase {

  private static final String BASE_DATABASE_ID = System.getProperty(
      "spanner.sample.database",
      "sampledb"
  );
  private static final String BASE_BACKUP_ID = "samplebk";

  protected static Spanner spanner;
  protected static DatabaseAdminClient databaseAdminClient;
  protected static InstanceAdminClient instanceAdminClient;
  protected static String projectId;
  protected static final String instanceId = System.getProperty("spanner.test.instance");
  protected static final String multiRegionalInstanceId =
      System.getProperty("spanner.test.instance.mr");
  protected static final String instanceConfigName = System
      .getProperty("spanner.test.instance.config");
  protected static SampleIdGenerator idGenerator;

  @BeforeClass
  public static void beforeClass() {
    final SpannerOptions options = SpannerOptions
        .newBuilder()
        .setAutoThrottleAdministrativeRequests()
        .build();
    projectId = options.getProjectId();
    spanner = options.getService();
    databaseAdminClient = spanner.getDatabaseAdminClient();
    instanceAdminClient = spanner.getInstanceAdminClient();
    idGenerator = new SampleIdGenerator(BASE_DATABASE_ID, BASE_BACKUP_ID);
  }

  @AfterClass
  public static void afterClass() {
    for (String databaseId : idGenerator.getDatabaseIds()) {
      try {
        databaseAdminClient.dropDatabase(instanceId, databaseId);
      } catch (Exception e1) {
        try {
          databaseAdminClient.dropDatabase(multiRegionalInstanceId, databaseId);
        } catch (Exception e2) {
          System.out.println(
              "Failed to drop database " + databaseId + " due to " + e2.getMessage()
                  + ", skipping..."
          );
        }
      }
    }
    for (String backupId : idGenerator.getBackupIds()) {
      try {
        databaseAdminClient.deleteBackup(instanceId, backupId);
      } catch (Exception e1) {
        try {
          databaseAdminClient.deleteBackup(multiRegionalInstanceId, backupId);
        } catch (Exception e2) {
          System.out.println(
              "Failed to delete backup " + backupId + " due to " + e2.getMessage() + ", skipping..."
          );
        }
      }
    }
    spanner.close();
  }
}

@@ -2276,37 +2276,175 @@
 eIds()) %7B%0A      
-try %7B
+System.out.println(%22Trying to drop %22 + databaseId);%0A      try %7B%0A        // If the database is not found, it is ignored (no exception is thrown)
 %0A        databas
@@ -2498,62 +2498,16 @@
 d);%0A
-      %7D catch (Exception e1) %7B%0A        try %7B%0A 
         
  dat
@@ -2494,33 +2494,32 @@
 aseId);%0A        
- 
 databaseAdminCli
@@ -2571,34 +2571,32 @@
 abaseId);%0A      
-  
 %7D catch (Excepti
@@ -2591,39 +2591,36 @@
 tch (Exception e
-2
 ) %7B%0A
-  
         System.o
@@ -2635,34 +2635,32 @@
 ln(%0A            
-  
 %22Failed to drop 
@@ -2691,33 +2691,32 @@
 + %22 due to %22 + e
-2
 .getMessage()%0A  
@@ -2716,26 +2716,8 @@
 ge()
-%0A                 
  + %22
@@ -2735,38 +2735,26 @@
 ..%22%0A        
-  );%0A        %7D
+);
 %0A      %7D%0A   
@@ -2817,32 +2817,110 @@
 ) %7B%0A      try %7B%0A
+        // If the backup is not found, it is ignored (no exception is thrown)%0A
         database
@@ -2971,55 +2971,8 @@
 d);%0A
-      %7D catch (Exception e1) %7B%0A        try %7B%0A  
     
@@ -3046,26 +3046,24 @@
 pId);%0A      
-  
 %7D catch (Exc
@@ -3070,17 +3070,16 @@
 eption e
-2
 ) %7B%0A    
@@ -3078,26 +3078,24 @@
 ) %7B%0A        
-  
 System.out.p
@@ -3094,34 +3094,32 @@
 em.out.println(%0A
-  
             %22Fai
@@ -3168,17 +3168,16 @@
 to %22 + e
-2
 .getMess
@@ -3212,23 +3212,11 @@
     
- 
- 
 );%0A
-        %7D%0A
     

drop databases after sample tests (#1401)