11/**
2- * Copyright 2017-2018 The OpenTracing Authors
2+ * Copyright 2017-2019 The OpenTracing Authors
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
55 * in compliance with the License. You may obtain a copy of the License at
1313 */
1414package io .opentracing .contrib .spring .cloud .mongo ;
1515
16- import static org .hamcrest .Matchers .is ;
17- import static org .junit .Assert .assertNotNull ;
18- import static org .junit .Assert .assertThat ;
1916import static org .mockito .Mockito .mock ;
2017
2118import com .mongodb .MongoClient ;
19+ import com .mongodb .MongoClientURI ;
2220import io .opentracing .Tracer ;
21+ import io .opentracing .contrib .spring .tracer .configuration .TracerAutoConfiguration ;
22+ import org .assertj .core .api .Assertions ;
2323import org .junit .Test ;
24- import org .springframework .boot .test .util .TestPropertyValues ;
25- import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
24+ import org .springframework .boot .autoconfigure .AutoConfigurations ;
25+ import org .springframework .boot .autoconfigure .mongo .MongoAutoConfiguration ;
26+ import org .springframework .boot .context .annotation .UserConfigurations ;
27+ import org .springframework .boot .test .context .runner .ApplicationContextRunner ;
2628import org .springframework .context .annotation .Bean ;
2729import org .springframework .context .annotation .Configuration ;
2830
3234public class MongoTracingAutoConfigurationTest {
3335
3436 @ Test
35- public void loadMongoTracingByDefault () {
36- AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ();
37- context .register (TracerConfig .class , MongoTracingAutoConfiguration .class );
38- context .refresh ();
39- MongoClient mongoClient = context .getBean (MongoClient .class );
40- assertNotNull (mongoClient );
37+ public void createsTracingPostProcessor () {
38+ final ApplicationContextRunner contextRunner = new ApplicationContextRunner ()
39+ .withConfiguration (UserConfigurations .of (TracerConfig .class , MongoConfig .class ))
40+ .withConfiguration (AutoConfigurations .of (MongoTracingAutoConfiguration .class ));
41+
42+ contextRunner .run (context -> Assertions .assertThat (context ).hasSingleBean (TracingMongoClientPostProcessor .class ));
43+ }
44+
45+ @ Test
46+ public void doesNotCreateTracingPostProcessorWhenNoTracer () {
47+ final ApplicationContextRunner contextRunner = new ApplicationContextRunner ()
48+ .withConfiguration (UserConfigurations .of (MongoConfig .class ))
49+ .withConfiguration (AutoConfigurations .of (MongoTracingAutoConfiguration .class ));
50+
51+ contextRunner .run (context -> Assertions .assertThat (context ).doesNotHaveBean (TracingMongoClientPostProcessor .class ));
52+ }
53+
54+ @ Test
55+ public void doesNotCreateTracingPostProcessorWhenNoMongoClient () {
56+ final ApplicationContextRunner contextRunner = new ApplicationContextRunner ()
57+ .withConfiguration (UserConfigurations .of (TracerConfig .class ))
58+ .withConfiguration (AutoConfigurations .of (MongoTracingAutoConfiguration .class ));
59+
60+ contextRunner .run (context -> Assertions .assertThat (context ).doesNotHaveBean (TracingMongoClientPostProcessor .class ));
61+ }
62+
63+ @ Test
64+ public void doesNotCreateTracingPostProcessorWhenDisabled () {
65+ final ApplicationContextRunner contextRunner = new ApplicationContextRunner ()
66+ .withPropertyValues ("opentracing.spring.cloud.mongo.enabled=false" )
67+ .withConfiguration (UserConfigurations .of (TracerConfig .class , MongoConfig .class ))
68+ .withConfiguration (AutoConfigurations .of (MongoTracingAutoConfiguration .class ));
69+
70+ contextRunner .run (context -> Assertions .assertThat (context ).doesNotHaveBean (TracingMongoClientPostProcessor .class ));
4171 }
4272
4373 @ Test
44- public void disableMongoTracing () {
45- AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ();
46- context .register (TracerConfig .class , MongoTracingAutoConfiguration .class );
47- TestPropertyValues .of ("opentracing.spring.cloud.mongo.enabled:false" ).applyTo (context );
48- context .refresh ();
49- String [] tracingMongoClientBeans = context .getBeanNamesForType (MongoClient .class );
50- assertThat (tracingMongoClientBeans .length , is (0 ));
74+ public void createsTracingPostProcessorWhenAutoConfigured () {
75+ final ApplicationContextRunner contextRunner = new ApplicationContextRunner ()
76+ .withConfiguration (AutoConfigurations .of (
77+ MongoTracingAutoConfiguration .class ,
78+ TracerAutoConfiguration .class ,
79+ MongoAutoConfiguration .class
80+ ));
81+
82+ contextRunner .run (context -> Assertions .assertThat (context ).hasSingleBean (TracingMongoClientPostProcessor .class ));
5183 }
5284
5385 @ Configuration
@@ -58,4 +90,13 @@ public Tracer tracer() {
5890 return mock (Tracer .class );
5991 }
6092 }
61- }
93+
94+ @ Configuration
95+ static class MongoConfig {
96+
97+ @ Bean
98+ public MongoClient client () {
99+ return new MongoClient (new MongoClientURI ("mongodb://localhost/test" ));
100+ }
101+ }
102+ }
0 commit comments