This is an automated email from the ASF dual-hosted git repository.

gtully pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/activemq-artemis.git


The following commit(s) were added to refs/heads/main by this push:
     new bd72a4f38d ARTEMIS-3168 - more idomatic usage of mock-server-netty - 
with hasStarted
bd72a4f38d is described below

commit bd72a4f38dbd3dbbeb6079977e8c9680bd24e00f
Author: Gary Tully <[email protected]>
AuthorDate: Mon Dec 5 11:14:56 2022 +0000

    ARTEMIS-3168 - more idomatic usage of mock-server-netty - with hasStarted
---
 .../kubernetes/client/KubernetesClientImpl.java    |   7 +-
 .../client/KubernetesClientImplTest.java           | 118 +++++++++++++++------
 2 files changed, 88 insertions(+), 37 deletions(-)

diff --git 
a/artemis-server/src/main/java/org/apache/activemq/artemis/spi/core/security/jaas/kubernetes/client/KubernetesClientImpl.java
 
b/artemis-server/src/main/java/org/apache/activemq/artemis/spi/core/security/jaas/kubernetes/client/KubernetesClientImpl.java
index 6172bcd15a..605d59e8e3 100644
--- 
a/artemis-server/src/main/java/org/apache/activemq/artemis/spi/core/security/jaas/kubernetes/client/KubernetesClientImpl.java
+++ 
b/artemis-server/src/main/java/org/apache/activemq/artemis/spi/core/security/jaas/kubernetes/client/KubernetesClientImpl.java
@@ -66,12 +66,13 @@ public class KubernetesClientImpl implements 
KubernetesClient {
       String host = getParam(KUBERNETES_HOST);
       String port = getParam(KUBERNETES_PORT);
       this.apiUri = 
URI.create(String.format(KUBERNETES_TOKENREVIEW_URI_PATTERN, host, port));
+      logger.debug("using apiUri {}", apiUri);
    }
 
