Skip to content

Commit 0e5af05

Browse files
committed
Reapply "Add JaCoCo reporting and backfill unit test coverage across all modules"
This reverts commit bc675a6.
1 parent bc675a6 commit 0e5af05

33 files changed

Lines changed: 4261 additions & 38 deletions

pom.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
<maven-release-plugin.version>2.5.3</maven-release-plugin.version>
3838
<maven-source-plugin.version>3.3.1</maven-source-plugin.version>
3939
<maven-javadoc-plugin.version>3.1.0</maven-javadoc-plugin.version>
40+
<jacoco-maven-plugin.version>0.8.8</jacoco-maven-plugin.version>
4041
</properties>
4142

4243
<scm>
@@ -185,6 +186,32 @@
185186
</plugins>
186187
</pluginManagement>
187188

189+
<plugins>
190+
<plugin>
191+
<groupId>org.jacoco</groupId>
192+
<artifactId>jacoco-maven-plugin</artifactId>
193+
<version>${jacoco-maven-plugin.version}</version>
194+
<executions>
195+
<execution>
196+
<id>prepare-agent</id>
197+
<goals>
198+
<goal>prepare-agent</goal>
199+
</goals>
200+
<configuration>
201+
<propertyName>surefireArgLine</propertyName>
202+
</configuration>
203+
</execution>
204+
<execution>
205+
<id>report</id>
206+
<phase>test</phase>
207+
<goals>
208+
<goal>report</goal>
209+
</goals>
210+
</execution>
211+
</executions>
212+
</plugin>
213+
</plugins>
214+
188215
<extensions>
189216
<extension>
190217
<groupId>org.apache.maven.wagon</groupId>

reportium-import-java/src/test/java/com/perfecto/reportium/imports/client/ReportiumImportClientTest.java

