1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.activeinstalls.internal.client; |
21 |
|
|
22 |
|
import java.net.UnknownHostException; |
23 |
|
|
24 |
|
import org.junit.Rule; |
25 |
|
import org.junit.Test; |
26 |
|
import org.xwiki.activeinstalls.ActiveInstallsConfiguration; |
27 |
|
import org.xwiki.activeinstalls.internal.DefaultJestClientManager; |
28 |
|
import org.xwiki.test.LogRule; |
29 |
|
import org.xwiki.test.annotation.ComponentList; |
30 |
|
import org.xwiki.test.mockito.MockitoComponentManagerRule; |
31 |
|
|
32 |
|
import com.github.tomakehurst.wiremock.client.WireMock; |
33 |
|
import com.github.tomakehurst.wiremock.junit.WireMockRule; |
34 |
|
|
35 |
|
import io.searchbox.client.AbstractJestClient; |
36 |
|
|
37 |
|
import static com.github.tomakehurst.wiremock.client.RequestPatternBuilder.allRequests; |
38 |
|
import static com.github.tomakehurst.wiremock.client.WireMock.*; |
39 |
|
import static org.junit.Assert.*; |
40 |
|
import static org.mockito.Mockito.*; |
41 |
|
|
42 |
|
|
43 |
|
@link |
44 |
|
|
45 |
|
@version |
46 |
|
@since |
47 |
|
|
48 |
|
@ComponentList({ |
49 |
|
DefaultPingSender.class, |
50 |
|
DefaultJestClientManager.class |
51 |
|
}) |
|
|
| 92.3% |
Uncovered Elements: 2 (26) |
Complexity: 5 |
Complexity Density: 0.22 |
|
52 |
|
public class DefaultPingSenderIntegrationTest |
53 |
|
{ |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
@Rule |
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
58 |
2 |
public LogRule logRule = new LogRule() {{... |
59 |
2 |
record(LogLevel.ERROR); |
60 |
2 |
recordLoggingForType(AbstractJestClient.class); |
61 |
|
}}; |
62 |
|
|
63 |
|
@Rule |
64 |
|
public MockitoComponentManagerRule componentManager = new MockitoComponentManagerRule(); |
65 |
|
|
66 |
|
@Rule |
67 |
|
public WireMockRule proxyWireMockRule = new WireMockRule(8888); |
68 |
|
|
|
|
| 85.7% |
Uncovered Elements: 2 (14) |
Complexity: 3 |
Complexity Density: 0.21 |
1PASS
|
|
69 |
1 |
@Test... |
70 |
|
public void sendPingThroughProxy() throws Exception |
71 |
|
{ |
72 |
1 |
ActiveInstallsConfiguration configuration = |
73 |
|
this.componentManager.registerMockComponent(ActiveInstallsConfiguration.class); |
74 |
1 |
when(configuration.getPingInstanceURL()).thenReturn("http://unknownhostforxwikitest"); |
75 |
|
|
76 |
1 |
PingSender pingSender = this.componentManager.getInstance(PingSender.class); |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
1 |
try { |
81 |
1 |
pingSender.sendPing(); |
82 |
0 |
fail("Should have raised an exception here"); |
83 |
|
} catch (UnknownHostException expected) { |
84 |
|
|
85 |
|
} |
86 |
1 |
assertTrue("The HTTP server was not called by the ping sender", findAll(allRequests()).isEmpty()); |
87 |
|
|
88 |
|
|
89 |
|
|
90 |
1 |
System.setProperty("http.proxyHost", "localhost"); |
91 |
1 |
System.setProperty("http.proxyPort", "8888"); |
92 |
1 |
try { |
93 |
1 |
pingSender.sendPing(); |
94 |
0 |
fail("Should have raised an exception here"); |
95 |
|
} catch (Exception expected) { |
96 |
|
|
97 |
|
|
98 |
1 |
assertTrue(expected.getMessage().contains("404 Not Found")); |
99 |
|
} |
100 |
1 |
assertFalse("The HTTP server was called by the ping sender", findAll(allRequests()).isEmpty()); |
101 |
|
} |
102 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
103 |
1 |
@Test... |
104 |
|
public void pingIsSentWithUserAgent() throws Exception |
105 |
|
{ |
106 |
1 |
ActiveInstallsConfiguration configuration = |
107 |
|
this.componentManager.registerMockComponent(ActiveInstallsConfiguration.class); |
108 |
1 |
when(configuration.getPingInstanceURL()).thenReturn("http://localhost:8888/path"); |
109 |
1 |
when(configuration.getUserAgent()).thenReturn("customuseragent"); |
110 |
|
|
111 |
1 |
stubFor(WireMock.any(urlPathMatching(".*")) |
112 |
|
.willReturn(aResponse() |
113 |
|
.withStatus(200) |
114 |
|
.withHeader("Content-Type", "application/json"))); |
115 |
|
|
116 |
1 |
PingSender pingSender = this.componentManager.getInstance(PingSender.class); |
117 |
1 |
pingSender.sendPing(); |
118 |
|
|
119 |
1 |
WireMock.verify(putRequestedFor(urlPathEqualTo("/path/installs")) |
120 |
|
.withHeader("User-Agent", equalTo("customuseragent"))); |
121 |
|
} |
122 |
|
} |