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

File DefaultDataManagerTest.java

 

Code metrics

0
38
9
1
166
119
9
0.24
4.22
9
1

Classes

Class Line # Actions
DefaultDataManagerTest 66 38 0% 9 0
1.0100%
 

Contributing tests

This file is covered by 5 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;
21   
22    import java.util.Collections;
23    import java.util.Date;
24    import java.util.HashMap;
25    import java.util.Map;
26   
27    import org.joda.time.DateTime;
28    import org.joda.time.DateTimeZone;
29    import org.joda.time.format.DateTimeFormatter;
30    import org.joda.time.format.ISODateTimeFormat;
31    import org.junit.*;
32    import org.xwiki.activeinstalls.ActiveInstallsConfiguration;
33    import org.xwiki.activeinstalls.internal.server.DefaultDataManager;
34    import org.xwiki.activeinstalls.server.DataManager;
35    import org.xwiki.component.internal.ContextComponentManagerProvider;
36    import org.xwiki.properties.internal.DefaultConverterManager;
37    import org.xwiki.properties.internal.converter.ConvertUtilsConverter;
38    import org.xwiki.properties.internal.converter.EnumConverter;
39    import org.xwiki.test.annotation.ComponentList;
40    import org.xwiki.test.mockito.MockitoComponentManagerRule;
41   
42    import com.github.tlrx.elasticsearch.test.EsSetup;
43    import com.google.gson.JsonObject;
44   
45    import io.searchbox.params.Parameters;
46    import net.sf.json.JSONObject;
47    import static com.github.tlrx.elasticsearch.test.EsSetup.deleteAll;
48    import static com.github.tlrx.elasticsearch.test.EsSetup.index;
49    import static org.junit.Assert.assertEquals;
50    import static org.mockito.Mockito.*;
51   
52    /**
53    * Integration tests for {@link org.xwiki.activeinstalls.internal.server.DefaultDataManager}.
54    *
55    * @version $Id: f894e76e23772534bbe8fa1dcbc22bb5c106bfb0 $
56    * @since 5.2RC1
57    */
58    @ComponentList({
59    DefaultDataManager.class,
60    DefaultJestClientManager.class,
61    DefaultConverterManager.class,
62    ConvertUtilsConverter.class,
63    EnumConverter.class,
64    ContextComponentManagerProvider.class
65    })
 
66    public class DefaultDataManagerTest
67    {
68    private static final DateTimeFormatter DATE_FORMATTER = ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC);
69   
70    public static EsSetup esSetup;
71   
72    @Rule
73    public MockitoComponentManagerRule componentManager = new MockitoComponentManagerRule();
74   
 
75  1 toggle @BeforeClass
76    public static void setUpClass() throws Exception
77    {
78  1 esSetup = new EsSetup();
79  1 esSetup.execute(deleteAll());
80    }
81   
 
82  5 toggle @Before
83    public void setUp() throws Exception
84    {
85    // Add index 1 (non snapshot version)
86  5 addIndex("id1", DATE_FORMATTER.print(new Date().getTime()), "5.2");
87   
88    // Add index 2 (date is one year earlier than index1 and version is SNAPSHOT)
89  5 DateTime dt = new DateTime();
90  5 dt = dt.plusDays(-365);
91  5 addIndex("id2", DATE_FORMATTER.print(dt), "5.2-SNAPSHOT");
92   
93    // Add index 3 (version is SNAPSHOT)
94  5 addIndex("id3", DATE_FORMATTER.print(new Date().getTime()), "5.3-SNAPSHOT");
95   
96  5 ActiveInstallsConfiguration configuration =
97    this.componentManager.registerMockComponent(ActiveInstallsConfiguration.class);
98  5 when(configuration.getPingInstanceURL()).thenReturn("http://localhost:9200");
99    }
100   
 
101  1 toggle @AfterClass
102    public static void tearDownClass()
103    {
104  1 esSetup.terminate();
105    }
106   
 
107  1 toggle @Test
108    public void searchInstallsMatchingAll() throws Exception
109    {
110  1 DataManager manager = this.componentManager.getInstance(DataManager.class);
111  1 String query = "{ \"query\" : { \"match_all\": { } } }";
112  1 JsonObject json = manager.searchInstalls("install", query, Collections.<String, Object>emptyMap());
113  1 assertEquals(3, json.getAsJsonObject("hits").getAsJsonPrimitive("total").getAsLong());
114    }
115   
 
116  1 toggle @Test
117    public void searchInstallsMatchingVersion() throws Exception
118    {
119  1 DataManager manager = this.componentManager.getInstance(DataManager.class);
120  1 String query = "{ \"query\" : { \"term\": { \"distributionVersion\" : \"5.2\" } } }";
121  1 JsonObject json = manager.searchInstalls("install", query, Collections.<String, Object>emptyMap());
122  1 assertEquals(2, json.getAsJsonObject("hits").getAsJsonPrimitive("total").getAsLong());
123    }
124   
 
125  1 toggle @Test
126    public void searchInstallsMatchingVersionAndDate() throws Exception
127    {
128  1 DataManager manager = this.componentManager.getInstance(DataManager.class);
129  1 DateTime dt = new DateTime();
130  1 dt = dt.plusDays(-30);
131  1 String query = String.format("{ \"query\" : { \"query_string\": { \"query\" : \""
132    + "date:[%s TO *] AND distributionVersion:5.2\" } } }", DATE_FORMATTER.print(dt));
133  1 JsonObject json = manager.searchInstalls("install", query, Collections.<String, Object>emptyMap());
134  1 assertEquals(1, json.getAsJsonObject("hits").getAsJsonPrimitive("total").getAsLong());
135  1 assertEquals("5.2", json.getAsJsonObject("hits").getAsJsonArray("hits").get(0).getAsJsonObject()
136    .getAsJsonObject("_source").getAsJsonPrimitive("distributionVersion").getAsString());
137    }
138   
 
139  1 toggle @Test
140    public void searchInstallsExcludingSnapshotsAndUsingCountSearchType() throws Exception
141    {
142  1 DataManager manager = this.componentManager.getInstance(DataManager.class);
143  1 String query = "{ \"query\" : { \"query_string\": { \"query\" : \"-distributionVersion:*SNAPSHOT\" } } }";
144  1 JsonObject json = manager.searchInstalls("install", query,
145    Collections.<String, Object>singletonMap(Parameters.SEARCH_TYPE, "count"));
146  1 assertEquals(1, json.getAsJsonObject("hits").getAsJsonPrimitive("total").getAsLong());
147    }
148   
 
149  1 toggle @Test
150    public void countInstallsMatchingAll() throws Exception
151    {
152  1 DataManager manager = this.componentManager.getInstance(DataManager.class);
153  1 String query = "{ \"query\" : { \"match_all\": { } } }";
154  1 JsonObject json = manager.countInstalls("install", query, Collections.<String, Object>emptyMap());
155  1 assertEquals(3, json.getAsJsonPrimitive("count").getAsLong());
156    }
157   
 
158  15 toggle private void addIndex(String id, String date, String version)
159    {
160  15 Map<String, Object> jsonMap = new HashMap<>();
161  15 jsonMap.put("date", date);
162  15 jsonMap.put("distributionVersion", version);
163  15 String json = JSONObject.fromObject(jsonMap).toString();
164  15 esSetup.execute(index("installs", "install", id).withSource(json));
165    }
166    }