Skip to content

Commit ae32df5

Browse files
committed
Merge branch 'develop'
2 parents 35e9909 + ee32ab6 commit ae32df5

File tree

3 files changed

+50
-75
lines changed

3 files changed

+50
-75
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
kotlin.code.style=official
2-
version=1.0.0
2+
version=1.1.0
33
group=io.github.numichi
44
developerId=numichi
55
developerName=Donát Csongor

src/main/java/io/github/numichi/reactive/logger/java/MDCContext.java

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,49 +13,30 @@
1313

1414
import static io.github.numichi.reactive.logger.exception.Messages.CTXK_NOT_NULL;
1515
import static io.github.numichi.reactive.logger.exception.Messages.CTXW_NOT_NULL;
16-
import static io.github.numichi.reactive.logger.exception.Messages.CTX_NOT_NULL;
17-
import static io.github.numichi.reactive.logger.exception.Messages.MAP_NOT_NULL;
1816
import static io.github.numichi.reactive.logger.exception.Messages.MDC_NOT_NULL;
1917

2018
public final class MDCContext {
2119
private MDCContext() {
2220
}
2321

2422
@NonNull
25-
public static Context put(Context context, Map<String, String> mdc) {
23+
public static Context put(Context context, MDC... mdc) {
2624
try {
2725
Objects.requireNonNull(context, CTXK_NOT_NULL);
2826
Objects.requireNonNull(mdc, MDC_NOT_NULL);
2927
} catch (NullPointerException exception) {
3028
throw new IllegalArgumentException(exception);
3129
}
3230

33-
return context.put(Values.DEFAULT_REACTOR_CONTEXT_MDC_KEY, mdc);
34-
}
35-
36-
@NonNull
37-
public static Context put(Context context, String mdcContextKey, Map<String, String> mdc) {
38-
try {
39-
Objects.requireNonNull(context, CTX_NOT_NULL);
40-
Objects.requireNonNull(mdcContextKey, CTXK_NOT_NULL);
41-
Objects.requireNonNull(mdc, MAP_NOT_NULL);
42-
} catch (NullPointerException exception) {
43-
throw new IllegalArgumentException(exception);
44-
}
45-
46-
return context.put(mdcContextKey, mdc);
47-
}
48-
49-
@NonNull
50-
public static Context put(Context context, MDC mdc) {
51-
try {
52-
Objects.requireNonNull(context, CTXK_NOT_NULL);
53-
Objects.requireNonNull(mdc, MDC_NOT_NULL);
54-
} catch (NullPointerException exception) {
55-
throw new IllegalArgumentException(exception);
31+
for (MDC m : mdc) {
32+
if (m == null) {
33+
continue;
34+
}
35+
36+
context = context.put(m.getContextKey(), m.asMap());
5637
}
5738

58-
return context.put(mdc.getContextKey(), mdc.asMap());
39+
return context;
5940
}
6041

6142
@NonNull

src/test/java/io/github/numichi/reactive/logger/java/MDCContextTest.java

Lines changed: 41 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,45 @@ void shouldGiveTheMDCByDefault() {
6161
.verifyComplete();
6262
}
6363

64+
@Test
65+
@DisplayName("should return the appropriate contexts count")
66+
void shouldReturnTheAppropriateContextsCount() {
67+
MDC defaultMdc = new MDC();
68+
defaultMdc.put("mdcKey", "mdcValue");
69+
70+
MDC anotherMdc = new MDC(ANOTHER_CONTEXT_KEY);
71+
anotherMdc.put("mdcKey", "mdcValue");
72+
73+
Mono<Integer> contextSize1 = Mono.deferContextual(ctx -> Mono.just(ctx.size()))
74+
.contextWrite(it -> MDCContext.put(it, defaultMdc))
75+
.contextWrite(it -> MDCContext.put(it, anotherMdc));
76+
StepVerifier.create(contextSize1)
77+
.expectNext(2)
78+
.verifyComplete();
79+
80+
81+
Mono<Integer> contextSize2 = Mono.deferContextual(ctx -> Mono.just(ctx.size()))
82+
.contextWrite(it -> MDCContext.put(it, defaultMdc, anotherMdc));
83+
StepVerifier.create(contextSize2)
84+
.expectNext(2)
85+
.verifyComplete();
86+
87+
88+
Mono<Integer> contextSize3 = Mono.deferContextual(ctx -> Mono.just(ctx.size()))
89+
.contextWrite(it -> MDCContext.put(it, defaultMdc, null));
90+
StepVerifier.create(contextSize3)
91+
.expectNext(1)
92+
.verifyComplete();
93+
94+
95+
Mono<Integer> contextSize4 = Mono.deferContextual(ctx -> Mono.just(ctx.size()))
96+
.contextWrite(it -> MDCContext.put(it, defaultMdc, null))
97+
.contextWrite(it -> it.put("A", "B"));
98+
StepVerifier.create(contextSize4)
99+
.expectNext(2)
100+
.verifyComplete();
101+
}
102+
64103
@Test
65104
@DisplayName("should give the MDC you are looking for (more MDC Context)")
66105
void shouldGiveTheMDCWithMoreMdcContext() {
@@ -74,7 +113,6 @@ void shouldGiveTheMDCWithMoreMdcContext() {
74113
Mono<MDC> resultDefault = Mono.defer(MDCContext::read)
75114
.contextWrite(it -> MDCContext.put(it, defaultMdc))
76115
.contextWrite(it -> MDCContext.put(it, anotherMdc));
77-
78116
StepVerifier.create(resultDefault)
79117
.expectNext(defaultMdc)
80118
.verifyComplete();
@@ -83,57 +121,16 @@ void shouldGiveTheMDCWithMoreMdcContext() {
83121
Mono<MDC> resultAnother = Mono.defer(() -> MDCContext.read(ANOTHER_CONTEXT_KEY))
84122
.contextWrite(it -> MDCContext.put(it, defaultMdc))
85123
.contextWrite(it -> MDCContext.put(it, anotherMdc));
86-
87124
StepVerifier.create(resultAnother)
88125
.expectNext(anotherMdc)
89126
.verifyComplete();
90127
}
91128

92-
@Test
93-
@DisplayName("should need to be able to store a map")
94-
void shouldNeedToBeAbleToStoreAMap() {
95-
Map<String, String> mdcMap = new HashMap<>();
96-
mdcMap.put("mdcKey", "mdcValue");
97-
98-
99-
Mono<MDC> resultDefault = Mono.defer(MDCContext::read)
100-
.contextWrite(it -> MDCContext.put(it, mdcMap));
101-
102-
StepVerifier.create(resultDefault)
103-
.expectNextMatches(mdc1 -> mdc1.asMap().equals(mdcMap) && mdc1.getContextKey().equals(DEFAULT_REACTOR_CONTEXT_MDC_KEY))
104-
.verifyComplete();
105-
106-
107-
Mono<MDC> resultAnother = Mono.defer(() -> MDCContext.read(ANOTHER_CONTEXT_KEY))
108-
.contextWrite(it -> MDCContext.put(it, ANOTHER_CONTEXT_KEY, mdcMap));
109-
110-
StepVerifier.create(resultAnother)
111-
.expectNextMatches(mdc1 -> mdc1.asMap().equals(mdcMap) && mdc1.getContextKey().equals(ANOTHER_CONTEXT_KEY))
112-
.verifyComplete();
113-
}
114-
115-
@Test
116-
@DisplayName("should be MDC stored with the overridden context ID")
117-
void shouldBeStoredWithTheOverriddenContextIDTest() {
118-
MDC mdc = new MDC();
119-
mdc.put("mdcKey", "mdcValue");
120-
121-
Mono<MDC> result = Mono.defer(() -> MDCContext.read(ANOTHER_CONTEXT_KEY))
122-
.contextWrite(it -> MDCContext.put(it, ANOTHER_CONTEXT_KEY, mdc));
123-
124-
StepVerifier.create(result)
125-
.expectNextMatches(mdc1 -> mdc1.asMap().equals(mdc.asMap()) && mdc1.getContextKey().equals(ANOTHER_CONTEXT_KEY))
126-
.verifyComplete();
127-
}
128-
129129
@Test
130130
@DisplayName("should throw IllegalArgumentException if any parameter is NULL")
131131
void shouldThrowIllegalArgumentExceptionTest() {
132-
Map<String, String> emptyMap = new HashMap<>();
133-
134-
assertThrows(IllegalArgumentException.class, () -> MDCContext.put(null, "", emptyMap));
135-
assertThrows(IllegalArgumentException.class, () -> MDCContext.put(Context.empty(), null, emptyMap));
136-
assertThrows(IllegalArgumentException.class, () -> MDCContext.put(Context.empty(), "", null));
132+
assertThrows(IllegalArgumentException.class, () -> MDCContext.put(null, new MDC()));
133+
assertThrows(IllegalArgumentException.class, () -> MDCContext.put(Context.empty(), null));
137134

138135
StepVerifier.create(MDCContext.read((String) null))
139136
.expectError(IllegalArgumentException.class)
@@ -161,9 +158,6 @@ void shouldThrowInvalidContextDataException() {
161158
.verify();
162159

163160

164-
165-
166-
167161
Mono<MDC> result2 = Mono.defer(() -> MDCContext.read("not-exist-context-id"))
168162
.contextWrite(it -> MDCContext.put(it, mdc));
169163
StepVerifier.create(result2)

0 commit comments

Comments
 (0)