/*
 * Copyright 2023 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
 *
 *      https://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.google.cloud.orchestration.airflow.service.v1beta1;

import static com.google.cloud.orchestration.airflow.service.v1beta1.EnvironmentsClient.ListEnvironmentsPagedResponse;

import com.google.api.gax.core.NoCredentialsProvider;
import com.google.api.gax.httpjson.GaxHttpJsonProperties;
import com.google.api.gax.httpjson.testing.MockHttpService;
import com.google.api.gax.rpc.ApiClientHeaderProvider;
import com.google.api.gax.rpc.ApiException;
import com.google.api.gax.rpc.ApiExceptionFactory;
import com.google.api.gax.rpc.InvalidArgumentException;
import com.google.api.gax.rpc.StatusCode;
import com.google.api.gax.rpc.testing.FakeStatusCode;
import com.google.cloud.orchestration.airflow.service.v1beta1.stub.HttpJsonEnvironmentsStub;
import com.google.common.collect.Lists;
import com.google.longrunning.Operation;
import com.google.protobuf.Any;
import com.google.protobuf.Empty;
import com.google.protobuf.FieldMask;
import com.google.protobuf.Timestamp;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ExecutionException;
import javax.annotation.Generated;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

@Generated("by gapic-generator-java")
public class EnvironmentsClientHttpJsonTest {
  private static MockHttpService mockService;
  private static EnvironmentsClient client;

  @BeforeClass
  public static void startStaticServer() throws IOException {
    mockService =
        new MockHttpService(
            HttpJsonEnvironmentsStub.getMethodDescriptors(),
            EnvironmentsSettings.getDefaultEndpoint());
    EnvironmentsSettings settings =
        EnvironmentsSettings.newHttpJsonBuilder()
            .setTransportChannelProvider(
                EnvironmentsSettings.defaultHttpJsonTransportProviderBuilder()
                    .setHttpTransport(mockService)
                    .build())
            .setCredentialsProvider(NoCredentialsProvider.create())
            .build();
    client = EnvironmentsClient.create(settings);
  }

  @AfterClass
  public static void stopServer() {
    client.close();
  }

  @Before
  public void setUp() {}

  @After
  public void tearDown() throws Exception {
    mockService.reset();
  }

  @Test
  public void createEnvironmentTest() throws Exception {
    Environment expectedResponse =
        Environment.newBuilder()
            .setName(EnvironmentName.of("[PROJECT]", "[LOCATION]", "[ENVIRONMENT]").toString())
            .setConfig(EnvironmentConfig.newBuilder().build())
            .setUuid("uuid3601339")
            .setCreateTime(Timestamp.newBuilder().build())
            .setUpdateTime(Timestamp.newBuilder().build())
            .putAllLabels(new HashMap<String, String>())
            .build();
    Operation resultOperation =
        Operation.newBuilder()
            .setName("createEnvironmentTest")
            .setDone(true)
            .setResponse(Any.pack(expectedResponse))
            .build();
    mockService.addResponse(resultOperation);

    String parent = "projects/project-5833/locations/location-5833";
    Environment environment = Environment.newBuilder().build();

    Environment actualResponse = client.createEnvironmentAsync(parent, environment).get();
    Assert.assertEquals(expectedResponse, actualResponse);

    List<String> actualRequests = mockService.getRequestPaths();
    Assert.assertEquals(1, actualRequests.size());

    String apiClientHeaderKey =
        mockService
            .getRequestHeaders()
            .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
            .iterator()
            .next();
    Assert.assertTrue(
        GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
            .matcher(apiClientHeaderKey)
            .matches());
  }

  @Test
  public void createEnvironmentExceptionTest() throws Exception {
    ApiException exception =
        ApiExceptionFactory.createException(
            new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
    mockService.addException(exception);

    try {
      String parent = "projects/project-5833/locations/location-5833";
      Environment environment = Environment.newBuilder().build();
      client.createEnvironmentAsync(parent, environment).get();
      Assert.fail("No exception raised");
    } catch (ExecutionException e) {
    }
  }

  @Test
  public void getEnvironmentTest() throws Exception {
    Environment expectedResponse =
        Environment.newBuilder()
            .setName(EnvironmentName.of("[PROJECT]", "[LOCATION]", "[ENVIRONMENT]").toString())
            .setConfig(EnvironmentConfig.newBuilder().build())
            .setUuid("uuid3601339")
            .setCreateTime(Timestamp.newBuilder().build())
            .setUpdateTime(Timestamp.newBuilder().build())
            .putAllLabels(new HashMap<String, String>())
            .build();
    mockService.addResponse(expectedResponse);

    String name = "projects/project-7856/locations/location-7856/environments/environment-7856";

    Environment actualResponse = client.getEnvironment(name);
    Assert.assertEquals(expectedResponse, actualResponse);

    List<String> actualRequests = mockService.getRequestPaths();
    Assert.assertEquals(1, actualRequests.size());

    String apiClientHeaderKey =
        mockService
            .getRequestHeaders()
            .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
            .iterator()
            .next();
    Assert.assertTrue(
        GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
            .matcher(apiClientHeaderKey)
            .matches());
  }

  @Test
  public void getEnvironmentExceptionTest() throws Exception {
    ApiException exception =
        ApiExceptionFactory.createException(
            new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
    mockService.addException(exception);

    try {
      String name = "projects/project-7856/locations/location-7856/environments/environment-7856";
      client.getEnvironment(name);
      Assert.fail("No exception raised");
    } catch (InvalidArgumentException e) {
      // Expected exception.
    }
  }

  @Test
  public void listEnvironmentsTest() throws Exception {
    Environment responsesElement = Environment.newBuilder().build();
    ListEnvironmentsResponse expectedResponse =
        ListEnvironmentsResponse.newBuilder()
            .setNextPageToken("")
            .addAllEnvironments(Arrays.asList(responsesElement))
            .build();
    mockService.addResponse(expectedResponse);

    String parent = "projects/project-5833/locations/location-5833";

    ListEnvironmentsPagedResponse pagedListResponse = client.listEnvironments(parent);

    List<Environment> resources = Lists.newArrayList(pagedListResponse.iterateAll());

    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getEnvironmentsList().get(0), resources.get(0));

    List<String> actualRequests = mockService.getRequestPaths();
    Assert.assertEquals(1, actualRequests.size());

    String apiClientHeaderKey =
        mockService
            .getRequestHeaders()
            .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
            .iterator()
            .next();
    Assert.assertTrue(
        GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
            .matcher(apiClientHeaderKey)
            .matches());
  }

  @Test
  public void listEnvironmentsExceptionTest() throws Exception {
    ApiException exception =
        ApiExceptionFactory.createException(
            new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
    mockService.addException(exception);

    try {
      String parent = "projects/project-5833/locations/location-5833";
      client.listEnvironments(parent);
      Assert.fail("No exception raised");
    } catch (InvalidArgumentException e) {
      // Expected exception.
    }
  }

  @Test
  public void updateEnvironmentTest() throws Exception {
    Environment expectedResponse =
        Environment.newBuilder()
            .setName(EnvironmentName.of("[PROJECT]", "[LOCATION]", "[ENVIRONMENT]").toString())
            .setConfig(EnvironmentConfig.newBuilder().build())
            .setUuid("uuid3601339")
            .setCreateTime(Timestamp.newBuilder().build())
            .setUpdateTime(Timestamp.newBuilder().build())
            .putAllLabels(new HashMap<String, String>())
            .build();
    Operation resultOperation =
        Operation.newBuilder()
            .setName("updateEnvironmentTest")
            .setDone(true)
            .setResponse(Any.pack(expectedResponse))
            .build();
    mockService.addResponse(resultOperation);

    String name = "projects/project-7856/locations/location-7856/environments/environment-7856";
    Environment environment = Environment.newBuilder().build();
    FieldMask updateMask = FieldMask.newBuilder().build();

    Environment actualResponse = client.updateEnvironmentAsync(name, environment, updateMask).get();
    Assert.assertEquals(expectedResponse, actualResponse);

    List<String> actualRequests = mockService.getRequestPaths();
    Assert.assertEquals(1, actualRequests.size());

    String apiClientHeaderKey =
        mockService
            .getRequestHeaders()
            .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
            .iterator()
            .next();
    Assert.assertTrue(
        GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
            .matcher(apiClientHeaderKey)
            .matches());
  }

  @Test
  public void updateEnvironmentExceptionTest() throws Exception {
    ApiException exception =
        ApiExceptionFactory.createException(
            new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
    mockService.addException(exception);

    try {
      String name = "projects/project-7856/locations/location-7856/environments/environment-7856";
      Environment environment = Environment.newBuilder().build();
      FieldMask updateMask = FieldMask.newBuilder().build();
      client.updateEnvironmentAsync(name, environment, updateMask).get();
      Assert.fail("No exception raised");
    } catch (ExecutionException e) {
    }
  }

  @Test
  public void deleteEnvironmentTest() throws Exception {
    Empty expectedResponse = Empty.newBuilder().build();
    Operation resultOperation =
        Operation.newBuilder()
            .setName("deleteEnvironmentTest")
            .setDone(true)
            .setResponse(Any.pack(expectedResponse))
            .build();
    mockService.addResponse(resultOperation);

    String name = "projects/project-7856/locations/location-7856/environments/environment-7856";

    client.deleteEnvironmentAsync(name).get();

    List<String> actualRequests = mockService.getRequestPaths();
    Assert.assertEquals(1, actualRequests.size());

    String apiClientHeaderKey =
        mockService
            .getRequestHeaders()
            .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
            .iterator()
            .next();
    Assert.assertTrue(
        GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
            .matcher(apiClientHeaderKey)
            .matches());
  }

  @Test
  public void deleteEnvironmentExceptionTest() throws Exception {
    ApiException exception =
        ApiExceptionFactory.createException(
            new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
    mockService.addException(exception);

    try {
      String name = "projects/project-7856/locations/location-7856/environments/environment-7856";
      client.deleteEnvironmentAsync(name).get();
      Assert.fail("No exception raised");
    } catch (ExecutionException e) {
    }
  }

  @Test
  public void restartWebServerTest() throws Exception {
    Environment expectedResponse =
        Environment.newBuilder()
            .setName(EnvironmentName.of("[PROJECT]", "[LOCATION]", "[ENVIRONMENT]").toString())
            .setConfig(EnvironmentConfig.newBuilder().build())
            .setUuid("uuid3601339")
            .setCreateTime(Timestamp.newBuilder().build())
            .setUpdateTime(Timestamp.newBuilder().build())
            .putAllLabels(new HashMap<String, String>())
            .build();
    Operation resultOperation =
        Operation.newBuilder()
            .setName("restartWebServerTest")
            .setDone(true)
            .setResponse(Any.pack(expectedResponse))
            .build();
    mockService.addResponse(resultOperation);

    RestartWebServerRequest request =
        RestartWebServerRequest.newBuilder()
            .setName("projects/project-7856/locations/location-7856/environments/environment-7856")
            .build();

    Environment actualResponse = client.restartWebServerAsync(request).get();
    Assert.assertEquals(expectedResponse, actualResponse);

    List<String> actualRequests = mockService.getRequestPaths();
    Assert.assertEquals(1, actualRequests.size());

    String apiClientHeaderKey =
        mockService
            .getRequestHeaders()
            .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
            .iterator()
            .next();
    Assert.assertTrue(
        GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
            .matcher(apiClientHeaderKey)
            .matches());
  }

  @Test
  public void restartWebServerExceptionTest() throws Exception {
    ApiException exception =
        ApiExceptionFactory.createException(
            new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
    mockService.addException(exception);

    try {
      RestartWebServerRequest request =
          RestartWebServerRequest.newBuilder()
              .setName(
                  "projects/project-7856/locations/location-7856/environments/environment-7856")
              .build();
      client.restartWebServerAsync(request).get();
      Assert.fail("No exception raised");
    } catch (ExecutionException e) {
    }
  }

  @Test
  public void checkUpgradeTest() throws Exception {
    CheckUpgradeResponse expectedResponse =
        CheckUpgradeResponse.newBuilder()
            .setBuildLogUri("buildLogUri2048482198")
            .setPypiConflictBuildLogExtract("pypiConflictBuildLogExtract-1257030825")
            .setImageVersion("imageVersion949870333")
            .putAllPypiDependencies(new HashMap<String, String>())
            .build();
    Operation resultOperation =
        Operation.newBuilder()
            .setName("checkUpgradeTest")
            .setDone(true)
            .setResponse(Any.pack(expectedResponse))
            .build();
    mockService.addResponse(resultOperation);

    CheckUpgradeRequest request =
        CheckUpgradeRequest.newBuilder()
            .setEnvironment(
                "projects/project-4088/locations/location-4088/environments/environment-4088")
            .setImageVersion("imageVersion949870333")
            .build();

    CheckUpgradeResponse actualResponse = client.checkUpgradeAsync(request).get();
    Assert.assertEquals(expectedResponse, actualResponse);

    List<String> actualRequests = mockService.getRequestPaths();
    Assert.assertEquals(1, actualRequests.size());

    String apiClientHeaderKey =
        mockService
            .getRequestHeaders()
            .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
            .iterator()
            .next();
    Assert.assertTrue(
        GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
            .matcher(apiClientHeaderKey)
            .matches());
  }

  @Test
  public void checkUpgradeExceptionTest() throws Exception {
    ApiException exception =
        ApiExceptionFactory.createException(
            new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
    mockService.addException(exception);

    try {
      CheckUpgradeRequest request =
          CheckUpgradeRequest.newBuilder()
              .setEnvironment(
                  "projects/project-4088/locations/location-4088/environments/environment-4088")
              .setImageVersion("imageVersion949870333")
              .build();
      client.checkUpgradeAsync(request).get();
      Assert.fail("No exception raised");
    } catch (ExecutionException e) {
    }
  }

  @Test
  public void executeAirflowCommandTest() throws Exception {
    ExecuteAirflowCommandResponse expectedResponse =
        ExecuteAirflowCommandResponse.newBuilder()
            .setExecutionId("executionId-454906285")
            .setPod("pod111173")
            .setPodNamespace("podNamespace463962262")
            .setError("error96784904")
            .build();
    mockService.addResponse(expectedResponse);

    ExecuteAirflowCommandRequest request =
        ExecuteAirflowCommandRequest.newBuilder()
            .setEnvironment(
                "projects/project-4088/locations/location-4088/environments/environment-4088")
            .setCommand("command950394699")
            .setSubcommand("subcommand2099091723")
            .addAllParameters(new ArrayList<String>())
            .build();

    ExecuteAirflowCommandResponse actualResponse = client.executeAirflowCommand(request);
    Assert.assertEquals(expectedResponse, actualResponse);

    List<String> actualRequests = mockService.getRequestPaths();
    Assert.assertEquals(1, actualRequests.size());

    String apiClientHeaderKey =
        mockService
            .getRequestHeaders()
            .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
            .iterator()
            .next();
    Assert.assertTrue(
        GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
            .matcher(apiClientHeaderKey)
            .matches());
  }

  @Test
  public void executeAirflowCommandExceptionTest() throws Exception {
    ApiException exception =
        ApiExceptionFactory.createException(
            new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
    mockService.addException(exception);

    try {
      ExecuteAirflowCommandRequest request =
          ExecuteAirflowCommandRequest.newBuilder()
              .setEnvironment(
                  "projects/project-4088/locations/location-4088/environments/environment-4088")
              .setCommand("command950394699")
              .setSubcommand("subcommand2099091723")
              .addAllParameters(new ArrayList<String>())
              .build();
      client.executeAirflowCommand(request);
      Assert.fail("No exception raised");
    } catch (InvalidArgumentException e) {
      // Expected exception.
    }
  }

  @Test
  public void stopAirflowCommandTest() throws Exception {
    StopAirflowCommandResponse expectedResponse =
        StopAirflowCommandResponse.newBuilder()
            .setIsDone(true)
            .addAllOutput(new ArrayList<String>())
            .build();
    mockService.addResponse(expectedResponse);

    StopAirflowCommandRequest request =
        StopAirflowCommandRequest.newBuilder()
            .setEnvironment(
                "projects/project-4088/locations/location-4088/environments/environment-4088")
            .setExecutionId("executionId-454906285")
            .setPod("pod111173")
            .setPodNamespace("podNamespace463962262")
            .setForce(true)
            .build();

    StopAirflowCommandResponse actualResponse = client.stopAirflowCommand(request);
    Assert.assertEquals(expectedResponse, actualResponse);

    List<String> actualRequests = mockService.getRequestPaths();
    Assert.assertEquals(1, actualRequests.size());

    String apiClientHeaderKey =
        mockService
            .getRequestHeaders()
            .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
            .iterator()
            .next();
    Assert.assertTrue(
        GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
            .matcher(apiClientHeaderKey)
            .matches());
  }

  @Test
  public void stopAirflowCommandExceptionTest() throws Exception {
    ApiException exception =
        ApiExceptionFactory.createException(
            new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
    mockService.addException(exception);

    try {
      StopAirflowCommandRequest request =
          StopAirflowCommandRequest.newBuilder()
              .setEnvironment(
                  "projects/project-4088/locations/location-4088/environments/environment-4088")
              .setExecutionId("executionId-454906285")
              .setPod("pod111173")
              .setPodNamespace("podNamespace463962262")
              .setForce(true)
              .build();
      client.stopAirflowCommand(request);
      Assert.fail("No exception raised");
    } catch (InvalidArgumentException e) {
      // Expected exception.
    }
  }

  @Test
  public void pollAirflowCommandTest() throws Exception {
    PollAirflowCommandResponse expectedResponse =
        PollAirflowCommandResponse.newBuilder()
            .addAllOutput(new ArrayList<PollAirflowCommandResponse.Line>())
            .setOutputEnd(true)
            .setExitInfo(PollAirflowCommandResponse.ExitInfo.newBuilder().build())
            .build();
    mockService.addResponse(expectedResponse);

    PollAirflowCommandRequest request =
        PollAirflowCommandRequest.newBuilder()
            .setEnvironment(
                "projects/project-4088/locations/location-4088/environments/environment-4088")
            .setExecutionId("executionId-454906285")
            .setPod("pod111173")
            .setPodNamespace("podNamespace463962262")
            .setNextLineNumber(1176642216)
            .build();

    PollAirflowCommandResponse actualResponse = client.pollAirflowCommand(request);
    Assert.assertEquals(expectedResponse, actualResponse);

    List<String> actualRequests = mockService.getRequestPaths();
    Assert.assertEquals(1, actualRequests.size());

    String apiClientHeaderKey =
        mockService
            .getRequestHeaders()
            .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
            .iterator()
            .next();
    Assert.assertTrue(
        GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
            .matcher(apiClientHeaderKey)
            .matches());
  }

  @Test
  public void pollAirflowCommandExceptionTest() throws Exception {
    ApiException exception =
        ApiExceptionFactory.createException(
            new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
    mockService.addException(exception);

    try {
      PollAirflowCommandRequest request =
          PollAirflowCommandRequest.newBuilder()
              .setEnvironment(
                  "projects/project-4088/locations/location-4088/environments/environment-4088")
              .setExecutionId("executionId-454906285")
              .setPod("pod111173")
              .setPodNamespace("podNamespace463962262")
              .setNextLineNumber(1176642216)
              .build();
      client.pollAirflowCommand(request);
      Assert.fail("No exception raised");
    } catch (InvalidArgumentException e) {
      // Expected exception.
    }
  }

  @Test
  public void saveSnapshotTest() throws Exception {
    SaveSnapshotResponse expectedResponse =
        SaveSnapshotResponse.newBuilder().setSnapshotPath("snapshotPath-931648503").build();
    Operation resultOperation =
        Operation.newBuilder()
            .setName("saveSnapshotTest")
            .setDone(true)
            .setResponse(Any.pack(expectedResponse))
            .build();
    mockService.addResponse(resultOperation);

    SaveSnapshotRequest request =
        SaveSnapshotRequest.newBuilder()
            .setEnvironment(
                "projects/project-4088/locations/location-4088/environments/environment-4088")
            .setSnapshotLocation("snapshotLocation1625609625")
            .build();

    SaveSnapshotResponse actualResponse = client.saveSnapshotAsync(request).get();
    Assert.assertEquals(expectedResponse, actualResponse);

    List<String> actualRequests = mockService.getRequestPaths();
    Assert.assertEquals(1, actualRequests.size());

    String apiClientHeaderKey =
        mockService
            .getRequestHeaders()
            .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
            .iterator()
            .next();
    Assert.assertTrue(
        GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
            .matcher(apiClientHeaderKey)
            .matches());
  }

  @Test
  public void saveSnapshotExceptionTest() throws Exception {
    ApiException exception =
        ApiExceptionFactory.createException(
            new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
    mockService.addException(exception);

    try {
      SaveSnapshotRequest request =
          SaveSnapshotRequest.newBuilder()
              .setEnvironment(
                  "projects/project-4088/locations/location-4088/environments/environment-4088")
              .setSnapshotLocation("snapshotLocation1625609625")
              .build();
      client.saveSnapshotAsync(request).get();
      Assert.fail("No exception raised");
    } catch (ExecutionException e) {
    }
  }

  @Test
  public void loadSnapshotTest() throws Exception {
    LoadSnapshotResponse expectedResponse = LoadSnapshotResponse.newBuilder().build();
    Operation resultOperation =
        Operation.newBuilder()
            .setName("loadSnapshotTest")
            .setDone(true)
            .setResponse(Any.pack(expectedResponse))
            .build();
    mockService.addResponse(resultOperation);

    LoadSnapshotRequest request =
        LoadSnapshotRequest.newBuilder()
            .setEnvironment(
                "projects/project-4088/locations/location-4088/environments/environment-4088")
            .setSnapshotPath("snapshotPath-931648503")
            .setSkipPypiPackagesInstallation(true)
            .setSkipEnvironmentVariablesSetting(true)
            .setSkipAirflowOverridesSetting(true)
            .setSkipGcsDataCopying(true)
            .build();

    LoadSnapshotResponse actualResponse = client.loadSnapshotAsync(request).get();
    Assert.assertEquals(expectedResponse, actualResponse);

    List<String> actualRequests = mockService.getRequestPaths();
    Assert.assertEquals(1, actualRequests.size());

    String apiClientHeaderKey =
        mockService
            .getRequestHeaders()
            .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
            .iterator()
            .next();
    Assert.assertTrue(
        GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
            .matcher(apiClientHeaderKey)
            .matches());
  }

  @Test
  public void loadSnapshotExceptionTest() throws Exception {
    ApiException exception =
        ApiExceptionFactory.createException(
            new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
    mockService.addException(exception);

    try {
      LoadSnapshotRequest request =
          LoadSnapshotRequest.newBuilder()
              .setEnvironment(
                  "projects/project-4088/locations/location-4088/environments/environment-4088")
              .setSnapshotPath("snapshotPath-931648503")
              .setSkipPypiPackagesInstallation(true)
              .setSkipEnvironmentVariablesSetting(true)
              .setSkipAirflowOverridesSetting(true)
              .setSkipGcsDataCopying(true)
              .build();
      client.loadSnapshotAsync(request).get();
      Assert.fail("No exception raised");
    } catch (ExecutionException e) {
    }
  }

  @Test
  public void databaseFailoverTest() throws Exception {
    DatabaseFailoverResponse expectedResponse = DatabaseFailoverResponse.newBuilder().build();
    Operation resultOperation =
        Operation.newBuilder()
            .setName("databaseFailoverTest")
            .setDone(true)
            .setResponse(Any.pack(expectedResponse))
            .build();
    mockService.addResponse(resultOperation);

    DatabaseFailoverRequest request =
        DatabaseFailoverRequest.newBuilder()
            .setEnvironment(
                "projects/project-4088/locations/location-4088/environments/environment-4088")
            .build();

    DatabaseFailoverResponse actualResponse = client.databaseFailoverAsync(request).get();
    Assert.assertEquals(expectedResponse, actualResponse);

    List<String> actualRequests = mockService.getRequestPaths();
    Assert.assertEquals(1, actualRequests.size());

    String apiClientHeaderKey =
        mockService
            .getRequestHeaders()
            .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
            .iterator()
            .next();
    Assert.assertTrue(
        GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
            .matcher(apiClientHeaderKey)
            .matches());
  }

  @Test
  public void databaseFailoverExceptionTest() throws Exception {
    ApiException exception =
        ApiExceptionFactory.createException(
            new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
    mockService.addException(exception);

    try {
      DatabaseFailoverRequest request =
          DatabaseFailoverRequest.newBuilder()
              .setEnvironment(
                  "projects/project-4088/locations/location-4088/environments/environment-4088")
              .build();
      client.databaseFailoverAsync(request).get();
      Assert.fail("No exception raised");
    } catch (ExecutionException e) {
    }
  }

  @Test
  public void fetchDatabasePropertiesTest() throws Exception {
    FetchDatabasePropertiesResponse expectedResponse =
        FetchDatabasePropertiesResponse.newBuilder()
            .setPrimaryGceZone("primaryGceZone-1629643277")
            .setSecondaryGceZone("secondaryGceZone227989057")
            .setIsFailoverReplicaAvailable(true)
            .build();
    mockService.addResponse(expectedResponse);

    FetchDatabasePropertiesRequest request =
        FetchDatabasePropertiesRequest.newBuilder()
            .setEnvironment(
                EnvironmentName.of("[PROJECT]", "[LOCATION]", "[ENVIRONMENT]").toString())
            .build();

    FetchDatabasePropertiesResponse actualResponse = client.fetchDatabaseProperties(request);
    Assert.assertEquals(expectedResponse, actualResponse);

    List<String> actualRequests = mockService.getRequestPaths();
    Assert.assertEquals(1, actualRequests.size());

    String apiClientHeaderKey =
        mockService
            .getRequestHeaders()
            .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
            .iterator()
            .next();
    Assert.assertTrue(
        GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
            .matcher(apiClientHeaderKey)
            .matches());
  }

  @Test
  public void fetchDatabasePropertiesExceptionTest() throws Exception {
    ApiException exception =
        ApiExceptionFactory.createException(
            new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
    mockService.addException(exception);

    try {
      FetchDatabasePropertiesRequest request =
          FetchDatabasePropertiesRequest.newBuilder()
              .setEnvironment(
                  EnvironmentName.of("[PROJECT]", "[LOCATION]", "[ENVIRONMENT]").toString())
              .build();
      client.fetchDatabaseProperties(request);
      Assert.fail("No exception raised");
    } catch (InvalidArgumentException e) {
      // Expected exception.
    }
  }
}
