-
Notifications
You must be signed in to change notification settings - Fork 1k
/
ViewServiceUsesViewRequirementsTest.java
191 lines (156 loc) · 7.17 KB
/
ViewServiceUsesViewRequirementsTest.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
package org.graylog.plugins.enterprise.search.views;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.google.common.collect.ImmutableSet;
import com.lordofthejars.nosqlunit.annotation.UsingDataSet;
import com.lordofthejars.nosqlunit.core.LoadStrategyEnum;
import com.lordofthejars.nosqlunit.mongodb.InMemoryMongoDb;
import org.graylog.plugins.database.MongoConnectionRule;
import org.graylog2.bindings.providers.MongoJackObjectMapperProvider;
import org.graylog2.database.PaginatedList;
import org.graylog2.plugin.cluster.ClusterConfigService;
import org.graylog2.search.SearchQueryParser;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import static com.lordofthejars.nosqlunit.mongodb.InMemoryMongoDb.InMemoryMongoRuleBuilder.newInMemoryMongoDbRule;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class ViewServiceUsesViewRequirementsTest {
@ClassRule
public static final InMemoryMongoDb IN_MEMORY_MONGO_DB = newInMemoryMongoDbRule().build();
@Rule
public MongoConnectionRule mongoRule = MongoConnectionRule.build("test");
@Rule
public MockitoRule rule = MockitoJUnit.rule();
@Mock
private ClusterConfigService clusterConfigService;
private final SearchQueryParser searchQueryParser = new SearchQueryParser(ViewDTO.FIELD_TITLE, Collections.emptyMap());
class MongoJackObjectMapperProviderForTest extends MongoJackObjectMapperProvider {
public MongoJackObjectMapperProviderForTest(ObjectMapper objectMapper) {
super(objectMapper);
}
@Override
public ObjectMapper get() {
return super.get().registerModule(new Jdk8Module());
}
}
@Mock
private ViewRequirements.Factory viewRequirementsFactory;
@Mock
private ViewRequirements viewRequirements;
private ViewService viewService;
@Before
public void setUp() throws Exception {
final MongoJackObjectMapperProvider objectMapperProvider = new MongoJackObjectMapperProviderForTest(new ObjectMapper());
this.viewService = new ViewService(
mongoRule.getMongoConnection(),
objectMapperProvider,
clusterConfigService,
viewRequirementsFactory
);
when(viewRequirementsFactory.create(any(ViewDTO.class))).then(invocation -> new ViewRequirements(Collections.emptySet(), invocation.getArgument(0)));
}
@After
public void tearDown() {
mongoRule.getMongoConnection().getMongoDatabase().drop();
}
@Test
public void saveGetsViewRequirements() {
final ViewDTO dto1 = ViewDTO.builder()
.title("View 1")
.summary("This is a nice view")
.description("This contains lots of descriptions for the view.")
.searchId("abc123")
.properties(ImmutableSet.of("read-only"))
.state(Collections.emptyMap())
.owner("peter")
.build();
viewService.save(dto1);
verify(viewRequirementsFactory, times(1)).create(dto1);
}
@Test
public void updateGetsViewRequirements() {
final ViewDTO dto1 = ViewDTO.builder()
.id("5cee4e458fb0b6ba14faa203")
.title("View 1")
.summary("This is a nice view")
.description("This contains lots of descriptions for the view.")
.searchId("abc123")
.properties(ImmutableSet.of("read-only"))
.state(Collections.emptyMap())
.owner("peter")
.build();
viewService.update(dto1);
verify(viewRequirementsFactory, times(1)).create(dto1);
}
@Test
@UsingDataSet(locations = "views.json", loadStrategy = LoadStrategyEnum.CLEAN_INSERT)
public void getReturnsViewWithViewRequirements() {
final ViewDTO view = viewService.get("5ced4a4485b52a86b96a0a63")
.orElseThrow(() -> new IllegalStateException("This should not happen!"));
verify(viewRequirementsFactory, times(1)).create(view);
}
@Test
@UsingDataSet(locations = "views.json", loadStrategy = LoadStrategyEnum.CLEAN_INSERT)
public void getDefaultReturnsViewWithViewRequirements() {
when(clusterConfigService.get(ViewClusterConfig.class)).thenReturn(
ViewClusterConfig.builder().defaultViewId("5ced4df1d6e8104c16f50e00").build()
);
final ViewDTO defaultView = viewService.getDefault()
.orElseThrow(() -> new IllegalStateException("This should not happen!"));
verify(viewRequirementsFactory, times(1)).create(defaultView);
}
@Test
@UsingDataSet(locations = "views.json", loadStrategy = LoadStrategyEnum.CLEAN_INSERT)
public void forSearchReturnsViewWithViewRequirements() {
final ViewDTO view = new ArrayList<>(viewService.forSearch("5ced4deed6e8104c16f50df9")).get(0);
verify(viewRequirementsFactory, times(1)).create(view);
}
@Test
@UsingDataSet(locations = "views.json", loadStrategy = LoadStrategyEnum.CLEAN_INSERT)
public void searchPaginatedReturnsViewWithViewRequirements() {
final PaginatedList<ViewDTO> views = viewService.searchPaginated(
searchQueryParser.parse("*"),
view -> true,
"desc",
"title",
1,
5
);
final ArgumentCaptor<ViewDTO> captor = ArgumentCaptor.forClass(ViewDTO.class);
verify(viewRequirementsFactory, times(views.delegate().size())).create(captor.capture());
assertThat(captor.getAllValues()).isEqualTo(views.delegate());
}
@Test
@UsingDataSet(locations = "views.json", loadStrategy = LoadStrategyEnum.CLEAN_INSERT)
public void streamAllReturnsViewWithViewRequirements() {
final List<ViewDTO> views = viewService.streamAll().collect(Collectors.toList());
final ArgumentCaptor<ViewDTO> captor = ArgumentCaptor.forClass(ViewDTO.class);
verify(viewRequirementsFactory, times(views.size())).create(captor.capture());
assertThat(captor.getAllValues()).isEqualTo(views);
}
@Test
@UsingDataSet(locations = "views.json", loadStrategy = LoadStrategyEnum.CLEAN_INSERT)
public void streamByIdsReturnsViewWithViewRequirements() {
final List<ViewDTO> views = viewService.streamByIds(
ImmutableSet.of("5ced4df1d6e8104c16f50e00", "5ced4a4485b52a86b96a0a63")
).collect(Collectors.toList());
final ArgumentCaptor<ViewDTO> captor = ArgumentCaptor.forClass(ViewDTO.class);
verify(viewRequirementsFactory, times(views.size())).create(captor.capture());
assertThat(captor.getAllValues()).isEqualTo(views);
}
}