dimas-b commented on code in PR #461:
URL: https://github.com/apache/polaris/pull/461#discussion_r1861147455


##########
polaris-core/src/test/java/org/apache/polaris/core/persistence/secrets/PrincipalSecretsGeneratorTest.java:
##########
@@ -0,0 +1,113 @@
+/*
+ * 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.polaris.core.persistence.secrets;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.doReturn;
+
+import org.apache.polaris.core.entity.PolarisPrincipalSecrets;
+import org.jetbrains.annotations.Nullable;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+class PrincipalSecretsGeneratorTest {
+
+  @Test
+  void testRandomSecrets() {
+    RandomPrincipalSecretsGenerator rpsg = new 
RandomPrincipalSecretsGenerator("realm");
+    PolarisPrincipalSecrets s = rpsg.produceSecrets("name1", 123);
+    assertThat(s).isNotNull();
+    assertThat(s.getPrincipalId()).isEqualTo(123);
+    assertThat(s.getPrincipalClientId()).isNotNull();
+    assertThat(s.getMainSecret()).isNotNull();
+    assertThat(s.getSecondarySecret()).isNotNull();
+  }
+
+  @Test
+  void testRandomSecretsNullRealm() {
+    RandomPrincipalSecretsGenerator rpsg = new 
RandomPrincipalSecretsGenerator(null);
+    PolarisPrincipalSecrets s = rpsg.produceSecrets("name1", 123);
+    assertThat(s).isNotNull();
+    assertThat(s.getPrincipalId()).isEqualTo(123);
+    assertThat(s.getPrincipalClientId()).isNotNull();
+    assertThat(s.getMainSecret()).isNotNull();
+    assertThat(s.getSecondarySecret()).isNotNull();
+  }
+
+  @Test
+  void testEnvVariableSecrets() {
+    EnvVariablePrincipalSecretsGenerator psg =
+        Mockito.spy(new EnvVariablePrincipalSecretsGenerator("REALM"));
+
+    String clientIdKey = "POLARIS_BOOTSTRAP_REALM_PRINCIPAL_CLIENT_ID";
+    String clientSecretKey = "POLARIS_BOOTSTRAP_REALM_PRINCIPAL_CLIENT_SECRET";
+
+    doReturn("test-id").when(psg).getEnvironmentVariable(clientIdKey);

Review Comment:
   I think it would be nicer to allow `EnvVariablePrincipalSecretsGenerator` to 
take an explicit `Map` (or a `Function<String, String>`) as a constructor 
argument. The the "environment" part cab be just a static factory method like 
`EnvVariablePrincipalSecretsGenerator.fromEnv()`.
   
   Testing the static method would not really be necessary because it would be 
a one-line redirect to `System.env()`, but it would allow for nicer design that 
is testable without `Mockito`.
   
   Just my 2 cents :)



##########
polaris-core/src/test/java/org/apache/polaris/core/persistence/secrets/PrincipalSecretsGeneratorTest.java:
##########
@@ -0,0 +1,113 @@
+/*
+ * 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.polaris.core.persistence.secrets;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.doReturn;
+
+import org.apache.polaris.core.entity.PolarisPrincipalSecrets;
+import org.jetbrains.annotations.Nullable;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+class PrincipalSecretsGeneratorTest {
+
+  @Test
+  void testRandomSecrets() {
+    RandomPrincipalSecretsGenerator rpsg = new 
RandomPrincipalSecretsGenerator("realm");
+    PolarisPrincipalSecrets s = rpsg.produceSecrets("name1", 123);
+    assertThat(s).isNotNull();
+    assertThat(s.getPrincipalId()).isEqualTo(123);
+    assertThat(s.getPrincipalClientId()).isNotNull();
+    assertThat(s.getMainSecret()).isNotNull();
+    assertThat(s.getSecondarySecret()).isNotNull();
+  }
+
+  @Test
+  void testRandomSecretsNullRealm() {
+    RandomPrincipalSecretsGenerator rpsg = new 
RandomPrincipalSecretsGenerator(null);
+    PolarisPrincipalSecrets s = rpsg.produceSecrets("name1", 123);
+    assertThat(s).isNotNull();
+    assertThat(s.getPrincipalId()).isEqualTo(123);
+    assertThat(s.getPrincipalClientId()).isNotNull();
+    assertThat(s.getMainSecret()).isNotNull();
+    assertThat(s.getSecondarySecret()).isNotNull();
+  }
+
+  @Test
+  void testEnvVariableSecrets() {
+    EnvVariablePrincipalSecretsGenerator psg =
+        Mockito.spy(new EnvVariablePrincipalSecretsGenerator("REALM"));
+
+    String clientIdKey = "POLARIS_BOOTSTRAP_REALM_PRINCIPAL_CLIENT_ID";
+    String clientSecretKey = "POLARIS_BOOTSTRAP_REALM_PRINCIPAL_CLIENT_SECRET";
+
+    doReturn("test-id").when(psg).getEnvironmentVariable(clientIdKey);
+    doReturn("test-secret").when(psg).getEnvironmentVariable(clientSecretKey);
+
+    // Invoke the method
+    PolarisPrincipalSecrets secrets = psg.produceSecrets("PRINCIPAL", 123);
+
+    // Verify the result
+    Assertions.assertNotNull(secrets);
+    Assertions.assertEquals(123, secrets.getPrincipalId());
+    Assertions.assertEquals("test-id", secrets.getPrincipalClientId());
+    Assertions.assertEquals("test-secret", secrets.getMainSecret());
+  }
+
+  @Test
+  void testBoostrapGeneratorDelegationToRandomPrincipalSecrets() {
+    EnvVariablePrincipalSecretsGenerator 
mockedEnvVariablePrincipalSecretsGenerator =
+        Mockito.spy(new EnvVariablePrincipalSecretsGenerator("REALM"));
+
+    String clientIdKey = "POLARIS_BOOTSTRAP_REALM_PRINCIPAL_CLIENT_ID";
+    String clientSecretKey = "POLARIS_BOOTSTRAP_REALM_PRINCIPAL_CLIENT_SECRET";
+
+    doReturn("test-id")
+        .when(mockedEnvVariablePrincipalSecretsGenerator)
+        .getEnvironmentVariable(clientIdKey);
+    doReturn("test-secret")
+        .when(mockedEnvVariablePrincipalSecretsGenerator)
+        .getEnvironmentVariable(clientSecretKey);
+
+    class ExposingPrincipalSecretsGenerator extends 
BootstrapPrincipalSecretsGenerator {
+      public ExposingPrincipalSecretsGenerator(@Nullable String realmName) {
+        super(realmName);
+      }
+
+      @Override
+      protected PrincipalSecretsGenerator 
buildEnvVariablePrincipalSecretsGenerator(

Review Comment:
   same here: I think we could have a simple factory that bind to 
`System.env()` in runtime, but use explicit constructor parameters in tests... 
This will remove reliance on `Mockito`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to