-   private String getParam(String name, String defaultValue) {
-      String value = System.getenv(name);
+   public String getParam(String name, String defaultValue) {
+      String value = System.getProperty(name);
       if (value == null) {
-         value = System.getProperty(name, defaultValue);
+         value = System.getenv(name);
       }
       if (value == null) {
          return defaultValue;
diff --git 
a/artemis-server/src/test/java/org/apache/activemq/artemis/spi/core/security/jaas/kubernetes/client/KubernetesClientImplTest.java
 
b/artemis-server/src/test/java/org/apache/activemq/artemis/spi/core/security/jaas/kubernetes/client/KubernetesClientImplTest.java
index 2e7c9f1e26..fc76158eb0 100644
--- 
a/artemis-server/src/test/java/org/apache/activemq/artemis/spi/core/security/jaas/kubernetes/client/KubernetesClientImplTest.java
+++ 
b/artemis-server/src/test/java/org/apache/activemq/artemis/spi/core/security/jaas/kubernetes/client/KubernetesClientImplTest.java
@@ -23,6 +23,7 @@ import static 
org.apache.activemq.artemis.spi.core.security.jaas.KubernetesLogin
 import static 
org.apache.activemq.artemis.spi.core.security.jaas.KubernetesLoginModuleTest.USERNAME;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
@@ -31,20 +32,29 @@ import static org.mockserver.model.HttpRequest.request;
 import static org.mockserver.model.HttpResponse.response;
 import static org.mockserver.model.JsonBody.json;
 
+import java.lang.invoke.MethodHandles;
 import java.net.URL;
+import java.util.Map;
+import java.util.Set;
 
 import 
org.apache.activemq.artemis.spi.core.security.jaas.kubernetes.model.TokenReview;
 import org.junit.AfterClass;
+import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
+import org.mockserver.configuration.Configuration;
 import org.mockserver.configuration.ConfigurationProperties;
 import org.mockserver.integration.ClientAndServer;
 import org.mockserver.matchers.MatchType;
 import org.mockserver.socket.PortFactory;
 import org.mockserver.verify.VerificationTimes;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class KubernetesClientImplTest {
 
+   private static final Logger logger = 
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+
    private static final String API_PATH = 
"/apis/authentication.k8s.io/v1/tokenreviews";
    private static ClientAndServer mockServer;
    private static final String host = "localhost";
@@ -60,53 +70,25 @@ public class KubernetesClientImplTest {
    public static void startServer() {
       
ConfigurationProperties.dynamicallyCreateCertificateAuthorityCertificate(true);
       
ConfigurationProperties.directoryToSaveDynamicSSLCertificate("target/test-classes");
-      ConfigurationProperties.preventCertificateDynamicUpdate(true);
+      ConfigurationProperties.preventCertificateDynamicUpdate(false);
       ConfigurationProperties.proactivelyInitialiseTLS(true);
 
-      mockServer = 
ClientAndServer.startClientAndServer(PortFactory.findFreePort());
+      Configuration configuration = Configuration.configuration();
+
+      mockServer = ClientAndServer.startClientAndServer(configuration, 
PortFactory.findFreePort());
       port = Integer.toString(mockServer.getPort());
 
       assertNotNull(mockServer);
-      assertTrue(mockServer.isRunning());
+      assertTrue(mockServer.hasStarted());
       System.setProperty("KUBERNETES_SERVICE_HOST", host);
       System.setProperty("KUBERNETES_SERVICE_PORT", port);
       System.setProperty("KUBERNETES_TOKEN_PATH",
             
KubernetesClientImplTest.class.getClassLoader().getResource("client_token").getPath());
 
-      mockServer.when(
-            request()
-                  .withMethod("POST")
-                  .withPath(API_PATH)
-                  .withBody(json(BOB_REQUEST, MatchType.STRICT)))
-            .respond(
-                  response()
-                        .withStatusCode(HTTP_CREATED)
-                        .withBody(UNAUTH_JSON));
-
-      mockServer.when(
-            request()
-                  .withMethod("POST")
-                  .withPath(API_PATH)
-                  .withBody(json(KERMIT_REQUEST, MatchType.STRICT)))
-            .respond(
-                  response()
-                        .withStatusCode(HTTP_CREATED)
-                        .withBody(AUTH_JSON));
-
-      mockServer.when(
-            request()
-                  .withMethod("POST")
-                  .withPath(API_PATH))
-            .respond(
-                  response()
-                        .withStatusCode(HTTP_INTERNAL_ERROR));
-
-
-      // proactivelyInitialiseTLS to 
dynamicallyCreateCertificateAuthorityCertificate
-      // only kicks in when the client is created to support the mock responses
       URL caPath = KubernetesClientImplTest.class.getClassLoader()
          .getResource("CertificateAuthorityCertificate.pem");
       assertNotNull(caPath);
+      logger.info("Setting KUBERNETES_CA_PATH {}", caPath.getPath());
       System.setProperty("KUBERNETES_CA_PATH", caPath.getPath());
    }
 
@@ -119,9 +101,42 @@ public class KubernetesClientImplTest {
       mockServer.stop();
    }
 
+   @Before
+   public void reset() {
+      mockServer.reset();
+   }
+
    @Test
    public void testGetTokenReview() {
 
+      mockServer.when(
+            request()
+               .withMethod("POST")
+               .withPath(API_PATH)
+               .withBody(json(BOB_REQUEST, MatchType.STRICT)))
+         .respond(
+            response()
+               .withStatusCode(HTTP_CREATED)
+               .withBody(UNAUTH_JSON));
+
+      mockServer.when(
+            request()
+               .withMethod("POST")
+               .withPath(API_PATH)
+               .withBody(json(KERMIT_REQUEST, MatchType.STRICT)))
+         .respond(
+            response()
+               .withStatusCode(HTTP_CREATED)
+               .withBody(AUTH_JSON));
+
+      mockServer.when(
+            request()
+               .withMethod("POST")
+               .withPath(API_PATH))
+         .respond(
+            response()
+               .withStatusCode(HTTP_INTERNAL_ERROR));
+
       KubernetesClient client = new KubernetesClientImpl();
 
       TokenReview tr = client.getTokenReview("bob_token");
@@ -144,4 +159,39 @@ public class KubernetesClientImplTest {
 
    }
 
+   @Test
+   public void testGetParam() throws Exception {
+      Set<Map.Entry<String, String>> env = System.getenv().entrySet();
+
+      for (Map.Entry<String, String> envKv : env) {
+
+         if (System.getProperty(envKv.getKey()) == null) {
+
+            KubernetesClientImpl clientImpl = new KubernetesClientImpl();
+            assertEquals(envKv.getValue(), clientImpl.getParam(envKv.getKey(), 
null));
+
+            final String valFromProp = "bla";
+            try {
+               System.setProperty(envKv.getKey(), valFromProp);
+               assertEquals(valFromProp, clientImpl.getParam(envKv.getKey(), 
null));
+            } finally {
+               System.clearProperty(envKv.getKey());
+            }
+
+            // verify default param for non exist env or prop
+            String candidate = valFromProp;
+            for (int i = 0; i < 10; i++) {
+               if (System.getenv(candidate) == null && 
System.getProperty(candidate) == null) {
+                  assertEquals(candidate, clientImpl.getParam(candidate, 
candidate));
+                  break;
+               }
+               candidate += i;
+            }
+
+            // one test is sufficient!
+            break;
+         }
+      }
+   }
+
 }

Reply via email to