// Path: src/main/java/com/google/fleetengine/auth/token/FleetEngineToken.java
// @AutoValue
// public abstract class FleetEngineToken {
//   /** Token creation timestamp. */
//   public abstract Date creationTimestamp();
// 
//   /** Token expiration timestamp. */
//   public abstract Date expirationTimestamp();
// 
//   /** Audience (aud claim) of the JWT. */
//   public abstract String audience();
// 
//   /** Signed and base64 encoded JWT. */
//   @Nullable
//   public abstract String jwt();
// 
//   /** Scope of the authorization token. */
//   public abstract FleetEngineTokenType tokenType();
// 
//   /** Custom authorization claims written to JWT. */
//   public abstract FleetEngineTokenClaims authorizationClaims();
// 
//   /** Provides a builder for {@link FleetEngineToken}. */
//   public static Builder builder() {
//     return new AutoValue_FleetEngineToken.Builder()
//         .setAudience(CommonConstants.DEFAULT_JWT_AUDIENCE);
//   }
// 
//   public abstract FleetEngineToken.Builder toBuilder();
// 
//   /** Builder class for FleetEngineToken. */
//   @AutoValue.Builder
//   public abstract static class Builder {
// 
//     /** Setter for token creation timestamp. */
//     public abstract Builder setCreationTimestamp(Date creationTimestamp);
// 
//     /** Setter for token expiration timestamp. */
//     public abstract Builder setExpirationTimestamp(Date expirationTimestamp);
// 
//     /** Setter for a signed JWT to be associated with the token object. */
//     public abstract Builder setJwt(String token);
// 
//     /** Setter for the token type defining the scope of the token. */
//     public abstract Builder setTokenType(FleetEngineTokenType type);
// 
//     /** Setter for token audience (aud claim). */
//     public abstract Builder setAudience(String audience);
// 
//     /** Sets custom authorization claims written to JWT. */
//     public abstract Builder setAuthorizationClaims(FleetEngineTokenClaims claims);
// 
//     /** Builds the token object. */
//     public abstract FleetEngineToken build();
//   }
// }
// 
// Path: src/main/java/com/google/fleetengine/auth/token/FleetEngineTokenType.java
// public enum FleetEngineTokenType {
//   /**
//    * Consumer tokens are usually service accounts associated with the Fleet Engine Consumer SDK User
//    * role on the Google Cloud project.
//    */
//   CONSUMER,
// 
//   /**
//    * Server tokens are usually service accounts associated with the Fleet Engine Super User role on
//    * the Google Cloud project.
//    */
//   SERVER,
// 
//   /**
//    * Driver tokens are usually service accounts associated with the Fleet Engine Driver SDK User
//    * role on the Google Cloud project.
//    */
//   DRIVER,
// 
//   /**
//    * Delivery server tokens are usually service accounts associated with the Fleet Engine Delivery
//    * Super User role on the Google Cloud project.
//    */
//   DELIVERY_SERVER,
// 
//   /**
//    * Driver tokens are usually service accounts associated with the Fleet Engine Delivery Consumer
//    * User role on the Google Cloud project.
//    */
//   DELIVERY_CONSUMER,
// 
//   /**
//    * Driver tokens are usually service accounts associated with the Fleet Engine Delivery Untrusted
//    * Driver User role on the Google Cloud project.
//    */
//   UNTRUSTED_DELIVERY_DRIVER,
// 
//   /**
//    * Driver tokens are usually service accounts associated with the Fleet Engine Delivery Trusted
//    * Driver User role on the Google Cloud project.
//    */
//   TRUSTED_DELIVERY_DRIVER,
// 
//   /**
//    * Fleet reader tokens are usually service accounts associated with the Fleet Engine Delivery
//    * Fleet Reader User role on the Google Cloud project.
//    */
//   DELIVERY_FLEET_READER,
// 
//   /** Custom token type associated with any Fleet Engine Role on the Google Cloud project. */
//   CUSTOM,
// }


import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import com.google.fleetengine.auth.token.FleetEngineToken;
import com.google.fleetengine.auth.token.FleetEngineTokenType;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.Date;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
// 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.google.fleetengine.auth;



@RunWith(JUnit4.class)
public class FleetEngineTokenExpiryValidatorTest {

  private FleetEngineToken defaultToken;
  private Clock fakeNowClock;

  @Before
  public void setup() {
    this.fakeNowClock = mock(Clock.class);

    // Setup default token, expiration timestamp will be changed in each test method.
    defaultToken =
        FleetEngineToken.builder()
            .setCreationTimestamp(Date.from(Instant.EPOCH))
            .setExpirationTimestamp(Date.from(Instant.EPOCH))
            .setAuthorizationClaims(EmptyFleetEngineTokenClaims.INSTANCE)
            .setTokenType(FleetEngineTokenType.SERVER)
            .build();
  }

