junits fixes
[policy/drools-pdp.git] / policy-management / src / test / java / org / onap / policy / drools / persistence / test / SystemPersistenceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.policy.drools.persistence.test;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertTrue;
25
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.io.OutputStream;
29 import java.nio.file.Files;
30 import java.nio.file.Path;
31 import java.nio.file.Paths;
32 import java.util.Properties;
33
34 import org.junit.AfterClass;
35 import org.junit.BeforeClass;
36 import org.junit.FixMethodOrder;
37 import org.junit.Test;
38 import org.junit.runners.MethodSorters;
39 import org.onap.policy.drools.persistence.FileSystemPersistence;
40 import org.onap.policy.drools.persistence.SystemPersistence;
41 import org.onap.policy.drools.properties.PolicyProperties;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * (File) System Persistence Tests
47  */
48 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
49 public class SystemPersistenceTest {
50
51   /**
52    * logger
53    */
54   private static final Logger logger = LoggerFactory.getLogger(SystemPersistenceTest.class);
55
56   /**
57    * sample configuration dir
58    */
59   public static final String OTHER_CONFIG_DIR = "tmp";
60
61   /**
62    * Test JUnit Controller Name
63    */
64   public static final String TEST_CONTROLLER_NAME = "foo";
65
66   /**
67    * Test JUnit Controller Name
68    */
69   public static final String TEST_CONTROLLER_FILE = TEST_CONTROLLER_NAME + "-controller.properties";
70
71   /**
72    * Test JUnit Controller Name Backup
73    */
74   public static final String TEST_CONTROLLER_FILE_BAK = TEST_CONTROLLER_FILE + ".bak";
75
76   /**
77    * Test JUnit Environment/Engine properties
78    */
79   private static final String ENV_PROPS = "envProps";
80   private static final String ENV_PROPS_FILE = ENV_PROPS + ".environment";
81   private static final String POLICY_ENGINE_PROPERTIES_FILE = "policy-engine.properties";
82
83   @BeforeClass
84   public static void setUp() throws IOException {
85     cleanUpWorkingDirs();
86   }
87
88   @AfterClass
89   public static void tearDown() throws IOException {
90     cleanUpWorkingDirs();
91   }
92
93   @Test
94   public void test1NonDefaultConfigDir() throws IOException {
95     logger.info("enter");
96
97     SystemPersistence.manager.setConfigurationDir(OTHER_CONFIG_DIR);
98     assertTrue(
99         SystemPersistence.manager.getConfigurationPath().toString().equals(OTHER_CONFIG_DIR));
100
101     SystemPersistence.manager.setConfigurationDir(null);
102     assertTrue(SystemPersistence.manager.getConfigurationPath().toString()
103         .equals(SystemPersistence.DEFAULT_CONFIGURATION_DIR));
104   }
105
106   @Test
107   public void test2Engine() throws IOException {
108     logger.info("enter");
109
110     SystemPersistence.manager.setConfigurationDir(OTHER_CONFIG_DIR);
111
112     final Path policyEnginePropsPath =
113         Paths.get(SystemPersistence.manager.getConfigurationPath().toString(),
114             FileSystemPersistence.PROPERTIES_FILE_ENGINE);
115
116     final Properties engineProps = new Properties();
117     engineProps.setProperty("foo", "bar");
118     engineProps.setProperty("fiz", "buz");
119     if (Files.notExists(policyEnginePropsPath)) {
120       try (final OutputStream fout = new FileOutputStream(policyEnginePropsPath.toFile())) {
121         engineProps.store(fout, "");
122       }
123     }
124
125     assertEquals(SystemPersistence.manager.getEngineProperties(), engineProps);
126
127     final Path environmentPropertiesPath =
128         Paths.get(SystemPersistence.manager.getConfigurationPath().toString(), ENV_PROPS_FILE);
129     if (Files.notExists(environmentPropertiesPath)) {
130       Files.createFile(environmentPropertiesPath);
131     }
132     assertTrue(SystemPersistence.manager.getEnvironmentProperties(ENV_PROPS).isEmpty());
133     assertTrue(SystemPersistence.manager.getEnvironmentProperties().size() == 1);
134   }
135
136   @Test
137   public void test3PersistConfiguration() {
138     logger.info("enter");
139
140     SystemPersistence.manager.setConfigurationDir(null);
141
142     final Path controllerPath = Paths
143         .get(SystemPersistence.manager.getConfigurationPath().toString(), TEST_CONTROLLER_FILE);
144
145     final Path controllerBakPath = Paths
146         .get(SystemPersistence.manager.getConfigurationPath().toString(), TEST_CONTROLLER_FILE_BAK);
147
148     assertTrue(Files.notExists(controllerPath));
149     assertTrue(Files.notExists(controllerBakPath));
150
151     Properties properties = new Properties();
152     properties.put(PolicyProperties.PROPERTY_CONTROLLER_NAME, TEST_CONTROLLER_NAME);
153     SystemPersistence.manager.storeController(TEST_CONTROLLER_NAME, properties);
154
155     assertTrue(Files.exists(controllerPath));
156
157     properties = SystemPersistence.manager.getControllerProperties(TEST_CONTROLLER_NAME);
158     assertTrue(properties != null);
159
160     SystemPersistence.manager.backupController(TEST_CONTROLLER_NAME);
161     assertTrue(Files.exists(controllerBakPath));
162
163     assertFalse(SystemPersistence.manager.getControllerProperties().isEmpty());
164
165     SystemPersistence.manager.deleteController(TEST_CONTROLLER_NAME);
166     assertTrue(Files.notExists(controllerPath));
167   }
168
169   public static void cleanUpWorkingDirs() throws IOException {
170
171     SystemPersistence.manager.setConfigurationDir(null);
172
173     final Path testControllerPath = Paths
174         .get(SystemPersistence.manager.getConfigurationPath().toString(), TEST_CONTROLLER_FILE);
175     final Path testControllerBakPath = Paths
176         .get(SystemPersistence.manager.getConfigurationPath().toString(), TEST_CONTROLLER_FILE_BAK);
177
178     final Path policyEnginePath = Paths.get(OTHER_CONFIG_DIR, POLICY_ENGINE_PROPERTIES_FILE);
179     final Path environmentPath = Paths.get(OTHER_CONFIG_DIR, ENV_PROPS_FILE);
180
181     Files.deleteIfExists(testControllerPath);
182     Files.deleteIfExists(testControllerBakPath);
183     Files.deleteIfExists(policyEnginePath);
184     Files.deleteIfExists(environmentPath);
185   }
186
187 }