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; |
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 |
|
@link |
54 |
|
|
55 |
|
@version |
56 |
|
@since |
57 |
|
|
58 |
|
@ComponentList({ |
59 |
|
DefaultDataManager.class, |
60 |
|
DefaultJestClientManager.class, |
61 |
|
DefaultConverterManager.class, |
62 |
|
ConvertUtilsConverter.class, |
63 |
|
EnumConverter.class, |
64 |
|
ContextComponentManagerProvider.class |
65 |
|
}) |
|
|
| 100% |
Uncovered Elements: 0 (47) |
Complexity: 9 |
Complexity Density: 0.24 |
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
75 |
1 |
@BeforeClass... |
76 |
|
public static void setUpClass() throws Exception |
77 |
|
{ |
78 |
1 |
esSetup = new EsSetup(); |
79 |
1 |
esSetup.execute(deleteAll()); |
80 |
|
} |
81 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
82 |
5 |
@Before... |
83 |
|
public void setUp() throws Exception |
84 |
|
{ |
85 |
|
|
86 |
5 |
addIndex("id1", DATE_FORMATTER.print(new Date().getTime()), "5.2"); |
87 |
|
|
88 |
|
|
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 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
101 |
1 |
@AfterClass... |
102 |
|
public static void tearDownClass() |
103 |
|
{ |
104 |
1 |
esSetup.terminate(); |
105 |
|
} |
106 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
107 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
116 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
125 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
139 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
149 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
158 |
15 |
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 |
|
} |