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
|
/*-
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
* Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2024 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
*/
package org.onap.policy.drools.persistence;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Properties;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.onap.policy.drools.properties.DroolsPropertyConstants;
/**
* (File) System Persistence Tests.
*/
@TestMethodOrder(MethodOrderer.MethodName.class)
public class SystemPersistenceTest {
/**
* sample configuration dir.
*/
private static final String OTHER_CONFIG_DIR = "tmp";
/**
* Test JUnit Controller Name.
*/
private static final String TEST_CONTROLLER_NAME = "foo";
/**
* Test JUnit Topic Name.
*/
private static final String TEST_TOPIC_NAME = TEST_CONTROLLER_NAME;
/**
* Test JUnit HTTP Server Name.
*/
private static final String TEST_HTTP_SERVER_NAME = TEST_CONTROLLER_NAME;
/**
* Test JUnit HTTP Client Name.
*/
private static final String TEST_HTTP_CLIENT_NAME = TEST_CONTROLLER_NAME;
/**
* Test JUnit Controller File.
*/
private static final String TEST_CONTROLLER_FILE = TEST_CONTROLLER_NAME + "-controller.properties";
/**
* Test JUnit Controller Backup File.
*/
private static final String TEST_CONTROLLER_FILE_BAK = TEST_CONTROLLER_FILE + ".bak";
/**
* Test JUnit Topic File.
*/
private static final String TEST_TOPIC_FILE = TEST_CONTROLLER_NAME + "-topic.properties";
/**
* Test JUnit Controller Name Backup.
*/
private static final String TEST_TOPIC_FILE_BAK = TEST_TOPIC_FILE + ".bak";
/**
* Test JUnit Http Server File.
*/
private static final String TEST_HTTP_SERVER_FILE = TEST_CONTROLLER_NAME
+ FileSystemPersistence.PROPERTIES_FILE_HTTP_SERVER_SUFFIX;
/**
* Test JUnit Backup Http Server File.
*/
private static final String TEST_HTTP_SERVER_FILE_BAK = TEST_HTTP_SERVER_FILE + ".bak";
/**
* Test JUnit Http Client File.
*/
private static final String TEST_HTTP_CLIENT_FILE = TEST_CONTROLLER_NAME
+ FileSystemPersistence.PROPERTIES_FILE_HTTP_CLIENT_SUFFIX;
/**
* Test JUnit Backup Http Server File.
*/
private static final String TEST_HTTP_CLIENT_FILE_BAK = TEST_HTTP_CLIENT_FILE + ".bak";
/**
* Test JUnit Environment/Engine properties.
*/
private static final String ENV_PROPS = TEST_CONTROLLER_NAME;
private static final String ENV_PROPS_FILE = ENV_PROPS + ".environment";
/**
* Test JUnit system properties.
*/
private static final String SYSTEM_PROPS = TEST_CONTROLLER_NAME;
private static final String SYSTEM_PROPS_FILE = SYSTEM_PROPS + "-system.properties";
@BeforeAll
public static void setUp() throws IOException {
cleanUpWorkingDirs();
}
@AfterAll
public static void tearDown() throws IOException {
cleanUpWorkingDirs();
}
@Test
void test1NonDefaultConfigDir() {
SystemPersistenceConstants.getManager().setConfigurationDir(OTHER_CONFIG_DIR);
assertEquals(OTHER_CONFIG_DIR, SystemPersistenceConstants.getManager().getConfigurationPath().toString());
SystemPersistenceConstants.getManager().setConfigurationDir(null);
assertEquals(SystemPersistenceConstants.DEFAULT_CONFIGURATION_DIR,
SystemPersistenceConstants.getManager().getConfigurationPath().toString());
SystemPersistenceConstants.getManager().setConfigurationDir();
assertEquals(SystemPersistenceConstants.DEFAULT_CONFIGURATION_DIR,
SystemPersistenceConstants.getManager().getConfigurationPath().toString());
}
@Test
void test2Engine_Environment_System() throws IOException {
SystemPersistenceConstants.getManager().setConfigurationDir(OTHER_CONFIG_DIR);
final Path policyEnginePropsPath =
Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(),
FileSystemPersistence.PROPERTIES_FILE_ENGINE);
final Properties engineProps = new Properties();
engineProps.setProperty("foo", "bar");
engineProps.setProperty("fiz", "buz");
if (Files.notExists(policyEnginePropsPath)) {
try (final OutputStream fout = new FileOutputStream(policyEnginePropsPath.toFile())) {
engineProps.store(fout, "");
}
}
assertEquals(engineProps, SystemPersistenceConstants.getManager().getEngineProperties());
final Path environmentPropertiesPath =
Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(), ENV_PROPS_FILE);
if (Files.notExists(environmentPropertiesPath)) {
Files.createFile(environmentPropertiesPath);
}
assertNotNull(SystemPersistenceConstants.getManager().getEnvironmentProperties(ENV_PROPS));
assertTrue(SystemPersistenceConstants.getManager().getEnvironmentProperties(ENV_PROPS).isEmpty());
assertEquals(1, SystemPersistenceConstants.getManager().getEnvironmentProperties().size());
assertEquals(SystemPersistenceConstants.getManager().getEnvironmentProperties(ENV_PROPS),
SystemPersistenceConstants.getManager().getEnvironmentProperties().get(0));
Path systemPropertiesPath =
Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(), SYSTEM_PROPS_FILE);
if (Files.notExists(systemPropertiesPath)) {
Files.createFile(systemPropertiesPath);
}
assertNotNull(SystemPersistenceConstants.getManager().getSystemProperties(SYSTEM_PROPS));
assertTrue(SystemPersistenceConstants.getManager().getSystemProperties(SYSTEM_PROPS).isEmpty());
assertEquals(1, SystemPersistenceConstants.getManager().getSystemProperties().size());
assertEquals(SystemPersistenceConstants.getManager().getSystemProperties(SYSTEM_PROPS),
SystemPersistenceConstants.getManager().getSystemProperties().get(0));
}
@Test
void test3Topic() {
SystemPersistenceConstants.getManager().setConfigurationDir(null);
Path topicPath = Paths
.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(), TEST_TOPIC_FILE);
Path topicBakPath = Paths
.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(), TEST_TOPIC_FILE_BAK);
assertTrue(Files.notExists(topicPath));
assertTrue(Files.notExists(topicBakPath));
SystemPersistenceConstants.getManager().storeTopic(TEST_TOPIC_NAME, new Properties());
assertTrue(Files.exists(topicPath));
Properties properties = SystemPersistenceConstants.getManager().getTopicProperties(TEST_TOPIC_NAME);
assertNotNull(properties);
List<Properties> topicPropsList = SystemPersistenceConstants.getManager().getTopicProperties();
assertEquals(1, topicPropsList.size());
SystemPersistenceConstants.getManager().backupTopic(TEST_TOPIC_NAME);
assertTrue(Files.exists(topicBakPath));
SystemPersistenceConstants.getManager().deleteTopic(TEST_TOPIC_NAME);
assertTrue(Files.notExists(topicPath));
}
@Test
void test4HttpServer() {
SystemPersistenceConstants.getManager().setConfigurationDir(null);
Path httpServerPath = Paths
.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(), TEST_HTTP_SERVER_FILE);
Path httpServerBakPath = Paths
.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(), TEST_HTTP_SERVER_FILE_BAK);
assertTrue(Files.notExists(httpServerPath));
assertTrue(Files.notExists(httpServerBakPath));
SystemPersistenceConstants.getManager().storeHttpServer(TEST_HTTP_SERVER_NAME, new Properties());
assertTrue(Files.exists(httpServerPath));
Properties properties = SystemPersistenceConstants.getManager().getHttpServerProperties(TEST_HTTP_SERVER_NAME);
assertNotNull(properties);
List<Properties> httpServerPropsList = SystemPersistenceConstants.getManager().getHttpServerProperties();
assertEquals(1, httpServerPropsList.size());
SystemPersistenceConstants.getManager().backupHttpServer(TEST_HTTP_SERVER_NAME);
assertTrue(Files.exists(httpServerBakPath));
SystemPersistenceConstants.getManager().deleteHttpServer(TEST_HTTP_SERVER_NAME);
assertTrue(Files.notExists(httpServerPath));
}
@Test
void test5HttpClient() {
SystemPersistenceConstants.getManager().setConfigurationDir(null);
Path httpClientPath = Paths
.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(), TEST_HTTP_CLIENT_FILE);
Path httpClientBakPath = Paths
.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(), TEST_HTTP_CLIENT_FILE_BAK);
assertTrue(Files.notExists(httpClientPath));
assertTrue(Files.notExists(httpClientBakPath));
SystemPersistenceConstants.getManager().storeHttpClient(TEST_HTTP_CLIENT_NAME, new Properties());
assertTrue(Files.exists(httpClientPath));
Properties properties = SystemPersistenceConstants.getManager().getHttpClientProperties(TEST_HTTP_CLIENT_NAME);
assertNotNull(properties);
List<Properties> httpClientPropsList = SystemPersistenceConstants.getManager().getHttpClientProperties();
assertEquals(1, httpClientPropsList.size());
SystemPersistenceConstants.getManager().backupHttpClient(TEST_HTTP_CLIENT_NAME);
assertTrue(Files.exists(httpClientBakPath));
SystemPersistenceConstants.getManager().deleteHttpClient(TEST_HTTP_CLIENT_NAME);
assertTrue(Files.notExists(httpClientPath));
}
@Test
void test6Controller() {
SystemPersistenceConstants.getManager().setConfigurationDir(null);
Path controllerPath = Paths
.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(), TEST_CONTROLLER_FILE);
Path controllerBakPath = Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(),
TEST_CONTROLLER_FILE_BAK);
assertTrue(Files.notExists(controllerPath));
assertTrue(Files.notExists(controllerBakPath));
SystemPersistenceConstants.getManager().storeController(TEST_CONTROLLER_NAME, new Properties());
assertTrue(Files.exists(controllerPath));
Properties properties = SystemPersistenceConstants.getManager().getControllerProperties(TEST_CONTROLLER_NAME);
assertNotNull(properties);
List<Properties> controllerPropsList = SystemPersistenceConstants.getManager().getControllerProperties();
assertEquals(1, controllerPropsList.size());
assertEquals(TEST_CONTROLLER_NAME, controllerPropsList
.get(0).getProperty(DroolsPropertyConstants.PROPERTY_CONTROLLER_NAME));
SystemPersistenceConstants.getManager().backupController(TEST_CONTROLLER_NAME);
assertTrue(Files.exists(controllerBakPath));
SystemPersistenceConstants.getManager().deleteController(TEST_CONTROLLER_NAME);
assertTrue(Files.notExists(controllerPath));
}
/**
* Clean up the working directories.
*
* @throws IOException throws IO exception
*/
private static void cleanUpWorkingDirs() throws IOException {
SystemPersistenceConstants.getManager().setConfigurationDir(null);
for (Properties properties : SystemPersistenceConstants.getManager().getControllerProperties()) {
SystemPersistenceConstants.getManager().deleteController(properties
.getProperty(DroolsPropertyConstants.PROPERTY_CONTROLLER_NAME));
}
SystemPersistenceConstants.getManager().deleteTopic(TEST_TOPIC_NAME);
SystemPersistenceConstants.getManager().deleteHttpServer(TEST_HTTP_SERVER_NAME);
SystemPersistenceConstants.getManager().deleteHttpClient(TEST_HTTP_CLIENT_NAME);
final Path testControllerBakPath =
Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(),
TEST_CONTROLLER_FILE_BAK);
final Path testTopicBakPath = Paths
.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(), TEST_TOPIC_FILE_BAK);
final Path testHttpServerBakPath = Paths
.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(), TEST_HTTP_SERVER_FILE_BAK);
final Path testHttpClientBakPath = Paths
.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(), TEST_HTTP_CLIENT_FILE_BAK);
final Path policyEnginePath = Paths.get(OTHER_CONFIG_DIR, FileSystemPersistence.PROPERTIES_FILE_ENGINE);
final Path environmentPath = Paths.get(OTHER_CONFIG_DIR, ENV_PROPS_FILE);
final Path systemPath = Paths.get(OTHER_CONFIG_DIR, SYSTEM_PROPS_FILE);
Files.deleteIfExists(testControllerBakPath);
Files.deleteIfExists(testTopicBakPath);
Files.deleteIfExists(testHttpServerBakPath);
Files.deleteIfExists(testHttpClientBakPath);
Files.deleteIfExists(policyEnginePath);
Files.deleteIfExists(environmentPath);
Files.deleteIfExists(systemPath);
}
}
|