|
| 1 | +/** |
| 2 | + * Copyright 2025 the original author or authors. |
| 3 | + * <p> |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 | + * in compliance with the License. You may obtain a copy of the License at |
| 6 | + * <p> |
| 7 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * <p> |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 10 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 11 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 12 | + * the License. |
| 13 | + */ |
| 14 | +package io.micrometer.tracing.contextpropagation.reactor; |
| 15 | + |
| 16 | +import io.micrometer.tracing.contextpropagation.BaggageToPropagate; |
| 17 | +import io.micrometer.tracing.contextpropagation.ObservationAwareBaggageThreadLocalAccessor; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | +import reactor.util.context.Context; |
| 20 | + |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import static org.assertj.core.api.BDDAssertions.then; |
| 25 | + |
| 26 | +class ReactorBaggageTests { |
| 27 | + |
| 28 | + @Test |
| 29 | + void should_append_single_baggage_entry_to_context() { |
| 30 | + Context updatedContext = ReactorBaggage.append("foo", "bar").apply(Context.empty()); |
| 31 | + |
| 32 | + BaggageToPropagate baggage = updatedContext.get(ObservationAwareBaggageThreadLocalAccessor.KEY); |
| 33 | + then(baggage).isNotNull(); |
| 34 | + then(baggage.getBaggage()).hasSize(1); |
| 35 | + then(baggage.getBaggage()).containsEntry("foo", "bar"); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + void should_append_multiple_baggage_entries_to_context() { |
| 40 | + Map<String, String> baggageEntries = new HashMap<>(); |
| 41 | + baggageEntries.put("foo", "bar"); |
| 42 | + baggageEntries.put("baz", "bar2"); |
| 43 | + |
| 44 | + Context updatedContext = ReactorBaggage.append(baggageEntries).apply(Context.empty()); |
| 45 | + |
| 46 | + BaggageToPropagate baggage = updatedContext.get(ObservationAwareBaggageThreadLocalAccessor.KEY); |
| 47 | + then(baggage).isNotNull(); |
| 48 | + then(baggage.getBaggage()).hasSize(2); |
| 49 | + then(baggage.getBaggage()).containsExactlyInAnyOrderEntriesOf(baggageEntries); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void should_merge_existing_baggage_with_new_entries() { |
| 54 | + Map<String, String> initialBaggage = new HashMap<>(); |
| 55 | + initialBaggage.put("foo", "bar"); |
| 56 | + initialBaggage.put("baz", "bar2"); |
| 57 | + Context context = Context.of(ObservationAwareBaggageThreadLocalAccessor.KEY, |
| 58 | + new BaggageToPropagate(initialBaggage)); |
| 59 | + |
| 60 | + Map<String, String> newBaggage = new HashMap<>(); |
| 61 | + newBaggage.put("foo", "bar1"); |
| 62 | + |
| 63 | + Context updatedContext = ReactorBaggage.append(newBaggage).apply(context); |
| 64 | + |
| 65 | + BaggageToPropagate baggage = updatedContext.get(ObservationAwareBaggageThreadLocalAccessor.KEY); |
| 66 | + then(baggage).isNotNull(); |
| 67 | + then(baggage.getBaggage()).hasSize(2); |
| 68 | + then(baggage.getBaggage()).containsEntry("foo", "bar1").containsEntry("baz", "bar2"); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void should_merge_existing_baggage_with_single_entry() { |
| 73 | + Map<String, String> initialBaggage = new HashMap<>(); |
| 74 | + initialBaggage.put("foo", "bar"); |
| 75 | + initialBaggage.put("baz", "bar2"); |
| 76 | + Context context = Context.of(ObservationAwareBaggageThreadLocalAccessor.KEY, |
| 77 | + new BaggageToPropagate(initialBaggage)); |
| 78 | + |
| 79 | + Context updatedContext = ReactorBaggage.append("foo", "bar1").apply(context); |
| 80 | + |
| 81 | + BaggageToPropagate baggage = updatedContext.get(ObservationAwareBaggageThreadLocalAccessor.KEY); |
| 82 | + then(baggage).isNotNull(); |
| 83 | + then(baggage.getBaggage()).hasSize(2); |
| 84 | + then(baggage.getBaggage()).containsEntry("foo", "bar1").containsEntry("baz", "bar2"); |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments