1515import com .azure .core .util .ConfigurationBuilder ;
1616import com .azure .core .util .CoreUtils ;
1717import com .azure .core .util .logging .ClientLogger ;
18- import com .azure .identity .DefaultAzureCredentialBuilder ;
1918import com .azure .messaging .eventhubs .models .SendOptions ;
2019import org .junit .jupiter .api .AfterEach ;
2120import org .junit .jupiter .api .Assertions ;
22- import org .junit .jupiter .api .Assumptions ;
2321import org .junit .jupiter .api .BeforeEach ;
2422import org .mockito .Mockito ;
2523import reactor .core .Disposable ;
3836import java .util .stream .Collectors ;
3937import java .util .stream .IntStream ;
4038
39+ import static com .azure .messaging .eventhubs .TestUtils .AZURE_EVENTHUBS_EVENT_HUB_NAME ;
40+ import static com .azure .messaging .eventhubs .TestUtils .AZURE_EVENTHUBS_FULLY_QUALIFIED_DOMAIN_NAME ;
41+ import static org .junit .jupiter .api .Assumptions .assumeFalse ;
42+ import static org .junit .jupiter .api .Assumptions .assumeTrue ;
43+
4144/**
4245 * Test base for running integration tests.
4346 */
@@ -61,9 +64,6 @@ public abstract class IntegrationTestBase extends TestBase {
6164
6265 protected String testName ;
6366
64- private static final ClientOptions OPTIONS_WITH_TRACING
65- = new ClientOptions ().setTracingOptions (new LoggingTracerProvider .LoggingTracingOptions ());
66-
6767 private Scheduler scheduler ;
6868 private static Map <String , IntegrationTestEventData > testEventData ;
6969 private List <AutoCloseable > toClose = new ArrayList <>();
@@ -81,7 +81,7 @@ public void setupTest(TestContextManager testContextManager) {
8181 testContextManager .getTestPlaybackRecordingName ());
8282
8383 testName = testContextManager .getTrackerTestName ();
84- skipIfNotRecordMode ();
84+ skipIfNotRecordOrLiveMode ();
8585 toClose = new ArrayList <>();
8686
8787 scheduler = Schedulers .newParallel ("eh-integration" );
@@ -116,62 +116,56 @@ public void teardownTest() {
116116 }
117117
118118 /**
119- * Creates a new instance of {@link EventHubClientBuilder} with the default integration test settings and uses a
120- * connection string to authenticate .
119+ * Creates a new instance of {@link EventHubClientBuilder} with the default integration test settings and does not
120+ * share a connection string .
121121 */
122122 protected EventHubClientBuilder createBuilder () {
123123 return createBuilder (false );
124124 }
125125
126126 /**
127- * Creates a new instance of {@link EventHubClientBuilder} with the default integration test settings and uses a
128- * connection string to authenticate if {@code useCredentials} is false. Otherwise, uses a service principal through
129- * {@link com.azure.identity.ClientSecretCredential}.
127+ * Creates a new instance of {@link EventHubClientBuilder} with the default integration test settings. Assumes test
128+ * mode is not {@link TestMode#PLAYBACK}.
130129 */
131130 protected EventHubClientBuilder createBuilder (boolean shareConnection ) {
131+ skipIfNotRecordOrLiveMode ();
132+
133+ // Enables tracing.
134+ final ClientOptions clientOptions
135+ = new ClientOptions ().setTracingOptions (new LoggingTracerProvider .LoggingTracingOptions ());
136+
132137 final EventHubClientBuilder builder = new EventHubClientBuilder ().proxyOptions (ProxyOptions .SYSTEM_DEFAULTS )
133138 .retryOptions (RETRY_OPTIONS )
134- .clientOptions (OPTIONS_WITH_TRACING )
139+ .clientOptions (clientOptions )
135140 .transportType (AmqpTransportType .AMQP )
136141 .scheduler (scheduler )
137142 .configuration (new ConfigurationBuilder ().putProperty ("com.azure.messaging.eventhubs.v2" , "true" ).build ());
138143
139- final String fullyQualifiedDomainName = TestUtils .getFullyQualifiedDomainName ();
140- final String eventHubName = TestUtils .getEventHubName ();
141-
142144 if (shareConnection ) {
143145 builder .shareConnection ();
144146 }
145147
146- switch (getTestMode ()) {
147- case PLAYBACK :
148- Assumptions .assumeTrue (false , "Integration tests are not enabled in playback mode." );
149- return null ;
150-
151- case LIVE :
152- Assumptions .assumeTrue (!CoreUtils .isNullOrEmpty (fullyQualifiedDomainName ),
153- "FullyQualifiedDomainName is not set." );
154- Assumptions .assumeTrue (!CoreUtils .isNullOrEmpty (fullyQualifiedDomainName ), "EventHubName is not set." );
155-
156- final TokenCredential credential = TestUtils .getPipelineCredential (credentialCached );
157- return builder .credential (fullyQualifiedDomainName , eventHubName , credential );
158-
159- case RECORD :
160- final String connectionString = TestUtils .getConnectionString (false );
161-
162- Assumptions .assumeTrue (!CoreUtils .isNullOrEmpty (eventHubName ), "EventHubName is not set." );
163- Assumptions .assumeTrue (!CoreUtils .isNullOrEmpty (fullyQualifiedDomainName ),
164- "FullyQualifiedDomainName is not set." );
165-
166- if (CoreUtils .isNullOrEmpty (connectionString )) {
167- final TokenCredential tokenCredential = new DefaultAzureCredentialBuilder ().build ();
168- return builder .credential (fullyQualifiedDomainName , eventHubName , tokenCredential );
169- } else {
170- return builder .connectionString (connectionString ).eventHubName (eventHubName );
171- }
172- default :
173- return null ;
148+ final TokenCredential tokenCredential = TestUtils .getTokenCredential (getTestMode (), credentialCached );
149+
150+ assumeTrue (tokenCredential != null , "Token credential could not be created using " + getTestMode ());
151+
152+ if (getTestMode () == TestMode .PLAYBACK ) {
153+ return builder .credential (tokenCredential );
174154 }
155+
156+ final String fullyQualifiedDomainName = TestUtils .getFullyQualifiedDomainName ();
157+ final String eventHubName = TestUtils .getEventHubName ();
158+
159+ assumeFalse (CoreUtils .isNullOrEmpty (fullyQualifiedDomainName ),
160+ "Env variable: '" + AZURE_EVENTHUBS_FULLY_QUALIFIED_DOMAIN_NAME + "' must be set." );
161+ assumeFalse (CoreUtils .isNullOrEmpty (eventHubName ),
162+ "Env variable: '" + AZURE_EVENTHUBS_EVENT_HUB_NAME + "' must be set." );
163+
164+ return builder .credential (fullyQualifiedDomainName , eventHubName , tokenCredential );
165+ }
166+
167+ protected TokenCredential getOrCreateTokenCredential () {
168+ return TestUtils .getTokenCredential (getTestMode (), credentialCached );
175169 }
176170
177171 /**
@@ -247,8 +241,7 @@ protected void dispose() {
247241 toClose .clear ();
248242 }
249243
250- private void skipIfNotRecordMode () {
251- Assumptions . assumeTrue (getTestMode () != TestMode .PLAYBACK , "Is not in RECORD/LIVE mode." );
244+ private void skipIfNotRecordOrLiveMode () {
245+ assumeTrue (getTestMode () != TestMode .PLAYBACK , "Is not in RECORD/LIVE mode." );
252246 }
253-
254247}
0 commit comments