Skip to content

Commit

Permalink
Replace use of deprecated methods in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed Jun 3, 2024
1 parent 2c6ef91 commit 43c32dd
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,7 +24,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import io.micrometer.context.ContextSnapshot;
import io.micrometer.context.ContextSnapshotFactory;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand Down Expand Up @@ -60,7 +60,7 @@ public class BatchMappingPrincipalMethodArgumentResolverTests extends BatchMappi
ReactiveSecurityContextHolder.withAuthentication(this.authentication);

private final Function<Context, Context> threadLocalContextWriter = context ->
ContextSnapshot.captureAll().updateContext(context);
ContextSnapshotFactory.builder().build().captureAll().updateContext(context);


private static Stream<Arguments> controllers() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,6 +23,7 @@

import graphql.GraphqlErrorBuilder;
import io.micrometer.context.ContextSnapshot;
import io.micrometer.context.ContextSnapshotFactory;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -70,8 +71,10 @@ public class SchemaMappingPrincipalMethodArgumentResolverTests {
private final Function<Context, Context> reactiveContextWriterWithoutAuthentication = context ->
ReactiveSecurityContextHolder.withSecurityContext(Mono.just(SecurityContextHolder.createEmptyContext()));

private final Function<Context, Context> threadLocalContextWriter = context ->
ContextSnapshot.captureAll().updateContext(context);
private final Function<Context, Context> threadLocalContextWriter = context -> {
ContextSnapshot snapshot = ContextSnapshotFactory.builder().build().captureAll();
return snapshot.updateContext(context);
};

private final GreetingController greetingController = new GreetingController();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -25,6 +25,7 @@
import graphql.GraphqlErrorBuilder;
import io.micrometer.context.ContextRegistry;
import io.micrometer.context.ContextSnapshot;
import io.micrometer.context.ContextSnapshotFactory;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
Expand Down Expand Up @@ -109,7 +110,8 @@ void resolveExceptionWithThreadLocal() {
.toGraphQl();

ExecutionInput input = ExecutionInput.newExecutionInput(query).build();
ContextSnapshot.captureAll().updateContext(input.getGraphQLContext());
ContextSnapshot snapshot = ContextSnapshotFactory.builder().build().captureAll();
snapshot.updateContext(input.getGraphQLContext());

Flux<ResponseHelper> flux = Mono.defer(() -> Mono.fromFuture(graphQL.executeAsync(input)))
.map(ResponseHelper::forSubscription)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import graphql.schema.idl.SchemaDirectiveWiringEnvironment;
import io.micrometer.context.ContextRegistry;
import io.micrometer.context.ContextSnapshot;
import io.micrometer.context.ContextSnapshotFactory;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
Expand Down Expand Up @@ -181,7 +182,8 @@ void dataFetcherWithThreadLocalContext() {
.toGraphQl();

ExecutionInput input = ExecutionInput.newExecutionInput().query("{ greeting }").build();
ContextSnapshot.captureAll().updateContext(input.getGraphQLContext());
ContextSnapshot snapshot = ContextSnapshotFactory.builder().build().captureAll();
snapshot.updateContext(input.getGraphQLContext());

Mono<ExecutionResult> resultMono = Mono.delay(Duration.ofMillis(10))
.flatMap((aLong) -> Mono.fromFuture(graphQl.executeAsync(input)));
Expand All @@ -202,7 +204,7 @@ void dataFetcherDecoratedWithDataFetcherFactories() {
@SuppressWarnings("unchecked")
@Override
public GraphQLFieldDefinition onField(SchemaDirectiveWiringEnvironment<GraphQLFieldDefinition> env) {
if (env.getDirective("UpperCase") != null) {
if (env.getAppliedDirective("UpperCase") != null) {
return env.setFieldDataFetcher(DataFetcherFactories.wrapDataFetcher(
env.getFieldDataFetcher(),
((dataFetchingEnv, value) -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,6 +24,7 @@
import graphql.GraphqlErrorBuilder;
import io.micrometer.context.ContextRegistry;
import io.micrometer.context.ContextSnapshot;
import io.micrometer.context.ContextSnapshotFactory;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono;

Expand Down Expand Up @@ -100,7 +101,8 @@ void resolveExceptionWithThreadLocal() {
.build());

resolver.setThreadLocalContextAware(true);
ContextSnapshot.captureAll().updateContext(this.input.getGraphQLContext());
ContextSnapshot snapshot = ContextSnapshotFactory.builder().build().captureAll();
snapshot.updateContext(this.input.getGraphQLContext());

Mono<ExecutionResult> result = Mono.delay(Duration.ofMillis(10)).flatMap((aLong) ->
Mono.fromFuture(this.graphQlSetup.exceptionResolver(resolver).toGraphQl().executeAsync(this.input)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import io.micrometer.context.ContextRegistry;
import io.micrometer.context.ContextSnapshot;
import io.micrometer.context.ContextSnapshotFactory;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Flux;
Expand Down Expand Up @@ -403,7 +404,8 @@ void contextPropagation() throws Exception {
GraphQlWebSocketHandler handler = initWebSocketHandler(threadLocalInterceptor);

// Ensure ContextSnapshot is present in WebSocketSession attributes
this.session.getAttributes().put(ContextSnapshot.class.getName(), ContextSnapshot.captureAll());
ContextSnapshot snapshot = ContextSnapshotFactory.builder().build().captureAll();
this.session.getAttributes().put(ContextSnapshot.class.getName(), snapshot);

// Context should propagate, if message is handled on different thread
Thread thread = new Thread(() -> {
Expand Down Expand Up @@ -452,7 +454,8 @@ private void handle(GraphQlWebSocketHandler handler, TextMessage... textMessages

if (!this.session.getAttributes().containsKey(ContextSnapshot.class.getName())) {
// Ensure ContextSnapshot is present in WebSocketSession attributes
this.session.getAttributes().put(ContextSnapshot.class.getName(), ContextSnapshot.captureAll());
ContextSnapshot snapshot = ContextSnapshotFactory.builder().build().captureAll();
this.session.getAttributes().put(ContextSnapshot.class.getName(), snapshot);
}

for (TextMessage message : textMessages) {
Expand Down

0 comments on commit 43c32dd

Please sign in to comment.