Copyright (C) 2018 Ericsson. All rights reserved.
Modifications Copyright (C) 2018-2021 AT&T Intellectual Property. All rights reserved.
Modifications Copyright (C) 2019-2020 Nordix Foundation.
+ Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
================================================================================
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
<cambria.version>1.2.1-oss</cambria.version>
<http.client.version>4.5.5</http.client.version>
<http.core.version>4.4.4</http.core.version>
+ <io.prometheus.version>0.11.0</io.prometheus.version>
</properties>
<dependencies>
<artifactId>jackson-module-jaxb-annotations</artifactId>
</dependency>
+ <dependency>
+ <groupId>com.fasterxml.jackson.core</groupId>
+ <artifactId>jackson-annotations</artifactId>
+ <version>${version.jackson}</version>
+ </dependency>
+
<dependency>
<groupId>org.onap.dmaap.messagerouter.dmaapclient</groupId>
<artifactId>dmaapClient</artifactId>
<artifactId>swagger-jersey2-jaxrs</artifactId>
</dependency>
+ <dependency>
+ <groupId>io.prometheus</groupId>
+ <artifactId>simpleclient_hotspot</artifactId>
+ <version>${io.prometheus.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>io.prometheus</groupId>
+ <artifactId>simpleclient_servlet</artifactId>
+ <version>${io.prometheus.version}</version>
+ </dependency>
+
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
* ================================================================================
* Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2020 Nordix Foundation.
+ * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*/
void addServletClass(String servletPath, String restClass);
+ /**
+ * Adds a Java Servlet.
+ *
+ * @param servletPath servlet path
+ * @param plainServletClass servlet class
+ */
+ void addStdServletClass(String servletPath, String plainServletClass);
+
/**
* Adds a package containing JAX-RS classes to serve REST requests.
*
* @throws InterruptedException if the blocking operation is interrupted
*/
boolean waitedStart(long maxWaitTime) throws InterruptedException;
+
+ /**
+ * Are prometheus metrics enabled?.
+ */
+ public boolean isPrometheus();
+
+ /**
+ * Enable prometheus metrics.
+ */
+ public void setPrometheus(String metricsPath);
}
* ================================================================================
* Copyright (C) 2017-2019, 2021 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2020 Nordix Foundation.
+ * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
final var restUriPath = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_REST_URIPATH_SUFFIX, null);
addFilterClasses(props, service, restUriPath);
- addServletClasses(props, service, restUriPath);
+ addRestServletClasses(props, service, restUriPath);
addServletPackages(props, service, restUriPath);
+ addServletClass(props, service);
+ setPrometheus(props, service);
+
serviceList.add(service);
}
}
}
+ private void setPrometheus(PropertyUtils props, HttpServletServer service) {
+ if (props.getBoolean(PolicyEndPointProperties.PROPERTY_HTTP_PROMETHEUS_SUFFIX, false)) {
+ service.setPrometheus("/metrics");
+ }
+ }
+
private void addFilterClasses(PropertyUtils props, HttpServletServer service, final String restUriPath) {
final var filterClasses =
}
}
- private void addServletClasses(PropertyUtils props, HttpServletServer service, final String restUriPath) {
-
+ private void addRestServletClasses(PropertyUtils props, HttpServletServer service, final String restUriPath) {
final var restClasses = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX, null);
if (!StringUtils.isBlank(restClasses)) {
}
}
+ private void addServletClass(PropertyUtils props, HttpServletServer service) {
+ var servletClass = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_SERVLET_CLASS_SUFFIX, null);
+ var servletUriPath = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_SERVLET_URIPATH_SUFFIX, null);
+
+ if (!StringUtils.isBlank(servletClass) && !StringUtils.isBlank(servletUriPath)) {
+ service.addStdServletClass(servletUriPath, servletClass);
+ }
+ }
+
private void addServletPackages(PropertyUtils props, HttpServletServer service, final String restUriPath) {
final var restPackages = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_REST_PACKAGES_SUFFIX, null);
* ============LICENSE_START=======================================================
* Copyright (C) 2019 Nordix Foundation.
* Modifications Copyright (C) 2019-2021 AT&T Intellectual Property.
+ * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
import java.util.Arrays;
import java.util.List;
+import java.util.Optional;
import java.util.Properties;
import java.util.stream.Collectors;
import lombok.ToString;
props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER,
String.join(",", GsonMessageBodyHandler.class.getName(), YamlMessageBodyHandler.class.getName(),
JsonExceptionMapper.class.getName(), YamlExceptionMapper.class.getName()));
+
+ props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERVLET_URIPATH_SUFFIX,
+ Optional.ofNullable(restServerParameters.getServletUriPath()).orElse(""));
+ props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERVLET_CLASS_SUFFIX,
+ Optional.ofNullable(restServerParameters.getServletClass()).orElse(""));
+ props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_PROMETHEUS_SUFFIX,
+ String.valueOf(restServerParameters.isPrometheus()));
return props;
}
* ================================================================================
* Copyright (C) 2017-2021 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2019-2020 Nordix Foundation.
+ * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
package org.onap.policy.common.endpoints.http.server.internal;
import io.swagger.jersey.config.JerseyJaxrsConfig;
-import java.util.HashMap;
-import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jetty.servlet.ServletHolder;
import org.glassfish.jersey.server.ServerProperties;
*/
protected static Logger logger = LoggerFactory.getLogger(JettyJerseyServer.class);
- /**
- * Container for servlets.
- */
- protected final Map<String, ServletHolder> servlets = new HashMap<>();
-
/**
* Swagger ID.
*/
*/
protected void attachSwaggerServlet(boolean https) {
- ServletHolder swaggerServlet = context.addServlet(JerseyJaxrsConfig.class, "/");
+ ServletHolder swaggerServlet = getServlet(JerseyJaxrsConfig.class, "/");
String hostname = this.connector.getHost();
if (StringUtils.isBlank(hostname) || hostname.equals(NetworkUtil.IPV4_WILDCARD_ADDRESS)) {
* @throws IllegalArgumentException if invalid arguments are provided
*/
protected synchronized ServletHolder getServlet(String servletPath) {
-
- return servlets.computeIfAbsent(servletPath, key -> {
-
- ServletHolder jerseyServlet =
- context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, servletPath);
- jerseyServlet.setInitOrder(0);
-
- return jerseyServlet;
- });
+ ServletHolder jerseyServlet =
+ super.getServlet(org.glassfish.jersey.servlet.ServletContainer.class, servletPath);
+ jerseyServlet.setInitOrder(0);
+ return jerseyServlet;
}
@Override
* ================================================================================
* Copyright (C) 2017-2021 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2019-2020 Nordix Foundation.
- * Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
+ * Modifications Copyright (C) 2020-2021 Bell Canada. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
package org.onap.policy.common.endpoints.http.server.internal;
+import io.prometheus.client.exporter.MetricsServlet;
+import io.prometheus.client.hotspot.DefaultExports;
import java.util.EnumSet;
+import java.util.HashMap;
+import java.util.Map;
import javax.servlet.DispatcherType;
+import javax.servlet.Servlet;
import lombok.Getter;
+import lombok.NonNull;
import lombok.ToString;
import org.eclipse.jetty.security.ConstraintMapping;
import org.eclipse.jetty.security.ConstraintSecurityHandler;
import org.eclipse.jetty.server.Slf4jRequestLogWriter;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletContextHandler;
+import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.security.Constraint;
import org.eclipse.jetty.util.security.Credential;
import org.eclipse.jetty.util.ssl.SslContextFactory;
*/
protected Thread jettyThread;
+ /**
+ * Container for default servlets.
+ */
+ protected final Map<String, ServletHolder> servlets = new HashMap<>();
+
/**
* Start condition.
*/
context.addFilter(filterClass, tempFilterPath, EnumSet.of(DispatcherType.INCLUDE, DispatcherType.REQUEST));
}
+ protected ServletHolder getServlet(@NonNull Class<? extends Servlet> servlet, @NonNull String servletPath) {
+ synchronized (servlets) {
+ return servlets.computeIfAbsent(servletPath, key -> context.addServlet(servlet, servletPath));
+ }
+ }
+
+ protected ServletHolder getServlet(String servletClass, String servletPath) {
+ synchronized (servlets) {
+ return servlets.computeIfAbsent(servletPath, key -> context.addServlet(servletClass, servletPath));
+ }
+ }
+
/**
* Returns the https connector.
*
}
@Override
- public void addServletClass(String servletPath, String restClass) {
+ public void addServletClass(String servletPath, String servletClass) {
throw new UnsupportedOperationException("addServletClass()" + NOT_SUPPORTED);
}
+ @Override
+ public void addStdServletClass(@NonNull String servletPath, @NonNull String plainServletClass) {
+ this.getServlet(plainServletClass, servletPath);
+ }
+
+ @Override
+ public void setPrometheus(String metricsPath) {
+ this.getServlet(MetricsServlet.class, metricsPath);
+ DefaultExports.initialize();
+ }
+
+ @Override
+ public boolean isPrometheus() {
+ for (ServletHolder servlet : context.getServletHandler().getServlets()) {
+ if (MetricsServlet.class.getName().equals(servlet.getClassName())) {
+ return true;
+ }
+ }
+ return false;
+ }
+
@Override
public void addServletPackage(String servletPath, String restPackage) {
throw new UnsupportedOperationException("addServletPackage()" + NOT_SUPPORTED);
package org.onap.policy.common.endpoints.http.server.internal;
-import java.util.HashMap;
-import java.util.Map;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jetty.servlet.DefaultServlet;
*/
protected static Logger logger = LoggerFactory.getLogger(JettyStaticResourceServer.class);
- /**
- * Container for default servlets.
- */
- protected final Map<String, ServletHolder> servlets = new HashMap<>();
-
/**
* Constructor.
*
* @throws IllegalArgumentException if invalid arguments are provided
*/
protected synchronized ServletHolder getDefaultServlet(String servletPath) {
-
- return servlets.computeIfAbsent(servletPath, key -> context.addServlet(DefaultServlet.class, servletPath));
+ return super.getServlet(DefaultServlet.class, servletPath);
}
@Override
- public synchronized void addServletResource(String servletPath, String resoureBase) {
+ public synchronized void addServletResource(String servletPath, String resourceBase) {
- if (StringUtils.isBlank(resoureBase)) {
+ if (StringUtils.isBlank(resourceBase)) {
throw new IllegalArgumentException("No resourceBase provided");
}
ServletHolder defaultServlet = this.getDefaultServlet(servletPath);
- defaultServlet.setInitParameter(SERVLET_HOLDER_RESOURCE_BASE, resoureBase);
+ defaultServlet.setInitParameter(SERVLET_HOLDER_RESOURCE_BASE, resourceBase);
defaultServlet.setInitParameter(SERVLET_HOLDER_DIR_ALLOWED, "false");
defaultServlet.setInitParameter(SERVLET_HOLDER_PATH_INFO_ONLY, "true");
* ============LICENSE_START=======================================================
* Copyright (C) 2019 Nordix Foundation.
* Modifications Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
private String password;
private boolean https;
private boolean aaf;
+ private boolean prometheus = true;
+ private String servletClass;
+ private String servletUriPath;
public RestServerParameters() {
super(RestServerParameters.class.getSimpleName());
public static final String PROPERTY_HTTP_REST_PACKAGES_SUFFIX = ".restPackages";
public static final String PROPERTY_HTTP_REST_URIPATH_SUFFIX = ".restUriPath";
+ public static final String PROPERTY_HTTP_SERVLET_URIPATH_SUFFIX = ".servletUriPath";
+ public static final String PROPERTY_HTTP_SERVLET_CLASS_SUFFIX = ".servletClass";
+ public static final String PROPERTY_HTTP_PROMETHEUS_SUFFIX = ".prometheus";
+
public static final String PROPERTY_HTTP_HTTPS_SUFFIX = ".https";
public static final String PROPERTY_HTTP_SWAGGER_SUFFIX = ".swagger";
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2021 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2018 Samsung Electronics Co., Ltd.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
+import io.prometheus.client.exporter.MetricsServlet;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
public void testHttpAuthClientProps() throws Exception {
final Properties httpProperties = new Properties();
+ /* PAP and PDP services */
+
httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES, "PAP,PDP");
+
+ /* PAP server service configuration */
+
httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PAP
+ PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, LOCALHOST);
httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PAP
PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PAP
+ PolicyEndPointProperties.PROPERTY_HTTP_FILTER_CLASSES_SUFFIX,
TestFilter.class.getName());
- httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PAP
+ httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PAP
+ PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true");
+ httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PAP
+ + PolicyEndPointProperties.PROPERTY_HTTP_SERVLET_CLASS_SUFFIX, MetricsServlet.class.getName());
+ httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PAP
+ + PolicyEndPointProperties.PROPERTY_HTTP_SERVLET_URIPATH_SUFFIX,
+ "/pap/test/random/metrics");
+
+ /* PDP server service configuration */
httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PDP
+ PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, LOCALHOST);
PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PDP
+ PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX,
RestMockHealthCheck.class.getName());
- httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PAP
+ httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PDP
+ PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true");
+ httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PDP
+ + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX, "true");
+ httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + DOT_PDP
+ + PolicyEndPointProperties.PROPERTY_HTTP_PROMETHEUS_SUFFIX, "true");
+
+ /* PDP and PAP client services */
httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES, "PAP,PDP");
+
+ /* PAP client service configuration */
+
httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PAP
+ PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, LOCALHOST);
httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PAP
httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PAP
+ PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true");
+ /* PDP client service configuration */
+
httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PDP
+ PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, LOCALHOST);
httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PDP
+ PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, "7778");
- httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PDP
- + PolicyEndPointProperties.PROPERTY_HTTP_URL_SUFFIX, "pdp");
httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PDP
+ PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX, FALSE_STRING);
httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + DOT_PDP
assertEquals(200, response.getStatus());
final HttpClient clientPdp = HttpClientFactoryInstance.getClientFactory().get("PDP");
- response = clientPdp.get("test");
+
+ response = clientPdp.get("pdp/test");
assertEquals(500, response.getStatus());
+ response = clientPdp.get("metrics");
+ assertEquals(200, response.getStatus());
+
+ response = clientPdp.get("swagger.json");
+ assertEquals(200, response.getStatus());
+
assertFalse(MyGsonProvider.hasWrittenSome());
// try with empty path
response = clientPap.get("");
assertEquals(200, response.getStatus());
+ response = clientPap.get("random/metrics");
+ assertEquals(200, response.getStatus());
+
+ response = clientPap.get("metrics");
+ assertEquals(404, response.getStatus());
+
// try it asynchronously, too
MyCallback callback = new MyCallback();
response = clientPap.get(callback, null).get();
* ================================================================================
* Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2020 Nordix Foundation.
+ * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
import static org.junit.Assert.assertTrue;
import com.google.gson.Gson;
+import io.prometheus.client.exporter.MetricsServlet;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
* HttpServletServer JUNIT tests.
*/
public class HttpServerTest {
+ private static final String JVM_MEMORY_BYTES_USED = "jvm_memory_bytes_used";
+ private static final String METRICS_URI = "/metrics";
+ private static final String PROMETHEUS = "prometheus";
private static final String LOCALHOST = "localhost";
private static final String JSON_MEDIA = "application/json";
private static final String YAML_MEDIA = YamlMessageBodyHandler.APPLICATION_YAML;
assertEquals(reqText, response);
}
+ /**
+ * This test checks a server from a plain java servlet (note it uses prometheus as the sample server).
+ */
+ @Test
+ public void testStdServletServer() throws Exception {
+ logger.info("-- testStdServletServer() --");
+
+ HttpServletServer server = HttpServletServerFactoryInstance.getServerFactory()
+ .build(PROMETHEUS, LOCALHOST, port, "/", false, true);
+
+ server.addStdServletClass("/prom-generic-servlet/metrics", MetricsServlet.class.getName());
+ server.waitedStart(5000);
+
+ assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
+ assertTrue(server.isPrometheus());
+
+ String response = http(portUrl + "/prom-generic-servlet/metrics");
+ assertThat(response).contains(JVM_MEMORY_BYTES_USED);
+ }
+
+ /**
+ * This test explicitly creates a prometheus server.
+ */
+ @Test
+ public void testExplicitPrometheusServer() throws Exception {
+ logger.info("-- testPrometheusServer() --");
+
+ HttpServletServer server = HttpServletServerFactoryInstance.getServerFactory()
+ .build(PROMETHEUS, LOCALHOST, port, "/", false, true);
+ server.setPrometheus(METRICS_URI);
+ server.waitedStart(5000);
+
+ assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
+ assertTrue(server.isPrometheus());
+
+ String response = http(portUrl + METRICS_URI);
+ assertThat(response).contains(JVM_MEMORY_BYTES_USED);
+ }
+
+ /**
+ * This test is an all-in-one for a single server: prometheus, jax-rs, servlet, swagger, and filters.
+ */
+ @Test
+ public void testPrometheusJaxRsFilterSwaggerServer() throws Exception {
+ logger.info("-- testPrometheusServer() --");
+
+ HttpServletServer server = HttpServletServerFactoryInstance.getServerFactory()
+ .build(PROMETHEUS, LOCALHOST, port, "/", true, true);
+
+ server.addServletClass("/*", RestEchoService.class.getName());
+ server.addFilterClass("/*", TestFilter.class.getName());
+ server.setPrometheus(METRICS_URI);
+
+ server.waitedStart(5000);
+
+ assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive());
+ assertTrue(server.isPrometheus());
+
+ String response = http(portUrl + METRICS_URI);
+ assertThat(response).contains(JVM_MEMORY_BYTES_USED);
+
+ RestEchoReqResp request = new RestEchoReqResp();
+ request.setRequestId(100);
+ request.setText(SOME_TEXT);
+ String reqText = gson.toJson(request);
+
+ response = http(portUrl + JUNIT_ECHO_FULL_REQUEST, JSON_MEDIA, reqText);
+ assertEquals(reqText, response);
+
+ response = http(portUrl + SWAGGER_JSON);
+ assertThat(response).contains("Swagger Server");
+ }
+
@Test
public void testJacksonClassServer() throws Exception {
logger.info("-- testJacksonClassServer() --");
* ONAP
* ================================================================================
* Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
+import io.prometheus.client.exporter.MetricsServlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import org.powermock.reflect.Whitebox;
public class RestServerTest {
+ private static final String METRICS_URI = "/metrics";
private static final String SERVER1 = "my-server-A";
private static final String SERVER2 = "my-server-B";
private static final String FACTORY_FIELD = "factory";
assertEquals(String.join(",", GsonMessageBodyHandler.class.getName(), YamlMessageBodyHandler.class.getName(),
JsonExceptionMapper.class.getName(), YamlExceptionMapper.class.getName()),
props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER));
+ assertEquals("false", props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_PROMETHEUS_SUFFIX));
+ }
+
+ @Test
+ public void testExplicitPrometheusAddedToProperty() {
+ when(params.isPrometheus()).thenReturn(true);
+ rest = new RestServer(params, Filter.class, Provider1.class, Provider2.class);
+ ArgumentCaptor<Properties> cap = ArgumentCaptor.forClass(Properties.class);
+ verify(serverFactory).build(cap.capture());
+
+ Properties props = cap.getValue();
+ String svcpfx = PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + PARAM_NAME;
+
+ assertEquals("true", props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_PROMETHEUS_SUFFIX));
+ assertThat(props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERVLET_URIPATH_SUFFIX)).isBlank();
+ assertThat(props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERVLET_CLASS_SUFFIX)).isBlank();
+ }
+
+ @Test
+ public void testStandardSevletAddedToProperty() {
+ when(params.getServletUriPath()).thenReturn("/metrics");
+ when(params.getServletClass()).thenReturn(MetricsServlet.class.getName());
+ rest = new RestServer(params, Filter.class, Provider1.class, Provider2.class);
+ ArgumentCaptor<Properties> cap = ArgumentCaptor.forClass(Properties.class);
+ verify(serverFactory).build(cap.capture());
+
+ Properties props = cap.getValue();
+ String svcpfx = PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + PARAM_NAME;
+
+ assertEquals("false", props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_PROMETHEUS_SUFFIX));
+ assertEquals(METRICS_URI,
+ props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERVLET_URIPATH_SUFFIX));
+ assertEquals(MetricsServlet.class.getName(),
+ props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERVLET_CLASS_SUFFIX));
}
@Test
"alive": false,
"host": "localhost",
"name": "echo",
- "port": ${obj.port}
+ "port": ${obj.port},
+ "prometheus":false
}