/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.beam.sdk.util;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Tests for {@link org.apache.beam.sdk.util.Secret}. */
@RunWith(JUnit4.class)
public class SecretTest {

  @Test
  public void testParseSecretOptionWithValidGcpSecret() {
    String secretOption = "type:gcpsecret;version_name:my_secret/versions/latest";
    Secret secret = Secret.parseSecretOption(secretOption);
    assertTrue(secret instanceof GcpSecret);
    assertEquals("my_secret/versions/latest", ((GcpSecret) secret).getVersionName());
  }

  @Test
  public void testParseSecretOptionWithValidGcpHsmGeneratedSecret() {
    String secretOption =
        "type:gcphsmgeneratedsecret;project_id:my-project;location_id:global;key_ring_id:my-key-ring;key_id:my-key;job_name:my-job";
    Secret secret = Secret.parseSecretOption(secretOption);
    assertTrue(secret instanceof GcpHsmGeneratedSecret);
    GcpHsmGeneratedSecret hsmSecret = (GcpHsmGeneratedSecret) secret;
    assertEquals("my-project", hsmSecret.getProjectId());
    assertEquals("global", hsmSecret.getLocationId());
    assertEquals("my-key-ring", hsmSecret.getKeyRingId());
    assertEquals("my-key", hsmSecret.getKeyId());
    assertEquals("HsmGeneratedSecret_my-job", hsmSecret.getSecretId());
  }

  @Test
  public void testParseSecretOptionWithMissingType() {
    String secretOption = "version_name:my_secret/versions/latest";
    Exception exception =
        assertThrows(RuntimeException.class, () -> Secret.parseSecretOption(secretOption));
    assertEquals("Secret string must contain a valid type parameter", exception.getMessage());
  }

  @Test
  public void testParseSecretOptionWithUnsupportedType() {
    String secretOption = "type:unsupported;version_name:my_secret/versions/latest";
    Exception exception =
        assertThrows(RuntimeException.class, () -> Secret.parseSecretOption(secretOption));
    assertTrue(exception.getMessage().contains("Invalid secret type unsupported"));
  }

  @Test
  public void testParseSecretOptionWithInvalidGcpSecretParameter() {
    String secretOption = "type:gcpsecret;invalid_param:some_value";
    Exception exception =
        assertThrows(RuntimeException.class, () -> Secret.parseSecretOption(secretOption));
    assertEquals(
        "Invalid secret parameter invalid_param, GcpSecret only supports the following parameters: [version_name]",
        exception.getMessage());
  }
}
