2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.openecomp.mso.bpmn.core;
24 import java.io.FileOutputStream;
25 import java.io.FileReader;
26 import java.io.IOException;
27 import java.nio.file.Files;
28 import java.nio.file.Path;
29 import java.nio.file.Paths;
30 import java.nio.file.StandardCopyOption;
31 import java.util.HashMap;
33 import java.util.Properties;
35 import org.openecomp.mso.logger.MsoLogger;
37 import org.openecomp.mso.bpmn.core.PropertyConfiguration;
40 * Sets up mso.bpmn.properties and mso.bpmn.urn.properties for unit tests.
42 public class PropertyConfigurationSetup {
44 private static Path msoConfigPath = null;
45 private static Path bpmnPropertiesPath = null;
46 private static Path bpmnUrnPropertiesPath = null;
47 private static boolean modifiedConfiguration = false;
49 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
52 * Ensures that the the PropertyConfiguration is initialized and that the
53 * property data is reset to initial values. Any extra properties that are
54 * specified will be merged with the initial values. The following example
55 * shows how a test can specify a replacement URN mapping property.
57 * Map<String, String> urnProperties =
58 * PropertyConfigurationSetup.createBpmnUrnProperties();
59 * urnProperties.add("mso.po.timeout", "PT1M");
60 * PropertyConfiguration.init(urnProperties);
62 * @param args one or more maps created with createBpmnProperties()
63 * and/or createBpmnUrnProperties()
65 public static synchronized void init(Object ... args) throws IOException {
67 Map<String, String> extraBpmnProperties = null;
68 Map<String, String> extraBpmnUrnProperties = null;
70 boolean propertiesSpecified = false;
72 for (Object arg : args) {
73 @SuppressWarnings("unchecked")
74 Map<String, String> properties = (Map<String, String>) arg;
76 String type = properties.get("PROPERTIES-TYPE");
78 if (PropertyConfiguration.MSO_BPMN_PROPERTIES.equals(type)) {
79 if (properties.size() > 1) {
80 extraBpmnProperties = properties;
81 propertiesSpecified = false;
83 } else if (PropertyConfiguration.MSO_BPMN_URN_PROPERTIES.equals(type)) {
84 if (properties.size() > 1) {
85 extraBpmnUrnProperties = properties;
86 propertiesSpecified = false;
89 throw new IllegalArgumentException("Not a supported PROPERTIES-TYPE map");
93 // There are three cases in which we need to change the existing configuration:
94 // 1) There is no existing configuration, i.e. first time setup
95 // 2) The existing configuration was modified, i.e. it has non-default values
96 // 3) Non-default values are specified for this initialization
98 if (msoConfigPath == null || modifiedConfiguration || propertiesSpecified) {
99 modifiedConfiguration = propertiesSpecified;
101 Path bpmnPropertiesSourcePath = Paths.get("src", "test", "resources", "mso.bpmn.properties");
102 Path bpmnUrnPropertiesSourcePath = Paths.get("src", "test", "resources", "mso.bpmn.urn.properties");
104 if (msoConfigPath == null) {
105 // Initialize from scratch.
106 msoConfigPath = Files.createTempDirectory("mso-config-path-");
107 System.setProperty("mso.config.path", msoConfigPath.toString());
108 msoConfigPath.toFile().deleteOnExit();
110 bpmnPropertiesPath = msoConfigPath.resolve("mso.bpmn.properties");
111 mergeCopy(bpmnPropertiesSourcePath, extraBpmnProperties, bpmnPropertiesPath);
112 bpmnPropertiesPath.toFile().deleteOnExit();
114 bpmnUrnPropertiesPath = msoConfigPath.resolve("mso.bpmn.urn.properties");
115 mergeCopy(bpmnUrnPropertiesSourcePath, extraBpmnUrnProperties, bpmnUrnPropertiesPath);
116 bpmnUrnPropertiesPath.toFile().deleteOnExit();
118 PropertyConfiguration.getInstance();
120 // Just reset the data.
121 PropertyConfiguration.getInstance().clearCache();
122 mergeCopy(bpmnPropertiesSourcePath, extraBpmnProperties, bpmnPropertiesPath);
123 mergeCopy(bpmnUrnPropertiesSourcePath, extraBpmnUrnProperties, bpmnUrnPropertiesPath);
129 * Resets the PropertyConfiguration to its initial state, as if it had never
130 * been started. Note that this is a very expensive option and should not
131 * be needed by most unit tests.
132 * @throws IOException
134 public static synchronized void nuke() throws IOException {
135 if (msoConfigPath == null) {
139 PropertyConfiguration.getInstance().shutDown();
141 bpmnUrnPropertiesPath.toFile().delete();
142 bpmnUrnPropertiesPath = null;
144 bpmnPropertiesPath.toFile().delete();
145 bpmnPropertiesPath = null;
147 msoConfigPath.toFile().delete();
148 msoConfigPath = null;
150 System.setProperty("mso.config.path", null);
152 modifiedConfiguration = false;
156 * Create a map to hold properties to be added to mso.bpmn.properties.
158 public static Map<String, String> createBpmnProperties() {
159 Map<String, String> properties = new HashMap<>();
160 properties.put("PROPERTIES-TYPE", PropertyConfiguration.MSO_BPMN_PROPERTIES);
165 * Create a map to hold properties to be added to mso.bpmn.urn.properties.
167 public static Map<String, String> createBpmnUrnProperties() {
168 Map<String, String> properties = new HashMap<>();
169 properties.put("PROPERTIES-TYPE", PropertyConfiguration.MSO_BPMN_URN_PROPERTIES);
174 * Adds (or replaces) the specified values in the mso.bpmn.urn.properties file.
175 * Note that properties added this way may take some time to be loaded by the
176 * PropertyConfiguration, just like they do when a property file is updated on
177 * a real MSO system. This method will optionally wait for the new properties
178 * to be loaded. Timeout results in an IOException.
179 * @param properties new properties
180 * @param wait maximum amount of time to wait for new properties to be loaded,
181 * in milliseconds. A value of zero means, "Do not wait."
182 * @throws IOException
184 public static synchronized void addProperties(Map<String, String> properties, long wait)
185 throws IOException, InterruptedException {
187 if (msoConfigPath == null) {
188 throw new IllegalStateException();
191 String type = properties.get("PROPERTIES-TYPE");
194 if (PropertyConfiguration.MSO_BPMN_PROPERTIES.equals(type)) {
195 path = bpmnPropertiesPath;
196 } else if (PropertyConfiguration.MSO_BPMN_URN_PROPERTIES.equals(type)) {
197 path = bpmnUrnPropertiesPath;
199 throw new IllegalArgumentException("Not a supported PROPERTIES-TYPE map");
202 String oldTimestamp = PropertyConfiguration.getInstance().getProperties(type)
203 .get(PropertyConfiguration.TIMESTAMP_PROPERTY);
205 modifiedConfiguration = true;
206 addProperties(properties, path);
212 long endTime = System.currentTimeMillis() + wait;
217 String newTimestamp = PropertyConfiguration.getInstance().getProperties(type)
218 .get(PropertyConfiguration.TIMESTAMP_PROPERTY);
220 if (!newTimestamp.equals(oldTimestamp)) {
224 long now = System.currentTimeMillis();
226 if (now >= endTime) {
227 throw new IOException("Timed out after " + wait
228 + "ms waiting for PropertyConfiguration change");
234 * Helper method that adds properties to the specified file.
236 private static void addProperties(Map<String, String> values, Path path)
239 FileReader fileReader = null;
240 FileOutputStream outputStream = null;
243 fileReader = new FileReader(path.toFile());
244 Properties properties = new Properties();
245 properties.load(fileReader);
247 for (String key : values.keySet()) {
248 if (!key.equals("PROPERTIES-TYPE")) {
249 properties.setProperty(key, values.get(key));
253 outputStream = new FileOutputStream(path.toFile());
254 properties.store(outputStream, "Custom Test Properties");
256 if (fileReader != null) {
259 } catch (IOException e) {
260 LOGGER.debug("Exception :",e);
264 if (outputStream != null) {
266 outputStream.close();
267 } catch (IOException e) {
268 LOGGER.debug("Exception :",e);
275 * Helper method that copies properties from the specified source file, and
276 * optionally merges them with the specified extra values, then writes the
277 * whole mess to the destination file.
279 private static void mergeCopy(Path sourcePath, Map<String, String> extraValues, Path destPath)
281 if (extraValues == null || extraValues.isEmpty()) {
282 Files.copy(sourcePath, destPath, StandardCopyOption.REPLACE_EXISTING);
286 FileReader fileReader = null;
287 FileOutputStream outputStream = null;
290 fileReader = new FileReader(sourcePath.toFile());
291 Properties properties = new Properties();
292 properties.load(fileReader);
294 for (String key : extraValues.keySet()) {
295 if (!key.equals("PROPERTIES-TYPE")) {
296 properties.setProperty(key, extraValues.get(key));
300 outputStream = new FileOutputStream(destPath.toFile());
301 properties.store(outputStream, "Custom Test Properties");
303 if (fileReader != null) {
306 } catch (IOException e) {
307 LOGGER.debug("Exception :",e);
311 if (outputStream != null) {
313 outputStream.close();
314 } catch (IOException e) {
315 LOGGER.debug("Exception :",e);