Skip to content

Commit 3dd89cd

Browse files
committed
Added unit tests for configuration package
1 parent 0ec532b commit 3dd89cd

File tree

12 files changed

+469
-33
lines changed

12 files changed

+469
-33
lines changed

src/main/java/com/exceptionless/exceptionlessclient/configuration/Configuration.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ public class Configuration {
1111

1212
@Getter private String apiKey;
1313
@Getter private String serverUrl;
14-
@Getter private String configServerUrl;
1514
@Getter private String heartbeatServerUrl;
1615
@Getter private Long updateSettingsWhenIdleInterval;
1716
@Getter private Integer submissionBatchSize;
@@ -31,10 +30,6 @@ public Configuration(
3130
Integer settingsClientTimeoutInMillis) {
3231
this.apiKey = apiKey;
3332
this.serverUrl = serverUrl == null ? "https://collector.exceptionless.io" : serverUrl;
34-
this.configServerUrl =
35-
configServerUrl == null
36-
? (serverUrl == null ? "https://config.exceptionless.io" : serverUrl)
37-
: configServerUrl;
3833
this.heartbeatServerUrl =
3934
heartbeatServerUrl == null
4035
? (serverUrl == null ? "https://heartbeat.exceptionless.io" : serverUrl)
@@ -69,11 +64,6 @@ public void setServerUrl(String serverUrl) {
6964
propertyChangeSupport.firePropertyChange("serverUrl", prevValue, serverUrl);
7065
}
7166

72-
public void setConfigServerUrl(String configServerUrl) {
73-
String prevValue = this.configServerUrl;
74-
this.configServerUrl = configServerUrl;
75-
propertyChangeSupport.firePropertyChange("configServerUrl", prevValue, configServerUrl);
76-
}
7767

7868
public void setHeartbeatServerUrl(String heartbeatServerUrl) {
7969
String prevValue = this.heartbeatServerUrl;

src/main/java/com/exceptionless/exceptionlessclient/configuration/ConfigurationManager.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.exceptionless.exceptionlessclient.configuration;
22

3-
import com.exceptionless.exceptionlessclient.exceptions.SubmissionException;
3+
import ch.qos.logback.core.Context;
4+
import com.exceptionless.exceptionlessclient.exceptions.InvalidApiKeyException;
45
import com.exceptionless.exceptionlessclient.lastreferenceidmanager.DefaultLastReferenceIdManager;
56
import com.exceptionless.exceptionlessclient.lastreferenceidmanager.LastReferenceIdManagerIF;
67
import com.exceptionless.exceptionlessclient.logging.LogCapturerAppender;
@@ -134,7 +135,7 @@ private void checkApiKeyIsValid() {
134135
return;
135136
}
136137

137-
throw new SubmissionException(
138+
throw new InvalidApiKeyException(
138139
String.format("Apikey is not valid: [%s]", this.configuration.getApiKey()));
139140
}
140141

@@ -143,7 +144,10 @@ private void addLogCapturer(LogCapturerIF logCatpurer) {
143144

144145
ch.qos.logback.classic.Logger logBackRootLogger =
145146
(ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
146-
logBackRootLogger.addAppender(LogCapturerAppender.builder().logCapturer(logCatpurer).build());
147+
LogCapturerAppender appender = LogCapturerAppender.builder().logCapturer(logCatpurer).build();
148+
appender.setContext((Context) LoggerFactory.getILoggerFactory());
149+
appender.start();
150+
logBackRootLogger.addAppender(appender);
147151
}
148152

149153
public void addDefaultTags(String... tags) {
@@ -212,6 +216,10 @@ public void removePlugin(String name) {
212216
pluginManager.removePlugin(name);
213217
}
214218

219+
public List<EventPluginIF> getPlugins() {
220+
return pluginManager.getPlugins();
221+
}
222+
215223
public void setVersion(String version) {
216224
this.defaultData.put(EventPropertyKey.VERSION.value(), version);
217225
}
@@ -229,7 +237,7 @@ public void setUserIdentity(UserInfo userInfo) {
229237
}
230238

231239
public void useSessions() {
232-
useSessions(30000);
240+
useSessions(30);
233241
}
234242

235243
public void useSessions(int heartbeatIntervalInSecs) {
@@ -249,8 +257,4 @@ private void changed() {
249257
}
250258
}
251259
}
252-
253-
public List<EventPluginIF> getPlugins() {
254-
return pluginManager.getPlugins();
255-
}
256260
}

src/main/java/com/exceptionless/exceptionlessclient/configuration/PrivateInformationInclusions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void applyToAll(Boolean include) {
4949
public void setQueryString(Boolean queryString) {
5050
Boolean prevValue = this.queryString;
5151
this.queryString = queryString;
52-
propertyChangeSupport.firePropertyChange("querString", prevValue, queryString);
52+
propertyChangeSupport.firePropertyChange("queryString", prevValue, queryString);
5353
}
5454

5555
public void setPostData(Boolean postData) {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.exceptionless.exceptionlessclient.exceptions;
2+
3+
public class InvalidApiKeyException extends RuntimeException {
4+
public InvalidApiKeyException(String message) {
5+
super(message);
6+
}
7+
}

src/main/java/com/exceptionless/exceptionlessclient/logging/LogCapturerAppender.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public LogCapturerAppender(LogCapturerIF logCapturer) {
1919
protected void append(ILoggingEvent loggingEvent) {
2020
if (loggingEvent.getLevel().equals(Level.TRACE)) {
2121
logCapturer.trace(loggingEvent.getMessage());
22+
} else if (loggingEvent.getLevel().equals(Level.DEBUG)) {
23+
logCapturer.debug(loggingEvent.getMessage());
2224
} else if (loggingEvent.getLevel().equals(Level.INFO)) {
2325
logCapturer.info(loggingEvent.getMessage());
2426
} else if (loggingEvent.getLevel().equals(Level.WARN)) {

src/main/java/com/exceptionless/exceptionlessclient/logging/LogCapturerIF.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
public interface LogCapturerIF {
44
void trace(String message);
55

6+
void debug(String message);
7+
68
void info(String message);
79

810
void warn(String message);

src/main/java/com/exceptionless/exceptionlessclient/logging/NullLogCapturer.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ public NullLogCapturer() {}
99
@Override
1010
public void trace(String message) {}
1111

12+
@Override
13+
public void debug(String message) {}
14+
1215
@Override
1316
public void info(String message) {}
1417

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
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

Comments
 (0)