  @Test
  public void isTokenExpired_whenBeforeExpirationWindow_tokenIsNotExpired() {
    Duration expirationWindowDuration = Duration.ofDays(1);
    FleetEngineToken expiredToken =
        defaultToken.toBuilder().setExpirationTimestamp(parseDate("2020-06-15")).build();
    Instant now = parseDateTime("2020-06-13T23:00:00.00Z").toInstant();
    when(fakeNowClock.instant()).thenReturn(now);
// Path: src/main/java/com/google/fleetengine/auth/token/FleetEngineToken.java
// @AutoValue
// public abstract class FleetEngineToken {
//   /** Token creation timestamp. */
//   public abstract Date creationTimestamp();
// 
//   /** Token expiration timestamp. */
//   public abstract Date expirationTimestamp();
// 
//   /** Audience (aud claim) of the JWT. */
//   public abstract String audience();
// 
//   /** Signed and base64 encoded JWT. */
//   @Nullable
//   public abstract String jwt();
// 
//   /** Scope of the authorization token. */
//   public abstract FleetEngineTokenType tokenType();
// 
//   /** Custom authorization claims written to JWT. */
//   public abstract FleetEngineTokenClaims authorizationClaims();
// 
//   /** Provides a builder for {@link FleetEngineToken}. */
//   public static Builder builder() {
//     return new AutoValue_FleetEngineToken.Builder()
//         .setAudience(CommonConstants.DEFAULT_JWT_AUDIENCE);
//   }
// 
//   public abstract FleetEngineToken.Builder toBuilder();
// 
//   /** Builder class for FleetEngineToken. */
//   @AutoValue.Builder
//   public abstract static class Builder {
// 
//     /** Setter for token creation timestamp. */
//     public abstract Builder setCreationTimestamp(Date creationTimestamp);
// 
//     /** Setter for token expiration timestamp. */
//     public abstract Builder setExpirationTimestamp(Date expirationTimestamp);
// 
//     /** Setter for a signed JWT to be associated with the token object. */
//     public abstract Builder setJwt(String token);
// 
//     /** Setter for the token type defining the scope of the token. */
//     public abstract Builder setTokenType(FleetEngineTokenType type);
// 
//     /** Setter for token audience (aud claim). */
//     public abstract Builder setAudience(String audience);
// 
//     /** Sets custom authorization claims written to JWT. */
//     public abstract Builder setAuthorizationClaims(FleetEngineTokenClaims claims);
// 
//     /** Builds the token object. */
//     public abstract FleetEngineToken build();
//   }
// }
// 
// Path: src/main/java/com/google/fleetengine/auth/token/FleetEngineTokenType.java
// public enum FleetEngineTokenType {
//   /**
//    * Consumer tokens are usually service accounts associated with the Fleet Engine Consumer SDK User
//    * role on the Google Cloud project.
//    */
//   CONSUMER,
// 
//   /**
//    * Server tokens are usually service accounts associated with the Fleet Engine Super User role on
//    * the Google Cloud project.
//    */
//   SERVER,
// 
//   /**
//    * Driver tokens are usually service accounts associated with the Fleet Engine Driver SDK User
//    * role on the Google Cloud project.
//    */
//   DRIVER,
// 
//   /**
//    * Delivery server tokens are usually service accounts associated with the Fleet Engine Delivery
//    * Super User role on the Google Cloud project.
//    */
//   DELIVERY_SERVER,
// 
//   /**
//    * Driver tokens are usually service accounts associated with the Fleet Engine Delivery Consumer
//    * User role on the Google Cloud project.
//    */
//   DELIVERY_CONSUMER,
// 
//   /**
//    * Driver tokens are usually service accounts associated with the Fleet Engine Delivery Untrusted
//    * Driver User role on the Google Cloud project.
//    */
//   UNTRUSTED_DELIVERY_DRIVER,
// 
//   /**
//    * Driver tokens are usually service accounts associated with the Fleet Engine Delivery Trusted
//    * Driver User role on the Google Cloud project.
//    */
//   TRUSTED_DELIVERY_DRIVER,
// 
//   /**
//    * Fleet reader tokens are usually service accounts associated with the Fleet Engine Delivery
//    * Fleet Reader User role on the Google Cloud project.
//    */
//   DELIVERY_FLEET_READER,
// 
//   /** Custom token type associated with any Fleet Engine Role on the Google Cloud project. */
//   CUSTOM,
// }


// Path: src/test/java/com/google/fleetengine/auth/FleetEngineTokenExpiryValidatorTest.java
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import com.google.fleetengine.auth.token.FleetEngineToken;
import com.google.fleetengine.auth.token.FleetEngineTokenType;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.Date;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

// 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.google.fleetengine.auth;



@RunWith(JUnit4.class)
public class FleetEngineTokenExpiryValidatorTest {

  private FleetEngineToken defaultToken;
  private Clock fakeNowClock;

  @Before
  public void setup() {
    this.fakeNowClock = mock(Clock.class);

    // Setup default token, expiration timestamp will be changed in each test method.
    defaultToken =
        FleetEngineToken.builder()
            .setCreationTimestamp(Date.from(Instant.EPOCH))
            .setExpirationTimestamp(Date.from(Instant.EPOCH))
            .setAuthorizationClaims(EmptyFleetEngineTokenClaims.INSTANCE)
            .setTokenType(FleetEngineTokenType.SERVER)
            .build();
  }

  @Test
  public void isTokenExpired_whenBeforeExpirationWindow_tokenIsNotExpired() {
    Duration expirationWindowDuration = Duration.ofDays(1);
    FleetEngineToken expiredToken =
        defaultToken.toBuilder().setExpirationTimestamp(parseDate("2020-06-15")).build();
    Instant now = parseDateTime("2020-06-13T23:00:00.00Z").toInstant();
    when(fakeNowClock.instant()).thenReturn(now);
    FleetEngineTokenExpiryValidator expiryValidator =