BaggagePropagation implements Propagation<K> { public static FactoryBuilder newFactoryBuilder(Propagation.Factory delegate) { return new FactoryBuilder(delegate); } BaggagePropagation(Propagation<K> delegate); static FactoryBuilder newFactoryBuilder(Propagation.Factory delegate); @Override List<K> keys(); static List<String> allKeyNames(Propagation<String> propagation); @Override Injector<R> injector(Setter<R, K> setter); @Override Extractor<R> extractor(Getter<R, K> getter);  }
@Test public void newFactory_noFields() { assertThat(BaggagePropagation.newFactoryBuilder(B3Propagation.FACTORY).build()) .isSameAs(B3Propagation.FACTORY); }
@Test public void newFactory_sharingRemoteName() { BaggagePropagation.FactoryBuilder builder = newFactoryBuilder(B3Propagation.FACTORY); SingleBaggageField userName = SingleBaggageField.newBuilder(BaggageField.create("userName")).addKeyName("baggage").build(); SingleBaggageField userId = SingleBaggageField.newBuilder(BaggageField.create("userId")).addKeyName("baggage").build(); builder.add(userName); assertThatThrownBy(() -> builder.add(userId)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("Propagation key already in use: baggage"); }
@Test public void clear_and_add() { SingleBaggageField requestIdConfig = SingleBaggageField.newBuilder(vcapRequestId) .addKeyName("request-id") .addKeyName("request_id") .build(); SingleBaggageField traceIdConfig = SingleBaggageField.remote(amznTraceId); BaggagePropagation.FactoryBuilder builder = newFactoryBuilder(B3Propagation.FACTORY) .add(requestIdConfig) .add(traceIdConfig); Set<BaggagePropagationConfig> configs = builder.configs(); builder.clear(); configs.forEach(builder::add); assertThat(builder) .usingRecursiveComparison() .isEqualTo(newFactoryBuilder(B3Propagation.FACTORY) .add(requestIdConfig) .add(traceIdConfig)); }
@Test public void extract_field_multiple_key_names() { request = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); BaggageField userId = BaggageField.create("userId"); BaggageField sessionId = BaggageField.create("sessionId"); SingleBaggageField userIdConfig = SingleBaggageField.newBuilder(userId) .addKeyName("baggage-userId") .addKeyName("baggage_userId") .build(); SingleBaggageField sessionIdConfig = SingleBaggageField.newBuilder(sessionId) .addKeyName("baggage-sessionId") .addKeyName("baggage_sessionId") .build(); factory = newFactoryBuilder(B3Propagation.FACTORY) .add(userIdConfig) .add(sessionIdConfig) .build(); initialize(); injector.inject(context, request); request.put("baggage-userId", "bob"); request.put("baggage-sessionId", "12345"); context = extractor.extract(request).context(); assertThat(userId.getValue(context)).isEqualTo("bob"); assertThat(sessionId.getValue(context)).isEqualTo("12345"); }
@Test public void extract_no_overridden_key_names() { BaggageField userId = BaggageField.create("userId"); BaggageField sessionId = BaggageField.create("sessionId"); factory = newFactoryBuilder(B3Propagation.FACTORY) .add(SingleBaggageField.local(userId)) .add(SingleBaggageField.remote(sessionId)) .build(); initialize(); injector.inject(context, request); request.put("userid", "bob"); request.put("sessionid", "12345"); context = extractor.extract(request).context(); assertThat(userId.getValue(context)).isNull(); assertThat(sessionId.getValue(context)).isEqualTo("12345"); }
@Test public void inject_no_key_names() { BaggageField userId = BaggageField.create("userId"); BaggageField sessionId = BaggageField.create("sessionId"); factory = newFactoryBuilder(B3SinglePropagation.FACTORY) .add(SingleBaggageField.local(userId)) .add(SingleBaggageField.remote(sessionId)) .build(); initialize(); userId.updateValue(context, "bob"); sessionId.updateValue(context, "12345"); injector.inject(context, request); assertThat(request) .doesNotContainKey("userid") .containsEntry("sessionid", "12345"); }
@Test public void inject_field_multiple_key_names() { BaggageField userId = BaggageField.create("userId"); BaggageField sessionId = BaggageField.create("sessionId"); SingleBaggageField userIdConfig = SingleBaggageField.newBuilder(userId) .addKeyName("userId") .addKeyName("baggage-userId") .addKeyName("baggage_userId") .build(); SingleBaggageField sessionIdConfig = SingleBaggageField.newBuilder(sessionId) .addKeyName("sessionId") .addKeyName("baggage-sessionId") .addKeyName("baggage_sessionId") .build(); factory = newFactoryBuilder(B3SinglePropagation.FACTORY) .add(userIdConfig) .add(sessionIdConfig) .build(); initialize(); userId.updateValue(context, "bob"); sessionId.updateValue(context, "12345"); injector.inject(context, request); assertThat(request).containsOnly( entry("b3", B3SingleFormat.writeB3SingleFormat(context)), entry("userid", "bob"), entry("sessionid", "12345"), entry("baggage-userid", "bob"), entry("baggage-sessionid", "12345"), entry("baggage_userid", "bob"), entry("baggage_sessionid", "12345") ); }
@Test public void dupesNotOk() { SingleBaggageField userIdConfig = SingleBaggageField.local(BaggageField.create("userId")); BaggagePropagation.FactoryBuilder builder = newFactoryBuilder(B3Propagation.FACTORY) .add(userIdConfig); assertThatThrownBy(() -> builder.add(userIdConfig)) .isInstanceOf(IllegalArgumentException.class); }