1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.activeinstalls.internal.client

File DatePingDataProviderTest.java

 

Code metrics

0
24
2
1
116
78
2
0.08
12
2
1

Classes

Class Line # Actions
DatePingDataProviderTest 51 24 0% 2 0
1.0100%
 

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.util.Map;
23    import java.util.UUID;
24   
25    import org.junit.Rule;
26    import org.junit.Test;
27    import org.xwiki.activeinstalls.internal.JestClientManager;
28    import org.xwiki.activeinstalls.internal.client.data.DatePingDataProvider;
29    import org.xwiki.instance.InstanceId;
30    import org.xwiki.instance.InstanceIdManager;
31    import org.xwiki.test.mockito.MockitoComponentMockingRule;
32   
33    import com.google.gson.Gson;
34    import com.google.gson.JsonParser;
35   
36    import io.searchbox.client.JestClient;
37    import io.searchbox.core.Search;
38    import io.searchbox.core.SearchResult;
39   
40    import static org.junit.Assert.assertEquals;
41    import static org.mockito.ArgumentMatchers.any;
42    import static org.mockito.Mockito.mock;
43    import static org.mockito.Mockito.when;
44   
45    /**
46    * Unit tests for {@link org.xwiki.activeinstalls.internal.client.data.DatePingDataProvider}.
47    *
48    * @version $Id: dc2c8e8d14efc3f123882d3ee25404fa77d09b2a $
49    * @since 6.1M1
50    */
 
51    public class DatePingDataProviderTest
52    {
53    @Rule
54    public MockitoComponentMockingRule<DatePingDataProvider> mocker =
55    new MockitoComponentMockingRule<>(DatePingDataProvider.class);
56   
 
57  1 toggle @Test
58    public void provideMapping() throws Exception
59    {
60  1 Map<String, Object> mapping = this.mocker.getComponentUnderTest().provideMapping();
61  1 assertEquals(2, mapping.size());
62   
63  1 Map<String, Object> propertiesMapping = (Map<String, Object>) mapping.get("sinceDays");
64  1 assertEquals(1, propertiesMapping.size());
65  1 assertEquals("long", propertiesMapping.get("type"));
66   
67  1 propertiesMapping = (Map<String, Object>) mapping.get("firstPingDate");
68  1 assertEquals(1, propertiesMapping.size());
69  1 assertEquals("date", propertiesMapping.get("type"));
70    }
71   
 
72  1 toggle @Test
73    public void provideData() throws Exception
74    {
75  1 InstanceId id = new InstanceId(UUID.randomUUID().toString());
76  1 InstanceIdManager idManager = this.mocker.getInstance(InstanceIdManager.class);
77  1 when(idManager.getInstanceId()).thenReturn(id);
78   
79  1 JestClient client = mock(JestClient.class);
80  1 SearchResult searchResult = new SearchResult(new Gson());
81  1 String resultString = "{\n" +
82    " \"took\": 4,\n" +
83    " \"timed_out\": false,\n" +
84    " \"_shards\": {\n" +
85    " \"total\": 5,\n" +
86    " \"successful\": 5,\n" +
87    " \"failed\": 0\n" +
88    " },\n" +
89    " \"hits\": {\n" +
90    " \"total\": 2,\n" +
91    " \"max_score\": 0,\n" +
92    " \"hits\": []\n" +
93    " },\n" +
94    " \"aggregations\": {\n" +
95    " \"firstPingDate\": {\n" +
96    " \"value\": 1392854400000\n" +
97    " },\n" +
98    " \"serverTime\": {\n" +
99    " \"value\": 1393200000000\n" +
100    " }\n" +
101    " }\n" +
102    "}";
103  1 searchResult.setJsonString(resultString);
104  1 searchResult.setJsonObject(new JsonParser().parse(resultString).getAsJsonObject());
105  1 searchResult.setSucceeded(true);
106  1 when(client.execute(any(Search.class))).thenReturn(searchResult);
107   
108  1 JestClientManager jestManager = this.mocker.getInstance(JestClientManager.class);
109  1 when(jestManager.getClient()).thenReturn(client);
110   
111  1 Map<String, Object> data = this.mocker.getComponentUnderTest().provideData();
112  1 assertEquals(2, data.size());
113  1 assertEquals(4L, data.get("sinceDays"));
114  1 assertEquals(1392854400000L, data.get("firstPingDate"));
115    }
116    }