-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathPropertyRepositoryIT.java
More file actions
275 lines (236 loc) · 11.2 KB
/
PropertyRepositoryIT.java
File metadata and controls
275 lines (236 loc) · 11.2 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
package org.phoebus.channelfinder;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ResponseStatusException;
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@WebMvcTest(PropertyRepository.class)
@TestPropertySource(value = "classpath:application_test.properties")
@ContextConfiguration(classes = {PropertyRepository.class, ElasticConfig.class})
class PropertyRepositoryIT {
public static final String TEST_PROPERTY_NAME = "testProperty";
@Autowired ElasticConfig esService;
@Autowired PropertyRepository propertyRepository;
@Autowired ChannelRepository channelRepository;
@AfterAll
void tearDown() throws IOException {
ElasticConfigIT.teardown(esService);
}
/** index a single property */
@Test
void indexXmlProperty() {
Property testProperty = new Property(TEST_PROPERTY_NAME, "testOwner");
Property createdProperty = propertyRepository.index(testProperty);
// verify the property was created as expected
Assertions.assertEquals(testProperty, createdProperty, "Failed to create the property");
}
/** index multiple properties */
@Test
void indexXmlProperties() {
Property testProperty = new Property(TEST_PROPERTY_NAME, "testOwner");
Property testProperty1 = new Property(TEST_PROPERTY_NAME + 1, "testOwner1");
List<Property> testProperties = Arrays.asList(testProperty, testProperty1);
Iterable<Property> createdProperties = propertyRepository.indexAll(testProperties);
// verify the properties were created as expected
Assertions.assertTrue(
Iterables.elementsEqual(testProperties, createdProperties),
"Failed to create the list of properties");
}
/** save a single property */
@Test
void saveXmlProperty() {
Property testProperty = new Property(TEST_PROPERTY_NAME, "testOwner");
Property updateTestProperty = new Property(TEST_PROPERTY_NAME, "updateTestOwner");
Property updateTestProperty1 = new Property(TEST_PROPERTY_NAME + 1, "updateTestOwner1");
Property createdProperty = propertyRepository.index(testProperty);
Property updatedTestProperty = propertyRepository.save(updateTestProperty);
// verify the property was updated as expected
Assertions.assertEquals(
updateTestProperty,
updatedTestProperty,
"Failed to update the property with the same name");
Property updatedTestProperty1 =
propertyRepository.save(TEST_PROPERTY_NAME, updateTestProperty1);
// verify the property was updated as expected
Assertions.assertEquals(
updateTestProperty1,
updatedTestProperty1,
"Failed to update the property with a different name");
}
/** save multiple properties */
@Test
void saveXmlProperties() {
Property testProperty = new Property(TEST_PROPERTY_NAME, "testOwner");
Property updateTestProperty = new Property(TEST_PROPERTY_NAME, "updateTestOwner");
Property testProperty1 = new Property(TEST_PROPERTY_NAME + 1, "testOwner1");
Property updateTestProperty1 = new Property(TEST_PROPERTY_NAME + 1, "updateTestOwner1");
List<Property> testProperties = Arrays.asList(testProperty, testProperty1);
List<Property> updateTestProperties = Arrays.asList(updateTestProperty, updateTestProperty1);
Iterable<Property> createdProperties = propertyRepository.indexAll(testProperties);
Iterable<Property> updatedTestProperties = propertyRepository.saveAll(updateTestProperties);
// verify the properties were updated as expected
Assertions.assertTrue(
Iterables.elementsEqual(updateTestProperties, updatedTestProperties),
"Failed to update the properties");
}
/** find a single property */
@Test
void findXmlProperty() {
Property testProperty = new Property(TEST_PROPERTY_NAME, "testOwner");
Optional<Property> notFoundProperty = propertyRepository.findById(testProperty.getName());
// verify the property was not found as expected
Assertions.assertTrue(
notFoundProperty.isEmpty(),
"Found the property " + testProperty.getName() + " which should not exist.");
Property createdProperty = propertyRepository.index(testProperty);
Optional<Property> foundProperty = propertyRepository.findById(createdProperty.getName());
// verify the property was found as expected
Assertions.assertEquals(createdProperty, foundProperty.get(), "Failed to find the property");
testProperty.setValue("test");
Channel channel =
new Channel("testChannel", "testOwner", Arrays.asList(testProperty), new ArrayList<Tag>());
Channel createdChannel = channelRepository.index(channel);
foundProperty = propertyRepository.findById(createdProperty.getName(), true);
createdProperty.setChannels(Arrays.asList(channel));
// verify the property was found as expected
Property expectedProperty = new Property(createdProperty.getName(), createdProperty.getOwner());
expectedProperty.setChannels(Arrays.asList(createdChannel));
Assertions.assertEquals(expectedProperty, foundProperty.get(), "Failed to find the property");
}
/** check if a property exists */
@Test
void testPropertyExists() {
// check that non existing property returns false
Assertions.assertFalse(
propertyRepository.existsById("no-property"),
"Failed to check the non existing property :" + "no-property");
Property testProperty = new Property(TEST_PROPERTY_NAME, "testOwner");
Assertions.assertFalse(
propertyRepository.existsById(testProperty.getName()),
"Test property " + testProperty.getName() + " already exists");
Property createdProperty = propertyRepository.index(testProperty);
// verify the property exists as expected
Assertions.assertTrue(
propertyRepository.existsById(testProperty.getName()),
"Failed to check the existance of " + testProperty.getName());
}
/** find all properties */
@Test
void findAllXmlProperties() {
Property testProperty = new Property(TEST_PROPERTY_NAME, "testOwner");
Property testProperty1 = new Property(TEST_PROPERTY_NAME + 1, "testOwner1");
List<Property> testProperties = Arrays.asList(testProperty, testProperty1);
try {
Set<Property> createdProperties =
Sets.newHashSet(propertyRepository.indexAll(testProperties));
Set<Property> listedProperties = Sets.newHashSet(propertyRepository.findAll());
// verify the properties were listed as expected
Assertions.assertEquals(
createdProperties, listedProperties, "Failed to list all created properties");
} catch (Exception e) {
Assertions.fail(e);
}
}
/** find all properties */
@Test
void countXmlProperties() {
Property testProperty = new Property(TEST_PROPERTY_NAME, "testOwner");
Property testProperty1 = new Property(TEST_PROPERTY_NAME + 1, "testOwner1");
List<Property> testProperties = Arrays.asList(testProperty, testProperty1);
try {
Set<Property> createdProperties =
Sets.newHashSet(propertyRepository.indexAll(testProperties));
long listedPropertiesCount = propertyRepository.count();
// verify the properties were listed as expected
Assertions.assertEquals(
createdProperties.size(),
listedPropertiesCount,
"Failed to count all created properties");
} catch (Exception e) {
Assertions.fail(e);
}
}
/** find multiple properties */
@Test
void findXmlProperties() {
Property testProperty = new Property(TEST_PROPERTY_NAME, "testOwner");
Property testProperty1 = new Property(TEST_PROPERTY_NAME + 1, "testOwner1");
List<Property> testProperties = Arrays.asList(testProperty, testProperty1);
List<String> propertyNames = Arrays.asList(testProperty.getName(), testProperty1.getName());
Iterable<Property> notFoundProperties = null;
Iterable<Property> foundProperties = null;
try {
notFoundProperties = propertyRepository.findAllById(propertyNames);
} catch (ResponseStatusException e) {
} finally {
// verify the properties were not found as expected
Assertions.assertNotEquals(testProperties, notFoundProperties, "Found the properties");
}
Iterable<Property> createdProperties = propertyRepository.indexAll(testProperties);
try {
foundProperties = propertyRepository.findAllById(propertyNames);
} catch (ResponseStatusException e) {
} finally {
// verify the properties were found as expected
Assertions.assertEquals(createdProperties, foundProperties, "Failed to find the properties");
}
}
/** delete a single property */
@Test
void deleteXmlProperty() {
Property testProperty = new Property(TEST_PROPERTY_NAME, "testOwner");
Optional<Property> notFoundProperty = propertyRepository.findById(testProperty.getName());
Property createdProperty = propertyRepository.index(testProperty);
createdProperty.setValue("testValue");
Channel channel = new Channel("testChannel", "testOwner", Arrays.asList(createdProperty), null);
Channel createdChannel = channelRepository.index(channel);
propertyRepository.deleteById(createdProperty.getName());
// verify the property was deleted as expected
Assertions.assertNotEquals(
testProperty,
propertyRepository.findById(testProperty.getName()),
"Failed to delete property");
Channel foundChannel = channelRepository.findById("testChannel").get();
// verify the property was deleted from channels as expected
Assertions.assertTrue(
foundChannel.getProperties().isEmpty(), "Failed to remove property from channel");
MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
params.add(TEST_PROPERTY_NAME, "*");
List<Channel> chans = channelRepository.search(params).channels();
// verify the property was deleted from channels as expected
Assertions.assertTrue(chans.isEmpty(), "Failed to remove property from channel");
}
@AfterEach
public void cleanup() {
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.set("~name", "*");
channelRepository
.search(map)
.channels()
.forEach(c -> channelRepository.deleteById(c.getName()));
List.of(TEST_PROPERTY_NAME, TEST_PROPERTY_NAME + 1)
.forEach(pName -> propertyRepository.deleteById(pName));
}
}