1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.activeinstalls.internal.client

File DefaultPingSenderIntegrationTest.java

 

Code metrics

0
23
3
1
122
71
5
0.22
7.67
3
1.67

Classes

Class Line # Actions
DefaultPingSenderIntegrationTest 52 23 0% 5 2
0.923076992.3%
 

Contributing tests

This file is covered by 2 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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    * Integration tests for {@link org.xwiki.activeinstalls.internal.client.DefaultPingSender}.
44    *
45    * @version $Id: 46cc1b74a2743eb2882d4b32e6801582bad1ac19 $
46    * @since 6.2M1
47    */
48    @ComponentList({
49    DefaultPingSender.class,
50    DefaultJestClientManager.class
51    })
 
52    public class DefaultPingSenderIntegrationTest
53    {
54    // Capture the logs since we don't want anything printed in the console and our proxy isn't configured to return
55    // a valid return value which makes Jest Client choke but we don't care since all we care is verifying that our
56    // proxy is called, see below.
57    @Rule
 
58  2 toggle 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   
 
69  1 toggle @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    // First call the Ping Sender but since we haven't set up any proxy our Mock HTTP Server is not going to be
79    // called (since http://xwikitestserver/path will lead to nowhere...).
80  1 try {
81  1 pingSender.sendPing();
82  0 fail("Should have raised an exception here");
83    } catch (UnknownHostException expected) {
84    // Nothing to check, this proves the proxy isn't set up!
85    }
86  1 assertTrue("The HTTP server was not called by the ping sender", findAll(allRequests()).isEmpty());
87   
88    // Second, setup a proxy by using System Properties, then call again the ping sender and this time it should
89    // succeed since http://host will go to the proxy which is pointing to our Mock HTTP Server!
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    // We expect a 404 since we haven't configured the proxy to return something specific, but we don't need to
97    // do that since all we want is to ensure the proxy is called!
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   
 
103  1 toggle @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    }