Skip to content

Commit 5b8c5ee

Browse files
authored
Merge pull request #15 from exceptionless/testing
Added unit tests for services package
2 parents 651cbe9 + b6fbfde commit 5b8c5ee

File tree

10 files changed

+273
-37
lines changed

10 files changed

+273
-37
lines changed

src/main/java/com/exceptionless/exceptionlessclient/models/services/EnvironmentInfo.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@
1111
@NonFinal
1212
@EqualsAndHashCode(callSuper = true)
1313
public class EnvironmentInfo extends Model {
14-
private int processorCount;
15-
private long totalPhysicalMemory;
16-
private long availablePhysicalMemory;
17-
private String commandLine;
18-
private String processName;
19-
private String processId;
20-
private long processMemorySize;
21-
private String threadid;
22-
private String architecture;
23-
private String osName;
24-
private String osVersion;
25-
private String ipAddress;
26-
private String machineName;
27-
private String installId;
28-
private String runtimeVersion;
14+
Integer processorCount;
15+
Long totalPhysicalMemory;
16+
Long availablePhysicalMemory;
17+
String commandLine;
18+
String processName;
19+
String processId;
20+
Long processMemorySize;
21+
String threadId;
22+
String architecture;
23+
String osName;
24+
String osVersion;
25+
String ipAddress;
26+
String machineName;
27+
String installId;
28+
String runtimeVersion;
2929
}
Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.exceptionless.exceptionlessclient.models.services;
22

33
import com.exceptionless.exceptionlessclient.models.base.Model;
4+
import lombok.Builder;
45
import lombok.EqualsAndHashCode;
56
import lombok.Value;
67
import lombok.experimental.NonFinal;
78
import lombok.experimental.SuperBuilder;
89

10+
import java.util.HashMap;
911
import java.util.List;
1012
import java.util.Map;
1113

@@ -14,15 +16,19 @@
1416
@NonFinal
1517
@EqualsAndHashCode(callSuper = true)
1618
public class RequestInfo extends Model {
17-
private String userAgent;
18-
private String httpMethod;
19-
private boolean isSecure;
20-
private String host;
21-
private int port;
22-
private String path;
23-
private String referrer;
24-
private String clientIpAddress;
25-
private Map<String, String> cookies;
26-
private Object postData;
27-
private Map<String, List<String>> queryString;
19+
String userAgent;
20+
String httpMethod;
21+
Boolean secure;
22+
String host;
23+
Integer port;
24+
String path;
25+
String referrer;
26+
String clientIpAddress;
27+
@Builder.Default Map<String, String> cookies = new HashMap<>();
28+
Object postData;
29+
@Builder.Default Map<String, List<String>> queryString = new HashMap<>();
30+
31+
public Boolean isSecure() {
32+
return secure;
33+
}
2834
}