Lines changed: 478 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.perfecto.reportium.imports.client.connection;
2+
3+
import org.apache.http.HttpHost;
4+
import org.apache.http.auth.AuthScope;
5+
import org.apache.http.auth.UsernamePasswordCredentials;
6+
import org.apache.http.client.CredentialsProvider;
7+
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
8+
import org.apache.http.impl.client.BasicCredentialsProvider;
9+
import org.testng.annotations.Test;
10+
11+
import java.net.URI;
12+
import java.net.URISyntaxException;
13+
import java.util.Map;
14+
15+
import static org.testng.Assert.assertEquals;
16+
import static org.testng.Assert.assertNull;
17+
import static org.testng.Assert.assertTrue;
18+
19+
public class ConnectionTest {
20+
21+
private static final URI REPORTING_SERVER = create("https://tenant.reporting.perfectomobile.com");
22+
private static final String SECURITY_TOKEN = "token-123";
23+
24+
private static URI create(String uri) {
25+
try {
26+
return new URI(uri);
27+
} catch (URISyntaxException e) {
28+
throw new RuntimeException(e);
29+
}
30+
}
31+
32+
@Test
33+
public void constructor_setsReportingServerAndSecurityToken() {
34+
Connection tested = new Connection(REPORTING_SERVER, SECURITY_TOKEN);
35+
36+
assertEquals(tested.getReportingServer(), REPORTING_SERVER);
37+
assertEquals(tested.getSecurityToken(), SECURITY_TOKEN);
38+
assertNull(tested.getProxy());
39+
assertNull(tested.getCredentialsProvider());
40+
assertNull(tested.getSslSocketFactory());
41+
assertTrue(tested.getHeaders().isEmpty());
42+
}
43+
44+
@Test
45+
public void addHeader_addsToHeadersMap() {
46+
Connection tested = new Connection(REPORTING_SERVER, SECURITY_TOKEN);
47+
tested.addHeader("X-Custom", "value1");
48+
tested.addHeader("X-Other", "value2");
49+
50+
Map<String, String> headers = tested.getHeaders();
51+
assertEquals(headers.size(), 2);
52+
assertEquals(headers.get("X-Custom"), "value1");
53+
assertEquals(headers.get("X-Other"), "value2");
54+
}
55+
56+
@Test
57+
public void getHeaders_returnsDefensiveCopy() {
58+
Connection tested = new Connection(REPORTING_SERVER, SECURITY_TOKEN);
59+
tested.addHeader("X-Custom", "value1");
60+
61+
Map<String, String> headers = tested.getHeaders();
62+
headers.put("X-Injected", "hacked");
63+
64+
assertEquals(tested.getHeaders().size(), 1);
65+
}
66+
67+
@Test
68+
public void setProxy_getProxy() {
69+
Connection tested = new Connection(REPORTING_SERVER, SECURITY_TOKEN);
70+
HttpHost proxy = new HttpHost("proxy.example.com", 8080);
71+
tested.setProxy(proxy);
72+
73+
assertEquals(tested.getProxy(), proxy);
74+
}
75+
76+
@Test
77+
public void setCredentialsProvider_getCredentialsProvider() {
78+
Connection tested = new Connection(REPORTING_SERVER, SECURITY_TOKEN);
79+
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
80+
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("user", "pass"));
81+
tested.setCredentialsProvider(credentialsProvider);
82+
83+
assertEquals(tested.getCredentialsProvider(), credentialsProvider);
84+
}
85+
86+
@Test
87+
public void setSslSocketFactory_getSslSocketFactory() {
88+
Connection tested = new Connection(REPORTING_SERVER, SECURITY_TOKEN);
89+
SSLConnectionSocketFactory sslSocketFactory = SSLConnectionSocketFactory.getSocketFactory();
90+
tested.setSslSocketFactory(sslSocketFactory);
91+
92+
assertEquals(tested.getSslSocketFactory(), sslSocketFactory);
93+
}
94+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.perfecto.reportium.imports.client.connection;
2+
3+
import org.apache.http.HttpEntity;
4+
import org.apache.http.ProtocolVersion;
5+
import org.apache.http.StatusLine;
6+
import org.easymock.EasyMock;
7+
import org.easymock.IMocksControl;
8+
import org.testng.annotations.BeforeMethod;
9+
import org.testng.annotations.Test;
10+
11+
import java.io.ByteArrayInputStream;
12+
import java.io.IOException;
13+
14+
import static org.testng.Assert.assertEquals;
15+
16+
public class HttpResponseTest {
17+
18+
private IMocksControl mocksControl;
19+
private org.apache.http.HttpResponse responseEntityMock;
20+
private StatusLine statusLineMock;
21+
22+
@BeforeMethod
23+
public void beforeTest() {
24+
mocksControl = EasyMock.createControl();
25+
responseEntityMock = mocksControl.createMock(org.apache.http.HttpResponse.class);
26+
statusLineMock = mocksControl.createMock(StatusLine.class);
27+
}
28+
29+
@Test
30+
public void getStatus() {
31+
EasyMock.expect(responseEntityMock.getStatusLine()).andReturn(statusLineMock);
32+
EasyMock.expect(statusLineMock.getStatusCode()).andReturn(204);
33+
34+
mocksControl.replay();
35+
HttpResponse tested = new HttpResponse(responseEntityMock);
36+
assertEquals(tested.getStatus(), 204);
37+
mocksControl.verify();
38+
}
39+
40+
@Test
41+
public void getStatusReason() {
42+
EasyMock.expect(responseEntityMock.getStatusLine()).andReturn(statusLineMock);
43+
EasyMock.expect(statusLineMock.getReasonPhrase()).andReturn("No Content");
44+
45+
mocksControl.replay();
46+
HttpResponse tested = new HttpResponse(responseEntityMock);
47+
assertEquals(tested.getStatusReason(), "No Content");
48+
mocksControl.verify();
49+
}
50+
51+
@Test
52+
public void getBody_success() throws IOException {
53+
HttpEntity httpEntityMock = mocksControl.createMock(HttpEntity.class);
54+
EasyMock.expect(responseEntityMock.getEntity()).andReturn(httpEntityMock);
55+
EasyMock.expect(httpEntityMock.getContent()).andReturn(new ByteArrayInputStream("hello".getBytes()));
56+
57+
mocksControl.replay();
58+
HttpResponse tested = new HttpResponse(responseEntityMock);
59+
assertEquals(tested.getBody(), "hello");
60+
mocksControl.verify();
61+
}
62+
63+
@Test(expectedExceptions = RuntimeException.class)
64+
public void getBody_ioException() throws IOException {
65+
HttpEntity httpEntityMock = mocksControl.createMock(HttpEntity.class);
66+
EasyMock.expect(responseEntityMock.getEntity()).andReturn(httpEntityMock);
67+
EasyMock.expect(httpEntityMock.getContent()).andThrow(new IOException("boom"));
68+
69+
mocksControl.replay();
70+
HttpResponse tested = new HttpResponse(responseEntityMock);
71+
try {
72+
tested.getBody();
73+
} finally {
74+
mocksControl.verify();
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)