Refactor Element ACM for prometheus configuration 95/130595/3
authorFrancescoFioraEst <francesco.fiora@est.tech>
Wed, 31 Aug 2022 13:12:15 +0000 (14:12 +0100)
committerFrancesco Fiora <francesco.fiora@est.tech>
Thu, 1 Sep 2022 14:57:30 +0000 (14:57 +0000)
Issue-ID: POLICY-4337
Change-Id: Ic8e43748bc8ffc9a9513f7f278afae9f3f393bc9
Signed-off-by: FrancescoFioraEst <francesco.fiora@est.tech>
packages/policy-clamp-tarball/src/main/resources/etc/ElementParticipantParameters.yaml
participant/participant-impl/participant-impl-acelement/src/main/java/org/onap/policy/clamp/acm/element/config/MicrometerConfig.java
participant/participant-impl/participant-impl-acelement/src/main/java/org/onap/policy/clamp/acm/element/main/rest/AbstractRestController.java
participant/participant-impl/participant-impl-acelement/src/main/resources/config/application.yaml
participant/participant-impl/participant-impl-acelement/src/test/java/org/onap/policy/clamp/acm/element/rest/AcElementControllerTest.java
participant/participant-impl/participant-impl-acelement/src/test/java/org/onap/policy/clamp/acm/element/rest/ActuatorControllerTest.java [new file with mode: 0644]
participant/participant-impl/participant-impl-acelement/src/test/java/org/onap/policy/clamp/acm/element/utils/CommonActuatorController.java [new file with mode: 0644]
participant/participant-impl/participant-impl-acelement/src/test/resources/application-test.yaml

index 76dbeaa..4d77103 100644 (file)
@@ -1,22 +1,21 @@
 spring:
   security:
     user:
-      name: acmUser
-      password: zb!XztG34
+      name: ${REST_USER:acmUser}
+      password: ${REST_PASSWORD:zb!XztG34}
 
 server:
-  port: 8084
-  servlet:
-    context-path: /onap/policy/clamp/acelement/
+  port: ${PORT:8084}
   error:
     path: /error
 
 element:
   elementId:
-    name: onap.policy.clamp.ac.element1
-    version: 1.0.0
+    name: ${ELEMENT_ID:onap.policy.clamp.ac.element1}
+    version: ${ELEMENT_VERSION:1.0.0}
 management:
   endpoints:
     web:
+      base-path: /
       exposure:
         include: health, metrics, prometheus
index 8e83a77..d34b34a 100644 (file)
@@ -22,12 +22,23 @@ package org.onap.policy.clamp.acm.element.config;
 
 import io.micrometer.core.aop.TimedAspect;
 import io.micrometer.core.instrument.MeterRegistry;
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.beans.factory.config.BeanPostProcessor;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 
 @Configuration
 public class MicrometerConfig {
 
+    /**
+     * Load up the metrics registry.
+     */
+    @Bean
+    public InitializingBean forcePrometheusPostProcessor(BeanPostProcessor meterRegistryPostProcessor,
+            MeterRegistry registry) {
+        return () -> meterRegistryPostProcessor.postProcessAfterInitialization(registry, "");
+    }
+
     @Bean
     public TimedAspect timedAspect(MeterRegistry registry) {
         return new TimedAspect(registry);
index 408458f..7c963b6 100644 (file)
@@ -35,7 +35,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
  */
 // @formatter:off
 @RequestMapping(
-        value = "/v2",
+        value = "/onap/policy/clamp/acelement/v2",
         produces = {
             MediaType.APPLICATION_JSON,
             AbstractRestController.APPLICATION_YAML
index 4a0f0d3..4d77103 100644 (file)
@@ -1,22 +1,21 @@
 spring:
   security:
     user:
-      name: acmUser
-      password: zb!XztG34
+      name: ${REST_USER:acmUser}
+      password: ${REST_PASSWORD:zb!XztG34}
 
 server:
-  port: 8084
-  servlet:
-    context-path: /onap/policy/clamp/acelement/
+  port: ${PORT:8084}
   error:
     path: /error
 
 element:
   elementId:
-    name: onap.policy.clamp.ac.element1
-    version: 1.0.0
+    name: ${ELEMENT_ID:onap.policy.clamp.ac.element1}
+    version: ${ELEMENT_VERSION:1.0.0}
 management:
   endpoints:
     web:
+      base-path: /
       exposure:
-        include: health, metrics, prometheus
\ No newline at end of file
+        include: health, metrics, prometheus
index c40cdea..001b2be 100644 (file)
@@ -67,9 +67,9 @@ class AcElementControllerTest {
 
     private static final Coder CODER = new StandardCoder();
     private static final String ELEMENT_CONFIG_YAML = "src/test/resources/config.json";
-    private static final String RETRIEVE_CONFIG = "/v2/config";
-    private static final String ACTIVATE_CONFIG = "/v2/activate";
-    private static final String DEACTIVATE_CONFIG = "/v2/deactivate";
+    private static final String RETRIEVE_CONFIG = "/onap/policy/clamp/acelement/v2/config";
+    private static final String ACTIVATE_CONFIG = "/onap/policy/clamp/acelement/v2/activate";
+    private static final String DEACTIVATE_CONFIG = "/onap/policy/clamp/acelement/v2/deactivate";
     private static ElementConfig config;
 
 
diff --git a/participant/participant-impl/participant-impl-acelement/src/test/java/org/onap/policy/clamp/acm/element/rest/ActuatorControllerTest.java b/participant/participant-impl/participant-impl-acelement/src/test/java/org/onap/policy/clamp/acm/element/rest/ActuatorControllerTest.java
new file mode 100644 (file)
index 0000000..489056a
--- /dev/null
@@ -0,0 +1,91 @@
+/*-
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2022 Nordix Foundation.
+ * ================================================================================
+ * 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.clamp.acm.element.rest;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import javax.ws.rs.client.Invocation;
+import javax.ws.rs.core.Response;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.onap.policy.clamp.acm.element.utils.CommonActuatorController;
+import org.springframework.boot.test.autoconfigure.actuate.metrics.AutoConfigureMetrics;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
+import org.springframework.boot.web.server.LocalServerPort;
+import org.springframework.test.context.ActiveProfiles;
+import org.springframework.test.context.junit.jupiter.SpringExtension;
+
+@AutoConfigureMetrics
+@ExtendWith(SpringExtension.class)
+@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
+@ActiveProfiles("test")
+class ActuatorControllerTest extends CommonActuatorController {
+
+    private static final String HEALTH_ENDPOINT = "health";
+    private static final String METRICS_ENDPOINT = "metrics";
+    private static final String PROMETHEUS_ENDPOINT = "prometheus";
+
+    @LocalServerPort
+    private int randomServerPort;
+
+    @BeforeEach
+    public void setUpPort() {
+        super.setHttpPrefix(randomServerPort);
+    }
+
+    @Test
+    void testGetHealth_Unauthorized() throws Exception {
+        assertUnauthorizedActGet(HEALTH_ENDPOINT);
+    }
+
+    @Test
+    void testGetMetrics_Unauthorized() throws Exception {
+        assertUnauthorizedActGet(METRICS_ENDPOINT);
+    }
+
+    @Test
+    void testGetPrometheus_Unauthorized() throws Exception {
+        assertUnauthorizedActGet(PROMETHEUS_ENDPOINT);
+    }
+
+    @Test
+    void testGetHealth() throws Exception {
+        Invocation.Builder invocationBuilder = super.sendActRequest(HEALTH_ENDPOINT);
+        Response rawresp = invocationBuilder.buildGet().invoke();
+        assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
+    }
+
+    @Test
+    void testGetMetrics() throws Exception {
+        Invocation.Builder invocationBuilder = super.sendActRequest(METRICS_ENDPOINT);
+        Response rawresp = invocationBuilder.buildGet().invoke();
+        assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
+    }
+
+    @Test
+    void testGePrometheus() throws Exception {
+        Invocation.Builder invocationBuilder = super.sendActRequest(PROMETHEUS_ENDPOINT);
+        Response rawresp = invocationBuilder.buildGet().invoke();
+        assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
+    }
+}
diff --git a/participant/participant-impl/participant-impl-acelement/src/test/java/org/onap/policy/clamp/acm/element/utils/CommonActuatorController.java b/participant/participant-impl/participant-impl-acelement/src/test/java/org/onap/policy/clamp/acm/element/utils/CommonActuatorController.java
new file mode 100644 (file)
index 0000000..022925d
--- /dev/null
@@ -0,0 +1,107 @@
+/*-
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2022 Nordix Foundation.
+ * ================================================================================
+ * 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.clamp.acm.element.utils;
+
+import static org.junit.Assert.assertEquals;
+
+import javax.ws.rs.client.Client;
+import javax.ws.rs.client.ClientBuilder;
+import javax.ws.rs.client.Invocation;
+import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import org.glassfish.jersey.client.ClientProperties;
+import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
+import org.onap.policy.common.gson.GsonMessageBodyHandler;
+import org.onap.policy.common.utils.network.NetworkUtil;
+
+/**
+ * Class to perform Rest unit tests.
+ *
+ */
+public class CommonActuatorController {
+
+    public static final String SELF = NetworkUtil.getHostname();
+
+    private static String httpPrefix;
+
+    /**
+     * Sends a request to an actuator endpoint.
+     *
+     * @param endpoint the target endpoint
+     * @return a request builder
+     */
+    protected Invocation.Builder sendActRequest(final String endpoint) {
+        return sendFqeRequest(httpPrefix + endpoint, true);
+    }
+
+    /**
+     * Sends a request to an actuator endpoint, without any authorization header.
+     *
+     * @param endpoint the target endpoint
+     * @return a request builder
+     */
+    protected Invocation.Builder sendNoAuthActRequest(final String endpoint) {
+        return sendFqeRequest(httpPrefix + endpoint, false);
+    }
+
+    /**
+     * Sends a request to a fully qualified endpoint.
+     *
+     * @param fullyQualifiedEndpoint the fully qualified target endpoint
+     * @param includeAuth if authorization header should be included
+     * @return a request builder
+     */
+    protected Invocation.Builder sendFqeRequest(final String fullyQualifiedEndpoint, boolean includeAuth) {
+        final Client client = ClientBuilder.newBuilder().build();
+
+        client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true");
+        client.register(GsonMessageBodyHandler.class);
+
+        if (includeAuth) {
+            client.register(HttpAuthenticationFeature.basic("acmUser", "zb!XztG34"));
+        }
+
+        final WebTarget webTarget = client.target(fullyQualifiedEndpoint);
+
+        return webTarget.request(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN);
+    }
+
+    /**
+     * Assert that GET call to actuator endpoint is Unauthorized.
+     *
+     * @param endPoint the endpoint
+     */
+    protected void assertUnauthorizedActGet(final String endPoint) {
+        Response rawresp = sendNoAuthActRequest(endPoint).buildGet().invoke();
+        assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), rawresp.getStatus());
+    }
+
+    /**
+     * Set Up httpPrefix.
+     *
+     * @param port the port
+     */
+    protected void setHttpPrefix(int port) {
+        httpPrefix = "http://" + SELF + ":" + port + "/";
+    }
+
+}
index 76dbeaa..d521ad7 100644 (file)
@@ -6,8 +6,6 @@ spring:
 
 server:
   port: 8084
-  servlet:
-    context-path: /onap/policy/clamp/acelement/
   error:
     path: /error
 
@@ -18,5 +16,6 @@ element:
 management:
   endpoints:
     web:
+      base-path: /
       exposure:
         include: health, metrics, prometheus