src/main/java/com/exceptionless/exceptionlessclient/services/DefaultEnvironmentInfoCollector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public EnvironmentInfo getEnvironmentInfo(EnvironmentInfoGetArgs args) {
5454
.processName(defaultEnvironmentInfo.getProcessName())
5555
.processId(defaultEnvironmentInfo.getProcessId())
5656
.processMemorySize(ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed())
57-
.threadid(String.valueOf(Thread.currentThread().getId()))
57+
.threadId(String.valueOf(Thread.currentThread().getId()))
5858
.architecture(defaultEnvironmentInfo.getArchitecture())
5959
.osName(defaultEnvironmentInfo.getOsName())
6060
.osVersion(defaultEnvironmentInfo.getOsVersion())

src/main/java/com/exceptionless/exceptionlessclient/services/DefaultRequestInfoCollector.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public DefaultRequestInfoCollector() {}
2424
public RequestInfo getRequestInfo(HttpRequest request, RequestInfoGetArgs args) {
2525
RequestInfo.RequestInfoBuilder<?, ?> builder =
2626
RequestInfo.builder()
27-
.userAgent(request.headers().firstValue("user-agent").orElse(null))
28-
.isSecure(isSecure(request.uri()))
27+
.userAgent(request.headers().firstValue("User-Agent").orElse(null))
28+
.secure(isSecure(request.uri()))
2929
.httpMethod(request.method())
3030
.host(request.uri().getHost())
3131
.path(request.uri().getPath())
@@ -59,7 +59,7 @@ private <X> Map<String, X> filterExclusions(Map<String, X> map, Set<String> excl
5959
return map.entrySet().stream()
6060
.filter(
6161
entry ->
62-
exclusions.stream().anyMatch(exclusion -> !Utils.match(entry.getKey(), exclusion)))
62+
exclusions.stream().noneMatch(exclusion -> Utils.match(entry.getKey(), exclusion)))
6363
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
6464
}
6565

src/main/java/com/exceptionless/exceptionlessclient/services/EnvironmentInfoGetArgs.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
@Value
99
@NonFinal
1010
public class EnvironmentInfoGetArgs {
11-
private boolean includeMachineName;
12-
private boolean includeIpAddress;
11+
@Builder.Default Boolean includeMachineName = false;
12+
@Builder.Default Boolean includeIpAddress = false;
13+
14+
public Boolean isIncludeMachineName() {
15+
return includeIpAddress;
16+
}
17+
18+
public Boolean isIncludeIpAddress() {
19+
return includeIpAddress;
20+
}
1321
}

src/main/java/com/exceptionless/exceptionlessclient/services/RequestInfoGetArgs.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,28 @@
44
import lombok.Value;
55
import lombok.experimental.NonFinal;
66

7+
import java.util.HashSet;
78
import java.util.Set;
89

910
@Builder
1011
@Value
1112
@NonFinal
1213
public class RequestInfoGetArgs {
13-
private boolean includeIpAddress;
14-
private boolean includeCookies;
15-
private boolean includeQueryString;
16-
private boolean includePostData;
17-
private Set<String> exclusions;
14+
@Builder.Default Boolean includeIpAddress = false;
15+
@Builder.Default Boolean includeCookies = false;
16+
@Builder.Default Boolean includeQueryString = false;
17+
@Builder.Default Boolean includePostData = false;
18+
@Builder.Default Set<String> exclusions = new HashSet<>();
19+
20+
public Boolean isIncludeIpAddress() {
21+
return includeIpAddress;
22+
}
23+
24+
public Boolean isIncludeCookies() {
25+
return includeCookies;
26+
}
27+
28+
public Boolean isIncludeQueryString() {
29+
return includeQueryString;
30+
}
1831
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.exceptionless.exceptionlessclient.services;
2+
3+
import com.exceptionless.exceptionlessclient.models.services.EnvironmentInfo;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.assertj.core.api.Assertions.assertThat;
7+
8+
public class DefaultEnvironmentInfoCollectorTest {
9+
10+
@Test
11+
public void itShouldNotIncludeMachineNameAndIpAddressUntilExplicitlyTold() {
12+
EnvironmentInfo info =
13+
DefaultEnvironmentInfoCollector.builder()
14+
.build()
15+
.getEnvironmentInfo(EnvironmentInfoGetArgs.builder().build());
16+
17+
assertThat(info.getMachineName()).isNull();
18+
assertThat(info.getIpAddress()).isNull();
19+
20+
assertThat(info.getProcessorCount()).isNotNull();
21+
}
22+
23+
@Test
24+
public void itCanIncludeMachineNameAndIpAddress() {
25+
EnvironmentInfo info =
26+
DefaultEnvironmentInfoCollector.builder()
27+
.build()
28+
.getEnvironmentInfo(
29+
EnvironmentInfoGetArgs.builder()
30+
.includeIpAddress(true)
31+
.includeMachineName(true)
32+
.build());
33+
34+
assertThat(info.getMachineName()).isNotNull();
35+
assertThat(info.getIpAddress()).isNotNull();
36+
37+
assertThat(info.getProcessorCount()).isNotNull();
38+
}
39+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.exceptionless.exceptionlessclient.services;
2+
3+
import com.exceptionless.exceptionlessclient.models.services.error.Error;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.assertj.core.api.Assertions.assertThat;
7+
8+
public class DefaultErrorParserTest {
9+
@Test
10+
public void itCanParseAnException() {
11+
Error error = DefaultErrorParser.builder().build().parse(new RuntimeException("test"));
12+
13+
assertThat(error.getType()).isEqualTo("java.lang.RuntimeException");
14+
assertThat(error.getMessage()).isEqualTo("test");
15+
assertThat(error.getStackTrace()).isNotEmpty();
16+
// dependent on test file name
17+
assertThat(error.getStackTrace().get(0).getFilename()).isEqualTo("DefaultErrorParserTest.java");
18+
}
19+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.exceptionless.exceptionlessclient.services;
2+
3+
import com.exceptionless.exceptionlessclient.models.services.Module;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.util.List;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
10+
public class DefaultModuleCollectorTest {
11+
@Test
12+
public void itCanGetModules() {
13+
List<Module> modules = DefaultModuleCollector.builder().build().getModules();
14+
15+
assertThat(modules).isNotEmpty();
16+
}
17+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package com.exceptionless.exceptionlessclient.services;
2+
3+
import com.exceptionless.exceptionlessclient.models.services.RequestInfo;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.net.URI;
8+
import java.net.http.HttpRequest;
9+
import java.util.List;
10+
import java.util.Map;
11+
import java.util.Set;
12+
13+
import static org.assertj.core.api.Assertions.assertThat;
14+
15+
public class DefaultRequestInfoCollectorTest {
16+
private DefaultRequestInfoCollector defaultRequestInfoCollector;
17+
18+
@BeforeEach
19+
public void setup() {
20+
defaultRequestInfoCollector = DefaultRequestInfoCollector.builder().build();
21+
}
22+
23+
@Test
24+
public void itCanGetARequestInfoFromAHttpRequest() {
25+
HttpRequest httpRequest =
26+
HttpRequest.newBuilder().uri(URI.create("http://localhost:5000/test-path")).GET().build();
27+
28+
RequestInfo requestInfo =
29+
defaultRequestInfoCollector.getRequestInfo(
30+
httpRequest, RequestInfoGetArgs.builder().build());
31+
32+
assertThat(requestInfo.getUserAgent()).isNull();
33+
assertThat(requestInfo.isSecure()).isFalse();
34+
assertThat(requestInfo.getHttpMethod()).isEqualTo("GET");
35+
assertThat(requestInfo.getHost()).isEqualTo("localhost");
36+
assertThat(requestInfo.getPath()).isEqualTo("/test-path");
37+
assertThat(requestInfo.getPort()).isEqualTo(5000);
38+
}
39+
40+
@Test
41+
public void itCanGetAnUserAgentFromAHttpRequest() {
42+
HttpRequest httpRequest =
43+
HttpRequest.newBuilder()
44+
.uri(URI.create("http://localhost:5000/test-path"))
45+
.GET()
46+
.header("User-Agent", "test-user-agent")
47+
.build();
48+
49+
RequestInfo requestInfo =
50+
defaultRequestInfoCollector.getRequestInfo(
51+
httpRequest, RequestInfoGetArgs.builder().build());
52+
53+
assertThat(requestInfo.getUserAgent()).isEqualTo("test-user-agent");
54+
}
55+
56+
@Test
57+
public void itCanIdentifyASecuredUri() {
58+
HttpRequest httpRequest =
59+
HttpRequest.newBuilder().uri(URI.create("https://localhost:5000/test-path")).GET().build();
60+
61+
RequestInfo requestInfo =
62+
defaultRequestInfoCollector.getRequestInfo(
63+
httpRequest, RequestInfoGetArgs.builder().build());
64+
65+
assertThat(requestInfo.isSecure()).isTrue();
66+
}
67+
68+
@Test
69+
public void itShouldNotIncludeIpAddressCookiesQueryStringByDefault() {
70+
HttpRequest httpRequest =
71+
HttpRequest.newBuilder().uri(URI.create("https://localhost:5000/test-path")).GET().build();
72+
73+
RequestInfo requestInfo =
74+
defaultRequestInfoCollector.getRequestInfo(
75+
httpRequest, RequestInfoGetArgs.builder().build());
76+
77+
assertThat(requestInfo.getClientIpAddress()).isNull();
78+
assertThat(requestInfo.getCookies()).isEmpty();
79+
assertThat(requestInfo.getQueryString()).isEmpty();
80+
}
81+
82+
@Test
83+
public void itCanGetIpAddressCookiesAndQueryStringFromAHttpRequest() {
84+
HttpRequest httpRequest =
85+
HttpRequest.newBuilder()
86+
.uri(URI.create("https://localhost:5000/test-path?query-param-key=query-param-value"))
87+
.header("Cookie", "cookie1=value1;cookie2=value2")
88+
.GET()
89+
.build();
90+
91+
RequestInfo requestInfo =
92+
defaultRequestInfoCollector.getRequestInfo(
93+
httpRequest,
94+
RequestInfoGetArgs.builder()
95+
.includeCookies(true)
96+
.includeIpAddress(true)
97+
.includeQueryString(true)
98+
.build());
99+
100+
assertThat(requestInfo.getClientIpAddress()).isNotNull();
101+
assertThat(requestInfo.getCookies())
102+
.isEqualTo(Map.of("cookie1", "value1", "cookie2", "value2"));
103+
assertThat(requestInfo.getQueryString())
104+
.isEqualTo(Map.of("query-param-key", List.of("query-param-value")));
105+
}
106+
107+
@Test
108+
public void itCanExcludeData() {
109+
HttpRequest httpRequest =
110+
HttpRequest.newBuilder()
111+
.uri(
112+
URI.create(
113+
"https://localhost:5000/test-path?query-param-key=query-param-value&exclude-query-param=exclude-value"))
114+
.header("Cookie", "cookie1=value1;cookie2=value2;exclude-cookie=exclude-value")
115+
.GET()
116+
.build();
117+
118+
RequestInfo requestInfo =
119+
defaultRequestInfoCollector.getRequestInfo(
120+
httpRequest,
121+
RequestInfoGetArgs.builder()
122+
.includeCookies(true)
123+
.includeIpAddress(true)
124+
.includeQueryString(true)
125+
.exclusions(Set.of("exclude-query-param", "exclude-cookie"))
126+
.build());
127+
128+
assertThat(requestInfo.getClientIpAddress()).isNotNull();
129+
assertThat(requestInfo.getCookies())
130+
.isEqualTo(Map.of("cookie1", "value1", "cookie2", "value2"));
131+
assertThat(requestInfo.getQueryString())
132+
.isEqualTo(Map.of("query-param-key", List.of("query-param-value")));
133+
}
134+
}

0 commit comments

Comments
 (0)