Skip to content

Commit 277f165

Browse files
Add coverage to http classes (#38)
* Add coverage to http classes
1 parent b263a6f commit 277f165

File tree

11 files changed

+476
-70
lines changed

11 files changed

+476
-70
lines changed

pom.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030
<version>2.6</version>
3131
</dependency>
3232

33+
<dependency>
34+
<groupId>org.apache.commons</groupId>
35+
<artifactId>commons-lang3</artifactId>
36+
<version>3.9</version>
37+
</dependency>
38+
3339
<dependency>
3440
<groupId>org.apache.httpcomponents</groupId>
3541
<artifactId>httpclient</artifactId>
@@ -63,12 +69,33 @@
6369
<scope>test</scope>
6470
</dependency>
6571

72+
<dependency>
73+
<groupId>org.powermock</groupId>
74+
<artifactId>powermock-module-junit4</artifactId>
75+
<version>2.0.2</version>
76+
<scope>test</scope>
77+
</dependency>
78+
79+
<dependency>
80+
<groupId>org.powermock</groupId>
81+
<artifactId>powermock-api-mockito2</artifactId>
82+
<version>2.0.2</version>
83+
<scope>test</scope>
84+
</dependency>
85+
6686
<dependency>
6787
<groupId>org.powermock</groupId>
6888
<artifactId>powermock-reflect</artifactId>
6989
<version>2.0.2</version>
7090
<scope>test</scope>
7191
</dependency>
92+
93+
<dependency>
94+
<groupId>org.mock-server</groupId>
95+
<artifactId>mockserver-netty</artifactId>
96+
<version>5.6.1</version>
97+
<scope>test</scope>
98+
</dependency>
7299
</dependencies>
73100

74101
<build>

src/main/java/com/ericsson/eiffelcommons/JenkinsManager.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
import org.json.JSONObject;
3131

3232
import com.ericsson.eiffelcommons.helpers.MediaType;
33-
import com.ericsson.eiffelcommons.utils.HttpRequest;
34-
import com.ericsson.eiffelcommons.utils.HttpRequest.HttpMethod;
35-
import com.ericsson.eiffelcommons.utils.ResponseEntity;
33+
import com.ericsson.eiffelcommons.http.HttpRequest;
34+
import com.ericsson.eiffelcommons.http.HttpRequest.HttpMethod;
35+
import com.ericsson.eiffelcommons.http.ResponseEntity;
3636

3737
public class JenkinsManager {
3838

src/main/java/com/ericsson/eiffelcommons/utils/HttpExecutor.java renamed to src/main/java/com/ericsson/eiffelcommons/http/HttpExecutor.java

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
See the License for the specific language governing permissions and
1515
limitations under the License.
1616
*/
17-
package com.ericsson.eiffelcommons.utils;
17+
package com.ericsson.eiffelcommons.http;
1818

1919
import java.io.IOException;
2020

@@ -46,20 +46,6 @@ public static HttpExecutor getInstance() {
4646
return instance;
4747
}
4848

49-
/**
50-
* Close existing HttpClient and create a new one.
51-
*
52-
* @throws IOException
53-
*/
54-
public void recreateHttpClient() throws IOException {
55-
if (this.client != null) {
56-
this.client.close();
57-
58-
}
59-
this.client = HttpClientBuilder.create()
60-
.build();
61-
}
62-
6349
/**
6450
* Handle the response from a HTTP request
6551
*

src/main/java/com/ericsson/eiffelcommons/utils/HttpRequest.java renamed to src/main/java/com/ericsson/eiffelcommons/http/HttpRequest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
See the License for the specific language governing permissions and
1515
limitations under the License.
1616
*/
17-
package com.ericsson.eiffelcommons.utils;
17+
package com.ericsson.eiffelcommons.http;
1818

1919
import java.io.File;
2020
import java.io.IOException;
@@ -26,6 +26,7 @@
2626

2727
import org.apache.commons.codec.binary.Base64;
2828
import org.apache.commons.io.FileUtils;
29+
import org.apache.commons.lang3.StringUtils;
2930
import org.apache.http.Header;
3031
import org.apache.http.HttpHeaders;
3132
import org.apache.http.client.ClientProtocolException;
@@ -331,10 +332,12 @@ private URIBuilder addParametersToURIBuilder(URIBuilder builder) {
331332
* @throws URISyntaxException
332333
*/
333334
private URIBuilder createURIBuilder() throws URISyntaxException {
334-
if (endpoint.startsWith("/")) {
335+
if (!StringUtils.isEmpty(endpoint) && endpoint.startsWith("/")) {
335336
return new URIBuilder(baseUrl + endpoint);
336-
} else {
337+
} else if (!StringUtils.isEmpty(endpoint)) {
337338
return new URIBuilder(baseUrl + "/" + endpoint);
339+
} else {
340+
return new URIBuilder(baseUrl);
338341
}
339342
}
340343

src/main/java/com/ericsson/eiffelcommons/utils/ResponseEntity.java renamed to src/main/java/com/ericsson/eiffelcommons/http/ResponseEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
See the License for the specific language governing permissions and
1515
limitations under the License.
1616
*/
17-
package com.ericsson.eiffelcommons.utils;
17+
package com.ericsson.eiffelcommons.http;
1818

1919
import java.io.IOException;
2020

src/test/java/com/ericsson/eiffelcommons/Utilstest/HttpRequestTest.java

Lines changed: 0 additions & 47 deletions
This file was deleted.
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.ericsson.eiffelcommons.httptest;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.Assert.assertNull;
6+
import static org.mockserver.integration.ClientAndServer.startClientAndServer;
7+
import static org.mockserver.model.HttpRequest.request;
8+
import static org.mockserver.model.HttpResponse.response;
9+
10+
import java.io.IOException;
11+
import java.net.URI;
12+
import java.net.URISyntaxException;
13+
import java.net.UnknownHostException;
14+
15+
import org.apache.http.client.ClientProtocolException;
16+
import org.apache.http.client.methods.HttpGet;
17+
import org.apache.http.impl.client.CloseableHttpClient;
18+
import org.apache.http.impl.client.HttpClientBuilder;
19+
import org.junit.BeforeClass;
20+
import org.junit.Test;
21+
import org.mockserver.integration.ClientAndServer;
22+
import org.powermock.reflect.Whitebox;
23+
24+
import com.ericsson.eiffelcommons.http.HttpExecutor;
25+
import com.ericsson.eiffelcommons.http.ResponseEntity;
26+
27+
public class HttpExecutorTest {
28+
29+
private static final String URL_FAKE = "http://fake.com";
30+
private static final String URL_MOCK = "http://localhost:{port}";
31+
private static final String ENDPOINT_MOCK = "/endpoint";
32+
private static final String RESPONSE_MOCK = "{\"message\":\"dummy\"}";
33+
34+
private static ClientAndServer clientAndServer;
35+
36+
@BeforeClass
37+
public static void setUpMocks() throws IOException {
38+
clientAndServer = startClientAndServer();
39+
}
40+
41+
@Test(expected = UnknownHostException.class)
42+
public void testDefaultConstructor() throws IOException, URISyntaxException {
43+
HttpGet request = new HttpGet();
44+
request.setURI(new URI(URL_FAKE));
45+
46+
HttpExecutor executor = new HttpExecutor();
47+
executor.executeRequest(request);
48+
}
49+
50+
@Test(expected = IllegalStateException.class)
51+
public void testHttpClientConstructor() throws IOException, URISyntaxException {
52+
CloseableHttpClient client = HttpClientBuilder.create().build();
53+
client.close();
54+
55+
HttpGet request = new HttpGet();
56+
request.setURI(new URI(URL_FAKE));
57+
58+
HttpExecutor executor = new HttpExecutor(client);
59+
executor.executeRequest(request);
60+
}
61+
62+
@Test
63+
public void testGetInstance() throws IOException, URISyntaxException {
64+
HttpGet request = new HttpGet();
65+
request.setURI(new URI(URL_FAKE));
66+
67+
HttpExecutor executor = new HttpExecutor();
68+
HttpExecutor instance = Whitebox.getInternalState(executor, "instance", HttpExecutor.class);
69+
assertNull(instance);
70+
71+
// Test if instance null branch
72+
executor = HttpExecutor.getInstance();
73+
instance = Whitebox.getInternalState(executor, "instance", HttpExecutor.class);
74+
assertNotNull(instance);
75+
76+
// Test if instance not null branch
77+
executor = HttpExecutor.getInstance();
78+
instance = Whitebox.getInternalState(executor, "instance", HttpExecutor.class);
79+
assertNotNull(instance);
80+
81+
// Reset static instance
82+
instance = null;
83+
Whitebox.setInternalState(HttpExecutor.class, "instance", instance);
84+
}
85+
86+
@Test
87+
public void testExecuteRequest()
88+
throws URISyntaxException, ClientProtocolException, IOException {
89+
setUpMock();
90+
91+
HttpGet request = new HttpGet();
92+
String url = URL_MOCK.replace("{port}", String.valueOf(clientAndServer.getLocalPort()));
93+
URI uri = new URI(url + ENDPOINT_MOCK);
94+
request.setURI(uri);
95+
96+
HttpExecutor executor = new HttpExecutor();
97+
ResponseEntity response = executor.executeRequest(request);
98+
assertEquals(RESPONSE_MOCK, response.getBody());
99+
}
100+
101+
private void setUpMock() {
102+
clientAndServer.when(request().withMethod("GET").withPath(ENDPOINT_MOCK))
103+
.respond(response().withStatusCode(200).withBody(RESPONSE_MOCK));
104+
}
105+
}

0 commit comments

Comments
 (0)