mirror of
https://github.com/jlengrand/helidon.git
synced 2026-03-10 08:21:17 +00:00
Convert tests to use the new infrastructure (#2498)
* Convert tests to use the new infrastructure Signed-off-by: tim.quinn@oracle.com <tim.quinn@oracle.com>
This commit is contained in:
@@ -68,6 +68,11 @@
|
||||
<artifactId>hamcrest-all</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.helidon.microprofile.tests</groupId>
|
||||
<artifactId>helidon-microprofile-tests-junit5</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<profiles>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2018, 2020 Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2018, 2020 Oracle and/or its affiliates.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,21 +16,17 @@
|
||||
|
||||
package io.helidon.microprofile.metrics;
|
||||
|
||||
import org.eclipse.microprofile.metrics.MetricRegistry;
|
||||
|
||||
import javax.enterprise.context.RequestScoped;
|
||||
import javax.inject.Inject;
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.PUT;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static io.helidon.microprofile.metrics.MetricsMpServiceTest.getCounter;
|
||||
|
||||
/**
|
||||
* HelloWorldResource class.
|
||||
*/
|
||||
@@ -38,10 +34,13 @@ import static io.helidon.microprofile.metrics.MetricsMpServiceTest.getCounter;
|
||||
@RequestScoped
|
||||
public class HelloWorldResource {
|
||||
|
||||
@Inject
|
||||
MetricRegistry metricRegistry;
|
||||
|
||||
@GET
|
||||
@Produces(MediaType.TEXT_PLAIN)
|
||||
public String message() {
|
||||
getCounter("helloCounter").inc();
|
||||
metricRegistry.counter("helloCounter").inc();
|
||||
return "Hello World";
|
||||
}
|
||||
|
||||
|
||||
@@ -16,22 +16,12 @@
|
||||
|
||||
package io.helidon.microprofile.metrics;
|
||||
|
||||
import io.helidon.config.Config;
|
||||
import io.helidon.config.ConfigSources;
|
||||
import io.helidon.microprofile.server.Server;
|
||||
import org.eclipse.microprofile.metrics.SimpleTimer;
|
||||
import org.eclipse.microprofile.metrics.Tag;
|
||||
import org.hamcrest.core.Is;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import io.helidon.microprofile.tests.junit5.HelidonTest;
|
||||
import org.eclipse.microprofile.metrics.MetricRegistry;
|
||||
import org.eclipse.microprofile.metrics.annotation.RegistryType;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import javax.json.JsonObject;
|
||||
import javax.ws.rs.client.Entity;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import java.util.Properties;
|
||||
import java.util.stream.IntStream;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
@@ -40,18 +30,16 @@ import static org.hamcrest.MatcherAssert.assertThat;
|
||||
* Makes sure that no synthetic SimpleTimer metrics are created for JAX-RS endpoints when
|
||||
* the config disables that feature.
|
||||
*/
|
||||
@HelidonTest
|
||||
public class HelloWorldRestEndpointSimpleTimerDisabledTest extends HelloWorldTest {
|
||||
|
||||
@BeforeAll
|
||||
public static void initializeServer() {
|
||||
HelloWorldTest.initializeServer(new Properties());
|
||||
}
|
||||
@Inject
|
||||
@RegistryType(type = MetricRegistry.Type.BASE)
|
||||
MetricRegistry baseRegistry;
|
||||
|
||||
@Test
|
||||
public void testSyntheticSimpleTimer() {
|
||||
// Expect 0, because the config should have suppressed the synthetic SimpleTimer
|
||||
// metrics for JAX-RS endpoints, and the we will have just created the metric (by
|
||||
// looking for it) with an initialized count of 0.
|
||||
testSyntheticSimpleTimer(0L);
|
||||
assertThat("Synthetic simple timer for JAX-RS was created when that feature is turned off",
|
||||
isSyntheticSimpleTimerPresent(), is(false));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,17 +16,17 @@
|
||||
|
||||
package io.helidon.microprofile.metrics;
|
||||
|
||||
import java.util.Properties;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.json.JsonObject;
|
||||
import javax.ws.rs.client.Entity;
|
||||
import javax.ws.rs.client.WebTarget;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
|
||||
import io.helidon.config.Config;
|
||||
import io.helidon.config.ConfigSources;
|
||||
import io.helidon.metrics.RegistryFactory;
|
||||
import io.helidon.microprofile.server.Server;
|
||||
import io.helidon.microprofile.tests.junit5.AddConfig;
|
||||
import io.helidon.microprofile.tests.junit5.HelidonTest;
|
||||
import org.eclipse.microprofile.metrics.MetricRegistry;
|
||||
import org.eclipse.microprofile.metrics.SimpleTimer;
|
||||
import org.eclipse.microprofile.metrics.Tag;
|
||||
@@ -42,25 +42,17 @@ import static org.hamcrest.MatcherAssert.assertThat;
|
||||
/**
|
||||
* Class HelloWorldTest.
|
||||
*/
|
||||
@HelidonTest
|
||||
@AddConfig(key = "metrics." + MetricsCdiExtension.REST_ENDPOINTS_METRIC_ENABLED_PROPERTY_NAME, value = "true")
|
||||
public class HelloWorldTest extends MetricsMpServiceTest {
|
||||
|
||||
private static MetricRegistry syntheticSimpleTimerRegistry;
|
||||
@Inject
|
||||
WebTarget webTarget;
|
||||
|
||||
@BeforeAll
|
||||
public static void initializeServer() {
|
||||
public static void initialize() {
|
||||
System.setProperty("jersey.config.server.logging.verbosity", "FINE");
|
||||
Properties configProperties = new Properties();
|
||||
configProperties.setProperty("metrics." + MetricsCdiExtension.REST_ENDPOINTS_METRIC_ENABLED_PROPERTY_NAME,
|
||||
"true");
|
||||
initializeServer(configProperties);
|
||||
}
|
||||
|
||||
static void initializeServer(Properties configProperties) {
|
||||
syntheticSimpleTimerRegistry = initSyntheticSimpleTimerRegistry();
|
||||
Server.Builder builder = MetricsMpServiceTest.prepareServerBuilder();
|
||||
Config config = Config.create(ConfigSources.create(configProperties));
|
||||
builder.config(config);
|
||||
MetricsMpServiceTest.finishServerInitialization(builder);
|
||||
}
|
||||
|
||||
private static MetricRegistry initSyntheticSimpleTimerRegistry() {
|
||||
@@ -77,8 +69,10 @@ public class HelloWorldTest extends MetricsMpServiceTest {
|
||||
@Test
|
||||
public void testMetrics() {
|
||||
IntStream.range(0, 5).forEach(
|
||||
i -> client.target(baseUri())
|
||||
.path("helloworld").request().accept(MediaType.TEXT_PLAIN_TYPE)
|
||||
i -> webTarget
|
||||
.path("helloworld")
|
||||
.request()
|
||||
.accept(MediaType.TEXT_PLAIN_TYPE)
|
||||
.get(String.class));
|
||||
assertThat(getCounter("helloCounter").getCount(), is(5L));
|
||||
}
|
||||
@@ -90,27 +84,32 @@ public class HelloWorldTest extends MetricsMpServiceTest {
|
||||
|
||||
void testSyntheticSimpleTimer(long expectedSyntheticSimpleTimerCount) {
|
||||
IntStream.range(0, 6).forEach(
|
||||
i -> client.target(baseUri())
|
||||
.path("helloworld/withArg").request(MediaType.TEXT_PLAIN_TYPE)
|
||||
.put(Entity.text("Joe")).readEntity(String.class));
|
||||
i -> webTarget
|
||||
.path("helloworld/withArg")
|
||||
.request(MediaType.TEXT_PLAIN_TYPE)
|
||||
.put(Entity.text("Joe"))
|
||||
.readEntity(String.class));
|
||||
|
||||
SimpleTimer syntheticSimpleTimer = getSyntheticSimpleTimer();
|
||||
assertThat(syntheticSimpleTimer.getCount(), Is.is(expectedSyntheticSimpleTimerCount));
|
||||
}
|
||||
|
||||
static SimpleTimer getSyntheticSimpleTimer() {
|
||||
SimpleTimer getSyntheticSimpleTimer() {
|
||||
Tag[] tags = new Tag[] {new Tag("class", HelloWorldResource.class.getName()),
|
||||
new Tag("method", "messageWithArg_java.lang.String")};
|
||||
SimpleTimer syntheticSimpleTimer = syntheticSimpleTimerRegistry.simpleTimer(
|
||||
SimpleTimer syntheticSimpleTimer = syntheticSimpleTimerRegistry().simpleTimer(
|
||||
MetricsCdiExtension.SYNTHETIC_SIMPLE_TIMER_METADATA, tags);
|
||||
return syntheticSimpleTimer;
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void checkMetricsUrl() {
|
||||
JsonObject app = client.target(baseUri())
|
||||
.path("metrics").request().accept(MediaType.APPLICATION_JSON_TYPE)
|
||||
.get(JsonObject.class).getJsonObject("application");
|
||||
JsonObject app = webTarget
|
||||
.path("metrics")
|
||||
.request()
|
||||
.accept(MediaType.APPLICATION_JSON_TYPE)
|
||||
.get(JsonObject.class)
|
||||
.getJsonObject("application");
|
||||
assertThat(app.getJsonNumber("helloCounter").intValue(), is(5));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2018, 2020 Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2018, 2020 Oracle and/or its affiliates.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,49 +16,31 @@
|
||||
|
||||
package io.helidon.microprofile.metrics;
|
||||
|
||||
import javax.enterprise.inject.se.SeContainer;
|
||||
import javax.enterprise.inject.spi.CDI;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import io.helidon.microprofile.cdi.HelidonContainer;
|
||||
|
||||
import io.helidon.microprofile.tests.junit5.HelidonTest;
|
||||
import org.eclipse.microprofile.metrics.Metric;
|
||||
import org.eclipse.microprofile.metrics.MetricID;
|
||||
import org.eclipse.microprofile.metrics.MetricRegistry;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
|
||||
/**
|
||||
* Class MetricsBaseTest.
|
||||
*/
|
||||
@HelidonTest
|
||||
public class MetricsBaseTest {
|
||||
|
||||
private static final String METRIC_NAME_TEMPLATE = "%s.%s";
|
||||
|
||||
private static SeContainer cdiContainer;
|
||||
@Inject
|
||||
private MetricRegistry metricRegistry;
|
||||
|
||||
private static MetricRegistry metricRegistry;
|
||||
|
||||
@BeforeAll
|
||||
public synchronized static void startCdiContainer() {
|
||||
cdiContainer = HelidonContainer.instance().start();
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
public synchronized static void shutDownCdiContainer() {
|
||||
if (cdiContainer != null) {
|
||||
cdiContainer.close();
|
||||
}
|
||||
}
|
||||
|
||||
static synchronized MetricRegistry getMetricRegistry() {
|
||||
if (metricRegistry == null) {
|
||||
metricRegistry = CDI.current().select(MetricRegistry.class).get();
|
||||
}
|
||||
MetricRegistry getMetricRegistry() {
|
||||
return metricRegistry;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
static <T extends Metric> T getMetric(Object bean, String name) {
|
||||
<T extends Metric> T getMetric(Object bean, String name) {
|
||||
MetricID metricName = new MetricID(String.format(METRIC_NAME_TEMPLATE,
|
||||
MetricsCdiExtension.getRealClass(bean).getName(), // CDI proxies
|
||||
name));
|
||||
|
||||
@@ -16,76 +16,43 @@
|
||||
|
||||
package io.helidon.microprofile.metrics;
|
||||
|
||||
import javax.ws.rs.client.Client;
|
||||
import javax.ws.rs.client.ClientBuilder;
|
||||
|
||||
import io.helidon.metrics.RegistryFactory;
|
||||
import io.helidon.microprofile.server.Server;
|
||||
|
||||
import io.helidon.microprofile.tests.junit5.AddBean;
|
||||
import io.helidon.microprofile.tests.junit5.HelidonTest;
|
||||
import org.eclipse.microprofile.metrics.Counter;
|
||||
import org.eclipse.microprofile.metrics.Metadata;
|
||||
import org.eclipse.microprofile.metrics.MetricRegistry;
|
||||
import org.eclipse.microprofile.metrics.MetricType;
|
||||
import org.eclipse.microprofile.metrics.MetricUnits;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.eclipse.microprofile.metrics.Tag;
|
||||
import org.eclipse.microprofile.metrics.annotation.RegistryType;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* Class MetricsMpServiceTest.
|
||||
*/
|
||||
@HelidonTest
|
||||
@AddBean(HelloWorldResource.class)
|
||||
public class MetricsMpServiceTest {
|
||||
protected static Server server;
|
||||
protected static MetricRegistry registry;
|
||||
protected static Client client = ClientBuilder.newClient();
|
||||
|
||||
private static int port;
|
||||
private static String baseUri;
|
||||
@Inject
|
||||
private MetricRegistry registry;
|
||||
|
||||
@BeforeAll
|
||||
public static void initializeServer() throws Exception {
|
||||
finishServerInitialization(prepareServerBuilder());
|
||||
@Inject
|
||||
@RegistryType(type = MetricRegistry.Type.BASE)
|
||||
private MetricRegistry baseRegistry;
|
||||
|
||||
MetricRegistry syntheticSimpleTimerRegistry() {
|
||||
return baseRegistry;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes a server builder.
|
||||
* <p>
|
||||
* Splitting this code from the finish code allows a subclass to refine the builder before
|
||||
* using it to construct the builder.
|
||||
* </p>
|
||||
* @return {@code Server.Builder}
|
||||
*/
|
||||
static Server.Builder prepareServerBuilder() {
|
||||
return Server.builder()
|
||||
.addResourceClass(HelloWorldResource.class)
|
||||
.host("localhost")
|
||||
// choose a random available port
|
||||
.port(-1);
|
||||
boolean isSyntheticSimpleTimerPresent() {
|
||||
return !syntheticSimpleTimerRegistry().getSimpleTimers((metricID, metric) ->
|
||||
metricID.equals(MetricsCdiExtension.SYNTHETIC_SIMPLE_TIMER_METRIC_NAME))
|
||||
.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Completes the set-up of the server, using the (possibly refined) builder.
|
||||
*
|
||||
* @param builder the {@code Server.Builder} to use in constructing the server
|
||||
*/
|
||||
static void finishServerInitialization(Server.Builder builder) {
|
||||
server = builder.build();
|
||||
server.start();
|
||||
registry = RegistryFactory.getInstance().getRegistry(MetricRegistry.Type.APPLICATION);
|
||||
|
||||
port = server.port();
|
||||
baseUri = "http://localhost:" + port;
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
public static void terminateServer() {
|
||||
server.stop();
|
||||
}
|
||||
|
||||
protected String baseUri() {
|
||||
return baseUri;
|
||||
}
|
||||
|
||||
protected static void registerCounter(String name) {
|
||||
protected void registerCounter(String name) {
|
||||
Metadata meta = Metadata.builder()
|
||||
.withName(name)
|
||||
.withDisplayName(name)
|
||||
@@ -96,7 +63,7 @@ public class MetricsMpServiceTest {
|
||||
registry.counter(meta);
|
||||
}
|
||||
|
||||
protected static Counter getCounter(String name) {
|
||||
protected Counter getCounter(String name) {
|
||||
return registry.counter(name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,9 +25,9 @@ import org.eclipse.microprofile.metrics.annotation.Counted;
|
||||
@Dependent
|
||||
public class ResourceWithReusedMetricForInvocation {
|
||||
|
||||
private static final String TAG_1 = "tag_1=value_1";
|
||||
private static final String TAG_2 = "tag_2=value_2";
|
||||
private static final String OTHER_REUSED_NAME = "reusedRetrieveDelay";
|
||||
static final String TAG_1 = "tag_1=value_1";
|
||||
static final String TAG_2 = "tag_2=value_2";
|
||||
static final String OTHER_REUSED_NAME = "reusedRetrieveDelay";
|
||||
|
||||
@GET
|
||||
@Path("method1")
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2019, 2020 Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2019, 2020 Oracle and/or its affiliates.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,14 +16,30 @@
|
||||
*/
|
||||
package io.helidon.microprofile.metrics;
|
||||
|
||||
import io.helidon.microprofile.tests.junit5.HelidonTest;
|
||||
import org.eclipse.microprofile.metrics.Counter;
|
||||
import org.eclipse.microprofile.metrics.MetricRegistry;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
@HelidonTest
|
||||
public class ReusabilityInterceptorTest extends MetricsBaseTest {
|
||||
|
||||
@Inject
|
||||
private MetricRegistry metricRegistry;
|
||||
|
||||
@Test
|
||||
public void testReusedMetricWithInterceptor() {
|
||||
ResourceWithReusedMetricForInvocation resource = newBean(ResourceWithReusedMetricForInvocation.class);
|
||||
resource.method1();
|
||||
|
||||
Counter counter = metricRegistry.counter(ResourceWithReusedMetricForInvocation.OTHER_REUSED_NAME,
|
||||
MetricUtil.tags(new String[] {ResourceWithReusedMetricForInvocation.TAG_1,
|
||||
ResourceWithReusedMetricForInvocation.TAG_2}));
|
||||
assertThat(counter.getCount(), is(1L));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user