|
| 1 | +package com.exceptionless.exceptionlessclient.configuration; |
| 2 | + |
| 3 | +import com.exceptionless.exceptionlessclient.TestFixtures; |
| 4 | +import com.exceptionless.exceptionlessclient.exceptions.InvalidApiKeyException; |
| 5 | +import com.exceptionless.exceptionlessclient.logging.LogCapturerIF; |
| 6 | +import com.exceptionless.exceptionlessclient.models.EventPluginContext; |
| 7 | +import com.exceptionless.exceptionlessclient.models.UserInfo; |
| 8 | +import com.exceptionless.exceptionlessclient.models.enums.EventPropertyKey; |
| 9 | +import com.exceptionless.exceptionlessclient.models.settings.ServerSettings; |
| 10 | +import com.exceptionless.exceptionlessclient.plugins.EventPluginIF; |
| 11 | +import com.exceptionless.exceptionlessclient.storage.InMemoryStorage; |
| 12 | +import com.exceptionless.exceptionlessclient.storage.InMemoryStorageProvider; |
| 13 | +import com.exceptionless.exceptionlessclient.submission.DefaultSubmissionClient; |
| 14 | +import org.junit.jupiter.api.BeforeEach; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 17 | +import org.mockito.Mock; |
| 18 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 19 | +import org.slf4j.Logger; |
| 20 | +import org.slf4j.LoggerFactory; |
| 21 | + |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.Set; |
| 25 | +import java.util.function.Consumer; |
| 26 | +import java.util.stream.Collectors; |
| 27 | + |
| 28 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 29 | +import static org.assertj.core.api.Java6Assertions.assertThat; |
| 30 | +import static org.mockito.Mockito.*; |
| 31 | + |
| 32 | +@ExtendWith(MockitoExtension.class) |
| 33 | +public class ConfigurationManagerTest { |
| 34 | + private static final Logger LOG = LoggerFactory.getLogger(ConfigurationManagerTest.class); |
| 35 | + |
| 36 | + @Mock private InMemoryStorageProvider storageProvider; |
| 37 | + @Mock private DefaultSubmissionClient submissionClient; |
| 38 | + @Mock private Consumer<ConfigurationManager> handler; |
| 39 | + @Mock private LogCapturerIF logCapturer; |
| 40 | + private ConfigurationManager configurationManager; |
| 41 | + private InMemoryStorage<ServerSettings> storage; |
| 42 | + |
| 43 | + @BeforeEach |
| 44 | + public void setup() { |
| 45 | + storage = InMemoryStorage.<ServerSettings>builder().build(); |
| 46 | + configurationManager = |
| 47 | + TestFixtures.aDefaultConfigurationManager() |
| 48 | + .storageProvider(storageProvider) |
| 49 | + .submissionClient(submissionClient) |
| 50 | + .build(); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void itThrowsInvalidApiKeyExceptionForInvalidApiKeys() { |
| 55 | + assertThatThrownBy( |
| 56 | + () -> |
| 57 | + TestFixtures.aDefaultConfigurationManager() |
| 58 | + .configuration(Configuration.builder().apiKey("xxx").build()) |
| 59 | + .build()) |
| 60 | + .isInstanceOf(InvalidApiKeyException.class) |
| 61 | + .hasMessage("Apikey is not valid: [xxx]"); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void itCanAddALogCapturer() { |
| 66 | + configurationManager = |
| 67 | + TestFixtures.aDefaultConfigurationManager().logCatpurer(logCapturer).build(); |
| 68 | + // trace is disabled by default |
| 69 | + LOG.debug("debug message"); |
| 70 | + LOG.info("info message"); |
| 71 | + LOG.warn("warn message"); |
| 72 | + LOG.error("error message"); |
| 73 | + Exception e = new RuntimeException("test"); |
| 74 | + LOG.error("exception message", e); |
| 75 | + |
| 76 | + verify(logCapturer, times(1)).debug("debug message"); |
| 77 | + verify(logCapturer, times(1)).info("info message"); |
| 78 | + verify(logCapturer, times(1)).warn("warn message"); |
| 79 | + verify(logCapturer, times(1)).error("error message"); |
| 80 | + verify(logCapturer, times(1)).error("exception message", e); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void itCanAddDefaultTags() { |
| 85 | + configurationManager.addDefaultTags("tag1", "tag2"); |
| 86 | + |
| 87 | + assertThat(configurationManager.getDefaultTags()).isEqualTo(Set.of("tag1", "tag2")); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void itCanAddDataExclusions() { |
| 92 | + doReturn(storage).when(storageProvider).getSettings(); |
| 93 | + storage.save( |
| 94 | + ServerSettings.builder() |
| 95 | + .version(1L) |
| 96 | + .settings(Map.of("@@DataExclusions", "exclusion1,exclusion2")) |
| 97 | + .build()); |
| 98 | + |
| 99 | + configurationManager.addDataExclusions("exclusion3", "exclusion4"); |
| 100 | + |
| 101 | + assertThat(configurationManager.getDataExclusions()) |
| 102 | + .isEqualTo(Set.of("exclusion1", "exclusion2", "exclusion3", "exclusion4")); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void itCanAddUserAgentBotPatterns() { |
| 107 | + doReturn(storage).when(storageProvider).getSettings(); |
| 108 | + storage.save( |
| 109 | + ServerSettings.builder() |
| 110 | + .version(1L) |
| 111 | + .settings(Map.of("@@UserAgentBotPatterns", "pattern1,pattern2")) |
| 112 | + .build()); |
| 113 | + |
| 114 | + configurationManager.addUserAgentBotPatterns("pattern3", "pattern4"); |
| 115 | + |
| 116 | + assertThat(configurationManager.getUserAgentBotPatterns()) |
| 117 | + .isEqualTo(Set.of("pattern1", "pattern2", "pattern3", "pattern4")); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void itCanSubmitSessionHeartBeat() { |
| 122 | + configurationManager.submitSessionHeartbeat("test-user-id"); |
| 123 | + |
| 124 | + verify(submissionClient, times(1)).sendHeartBeat("test-user-id", false); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void itCanAddAndRemovePlugins() { |
| 129 | + configurationManager.addPlugin( |
| 130 | + new EventPluginIF() { |
| 131 | + @Override |
| 132 | + public int getPriority() { |
| 133 | + return Integer.MAX_VALUE; |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public String getName() { |
| 138 | + return "testPlugin"; |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public void run( |
| 143 | + EventPluginContext eventPluginContext, ConfigurationManager configurationManager) {} |
| 144 | + }); |
| 145 | + |
| 146 | + assertThat( |
| 147 | + configurationManager.getPlugins().stream() |
| 148 | + .anyMatch(plugin -> plugin.getName().equals("testPlugin"))) |
| 149 | + .isTrue(); |
| 150 | + |
| 151 | + configurationManager.removePlugin("testPlugin"); |
| 152 | + |
| 153 | + assertThat( |
| 154 | + configurationManager.getPlugins().stream() |
| 155 | + .anyMatch(plugin -> plugin.getName().equals("testPlugin"))) |
| 156 | + .isFalse(); |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + public void itCanAddVersionInDefaultData() { |
| 161 | + configurationManager.setVersion("123"); |
| 162 | + |
| 163 | + assertThat(configurationManager.getDefaultData()) |
| 164 | + .isEqualTo(Map.of(EventPropertyKey.VERSION.value(), "123")); |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + public void itCanAddAndRemoveUserIdentityInDefaultData() { |
| 169 | + UserInfo userInfo = UserInfo.builder().identity("test-identity").name("test-name").build(); |
| 170 | + |
| 171 | + configurationManager.setUserIdentity(userInfo); |
| 172 | + |
| 173 | + assertThat(configurationManager.getDefaultData()) |
| 174 | + .isEqualTo(Map.of(EventPropertyKey.USER.value(), userInfo)); |
| 175 | + |
| 176 | + configurationManager.removeUserIdentity(); |
| 177 | + |
| 178 | + assertThat(configurationManager.getDefaultData()).isEqualTo(Map.of()); |
| 179 | + } |
| 180 | + |
| 181 | + @Test |
| 182 | + public void itCanUseSessions() { |
| 183 | + configurationManager.useSessions(); |
| 184 | + List<EventPluginIF> plugins = |
| 185 | + configurationManager.getPlugins().stream() |
| 186 | + .filter(plugin -> plugin.getName().contains("HeartbeatPlugin")) |
| 187 | + .collect(Collectors.toList()); |
| 188 | + |
| 189 | + assertThat(plugins).hasSize(1); |
| 190 | + } |
| 191 | + |
| 192 | + @Test |
| 193 | + public void itCanDetectChanges() { |
| 194 | + configurationManager.onChanged(handler); |
| 195 | + configurationManager.getConfiguration().setApiKey("test-api-key"); |
| 196 | + |
| 197 | + verify(handler, times(1)).accept(configurationManager); |
| 198 | + } |
| 199 | +} |
0 commit comments