/*
 * Copyright 2017 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.google.cloud.spanner;

import static com.google.common.truth.Truth.assertThat;

import com.google.protobuf.Duration;
import com.google.rpc.RetryInfo;
import io.grpc.Context;
import io.grpc.Metadata;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
import io.grpc.protobuf.ProtoUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mockito;

/** Unit tests for {@link SpannerExceptionFactory}. */
@RunWith(JUnit4.class)
public class SpannerExceptionFactoryTest {
  @Test
  public void http2InternalErrorIsRetryable() {
    Status status =
        Status.fromCodeValue(Status.Code.INTERNAL.value())
            .withDescription("HTTP/2 error code: INTERNAL_ERROR");
    SpannerException e =
        SpannerExceptionFactory.newSpannerException(new StatusRuntimeException(status));
    assertThat(e.isRetryable()).isTrue();
  }

  @Test
  public void connectionClosedIsRetryable() {
    Status status =
        Status.fromCodeValue(Status.Code.INTERNAL.value())
            .withDescription("Connection closed with unknown cause");
    SpannerException e =
        SpannerExceptionFactory.newSpannerException(new StatusRuntimeException(status));
    assertThat(e.isRetryable()).isTrue();
  }

  @Test
  public void resourceExhausted() {
    Status status =
        Status.fromCodeValue(Status.Code.RESOURCE_EXHAUSTED.value())
            .withDescription("Memory pushback");
    SpannerException e =
        SpannerExceptionFactory.newSpannerException(new StatusRuntimeException(status));
    assertThat(e.isRetryable()).isFalse();
  }

  @Test
  public void resourceExhaustedWithBackoff() {
    Status status =
        Status.fromCodeValue(Status.Code.RESOURCE_EXHAUSTED.value())
            .withDescription("Memory pushback");
    Metadata trailers = new Metadata();
    Metadata.Key<RetryInfo> key = ProtoUtils.keyForProto(RetryInfo.getDefaultInstance());
    RetryInfo retryInfo =
        RetryInfo.newBuilder()
            .setRetryDelay(Duration.newBuilder().setNanos(1000000).setSeconds(1L))
            .build();
    trailers.put(key, retryInfo);
    SpannerException e =
        SpannerExceptionFactory.newSpannerException(new StatusRuntimeException(status, trailers));
    assertThat(e.isRetryable()).isTrue();
    assertThat(e.getRetryDelayInMillis()).isEqualTo(1001);
  }

  @Test
  public void abortWithRetryInfo() {
    Metadata.Key<RetryInfo> key = ProtoUtils.keyForProto(RetryInfo.getDefaultInstance());
    Status status = Status.fromCodeValue(Status.Code.ABORTED.value());
    Metadata trailers = new Metadata();
    RetryInfo retryInfo =
        RetryInfo.newBuilder()
            .setRetryDelay(Duration.newBuilder().setNanos(1000000).setSeconds(1L))
            .build();
    trailers.put(key, retryInfo);
    SpannerException e =
        SpannerExceptionFactory.newSpannerException(new StatusRuntimeException(status, trailers));
    assertThat(e).isInstanceOf(AbortedException.class);
    assertThat(((AbortedException) e).getRetryDelayInMillis()).isEqualTo(1001L);
  }

  @Test
  public void abortWithoutRetryInfo() {
    Status status = Status.fromCodeValue(Status.Code.ABORTED.value());
    SpannerException e =
        SpannerExceptionFactory.newSpannerException(new StatusRuntimeException(status));
    assertThat(e).isInstanceOf(AbortedException.class);
    assertThat(((AbortedException) e).getRetryDelayInMillis()).isEqualTo(-1L);
  }

  @Test
  public void abortWithoutDuration() {
    Metadata.Key<RetryInfo> key = ProtoUtils.keyForProto(RetryInfo.getDefaultInstance());
    Status status = Status.fromCodeValue(Status.Code.ABORTED.value());
    Metadata trailers = new Metadata();
    trailers.put(key, RetryInfo.getDefaultInstance());
    SpannerException e =
        SpannerExceptionFactory.newSpannerException(new StatusRuntimeException(status, trailers));
    assertThat(e).isInstanceOf(AbortedException.class);
    assertThat(((AbortedException) e).getRetryDelayInMillis()).isEqualTo(-1L);
  }

  @Test
  public void nullCancel() {
    Context context = Mockito.mock(Context.class);
    Mockito.when(context.isCancelled()).thenReturn(true);
    Mockito.when(context.cancellationCause()).thenReturn(null);
    SpannerException spannerException =
        SpannerExceptionFactory.newSpannerExceptionForCancellation(context, null);
    assertThat(spannerException.getMessage()).isEqualTo("CANCELLED: Current context was cancelled");
  }
}

@@ -682,16 +682,114 @@
 tThat;%0A%0A
+import com.google.api.gax.grpc.GrpcStatusCode;%0Aimport com.google.api.gax.rpc.ApiExceptionFactory;%0A
 import c
@@ -922,16 +922,44 @@
 Status;%0A
+import io.grpc.Status.Code;%0A
 import i
@@ -5169,11 +5169,1001 @@
 d%22);%0A  %7D
+%0A%0A  @Test%0A  public void statusRuntimeExceptionSessionNotFound() %7B%0A    SpannerException spannerException =%0A        SpannerExceptionFactory.newSpannerException(%0A            Status.NOT_FOUND%0A                .withDescription(%0A                    %22NOT_FOUND: Session not found: projects/%3Cproject%3E/instances/%3Cinstance%3E/databases/%3Cdatabase%3E/sessions/%3Csession id%3E%22)%0A                .asRuntimeException());%0A    assertThat(spannerException).isInstanceOf(SessionNotFoundException.class);%0A  %7D%0A%0A  @Test%0A  public void apiExceptionSessionNotFound() %7B%0A    SpannerException spannerException =%0A        SpannerExceptionFactory.newSpannerException(%0A            ApiExceptionFactory.createException(%0A                %22NOT_FOUND: Session not found: projects/%3Cproject%3E/instances/%3Cinstance%3E/databases/%3Cdatabase%3E/sessions/%3Csession id%3E%22,%0A                null,%0A                GrpcStatusCode.of(Code.NOT_FOUND),%0A                false));%0A    assertThat(spannerException).isInstanceOf(SessionNotFoundException.class);%0A  %7D
 %0A%7D%0A

add tests for translating to SessionNotFoundException (#6438)