-
Notifications
You must be signed in to change notification settings - Fork 1k
/
MongoIndexSetServiceTest.java
336 lines (289 loc) · 14.3 KB
/
MongoIndexSetServiceTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/**
* This file is part of Graylog.
*
* Graylog is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Graylog is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Graylog. If not, see <http://www.gnu.org/licenses/>.
*/
package org.graylog2.indexer.indexset;
import com.google.common.eventbus.Subscribe;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lordofthejars.nosqlunit.annotation.UsingDataSet;
import com.lordofthejars.nosqlunit.core.LoadStrategyEnum;
import com.lordofthejars.nosqlunit.mongodb.InMemoryMongoDb;
import org.bson.types.ObjectId;
import org.graylog2.bindings.providers.MongoJackObjectMapperProvider;
import org.graylog2.buffers.processors.fakestreams.FakeStream;
import org.graylog2.cluster.ClusterConfigServiceImpl;
import org.graylog2.database.MongoConnectionRule;
import org.graylog2.events.ClusterEventBus;
import org.graylog2.indexer.indexset.events.IndexSetCreatedEvent;
import org.graylog2.indexer.indexset.events.IndexSetDeletedEvent;
import org.graylog2.indexer.retention.strategies.NoopRetentionStrategy;
import org.graylog2.indexer.retention.strategies.NoopRetentionStrategyConfig;
import org.graylog2.indexer.rotation.strategies.MessageCountRotationStrategy;
import org.graylog2.indexer.rotation.strategies.MessageCountRotationStrategyConfig;
import org.graylog2.plugin.cluster.ClusterConfigService;
import org.graylog2.plugin.system.NodeId;
import org.graylog2.shared.bindings.providers.ObjectMapperProvider;
import org.graylog2.shared.plugins.ChainingClassLoader;
import org.graylog2.streams.StreamService;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.mongojack.DBQuery;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CopyOnWriteArrayList;
import static com.lordofthejars.nosqlunit.mongodb.InMemoryMongoDb.InMemoryMongoRuleBuilder.newInMemoryMongoDbRule;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;
public class MongoIndexSetServiceTest {
@ClassRule
public static final InMemoryMongoDb IN_MEMORY_MONGO_DB = newInMemoryMongoDbRule().build();
@Rule
public final MongoConnectionRule mongoRule = MongoConnectionRule.build("index-sets");
@Rule
public final MockitoRule mockitoRule = MockitoJUnit.rule();
private final ObjectMapper objectMapper = new ObjectMapperProvider().get();
private final MongoJackObjectMapperProvider objectMapperProvider = new MongoJackObjectMapperProvider(objectMapper);
private ClusterEventBus clusterEventBus;
private MongoIndexSetService indexSetService;
private ClusterConfigService clusterConfigService;
@Mock
private StreamService streamService;
@Mock
private NodeId nodeId;
@Before
public void setUp() throws Exception {
clusterEventBus = new ClusterEventBus();
clusterConfigService = new ClusterConfigServiceImpl(objectMapperProvider, mongoRule.getMongoConnection(),
nodeId, new ChainingClassLoader(getClass().getClassLoader()), clusterEventBus);
indexSetService = new MongoIndexSetService(mongoRule.getMongoConnection(), objectMapperProvider, streamService, clusterConfigService, clusterEventBus);
}
@Test
@UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT)
public void getWithStringId() throws Exception {
final Optional<IndexSetConfig> indexSetConfig = indexSetService.get("57f3d721a43c2d59cb750001");
assertThat(indexSetConfig)
.isPresent()
.contains(
IndexSetConfig.create(
"57f3d721a43c2d59cb750001",
"Test 1",
"This is the index set configuration for Test 1",
true,
"test_1",
4,
1,
MessageCountRotationStrategy.class.getCanonicalName(),
MessageCountRotationStrategyConfig.create(1000),
NoopRetentionStrategy.class.getCanonicalName(),
NoopRetentionStrategyConfig.create(10),
ZonedDateTime.of(2016, 10, 4, 17, 0, 0, 0, ZoneOffset.UTC)
)
);
}
@Test
@UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT)
public void getReturnsExistingIndexSetConfig() throws Exception {
final Optional<IndexSetConfig> indexSetConfig = indexSetService.get(new ObjectId("57f3d721a43c2d59cb750001"));
assertThat(indexSetConfig)
.isPresent()
.contains(
IndexSetConfig.create(
"57f3d721a43c2d59cb750001",
"Test 1",
"This is the index set configuration for Test 1",
true,
"test_1",
4,
1,
MessageCountRotationStrategy.class.getCanonicalName(),
MessageCountRotationStrategyConfig.create(1000),
NoopRetentionStrategy.class.getCanonicalName(),
NoopRetentionStrategyConfig.create(10),
ZonedDateTime.of(2016, 10, 4, 17, 0, 0, 0, ZoneOffset.UTC)
)
);
}
@Test
@UsingDataSet(loadStrategy = LoadStrategyEnum.DELETE_ALL)
public void getReturnsAbsentOptionalIfIndexSetConfigDoesNotExist() throws Exception {
final Optional<IndexSetConfig> indexSetConfig = indexSetService.get(new ObjectId("57f3d3f0a43c2d595eb0a348"));
assertThat(indexSetConfig).isEmpty();
}
@Test
@UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT)
public void getDefault() throws Exception {
clusterConfigService.write(DefaultIndexSetConfig.create("57f3d721a43c2d59cb750002"));
final IndexSetConfig indexSetConfig = indexSetService.getDefault();
assertThat(indexSetConfig).isNotNull();
assertThat(indexSetConfig.id()).isEqualTo("57f3d721a43c2d59cb750002");
}
@Test(expected = IllegalStateException.class)
@UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT)
public void getDefaultWithoutDefault() throws Exception {
indexSetService.getDefault();
}
@Test
public void findOne() throws Exception {
final Optional<IndexSetConfig> config3 = indexSetService.findOne(DBQuery.is("title", "Test 2"));
assertThat(config3).isPresent();
assertThat(config3.get().id()).isEqualTo("57f3d721a43c2d59cb750002");
final Optional<IndexSetConfig> config4 = indexSetService.findOne(DBQuery.is("title", "__yolo"));
assertThat(config4).isNotPresent();
}
@Test
@UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT)
public void findAll() throws Exception {
final List<IndexSetConfig> configs = indexSetService.findAll();
assertThat(configs)
.isNotEmpty()
.hasSize(2)
.containsExactly(
IndexSetConfig.create(
"57f3d721a43c2d59cb750001",
"Test 1",
"This is the index set configuration for Test 1",
true,
"test_1",
4,
1,
MessageCountRotationStrategy.class.getCanonicalName(),
MessageCountRotationStrategyConfig.create(1000),
NoopRetentionStrategy.class.getCanonicalName(),
NoopRetentionStrategyConfig.create(10),
ZonedDateTime.of(2016, 10, 4, 17, 0, 0, 0, ZoneOffset.UTC)
),
IndexSetConfig.create(
"57f3d721a43c2d59cb750002",
"Test 2",
null,
true,
"test_2",
1,
0,
MessageCountRotationStrategy.class.getCanonicalName(),
MessageCountRotationStrategyConfig.create(2500),
NoopRetentionStrategy.class.getCanonicalName(),
NoopRetentionStrategyConfig.create(25),
ZonedDateTime.of(2016, 10, 4, 18, 0, 0, 0, ZoneOffset.UTC)
)
);
}
@Test
@UsingDataSet(loadStrategy = LoadStrategyEnum.DELETE_ALL)
public void save() throws Exception {
final IndexSetCreatedSubscriber subscriber = new IndexSetCreatedSubscriber();
clusterEventBus.registerClusterEventSubscriber(subscriber);
final IndexSetConfig indexSetConfig = IndexSetConfig.create(
"Test 3",
null,
true,
"test_3",
10,
0,
MessageCountRotationStrategy.class.getCanonicalName(),
MessageCountRotationStrategyConfig.create(10000),
NoopRetentionStrategy.class.getCanonicalName(),
NoopRetentionStrategyConfig.create(5),
ZonedDateTime.of(2016, 10, 4, 12, 0, 0, 0, ZoneOffset.UTC)
);
final IndexSetConfig savedIndexSetConfig = indexSetService.save(indexSetConfig);
final Optional<IndexSetConfig> retrievedIndexSetConfig = indexSetService.get(savedIndexSetConfig.id());
assertThat(retrievedIndexSetConfig)
.isPresent()
.contains(savedIndexSetConfig);
assertThat(subscriber.getEvents())
.hasSize(1)
.containsExactly(IndexSetCreatedEvent.create(savedIndexSetConfig));
}
@Test
@UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT)
public void deleteWithStringId() throws Exception {
final IndexSetDeletedSubscriber subscriber = new IndexSetDeletedSubscriber();
clusterEventBus.registerClusterEventSubscriber(subscriber);
final int deletedEntries = indexSetService.delete("57f3d721a43c2d59cb750001");
assertThat(deletedEntries).isEqualTo(1);
assertThat(indexSetService.get("57f3d721a43c2d59cb750001")).isEmpty();
assertThat(subscriber.getEvents())
.hasSize(1)
.containsExactly(IndexSetDeletedEvent.create("57f3d721a43c2d59cb750001"));
}
@Test
@UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT)
public void deleteRemovesExistingIndexSetConfig() throws Exception {
final IndexSetDeletedSubscriber subscriber = new IndexSetDeletedSubscriber();
clusterEventBus.registerClusterEventSubscriber(subscriber);
final int deletedEntries = indexSetService.delete(new ObjectId("57f3d721a43c2d59cb750001"));
assertThat(deletedEntries).isEqualTo(1);
assertThat(indexSetService.get("57f3d721a43c2d59cb750001")).isEmpty();
assertThat(subscriber.getEvents())
.hasSize(1)
.containsExactly(IndexSetDeletedEvent.create("57f3d721a43c2d59cb750001"));
}
@Test
@UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT)
public void deleteDoesNothingIfIndexSetConfigDoesNotExist() throws Exception {
final IndexSetDeletedSubscriber subscriber = new IndexSetDeletedSubscriber();
clusterEventBus.registerClusterEventSubscriber(subscriber);
final int deletedEntries = indexSetService.delete("57f3d721a43c2d59cb750003");
assertThat(deletedEntries).isEqualTo(0);
assertThat(indexSetService.get("57f3d721a43c2d59cb750001")).isPresent();
assertThat(indexSetService.get("57f3d721a43c2d59cb750003")).isEmpty();
assertThat(indexSetService.findAll()).hasSize(2);
assertThat(subscriber.getEvents()).isEmpty();
}
@Test
@UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT)
public void deleteWithAssignedStreams() throws Exception {
final IndexSetDeletedSubscriber subscriber = new IndexSetDeletedSubscriber();
clusterEventBus.registerClusterEventSubscriber(subscriber);
final FakeStream stream1 = new FakeStream("Test stream 1");
final String streamId = "57f3d721a43c2d59cb750001";
stream1.setIndexSetId(streamId);
when(streamService.loadAllWithIndexSet(streamId)).thenReturn(Collections.singletonList(stream1));
final int deletedEntries = indexSetService.delete(streamId);
assertThat(deletedEntries).isEqualTo(0);
assertThat(indexSetService.get(streamId)).isPresent();
assertThat(indexSetService.findAll()).hasSize(2);
assertThat(subscriber.getEvents()).isEmpty();
}
private static class IndexSetCreatedSubscriber {
private final List<IndexSetCreatedEvent> events = new CopyOnWriteArrayList<>();
@Subscribe
public void createdEvent(IndexSetCreatedEvent event) {
events.add(event);
}
public List<IndexSetCreatedEvent> getEvents() {
return events;
}
}
private static class IndexSetDeletedSubscriber {
private final List<IndexSetDeletedEvent> events = new CopyOnWriteArrayList<>();
@Subscribe
public void createdEvent(IndexSetDeletedEvent event) {
events.add(event);
}
public List<IndexSetDeletedEvent> getEvents() {
return events;
}
}
}