chore: more sonarqube improvements, rename tracing service method
All checks were successful
Build / build (push) Successful in 2m23s
All checks were successful
Build / build (push) Successful in 2m23s
This commit is contained in:
parent
f591d514ec
commit
bc0110cc29
7 changed files with 151 additions and 174 deletions
|
@ -1,7 +1,6 @@
|
||||||
package ch.phoenix.oss.quarkus.commons.audit;
|
package ch.phoenix.oss.quarkus.commons.audit;
|
||||||
|
|
||||||
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
|
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
|
||||||
import static org.mockito.Mockito.mockStatic;
|
import static org.mockito.Mockito.mockStatic;
|
||||||
|
|
||||||
import io.quarkus.test.junit.QuarkusTest;
|
import io.quarkus.test.junit.QuarkusTest;
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package ch.phoenix.oss.quarkus.commons.audit;
|
package ch.phoenix.oss.quarkus.commons.audit;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
|
||||||
|
|
||||||
import io.quarkus.test.junit.QuarkusTest;
|
import io.quarkus.test.junit.QuarkusTest;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
@ -47,8 +46,6 @@ class RevisionTest {
|
||||||
var rev = new Revision();
|
var rev = new Revision();
|
||||||
rev.rev = 1;
|
rev.rev = 1;
|
||||||
|
|
||||||
assertThat(rev.toString())
|
assertThat(rev).as("Revision's toString should match expected value").hasToString("Revision{rev=1}");
|
||||||
.as("Revision's toString should match expected value")
|
|
||||||
.isEqualTo("Revision{rev=1}");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@ public interface TracingService {
|
||||||
|
|
||||||
String getActor();
|
String getActor();
|
||||||
|
|
||||||
String getRequestPath();
|
String getRequestPathRaw();
|
||||||
|
|
||||||
String getRequestMethod();
|
String getRequestMethod();
|
||||||
|
|
||||||
|
|
|
@ -33,7 +33,7 @@ class TracingServiceImpl implements TracingService {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getRequestPath() {
|
public String getRequestPathRaw() {
|
||||||
return (String) MDC.get(TracingConstants.REQUEST_PATH_RAW);
|
return (String) MDC.get(TracingConstants.REQUEST_PATH_RAW);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package ch.phoenix.oss.quarkus.commons.tracing;
|
package ch.phoenix.oss.quarkus.commons.tracing;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.params.provider.Arguments.arguments;
|
||||||
import static org.mockito.ArgumentMatchers.eq;
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
import static org.mockito.ArgumentMatchers.startsWith;
|
import static org.mockito.ArgumentMatchers.startsWith;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
|
@ -10,7 +11,11 @@ import io.quarkus.test.junit.mockito.InjectSpy;
|
||||||
import io.restassured.RestAssured;
|
import io.restassured.RestAssured;
|
||||||
import io.restassured.http.ContentType;
|
import io.restassured.http.ContentType;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.stream.Stream;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.Arguments;
|
||||||
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
|
||||||
@QuarkusTest
|
@QuarkusTest
|
||||||
class RoutePatternTest {
|
class RoutePatternTest {
|
||||||
|
@ -18,12 +23,46 @@ class RoutePatternTest {
|
||||||
@InjectSpy
|
@InjectSpy
|
||||||
TracingService tracingService;
|
TracingService tracingService;
|
||||||
|
|
||||||
@Test
|
static Stream<Arguments> get() {
|
||||||
void getBlankResource() {
|
return Stream.of(
|
||||||
var route = "/";
|
arguments("/", Map.of()),
|
||||||
RestAssured.given().accept(ContentType.TEXT).when().get(route).then().statusCode(200);
|
arguments("/leading-and-no-trailing", Map.of()),
|
||||||
|
arguments("/leading/{param}/{param2}", Map.of("param", "1", "param2", "2")),
|
||||||
|
arguments("/{param}/{param2}/trailing", Map.of("param", "1", "param2", "2")),
|
||||||
|
arguments("/leading-and-no-trailing/{param}", Map.of("param", "1")),
|
||||||
|
arguments("/leading-and-no-trailing/{param}/{param2}", Map.of("param", "1", "param2", "2")),
|
||||||
|
arguments("/leading-and-trailing", Map.of()),
|
||||||
|
arguments("/leading-and-trailing/{param}", Map.of("param", "1")),
|
||||||
|
arguments("/leading-and-trailing/{param}/{param2}", Map.of("param", "1", "param2", "2")),
|
||||||
|
arguments("/no-leading-and-no-trailing", Map.of()),
|
||||||
|
arguments("/no-leading-and-no-trailing/{param}", Map.of("param", "1")),
|
||||||
|
arguments("/no-leading-and-no-trailing/{param}/{param2}", Map.of("param", "1", "param2", "2")),
|
||||||
|
arguments("/no-leading-and-trailing", Map.of()),
|
||||||
|
arguments("/no-leading-and-trailing/{param}", Map.of("param", "1")),
|
||||||
|
arguments("/no-leading-and-trailing/{param}/{param2}", Map.of("param", "1", "param2", "2")));
|
||||||
|
}
|
||||||
|
|
||||||
verifyGetTracing(route, Map.of());
|
@MethodSource
|
||||||
|
@ParameterizedTest
|
||||||
|
void get(String route, Map<String, String> pathParams) {
|
||||||
|
RestAssured.given()
|
||||||
|
.accept(ContentType.TEXT)
|
||||||
|
.when()
|
||||||
|
.get(route, pathParams)
|
||||||
|
.then()
|
||||||
|
.statusCode(200);
|
||||||
|
|
||||||
|
verify(tracingService).trace("actor", "anonymous");
|
||||||
|
verify(tracingService).trace("request.method", "GET");
|
||||||
|
verify(tracingService).trace("request.route", route);
|
||||||
|
pathParams.forEach((key, value) -> verify(tracingService).trace("request.path.params." + key, value));
|
||||||
|
verify(tracingService).trace("request.headers.accept", "text/plain");
|
||||||
|
verify(tracingService).trace("request.headers.accept-encoding", "gzip,deflate");
|
||||||
|
verify(tracingService).trace("request.headers.connection", "Keep-Alive");
|
||||||
|
verify(tracingService).trace(eq("request.headers.host"), startsWith("localhost:"));
|
||||||
|
verify(tracingService).trace(eq("request.headers.user-agent"), startsWith("Apache-HttpClient"));
|
||||||
|
verify(tracingService).trace("request.client.ip", "127.0.0.1");
|
||||||
|
verifyNoMoreInteractions(tracingService);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -45,160 +84,4 @@ class RoutePatternTest {
|
||||||
verify(tracingService).trace("request.client.ip", "127.0.0.1");
|
verify(tracingService).trace("request.client.ip", "127.0.0.1");
|
||||||
verifyNoMoreInteractions(tracingService);
|
verifyNoMoreInteractions(tracingService);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
void getLeadingResource() {
|
|
||||||
var route = "/leading/{id}/{anotherId}";
|
|
||||||
RestAssured.given()
|
|
||||||
.accept(ContentType.TEXT)
|
|
||||||
.when()
|
|
||||||
.get(route, 1, 2)
|
|
||||||
.then()
|
|
||||||
.statusCode(200);
|
|
||||||
|
|
||||||
verifyGetTracing(route, Map.of("id", "1", "anotherId", "2"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void getTrailingResource() {
|
|
||||||
var route = "/{id}/{anotherId}/trailing";
|
|
||||||
RestAssured.given()
|
|
||||||
.accept(ContentType.TEXT)
|
|
||||||
.when()
|
|
||||||
.get(route, 1, 2)
|
|
||||||
.then()
|
|
||||||
.statusCode(200);
|
|
||||||
|
|
||||||
verifyGetTracing(route, Map.of("id", "1", "anotherId", "2"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void getLeadingAndNoTrailingResource() {
|
|
||||||
var route = "/leading-and-no-trailing";
|
|
||||||
RestAssured.given().accept(ContentType.TEXT).when().get(route).then().statusCode(200);
|
|
||||||
|
|
||||||
verifyGetTracing(route, Map.of());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void getLeadingAndNoTrailingWithSingleParamResource() {
|
|
||||||
var route = "/leading-and-no-trailing/{param}";
|
|
||||||
RestAssured.given().accept(ContentType.TEXT).when().get(route, 1).then().statusCode(200);
|
|
||||||
|
|
||||||
verifyGetTracing(route, Map.of("param", "1"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void getLeadingAndNoTrailingWithMultiParamResource() {
|
|
||||||
var route = "/leading-and-no-trailing/{param}/{param2}";
|
|
||||||
RestAssured.given()
|
|
||||||
.accept(ContentType.TEXT)
|
|
||||||
.when()
|
|
||||||
.get(route, 1, 2)
|
|
||||||
.then()
|
|
||||||
.statusCode(200);
|
|
||||||
|
|
||||||
verifyGetTracing(route, Map.of("param", "1", "param2", "2"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void getLeadingAndTrailingResource() {
|
|
||||||
var route = "/leading-and-trailing";
|
|
||||||
RestAssured.given().accept(ContentType.TEXT).when().get(route).then().statusCode(200);
|
|
||||||
|
|
||||||
verifyGetTracing(route, Map.of());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void getLeadingAndTrailingWithSingleParamResource() {
|
|
||||||
var route = "/leading-and-trailing/{param}";
|
|
||||||
RestAssured.given().accept(ContentType.TEXT).when().get(route, 1).then().statusCode(200);
|
|
||||||
|
|
||||||
verifyGetTracing(route, Map.of("param", "1"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void getLeadingAndTrailingWithMultiParamResource() {
|
|
||||||
var route = "/leading-and-trailing/{param}/{param2}";
|
|
||||||
RestAssured.given()
|
|
||||||
.accept(ContentType.TEXT)
|
|
||||||
.when()
|
|
||||||
.get(route, 1, 2)
|
|
||||||
.then()
|
|
||||||
.statusCode(200);
|
|
||||||
|
|
||||||
verifyGetTracing(route, Map.of("param", "1", "param2", "2"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void getNoLeadingAndNoTrailingResource() {
|
|
||||||
var route = "/no-leading-and-no-trailing";
|
|
||||||
RestAssured.given().accept(ContentType.TEXT).when().get(route).then().statusCode(200);
|
|
||||||
|
|
||||||
verifyGetTracing(route, Map.of());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void geNoLeadingAndNoTrailingWithSingleParamResource() {
|
|
||||||
var route = "/no-leading-and-no-trailing/{param}";
|
|
||||||
RestAssured.given().accept(ContentType.TEXT).when().get(route, 1).then().statusCode(200);
|
|
||||||
|
|
||||||
verifyGetTracing(route, Map.of("param", "1"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void getNoLeadingAndNoTrailingWithMultiParamResource() {
|
|
||||||
var route = "/no-leading-and-no-trailing/{param}/{param2}";
|
|
||||||
RestAssured.given()
|
|
||||||
.accept(ContentType.TEXT)
|
|
||||||
.when()
|
|
||||||
.get(route, 1, 2)
|
|
||||||
.then()
|
|
||||||
.statusCode(200);
|
|
||||||
|
|
||||||
verifyGetTracing(route, Map.of("param", "1", "param2", "2"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void getNoLeadingAndTrailingResource() {
|
|
||||||
var route = "/no-leading-and-trailing";
|
|
||||||
RestAssured.given().accept(ContentType.TEXT).when().get(route).then().statusCode(200);
|
|
||||||
|
|
||||||
verifyGetTracing(route, Map.of());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void getNoLeadingAndTrailingWithSingleParamResource() {
|
|
||||||
var route = "/no-leading-and-trailing/{param}";
|
|
||||||
RestAssured.given().accept(ContentType.TEXT).when().get(route, 1).then().statusCode(200);
|
|
||||||
|
|
||||||
verifyGetTracing(route, Map.of("param", "1"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void getNoLeadingAndTrailingWithMultiParamResource() {
|
|
||||||
var route = "/no-leading-and-trailing/{param}/{param2}";
|
|
||||||
RestAssured.given()
|
|
||||||
.accept(ContentType.TEXT)
|
|
||||||
.when()
|
|
||||||
.get(route, 1, 2)
|
|
||||||
.then()
|
|
||||||
.statusCode(200);
|
|
||||||
|
|
||||||
verifyGetTracing(route, Map.of("param", "1", "param2", "2"));
|
|
||||||
}
|
|
||||||
|
|
||||||
private void verifyGetTracing(String route, Map<String, String> pathParams) {
|
|
||||||
verify(tracingService).trace("actor", "anonymous");
|
|
||||||
verify(tracingService).trace("request.method", "GET");
|
|
||||||
verify(tracingService).trace("request.route", route);
|
|
||||||
pathParams.forEach((key, value) -> verify(tracingService).trace("request.path.params." + key, value));
|
|
||||||
verify(tracingService).trace("request.headers.accept", "text/plain");
|
|
||||||
verify(tracingService).trace("request.headers.accept-encoding", "gzip,deflate");
|
|
||||||
verify(tracingService).trace("request.headers.connection", "Keep-Alive");
|
|
||||||
verify(tracingService).trace(eq("request.headers.host"), startsWith("localhost:"));
|
|
||||||
verify(tracingService).trace(eq("request.headers.user-agent"), startsWith("Apache-HttpClient"));
|
|
||||||
verify(tracingService).trace("request.client.ip", "127.0.0.1");
|
|
||||||
verifyNoMoreInteractions(tracingService);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,98 @@
|
||||||
|
package ch.phoenix.oss.quarkus.commons.tracing;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
import io.opentelemetry.api.trace.Span;
|
||||||
|
import io.opentelemetry.instrumentation.annotations.WithSpan;
|
||||||
|
import io.quarkus.test.junit.QuarkusTest;
|
||||||
|
import jakarta.inject.Inject;
|
||||||
|
import org.jboss.logging.MDC;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
@QuarkusTest
|
||||||
|
class TracingServiceImplTest {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
TracingService tracingService;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
Span span;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
MDC.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getActor() {
|
||||||
|
tracingService.trace("actor", "abc");
|
||||||
|
assertThat(tracingService.getActor())
|
||||||
|
.as("Actor should match expected value")
|
||||||
|
.isEqualTo("abc");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getRequestPathRaw() {
|
||||||
|
tracingService.trace("request.path.raw", "/foo/bar");
|
||||||
|
assertThat(tracingService.getRequestPathRaw())
|
||||||
|
.as("Request Path Raw should match expected value")
|
||||||
|
.isEqualTo("/foo/bar");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getRequestMethod() {
|
||||||
|
tracingService.trace("request.method", "GET");
|
||||||
|
assertThat(tracingService.getRequestMethod())
|
||||||
|
.as("Request Method should match expected value")
|
||||||
|
.isEqualTo("GET");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getRequestId() {
|
||||||
|
tracingService.trace("request.headers.x-request-id", "ba458367-bfeb-46ba-87da-50b9343be8f9");
|
||||||
|
assertThat(tracingService.getRequestId())
|
||||||
|
.as("Request Id should match expected value")
|
||||||
|
.isEqualTo("ba458367-bfeb-46ba-87da-50b9343be8f9");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@WithSpan
|
||||||
|
void getTraceId() {
|
||||||
|
assertThat(tracingService.getTraceId())
|
||||||
|
.as("Request Trace Id should match expected value")
|
||||||
|
.isEqualTo(span.getSpanContext().getTraceId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@WithSpan
|
||||||
|
void getSpanId() {
|
||||||
|
assertThat(tracingService.getSpanId())
|
||||||
|
.as("Request Span Id should match expected value")
|
||||||
|
.isEqualTo(span.getSpanContext().getSpanId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getClientIp() {
|
||||||
|
tracingService.trace("request.client.ip", "127.0.0.1");
|
||||||
|
assertThat(tracingService.getClientIp())
|
||||||
|
.as("Request Client Iü should match expected value")
|
||||||
|
.isEqualTo("127.0.0.1");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getSchedulerJob() {
|
||||||
|
tracingService.trace("scheduler.job.name", "scheduler/abc");
|
||||||
|
assertThat(tracingService.getSchedulerJob())
|
||||||
|
.as("Scheduler Job Name should match expected value")
|
||||||
|
.isEqualTo("scheduler/abc");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void clearAll() {
|
||||||
|
tracingService.trace("aaa", "bbb");
|
||||||
|
assertThat(MDC.get("aaa")).isEqualTo("bbb");
|
||||||
|
tracingService.clearAll();
|
||||||
|
assertThat(MDC.get("aaa")).isNull();
|
||||||
|
}
|
||||||
|
}
|
|
@ -10,14 +10,14 @@ import jakarta.ws.rs.core.MediaType;
|
||||||
public class SlashResource {
|
public class SlashResource {
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
@Path("/leading/{id}/{anotherId}")
|
@Path("/leading/{param}/{param2}")
|
||||||
public String doubleLeading(int id, int anotherId) {
|
public String doubleLeading(int param, int param2) {
|
||||||
return "leading/" + id + "/" + anotherId;
|
return "leading/" + param + "/" + param2;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
@Path("{id}/{anotherId}/trailing/")
|
@Path("{param}/{param2}/trailing/")
|
||||||
public String doubleTrailing(int id, int anotherId) {
|
public String doubleTrailing(int param, int param2) {
|
||||||
return id + "/" + anotherId + "/trailing";
|
return param + "/" + param2 + "/trailing";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue