|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.exporter.otlp.profiles; |
| 7 | + |
| 8 | +import static org.assertj.core.api.Assertions.assertThat; |
| 9 | +import static org.mockito.ArgumentMatchers.same; |
| 10 | +import static org.mockito.Mockito.when; |
| 11 | + |
| 12 | +import io.opentelemetry.internal.testing.slf4j.SuppressLogger; |
| 13 | +import io.opentelemetry.sdk.common.CompletableResultCode; |
| 14 | +import java.util.Arrays; |
| 15 | +import java.util.Collections; |
| 16 | +import java.util.List; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 19 | +import org.mockito.ArgumentMatchers; |
| 20 | +import org.mockito.Mock; |
| 21 | +import org.mockito.Mockito; |
| 22 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 23 | + |
| 24 | +/** Unit tests for {@link MultiProfilesExporter}. */ |
| 25 | +@ExtendWith(MockitoExtension.class) |
| 26 | +public class MultiProfilesExporterTest { |
| 27 | + |
| 28 | + @Mock private ProfilesExporter profilesExporter1; |
| 29 | + @Mock private ProfilesExporter profilesExporter2; |
| 30 | + |
| 31 | + private static final List<ProfileData> PROFILE_LIST = |
| 32 | + Collections.singletonList(FakeTelemetryUtil.generateFakeProfileData()); |
| 33 | + |
| 34 | + @Test |
| 35 | + void empty() { |
| 36 | + ProfilesExporter multiProfilesExporter = ProfilesExporter.composite(Collections.emptyList()); |
| 37 | + multiProfilesExporter.export(PROFILE_LIST); |
| 38 | + multiProfilesExporter.shutdown(); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + void oneProfilesExporter() { |
| 43 | + ProfilesExporter multiProfilesExporter = |
| 44 | + ProfilesExporter.composite(Collections.singletonList(profilesExporter1)); |
| 45 | + |
| 46 | + when(profilesExporter1.export(same(PROFILE_LIST))) |
| 47 | + .thenReturn(CompletableResultCode.ofSuccess()); |
| 48 | + assertThat(multiProfilesExporter.export(PROFILE_LIST).isSuccess()).isTrue(); |
| 49 | + Mockito.verify(profilesExporter1).export(same(PROFILE_LIST)); |
| 50 | + |
| 51 | + when(profilesExporter1.flush()).thenReturn(CompletableResultCode.ofSuccess()); |
| 52 | + assertThat(multiProfilesExporter.flush().isSuccess()).isTrue(); |
| 53 | + Mockito.verify(profilesExporter1).flush(); |
| 54 | + |
| 55 | + when(profilesExporter1.shutdown()).thenReturn(CompletableResultCode.ofSuccess()); |
| 56 | + multiProfilesExporter.shutdown(); |
| 57 | + Mockito.verify(profilesExporter1).shutdown(); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void twoProfilesExporter() { |
| 62 | + ProfilesExporter multiProfilesExporter = |
| 63 | + ProfilesExporter.composite(Arrays.asList(profilesExporter1, profilesExporter2)); |
| 64 | + |
| 65 | + when(profilesExporter1.export(same(PROFILE_LIST))) |
| 66 | + .thenReturn(CompletableResultCode.ofSuccess()); |
| 67 | + when(profilesExporter2.export(same(PROFILE_LIST))) |
| 68 | + .thenReturn(CompletableResultCode.ofSuccess()); |
| 69 | + assertThat(multiProfilesExporter.export(PROFILE_LIST).isSuccess()).isTrue(); |
| 70 | + Mockito.verify(profilesExporter1).export(same(PROFILE_LIST)); |
| 71 | + Mockito.verify(profilesExporter2).export(same(PROFILE_LIST)); |
| 72 | + |
| 73 | + when(profilesExporter1.flush()).thenReturn(CompletableResultCode.ofSuccess()); |
| 74 | + when(profilesExporter2.flush()).thenReturn(CompletableResultCode.ofSuccess()); |
| 75 | + assertThat(multiProfilesExporter.flush().isSuccess()).isTrue(); |
| 76 | + Mockito.verify(profilesExporter1).flush(); |
| 77 | + Mockito.verify(profilesExporter2).flush(); |
| 78 | + |
| 79 | + when(profilesExporter1.shutdown()).thenReturn(CompletableResultCode.ofSuccess()); |
| 80 | + when(profilesExporter2.shutdown()).thenReturn(CompletableResultCode.ofSuccess()); |
| 81 | + multiProfilesExporter.shutdown(); |
| 82 | + Mockito.verify(profilesExporter1).shutdown(); |
| 83 | + Mockito.verify(profilesExporter2).shutdown(); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void twoProfilesExporter_OneReturnFailure() { |
| 88 | + ProfilesExporter multiProfilesExporter = |
| 89 | + ProfilesExporter.composite(Arrays.asList(profilesExporter1, profilesExporter2)); |
| 90 | + |
| 91 | + when(profilesExporter1.export(same(PROFILE_LIST))) |
| 92 | + .thenReturn(CompletableResultCode.ofSuccess()); |
| 93 | + when(profilesExporter2.export(same(PROFILE_LIST))) |
| 94 | + .thenReturn(CompletableResultCode.ofFailure()); |
| 95 | + assertThat(multiProfilesExporter.export(PROFILE_LIST).isSuccess()).isFalse(); |
| 96 | + Mockito.verify(profilesExporter1).export(same(PROFILE_LIST)); |
| 97 | + Mockito.verify(profilesExporter2).export(same(PROFILE_LIST)); |
| 98 | + |
| 99 | + when(profilesExporter1.flush()).thenReturn(CompletableResultCode.ofSuccess()); |
| 100 | + when(profilesExporter2.flush()).thenReturn(CompletableResultCode.ofFailure()); |
| 101 | + assertThat(multiProfilesExporter.flush().isSuccess()).isFalse(); |
| 102 | + Mockito.verify(profilesExporter1).flush(); |
| 103 | + Mockito.verify(profilesExporter2).flush(); |
| 104 | + |
| 105 | + when(profilesExporter1.shutdown()).thenReturn(CompletableResultCode.ofSuccess()); |
| 106 | + when(profilesExporter2.shutdown()).thenReturn(CompletableResultCode.ofFailure()); |
| 107 | + assertThat(multiProfilesExporter.shutdown().isSuccess()).isFalse(); |
| 108 | + Mockito.verify(profilesExporter1).shutdown(); |
| 109 | + Mockito.verify(profilesExporter2).shutdown(); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + @SuppressLogger(MultiProfilesExporter.class) |
| 114 | + void twoProfilesExporter_FirstThrows() { |
| 115 | + ProfilesExporter multiProfilesExporter = |
| 116 | + ProfilesExporter.composite(Arrays.asList(profilesExporter1, profilesExporter2)); |
| 117 | + |
| 118 | + Mockito.doThrow(new IllegalArgumentException("No export for you.")) |
| 119 | + .when(profilesExporter1) |
| 120 | + .export(ArgumentMatchers.anyList()); |
| 121 | + when(profilesExporter2.export(same(PROFILE_LIST))) |
| 122 | + .thenReturn(CompletableResultCode.ofSuccess()); |
| 123 | + assertThat(multiProfilesExporter.export(PROFILE_LIST).isSuccess()).isFalse(); |
| 124 | + Mockito.verify(profilesExporter1).export(same(PROFILE_LIST)); |
| 125 | + Mockito.verify(profilesExporter2).export(same(PROFILE_LIST)); |
| 126 | + |
| 127 | + Mockito.doThrow(new IllegalArgumentException("No flush for you.")) |
| 128 | + .when(profilesExporter1) |
| 129 | + .flush(); |
| 130 | + when(profilesExporter2.flush()).thenReturn(CompletableResultCode.ofSuccess()); |
| 131 | + assertThat(multiProfilesExporter.flush().isSuccess()).isFalse(); |
| 132 | + Mockito.verify(profilesExporter1).flush(); |
| 133 | + Mockito.verify(profilesExporter2).flush(); |
| 134 | + |
| 135 | + Mockito.doThrow(new IllegalArgumentException("No shutdown for you.")) |
| 136 | + .when(profilesExporter1) |
| 137 | + .shutdown(); |
| 138 | + when(profilesExporter2.shutdown()).thenReturn(CompletableResultCode.ofSuccess()); |
| 139 | + assertThat(multiProfilesExporter.shutdown().isSuccess()).isFalse(); |
| 140 | + Mockito.verify(profilesExporter1).shutdown(); |
| 141 | + Mockito.verify(profilesExporter2).shutdown(); |
| 142 | + } |
| 143 | +} |
0 commit comments