Skip to content

Commit 2823710

Browse files
committed
Refactor test method to separate configuration
1 parent 452093e commit 2823710

File tree

1 file changed

+45
-34
lines changed

1 file changed

+45
-34
lines changed

gcp-auth-extension/src/test/java/io/opentelemetry/contrib/gcp/auth/GcpAuthAutoConfigurationCustomizerProviderTest.java

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ public void testTraceCustomizerOtlpHttp() {
142142
});
143143
Mockito.when(mockOtlpHttpSpanExporter.toBuilder()).thenReturn(spyOtlpHttpSpanExporterBuilder);
144144

145+
// begin assertions
145146
try (MockedStatic<GoogleCredentials> googleCredentialsMockedStatic =
146147
Mockito.mockStatic(GoogleCredentials.class)) {
147148
googleCredentialsMockedStatic
@@ -189,37 +190,11 @@ public void testMetricCustomizerOtlpHttp() {
189190
OtlpHttpMetricExporterBuilder otlpMetricExporterBuilder = OtlpHttpMetricExporter.builder();
190191
OtlpHttpMetricExporterBuilder spyOtlpHttpMetricExporterBuilder =
191192
Mockito.spy(otlpMetricExporterBuilder);
192-
Mockito.when(spyOtlpHttpMetricExporterBuilder.build()).thenReturn(mockOtlpHttpMetricExporter);
193-
194-
Mockito.when(mockOtlpHttpMetricExporter.shutdown())
195-
.thenReturn(CompletableResultCode.ofSuccess());
196193
List<MetricData> exportedMetrics = new ArrayList<>();
197-
Mockito.when(mockOtlpHttpMetricExporter.export(Mockito.anyCollection()))
198-
.thenAnswer(
199-
invocationOnMock -> {
200-
exportedMetrics.addAll(invocationOnMock.getArgument(0));
201-
return CompletableResultCode.ofSuccess();
202-
});
203-
Mockito.when(mockOtlpHttpMetricExporter.toBuilder())
204-
.thenReturn(spyOtlpHttpMetricExporterBuilder);
205-
// mock the get default aggregation and aggregation temporality - they're required for valid
206-
// metric collection.
207-
Mockito.when(mockOtlpHttpMetricExporter.getDefaultAggregation(Mockito.any()))
208-
.thenAnswer(
209-
(Answer<Aggregation>)
210-
invocationOnMock -> {
211-
InstrumentType instrumentType = invocationOnMock.getArgument(0);
212-
return OtlpHttpMetricExporter.getDefault().getDefaultAggregation(instrumentType);
213-
});
214-
Mockito.when(mockOtlpHttpMetricExporter.getAggregationTemporality(Mockito.any()))
215-
.thenAnswer(
216-
(Answer<AggregationTemporality>)
217-
invocationOnMock -> {
218-
InstrumentType instrumentType = invocationOnMock.getArgument(0);
219-
return OtlpHttpMetricExporter.getDefault()
220-
.getAggregationTemporality(instrumentType);
221-
});
194+
configureHttpMockMetricExporter(
195+
mockOtlpHttpMetricExporter, spyOtlpHttpMetricExporterBuilder, exportedMetrics);
222196

197+
// begin assertions
223198
try (MockedStatic<GoogleCredentials> googleCredentialsMockedStatic =
224199
Mockito.mockStatic(GoogleCredentials.class)) {
225200
googleCredentialsMockedStatic
@@ -263,7 +238,7 @@ public void testMetricCustomizerOtlpHttp() {
263238
}
264239

265240
@Test
266-
public void testCustomizerOtlpGrpc() {
241+
public void testTraceCustomizerOtlpGrpc() {
267242
// Set resource project system property
268243
System.setProperty(
269244
ConfigurableOption.GOOGLE_CLOUD_PROJECT.getSystemProperty(), DUMMY_GCP_RESOURCE_PROJECT_ID);
@@ -273,9 +248,10 @@ public void testCustomizerOtlpGrpc() {
273248
OtlpGrpcSpanExporterBuilder spyOtlpGrpcSpanExporterBuilder =
274249
Mockito.spy(OtlpGrpcSpanExporter.builder());
275250
List<SpanData> exportedSpans = new ArrayList<>();
276-
configureGrpcMockSpanExporters(
251+
configureGrpcMockSpanExporter(
277252
mockOtlpGrpcSpanExporter, spyOtlpGrpcSpanExporterBuilder, exportedSpans);
278253

254+
// begin assertions
279255
try (MockedStatic<GoogleCredentials> googleCredentialsMockedStatic =
280256
Mockito.mockStatic(GoogleCredentials.class)) {
281257
googleCredentialsMockedStatic
@@ -367,7 +343,7 @@ public void testQuotaProjectBehavior(QuotaProjectIdTestBehavior testCase) throws
367343
OtlpGrpcSpanExporterBuilder spyOtlpGrpcSpanExporterBuilder =
368344
Mockito.spy(OtlpGrpcSpanExporter.builder());
369345
List<SpanData> exportedSpans = new ArrayList<>();
370-
configureGrpcMockSpanExporters(
346+
configureGrpcMockSpanExporter(
371347
mockOtlpGrpcSpanExporter, spyOtlpGrpcSpanExporterBuilder, exportedSpans);
372348

373349
try (MockedStatic<GoogleCredentials> googleCredentialsMockedStatic =
@@ -471,9 +447,9 @@ private static Stream<Arguments> provideQuotaBehaviorTestCases() {
471447
.build()));
472448
}
473449

474-
// Configure necessary behavior on the Grpc mock exporters to work
450+
// Configure necessary behavior on the gRPC mock span exporters to work.
475451
// TODO: Potential improvement - make this work for Http exporter as well.
476-
private static void configureGrpcMockSpanExporters(
452+
private static void configureGrpcMockSpanExporter(
477453
OtlpGrpcSpanExporter mockGrpcExporter,
478454
OtlpGrpcSpanExporterBuilder spyGrpcExporterBuilder,
479455
List<SpanData> exportedSpanContainer) {
@@ -488,6 +464,41 @@ private static void configureGrpcMockSpanExporters(
488464
});
489465
}
490466

467+
// Configure necessary behavior on the http mock metric exporters to work.
468+
private static void configureHttpMockMetricExporter(
469+
OtlpHttpMetricExporter mockOtlpHttpMetricExporter,
470+
OtlpHttpMetricExporterBuilder spyOtlpHttpMetricExporterBuilder,
471+
List<MetricData> exportedMetricContainer) {
472+
Mockito.when(spyOtlpHttpMetricExporterBuilder.build()).thenReturn(mockOtlpHttpMetricExporter);
473+
Mockito.when(mockOtlpHttpMetricExporter.shutdown())
474+
.thenReturn(CompletableResultCode.ofSuccess());
475+
Mockito.when(mockOtlpHttpMetricExporter.toBuilder())
476+
.thenReturn(spyOtlpHttpMetricExporterBuilder);
477+
Mockito.when(mockOtlpHttpMetricExporter.export(Mockito.anyCollection()))
478+
.thenAnswer(
479+
invocationOnMock -> {
480+
exportedMetricContainer.addAll(invocationOnMock.getArgument(0));
481+
return CompletableResultCode.ofSuccess();
482+
});
483+
// mock the get default aggregation and aggregation temporality - they're required for valid
484+
// metric collection.
485+
Mockito.when(mockOtlpHttpMetricExporter.getDefaultAggregation(Mockito.any()))
486+
.thenAnswer(
487+
(Answer<Aggregation>)
488+
invocationOnMock -> {
489+
InstrumentType instrumentType = invocationOnMock.getArgument(0);
490+
return OtlpHttpMetricExporter.getDefault().getDefaultAggregation(instrumentType);
491+
});
492+
Mockito.when(mockOtlpHttpMetricExporter.getAggregationTemporality(Mockito.any()))
493+
.thenAnswer(
494+
(Answer<AggregationTemporality>)
495+
invocationOnMock -> {
496+
InstrumentType instrumentType = invocationOnMock.getArgument(0);
497+
return OtlpHttpMetricExporter.getDefault()
498+
.getAggregationTemporality(instrumentType);
499+
});
500+
}
501+
491502
@AutoValue
492503
abstract static class QuotaProjectIdTestBehavior {
493504
// A null user specified quota represents the use case where user omits specifying quota

0 commit comments

Comments
 (0)