Skip to content

Commit

Permalink
Adapt to ContextAccessor change: new readValue method (#3124)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbasle committed Jul 25, 2022
1 parent f47fc56 commit d2c5a4d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
Expand Up @@ -21,6 +21,8 @@

import io.micrometer.context.ContextAccessor;

import reactor.util.annotation.Nullable;

/**
* A {@code ContextAccessor} to enable reading values from a Reactor
* {@link ContextView} and writing values to {@link Context}.
Expand Down Expand Up @@ -48,6 +50,12 @@ public void readValues(ContextView source, Predicate<Object> keyPredicate, Map<O
});
}

@Override
@Nullable
public <T> T readValue(ContextView sourceContext, Object key) {
return sourceContext.getOrDefault(key, null);
}

@Override
public boolean canWriteTo(Class<?> contextType) {
return Context.class.isAssignableFrom(contextType);
Expand Down
Expand Up @@ -104,4 +104,23 @@ void writeValuesWithPutAllMap() {
Mockito.verify(target, never()).putAll((ContextView) any());
Mockito.verify(target, times(1)).putAllMap(anyMap());
}

@Test
void readValueWhenKeyPresent() {
ReactorContextAccessor test = new ReactorContextAccessor();
String expectedValue = "A";
ContextView source = Context.of(1, expectedValue, 2, "B");

String readValue = test.readValue(source, 1);
assertThat(readValue).isSameAs(expectedValue);
}

@Test
void readValueReturnsNullWhenKeyAbsent() {
ReactorContextAccessor test = new ReactorContextAccessor();
ContextView source = Context.of(1, "A");

String readValue = test.readValue(source, 2);
assertThat(readValue).isNull();
}
}

0 comments on commit d2c5a4d

Please sign in to comment.