-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathChannelControllerIT.java
More file actions
565 lines (504 loc) · 22.6 KB
/
ChannelControllerIT.java
File metadata and controls
565 lines (504 loc) · 22.6 KB
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
package org.phoebus.channelfinder;
import com.google.common.collect.Iterables;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.phoebus.channelfinder.configuration.ElasticConfig;
import org.phoebus.channelfinder.entity.Channel;
import org.phoebus.channelfinder.entity.Property;
import org.phoebus.channelfinder.entity.Tag;
import org.phoebus.channelfinder.repository.ChannelRepository;
import org.phoebus.channelfinder.repository.PropertyRepository;
import org.phoebus.channelfinder.repository.TagRepository;
import org.phoebus.channelfinder.rest.api.IChannel;
import org.phoebus.channelfinder.rest.controller.ChannelController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.web.server.ResponseStatusException;
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@WebMvcTest(ChannelController.class)
@WithMockUser(roles = "CF-ADMINS")
@TestPropertySource(value = "classpath:application_test.properties")
@ContextConfiguration(
classes = {ChannelController.class, ElasticConfig.class, ChannelRepository.class})
class ChannelControllerIT {
@Autowired IChannel channelManager;
@Autowired TagRepository tagRepository;
@Autowired PropertyRepository propertyRepository;
@Autowired ChannelRepository channelRepository;
/** read a single channel */
@Test
void readXmlChannel() {
testProperties.forEach(prop -> prop.setValue("value"));
Channel testChannel0 = new Channel("testChannel0", "testOwner", testProperties, testTags);
cleanupTestChannels = Arrays.asList(testChannel0);
Channel createdChannel = channelManager.create(testChannel0.getName(), testChannel0);
Channel readChannel = channelManager.read(createdChannel.getName());
// verify the channel was read as expected
Assertions.assertEquals(createdChannel, readChannel, "Failed to read the channel");
}
/** attempt to read a single non existent channel */
@Test
void readNonExistingXmlChannel() {
// verify the channel failed to be read, as expected
Assertions.assertThrows(
ResponseStatusException.class, () -> channelManager.read("fakeChannel"));
}
/** create a simple channel */
@Test
void createXmlChannel() {
Channel testChannel0 = new Channel("testChannel0", "testOwner");
cleanupTestChannels = Arrays.asList(testChannel0);
// Create a simple channel
Channel createdChannel0 = channelManager.create(testChannel0.getName(), testChannel0);
// verify the channel was created as expected
Assertions.assertEquals(testChannel0, createdChannel0, "Failed to create the channel");
// Tag createdTag1 = tagManager.create("fakeTag", copy(testTag1));
// // verify the tag was created as expected
// assertEquals("Failed to create the tag",testTag1,createdTag1);
// Update the test channel with a new owner
testChannel0.setOwner("updateTestOwner");
Channel updatedChannel0 = channelManager.create(testChannel0.getName(), testChannel0);
// verify the channel was created as expected
Assertions.assertEquals(testChannel0, updatedChannel0, "Failed to create the channel");
}
/** Rename a simple channel using create */
@Test
void renameByCreateXmlChannel() {
Channel testChannel0 = new Channel("testChannel0", "testOwner");
Channel testChannel1 = new Channel("testChannel1", "testOwner");
cleanupTestChannels = Arrays.asList(testChannel0, testChannel1);
Channel createdChannel = channelManager.create(testChannel0.getName(), testChannel0);
createdChannel = channelManager.create(testChannel0.getName(), testChannel1);
// verify that the old channel "testChannel0" was replaced with the new "testChannel1"
Assertions.assertEquals(testChannel1, createdChannel, "Failed to create the channel");
// verify that the old channel is no longer present
Assertions.assertFalse(
channelRepository.existsById(testChannel0.getName()), "Failed to replace the old channel");
}
/** create a single channel with tags and properties */
@Test
void createXmlChannel2() {
testProperties.forEach(prop -> prop.setValue("value"));
Channel testChannel0 = new Channel("testChannel0", "testOwner", testProperties, testTags);
cleanupTestChannels = Arrays.asList(testChannel0);
Channel createdChannel = channelManager.create(testChannel0.getName(), testChannel0);
try {
Channel foundChannel = channelRepository.findById(testChannel0.getName()).get();
Assertions.assertEquals(
testChannel0,
foundChannel,
"Failed to create the channel. Expected "
+ testChannel0.toLog()
+ " found "
+ foundChannel.toLog());
} catch (Exception e) {
Assertions.fail("Failed to create/find the channel");
}
// Tag createdTag1 = tagManager.create("fakeTag", copy(testTag1));
// // verify the tag was created as expected
// assertEquals("Failed to create the tag",testTag1,createdTag1);
// Update the test channel with a new owner
testChannel0.setOwner("updateTestOwner");
createdChannel = channelManager.create(testChannel0.getName(), testChannel0);
try {
Channel foundChannel = channelRepository.findById(testChannel0.getName()).get();
Assertions.assertEquals(
testChannel0,
foundChannel,
"Failed to create the channel. Expected "
+ testChannel0.toLog()
+ " found "
+ foundChannel.toLog());
} catch (Exception e) {
Assertions.fail("Failed to create/find the channel");
}
}
/** Rename a single channel with tags and properties using create */
@Test
void renameByCreateXmlChannel2() {
testProperties.forEach(prop -> prop.setValue("value"));
Channel testChannel0 = new Channel("testChannel0", "testOwner", testProperties, testTags);
Channel testChannel1 = new Channel("testChannel1", "testOwner", testProperties, testTags);
cleanupTestChannels = Arrays.asList(testChannel0, testChannel1);
// Create the testChannel0
Channel createdChannel = channelManager.create(testChannel0.getName(), testChannel0);
// update the testChannel0 with testChannel1
createdChannel = channelManager.create(testChannel0.getName(), testChannel1);
// verify that the old channel "testChannel0" was replaced with the new "testChannel1"
try {
Channel foundChannel = channelRepository.findById(testChannel1.getName()).get();
Assertions.assertEquals(testChannel1, foundChannel, "Failed to create the channel");
} catch (Exception e) {
Assertions.fail("Failed to create/find the channel");
}
// verify that the old channel is no longer present
Assertions.assertFalse(
channelRepository.existsById(testChannel0.getName()), "Failed to replace the old channel");
}
/** create multiple channels */
@Test
void createXmlChannels() {
testProperties.forEach(prop -> prop.setValue("value"));
Channel testChannel0 = new Channel("testChannel0", "testOwner", testProperties, testTags);
Channel testChannel1 = new Channel("testChannel1", "testOwner", testProperties, testTags);
Channel testChannel2 = new Channel("testChannel2", "testOwner");
List<Channel> testChannels = Arrays.asList(testChannel0, testChannel1, testChannel2);
cleanupTestChannels = testChannels;
Iterable<Channel> createdChannels = channelManager.create(testChannels);
List<Channel> foundChannels = new ArrayList<Channel>();
testChannels.forEach(
chan -> foundChannels.add(channelRepository.findById(chan.getName()).get()));
// verify the channels were created as expected
Assertions.assertTrue(
Iterables.elementsEqual(testChannels, foundChannels), "Failed to create the channels");
}
/** create by overriding multiple channels */
@Test
void createXmlChannelsWithOverride() {
testProperties.forEach(prop -> prop.setValue("value"));
Channel testChannel0 = new Channel("testChannel0", "testOwner");
Channel testChannel1 = new Channel("testChannel1", "testOwner", testProperties, testTags);
List<Channel> testChannels = Arrays.asList(testChannel0, testChannel1);
cleanupTestChannels = testChannels;
// Create a set of original channels to be overriden
channelManager.create(testChannels);
// Now update the test channels
testChannel0.setOwner("testOwner-updated");
testChannel1.setTags(Collections.emptyList());
testChannel1.setProperties(Collections.emptyList());
List<Channel> updatedTestChannels = Arrays.asList(testChannel0, testChannel1);
channelManager.create(updatedTestChannels);
List<Channel> foundChannels = new ArrayList<Channel>();
testChannels.forEach(
chan -> foundChannels.add(channelRepository.findById(chan.getName()).get()));
// verify the channels were created as expected
Assertions.assertTrue(
Iterables.elementsEqual(updatedTestChannels, foundChannels),
"Failed to create the channels");
}
/** update a channel */
@Test
void updateXmlChannel() {
testProperties.forEach(prop -> prop.setValue("value"));
// A test channel with only name and owner
Channel testChannel0 = new Channel("testChannel0", "testOwner");
// A test channel with name, owner, tags and props
Channel testChannel1 = new Channel("testChannel1", "testOwner", testProperties, testTags);
cleanupTestChannels = Arrays.asList(testChannel0, testChannel1);
// Update on a non-existing channel should result in the creation of that channel
// 1. Test a simple channel
Channel returnedChannel = channelManager.update(testChannel0.getName(), testChannel0);
Assertions.assertEquals(
testChannel0, returnedChannel, "Failed to update channel " + testChannel0);
Assertions.assertEquals(
testChannel0,
channelRepository.findById(testChannel0.getName()).get(),
"Failed to update channel " + testChannel0);
// 2. Test a channel with tags and props
returnedChannel = channelManager.update(testChannel1.getName(), testChannel1);
Assertions.assertEquals(
testChannel1, returnedChannel, "Failed to update channel " + testChannel1);
Assertions.assertEquals(
testChannel1,
channelRepository.findById(testChannel1.getName()).get(),
"Failed to update channel " + testChannel1);
// Update the channel owner
testChannel0.setOwner("newTestOwner");
returnedChannel = channelManager.update(testChannel0.getName(), testChannel0);
Assertions.assertEquals(
testChannel0, returnedChannel, "Failed to update channel " + testChannel0);
Assertions.assertEquals(
testChannel0,
channelRepository.findById(testChannel0.getName()).get(),
"Failed to update channel " + testChannel0);
testChannel1.setOwner("newTestOwner");
returnedChannel = channelManager.update(testChannel1.getName(), testChannel1);
Assertions.assertEquals(
testChannel1, returnedChannel, "Failed to update channel " + testChannel1);
Assertions.assertEquals(
testChannel1,
channelRepository.findById(testChannel1.getName()).get(),
"Failed to update channel " + testChannel1);
}
/** Rename a channel using update */
@Test
void renameByUpdateXmlChannel() {
testProperties.forEach(prop -> prop.setValue("value"));
Channel testChannel0 = new Channel("testChannel0", "testOwner");
Channel testChannel1 = new Channel("testChannel1", "testOwner");
Channel testChannel2 = new Channel("testChannel2", "testOwner", testProperties, testTags);
Channel testChannel3 = new Channel("testChannel3", "testOwner", testProperties, testTags);
cleanupTestChannels = Arrays.asList(testChannel0, testChannel1, testChannel2, testChannel3);
// Create the testChannels
Channel createdChannel = channelManager.create(testChannel0.getName(), testChannel0);
Channel createdChannelWithItems = channelManager.create(testChannel2.getName(), testChannel2);
// update the testChannels
Channel renamedChannel = channelManager.update(testChannel0.getName(), testChannel1);
Channel renamedChannelWithItems = channelManager.update(testChannel2.getName(), testChannel3);
// verify that the old channels were replaced by the new ones
try {
Channel foundChannel = channelRepository.findById(testChannel1.getName()).get();
Assertions.assertEquals(testChannel1, foundChannel, "Failed to create the channel");
} catch (Exception e) {
Assertions.fail("Failed to create/find the channel");
}
// verify that the old channel is no longer present
Assertions.assertFalse(
channelRepository.existsById(testChannel0.getName()), "Failed to replace the old channel");
try {
Channel foundChannel = channelRepository.findById(testChannel3.getName()).get();
Assertions.assertEquals(testChannel3, foundChannel, "Failed to create the channel");
} catch (Exception e) {
Assertions.fail("Failed to create/find the channel");
}
// verify that the old channel is no longer present
Assertions.assertFalse(
channelRepository.existsById(testChannel2.getName()), "Failed to replace the old channel");
// TODO add test for failure case
}
/** update a channel by adding tags and adding properties and changing properties */
@Test
void updateXmlChannelItems() {
testProperties.forEach(prop -> prop.setValue("value"));
Channel testChannel0 =
new Channel(
"testChannel0",
"testOwner",
Arrays.asList(testProperties.get(0), testProperties.get(1)),
Arrays.asList(testTags.get(0), testTags.get(1)));
cleanupTestChannels = Arrays.asList(testChannel0);
// Create the testChannel
Channel createdChannel = channelManager.create(testChannel0.getName(), testChannel0);
// set up the new testChannel
testProperties.get(1).setValue("newValue");
testChannel0 =
new Channel(
"testChannel0",
"testOwner",
Arrays.asList(testProperties.get(1), testProperties.get(2)),
Arrays.asList(testTags.get(1), testTags.get(2)));
// update the testChannel
Channel updatedChannel = channelManager.update(testChannel0.getName(), testChannel0);
Channel expectedChannel = new Channel("testChannel0", "testOwner", testProperties, testTags);
Channel foundChannel = channelRepository.findById("testChannel0").get();
foundChannel
.getTags()
.sort(
(Tag o1, Tag o2) -> {
return o1.getName().compareTo(o2.getName());
});
foundChannel
.getProperties()
.sort(
(Property o1, Property o2) -> {
return o1.getName().compareTo(o2.getName());
});
Assertions.assertEquals(
expectedChannel,
foundChannel,
"Did not update channel correctly, expected "
+ expectedChannel.toLog()
+ " but actual was "
+ foundChannel.toLog());
}
/**
* update multiple channels first update non-existing channels which should create them second
* update the newly created channels which should change them
*/
@Test
void updateMultipleXmlChannels() {
testProperties.forEach(prop -> prop.setValue("value"));
// A test channel with only name and owner
Channel testChannel0 = new Channel("testChannel0", "testOwner");
// A test channel with name, owner, tags and props
Channel testChannel1 = new Channel("testChannel1", "testOwner", testProperties, testTags);
List<Channel> testChannels = Arrays.asList(testChannel0, testChannel1);
cleanupTestChannels = testChannels;
// Update on non-existing channels should result in the creation of those channels
Iterable<Channel> returnedChannels = channelManager.update(testChannels);
// 1. Test a simple channel
Assertions.assertEquals(
testChannel0,
channelRepository.findById(testChannel0.getName()).get(),
"Failed to update channel " + testChannel0);
// 2. Test a channel with tags and props
Assertions.assertEquals(
testChannel1,
channelRepository.findById(testChannel1.getName()).get(),
"Failed to update channel " + testChannel1);
// Update the channel owner
testChannel0.setOwner("newTestOwner");
testChannel1.setOwner("newTestOwner");
returnedChannels = channelManager.update(testChannels);
Assertions.assertEquals(
testChannel0,
channelRepository.findById(testChannel0.getName()).get(),
"Failed to update channel " + testChannel0);
Assertions.assertEquals(
testChannel1,
channelRepository.findById(testChannel1.getName()).get(),
"Failed to update channel " + testChannel1);
}
/** update multiple channels by adding tags and adding properties and changing properties */
@Test
void updateMultipleXmlChannelsWithItems() {
testProperties.forEach(prop -> prop.setValue("value"));
Channel testChannel0 =
new Channel(
"testChannel0",
"testOwner",
Arrays.asList(testProperties.get(0), testProperties.get(1)),
Arrays.asList(testTags.get(0), testTags.get(1)));
Channel testChannel1 =
new Channel(
"testChannel1",
"testOwner",
Arrays.asList(testProperties.get(0), testProperties.get(2)),
Arrays.asList(testTags.get(0), testTags.get(2)));
List<Channel> testChannels = Arrays.asList(testChannel0, testChannel1);
cleanupTestChannels = testChannels;
// Create the testChannel
Iterable<Channel> createdChannels = channelManager.create(testChannels);
// set up the new testChannel
testProperties.forEach(prop -> prop.setValue("newValue"));
testChannel0 =
new Channel(
"testChannel0",
"testOwner",
Arrays.asList(testProperties.get(0), testProperties.get(2)),
Arrays.asList(testTags.get(0), testTags.get(2)));
testChannel1 =
new Channel(
"testChannel1",
"testOwner",
Arrays.asList(testProperties.get(0), testProperties.get(1)),
Arrays.asList(testTags.get(0), testTags.get(1)));
testChannels = Arrays.asList(testChannel0, testChannel1);
// update the testChannel
Iterable<Channel> updatedChannels = channelManager.update(testChannels);
// set up the expected testChannels
testChannel0 =
new Channel(
"testChannel0",
"testOwner",
Arrays.asList(
testProperties.get(0),
new Property("testProperty1", "testPropertyOwner1", "value"),
testProperties.get(2)),
testTags);
testChannel1 =
new Channel(
"testChannel1",
"testOwner",
Arrays.asList(
testProperties.get(0),
testProperties.get(1),
new Property("testProperty2", "testPropertyOwner2", "value")),
testTags);
Iterable<Channel> expectedChannels = Arrays.asList(testChannel0, testChannel1);
Iterable<Channel> foundChannels =
channelRepository.findAllById(Arrays.asList("testChannel0", "testChannel1"));
foundChannels.forEach(
chan ->
chan.getTags()
.sort(
(Tag o1, Tag o2) -> {
return o1.getName().compareTo(o2.getName());
}));
foundChannels.forEach(
chan ->
chan.getProperties()
.sort(
(Property o1, Property o2) -> {
return o1.getName().compareTo(o2.getName());
}));
Assertions.assertEquals(
expectedChannels,
foundChannels,
"Did not update channel correctly, expected "
+ testChannel0.toLog()
+ " and "
+ testChannel1.toLog()
+ " but actual was "
+ foundChannels.iterator().next().toLog()
+ " and "
+ foundChannels.iterator().next().toLog());
}
/** delete a channel */
@Test
void deleteXmlChannel() {
Channel testChannel0 = new Channel("testChannel0", "testOwner");
cleanupTestChannels = Arrays.asList(testChannel0);
channelManager.create(testChannel0.getName(), testChannel0);
channelManager.remove(testChannel0.getName());
// verify the channel was deleted as expected
Assertions.assertFalse(
channelRepository.existsById(testChannel0.getName()), "Failed to delete the channel");
}
// Helper operations to create and clean up the resources needed for successful
// testing of the ChannelManager operations
private final List<Tag> testTags =
Arrays.asList(
new Tag("testTag0", "testTagOwner0"),
new Tag("testTag1", "testTagOwner1"),
new Tag("testTag2", "testTagOwner2"));
private final List<Property> testProperties =
Arrays.asList(
new Property("testProperty0", "testPropertyOwner0"),
new Property("testProperty1", "testPropertyOwner1"),
new Property("testProperty2", "testPropertyOwner2"));
private List<Channel> cleanupTestChannels = Collections.emptyList();
@BeforeEach
public void setup() {
tagRepository.indexAll(testTags);
propertyRepository.indexAll(testProperties);
}
@AfterEach
public void cleanup() {
// clean up
testTags.forEach(
tag -> {
try {
tagRepository.deleteById(tag.getName());
} catch (Exception e) {
System.out.println("Failed to clean up tag: " + tag.getName());
}
});
testProperties.forEach(
property -> {
try {
propertyRepository.deleteById(property.getName());
} catch (Exception e) {
System.out.println("Failed to clean up property: " + property.getName());
}
});
cleanupTestChannels.forEach(
channel -> {
if (channelRepository.existsById(channel.getName())) {
channelRepository.deleteById(channel.getName());
}
});
}
@Autowired ElasticConfig esService;
@BeforeAll
void setupAll() {
ElasticConfigIT.setUp(esService);
}
@AfterAll
void tearDown() throws IOException {
ElasticConfigIT.teardown(esService);
}
}