-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
V20190304102700_MigrateMessageListStructureTest.java
119 lines (102 loc) · 5.37 KB
/
V20190304102700_MigrateMessageListStructureTest.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
package org.graylog.plugins.enterprise.migration;
import com.lordofthejars.nosqlunit.annotation.UsingDataSet;
import com.lordofthejars.nosqlunit.core.LoadStrategyEnum;
import com.lordofthejars.nosqlunit.mongodb.InMemoryMongoDb;
import com.mongodb.BasicDBObject;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.graylog.plugins.database.MongoConnectionRule;
import org.graylog.plugins.enterprise.migrations.V20190304102700_MigrateMessageListStructure;
import org.graylog2.migrations.Migration;
import org.graylog2.plugin.cluster.ClusterConfigService;
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 java.time.ZonedDateTime;
import java.util.List;
import static com.lordofthejars.nosqlunit.mongodb.InMemoryMongoDb.InMemoryMongoRuleBuilder.newInMemoryMongoDbRule;
import static org.assertj.core.api.Assertions.assertThat;
public class V20190304102700_MigrateMessageListStructureTest {
@ClassRule
public static final InMemoryMongoDb IN_MEMORY_MONGO_DB = newInMemoryMongoDbRule().build();
@Rule
public final MongoConnectionRule mongoRule = MongoConnectionRule.build("test");
private Migration migration;
@Rule
public final MockitoRule mockitoRule = MockitoJUnit.rule();
@Mock
private ClusterConfigService clusterConfigService;
@Before
public void setUp() throws Exception {
migration = new V20190304102700_MigrateMessageListStructure(mongoRule.getMongoConnection(), clusterConfigService);
}
@Test
public void createdAt() {
// Test the date to detect accidental changes to it.
assertThat(migration.createdAt()).isEqualTo(ZonedDateTime.parse("2019-04-03T10:27:00Z"));
}
@Test
@UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT)
public void testMigratingViewStructure() {
final BasicDBObject dbQuery1 = new BasicDBObject();
dbQuery1.put("_id", new ObjectId("58458e442f857c314491344e"));
final MongoCollection<Document> collection = mongoRule.getMongoConnection()
.getMongoDatabase()
.getCollection("views");
migration.upgrade();
final FindIterable<Document> views = collection.find(dbQuery1);
final Document view1 = views.first();
@SuppressWarnings("unchecked")
final List<Document> widgets1 = (List) view1.get("state", Document.class).get("a2a804b7-27cf-4cac-8015-58d9a9640d33", Document.class).get("widgets");
assertThat(widgets1.size()).isEqualTo(2);
assertThat(widgets1.stream().filter(widget -> widget.getString("type").equals("messages")).count()).isEqualTo(1);
assertThat(widgets1.stream().filter(widget -> widget.getString("type").equals("messages")).allMatch((widget) -> {
final Document config = widget.get("config", Document.class);
@SuppressWarnings("unchecked")
final List<String> fields = (List) config.get("fields");
final boolean startWithTimestamp = fields.get(0).contains("timestamp");
final boolean showMessageRow = config.getBoolean("show_message_row");
return startWithTimestamp && showMessageRow;
})).isTrue();
final BasicDBObject dbQuery2 = new BasicDBObject();
dbQuery2.put("_id", new ObjectId("58458e442f857c314491344f"));
final FindIterable<Document> views2 = collection.find(dbQuery2);
final Document view2 = views2.first();
final Document states = view2.get("state", Document.class);
assertThat(states.values().size()).isEqualTo(13);
assertThat(states.keySet()).containsExactly(
"7c042319-530a-41b9-9dbb-9676fb1da1a4",
"9e5144be-a445-4289-a4cc-0f55142524bc",
"c13b2482-60e7-4b1e-98c9-0df8d6da8230",
"5adc9297-dfc8-4fd9-b422-cbb097715a62",
"ade8c853-503c-407f-b125-efbe2d368973",
"cc2bf983-b398-4295-bf01-1c10ed1a97e1",
"64feccae-9447-40ef-a401-79a7972078a2",
"7c7e04c6-f9f0-495c-91cc-865f60687f8c",
"eeaa8838-616f-40c0-88c0-1059ac64f37e",
"91c6f8c9-024c-48ec-a869-90548fad218a",
"955a71f2-673a-4e1c-a99f-ef97b1b4ae71",
"343ff7b6-4554-49d4-bc0b-1339fdc5dac0",
"7a84d053-e40a-48c1-a433-97521f7ce7ef");
states.values().forEach(state -> {
@SuppressWarnings("unchecked")
final List<Document> widgets2 = (List) ((Document) state).get("widgets");
assertThat(widgets2.stream().filter(widget -> widget.getString("type").equals("messages")).count()).isGreaterThan(0);
widgets2.stream().filter(widget -> widget.getString("type").equals("messages")).forEach((widget) -> {
final Document config = widget.get("config", Document.class);
@SuppressWarnings("unchecked")
final List<String> fields = (List) config.get("fields");
final boolean startWithTimestamp = fields.get(0).contains("timestamp");
final boolean showMessageRow = config.getBoolean("show_message_row");
assertThat(startWithTimestamp).isTrue();
assertThat(showMessageRow).isTrue();
});
});
}
}