Skip to content

Commit f50c60a

Browse files
authored
Merge pull request #16 from exceptionless/testing
Added unit tests for settings package
2 parents 5b8c5ee + 6db3253 commit f50c60a

File tree

7 files changed

+300
-21
lines changed

7 files changed

+300
-21
lines changed

src/main/java/com/exceptionless/exceptionlessclient/models/settings/ServerSettings.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,26 @@
22

33
import com.exceptionless.exceptionlessclient.models.enums.ServerSettingKey;
44
import com.exceptionless.exceptionlessclient.utils.Utils;
5+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
6+
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
57
import lombok.Builder;
68
import lombok.Value;
79
import lombok.experimental.NonFinal;
810

911
import java.util.*;
1012
import java.util.stream.Collectors;
1113

12-
@Builder
14+
@JsonDeserialize(builder = ServerSettings.ServerSettingsBuilder.class)
15+
@Builder(builderClassName = "ServerSettingsBuilder")
1316
@Value
1417
@NonFinal
1518
public class ServerSettings {
16-
long version;
17-
@Builder.Default
18-
Map<String, String> settings = new HashMap<>();
19+
Long version;
20+
@Builder.Default Map<String, String> settings = new HashMap<>();
21+
22+
// Required for Jackson to work with Lombok Immutable(@Value + @Builder)
23+
@JsonPOJOBuilder(withPrefix = "")
24+
public static class ServerSettingsBuilder {}
1925

2026
public Optional<String> getTypeAndSourceSetting(String type, String source) {
2127
String prefix = String.format("@@%s", type);

src/main/java/com/exceptionless/exceptionlessclient/models/submission/SettingsResponse.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@
99
@Value
1010
@NonFinal
1111
public class SettingsResponse {
12-
private boolean success;
13-
private ServerSettings settings;
14-
private Exception exception;
15-
private String message;
12+
@Builder.Default Boolean success = false;
13+
ServerSettings settings;
14+
Exception exception;
15+
String message;
16+
17+
public Boolean isSuccess() {
18+
return success;
19+
}
1620
}

src/main/java/com/exceptionless/exceptionlessclient/settings/DefaultSettingsClient.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,16 @@
44
import com.exceptionless.exceptionlessclient.models.settings.ServerSettings;
55
import com.exceptionless.exceptionlessclient.models.submission.SettingsResponse;
66
import com.exceptionless.exceptionlessclient.utils.Utils;
7+
import com.exceptionless.exceptionlessclient.utils.VisibleForTesting;
78
import com.fasterxml.jackson.core.type.TypeReference;
89
import lombok.Builder;
910

10-
import java.io.IOException;
1111
import java.net.URI;
12-
import java.net.URISyntaxException;
1312
import java.net.http.HttpClient;
1413
import java.net.http.HttpRequest;
1514
import java.net.http.HttpResponse;
1615
import java.time.Duration;
1716

18-
1917
public class DefaultSettingsClient implements SettingsClientIF {
2018
private final Configuration configuration;
2119
private final HttpClient httpClient;
@@ -26,14 +24,20 @@ public DefaultSettingsClient(Configuration configuration) {
2624
this.httpClient = HttpClient.newHttpClient();
2725
}
2826

27+
@VisibleForTesting
28+
DefaultSettingsClient(Configuration configuration, HttpClient httpClient) {
29+
this.configuration = configuration;
30+
this.httpClient = httpClient;
31+
}
32+
2933
@Override
3034
public SettingsResponse getSettings(long version) {
3135
try {
3236
URI uri =
3337
new URI(
3438
String.format(
3539
"%s/api/v2/projects/config?v=%s&access_token=%s",
36-
version, configuration.getServerUrl(), configuration.getApiKey()));
40+
configuration.getServerUrl(), version, configuration.getApiKey()));
3741

3842
HttpRequest request =
3943
HttpRequest.newBuilder()
@@ -54,7 +58,7 @@ public SettingsResponse getSettings(long version) {
5458
Utils.JSON_MAPPER.readValue(response.body(), new TypeReference<ServerSettings>() {});
5559

5660
return SettingsResponse.builder().success(true).settings(serverSettings).build();
57-
} catch (URISyntaxException | InterruptedException | IOException e) {
61+
} catch (Exception e) {
5862
return SettingsResponse.builder().success(false).exception(e).message(e.getMessage()).build();
5963
}
6064
}

src/main/java/com/exceptionless/exceptionlessclient/settings/SettingsManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ public void updateSettings() {
7171

7272
SettingsResponse response = settingsClient.getSettings(currentVersion);
7373
if (!response.isSuccess()) {
74-
LOG.warn(String.format("Unable to update settings: %s:", response.getMessage()));
74+
LOG.warn(String.format("Unable to update settings: %s", response.getMessage()));
7575
return;
7676
}
77-
ServerSettings prevValue = storageProvider.getSettings().peek().getValue();
77+
ServerSettings prevValue = getSavedServerSettings();
7878
storageProvider.getSettings().save(response.getSettings());
7979
propertyChangeSupport.firePropertyChange("settings", prevValue, response.getSettings());
8080
} finally {

src/main/java/com/exceptionless/exceptionlessclient/utils/Utils.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,16 @@ public static Map<String, List<String>> getQueryParams(URI uri) {
6161

6262
public static boolean match(String value, String pattern) {
6363
if (value == null) {
64-
// todo remove
65-
LOG.info("Null received for value");
6664
return false;
6765
}
6866
if (pattern == null) {
69-
// todo remove
70-
LOG.info("Null received for pattern");
7167
return false;
7268
}
7369
// todo check this works or not;
7470
boolean result = value.toLowerCase().matches(pattern);
71+
7572
if (result) {
76-
// todo remove
77-
LOG.info(String.format("Value [%s] matches pattern [%s]", value, pattern));
73+
LOG.trace(String.format("Value [%s] matches pattern [%s]", value, pattern));
7874
return true;
7975
}
8076
return false;
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.exceptionless.exceptionlessclient.settings;
2+
3+
import com.exceptionless.exceptionlessclient.TestFixtures;
4+
import com.exceptionless.exceptionlessclient.configuration.Configuration;
5+
import com.exceptionless.exceptionlessclient.models.settings.ServerSettings;
6+
import com.exceptionless.exceptionlessclient.models.submission.SettingsResponse;
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.api.extension.ExtendWith;
10+
import org.mockito.Mock;
11+
import org.mockito.junit.jupiter.MockitoExtension;
12+
13+
import java.io.IOException;
14+
import java.net.http.HttpClient;
15+
import java.net.http.HttpResponse;
16+
import java.time.Duration;
17+
import java.util.Map;
18+
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.mockito.ArgumentMatchers.any;
21+
import static org.mockito.ArgumentMatchers.argThat;
22+
import static org.mockito.Mockito.doReturn;
23+
import static org.mockito.Mockito.doThrow;
24+
25+
@ExtendWith(MockitoExtension.class)
26+
public class DefaultSettingsClientTest {
27+
@Mock private HttpClient httpClient;
28+
@Mock private HttpResponse<String> response;
29+
private DefaultSettingsClient settingsClient;
30+
31+
@BeforeEach
32+
public void setup() {
33+
Configuration configuration =
34+
TestFixtures.aDefaultConfiguration()
35+
.serverUrl("http://test-server-url")
36+
.apiKey("test-api-key")
37+
.settingsClientTimeoutInMillis(10)
38+
.build();
39+
settingsClient = new DefaultSettingsClient(configuration, httpClient);
40+
}
41+
42+
@Test
43+
public void itCanHandleASuccessfulResponse() throws IOException, InterruptedException {
44+
doReturn(response)
45+
.when(httpClient)
46+
.send(
47+
argThat(
48+
httpRequest ->
49+
httpRequest.method().equals("GET")
50+
&& httpRequest.timeout().isPresent()
51+
&& httpRequest.timeout().get().equals(Duration.ofMillis(10))
52+
&& httpRequest.headers().firstValue("X-Exceptionless-Client").isPresent()
53+
&& httpRequest
54+
.headers()
55+
.firstValue("X-Exceptionless-Client")
56+
.get()
57+
.equals("exceptionless-java")
58+
&& httpRequest
59+
.uri()
60+
.toString()
61+
.equals(
62+
"http://test-server-url/api/v2/projects/config?v=1&access_token=test-api-key")),
63+
any());
64+
doReturn(
65+
"{\n"
66+
+ "\t\"version\":1,\n"
67+
+ "\t\"settings\":{\n"
68+
+ "\t\t\"key\":\"value\"\n"
69+
+ "\t}\n"
70+
+ "}")
71+
.when(response)
72+
.body();
73+
doReturn(200).when(response).statusCode();
74+
75+
SettingsResponse response = settingsClient.getSettings(1);
76+
77+
assertThat(response.getMessage()).isNull();
78+
assertThat(response.getException()).isNull();
79+
assertThat(response.isSuccess()).isTrue();
80+
assertThat(response.getSettings())
81+
.isEqualTo(ServerSettings.builder().version(1L).settings(Map.of("key", "value")).build());
82+
}
83+
84+
@Test
85+
public void itCanHandleAUnsuccessfulResponse() throws IOException, InterruptedException {
86+
doReturn(response).when(httpClient).send(any(), any());
87+
doReturn("test-response").when(response).body();
88+
doReturn(400).when(response).statusCode();
89+
90+
SettingsResponse response = settingsClient.getSettings(1);
91+
92+
assertThat(response.getMessage()).isEqualTo("test-response");
93+
assertThat(response.getException()).isNull();
94+
assertThat(response.isSuccess()).isFalse();
95+
assertThat(response.getSettings()).isNull();
96+
}
97+
98+
@Test
99+
public void itCanHandleAnyException() throws IOException, InterruptedException {
100+
RuntimeException e = new RuntimeException("test");
101+
doThrow(e).when(httpClient).send(any(), any());
102+
103+
SettingsResponse response = settingsClient.getSettings(1);
104+
105+
assertThat(response.getMessage()).isEqualTo("test");
106+
assertThat(response.getException()).isEqualTo(e);
107+
assertThat(response.isSuccess()).isFalse();
108+
assertThat(response.getSettings()).isNull();
109+
}
110+
}

0 commit comments

Comments
 (0)