2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.mso.adapter_utils.tests;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
27 import java.io.FileNotFoundException;
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.concurrent.Callable;
31 import java.util.concurrent.ExecutionException;
32 import java.util.concurrent.ExecutorService;
33 import java.util.concurrent.Executors;
34 import java.util.concurrent.Future;
35 import java.util.concurrent.TimeUnit;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
40 import org.openecomp.mso.properties.AbstractMsoProperties;
41 import org.openecomp.mso.properties.MsoJavaProperties;
42 import org.openecomp.mso.properties.MsoPropertiesException;
43 import org.openecomp.mso.properties.MsoPropertiesFactory;
46 * This class implements test methods of the MsoPropertiesFactory features.
48 public class MsoPropertiesFactoryConcurrencyTest {
50 public static MsoPropertiesFactory msoPropertiesFactory = new MsoPropertiesFactory();
52 public static final String MSO_PROP_ID = "TEST_PROP";
53 public static final String PATH_MSO_PROP1 = MsoJavaProperties.class.getClassLoader().
54 getResource("mso.properties")
55 .toString().substring(5);
56 public static final String PATH_MSO_PROP2 = MsoJavaProperties.class.getClassLoader().
57 getResource("mso2.properties")
58 .toString().substring(5);
61 * This method is called before any test occurs. It creates a fake tree from
64 * @throws MsoPropertiesException
67 public static final void prepare() throws MsoPropertiesException {
68 // it's possible to have it already initialized, as tests are executed in the same JVM
69 msoPropertiesFactory.removeAllMsoProperties();
70 msoPropertiesFactory.initializeMsoProperties(MSO_PROP_ID, PATH_MSO_PROP1);
73 private Callable<Integer> taskReload = new Callable<Integer>() {
75 public Integer call() {
77 if (!msoPropertiesFactory.reloadMsoProperties()) {
80 } catch (Exception e) {
88 private Callable<Integer> taskRead = new Callable<Integer>() {
90 public Integer call() {
92 MsoJavaProperties msoProperties = msoPropertiesFactory.getMsoJavaProperties(MSO_PROP_ID);
93 String property1 = msoProperties.getProperty("ecomp.mso.cloud.1.cloudId",
95 String property2 = msoProperties.getProperty("ecomp.mso.cloud.1.keystoneUrl",
97 String property3 = msoProperties.getProperty("ecomp.mso.cloud.1.msoId",
99 String property4 = msoProperties.getProperty("ecomp.mso.cloud.1.publicNetId",
101 String property5 = msoProperties.getProperty("does.not.exist",
103 String property6 = msoProperties.getProperty("ecomp.mso.cloud.1.test",
105 String property7 = msoProperties.getProperty("ecomp.mso.cloud.1.boolean",
108 assertEquals(property1, "MT");
109 assertEquals(property2, "http://localhost:5000/v2.0");
110 assertEquals(property3, "John");
111 assertEquals(property4, "FD205490A48D48475607C36B9AD902BF");
112 assertEquals(property5, "defaultValue");
113 assertEquals(property6, "1234");
114 assertEquals(property7, "true");
116 } catch (MsoPropertiesException e) {
124 private Callable<Integer> taskReadAll = new Callable<Integer>() {
126 public Integer call() {
128 List<AbstractMsoProperties> msoPropertiesList = msoPropertiesFactory.getAllMsoProperties();
129 String property1 = ((MsoJavaProperties) msoPropertiesList.get(0)).
130 getProperty("ecomp.mso.cloud.1.cloudId", "defaultValue");
131 String property2 = ((MsoJavaProperties) msoPropertiesList.get(0)).
132 getProperty("ecomp.mso.cloud.1.keystoneUrl", "defaultValue");
133 String property3 = ((MsoJavaProperties) msoPropertiesList.get(0)).
134 getProperty("ecomp.mso.cloud.1.msoId", "defaultValue");
135 String property4 = ((MsoJavaProperties) msoPropertiesList.get(0)).
136 getProperty("ecomp.mso.cloud.1.publicNetId", "defaultValue");
137 String property5 = ((MsoJavaProperties) msoPropertiesList.get(0)).
138 getProperty("does.not.exist", "defaultValue");
139 String property6 = ((MsoJavaProperties) msoPropertiesList.get(0)).
140 getProperty("ecomp.mso.cloud.1.test", "defaultValue");
141 String property7 = ((MsoJavaProperties) msoPropertiesList.get(0)).
142 getProperty("ecomp.mso.cloud.1.boolean", "defaultValue");
144 assertEquals(property1, "MT");
145 assertEquals(property2, "http://localhost:5000/v2.0");
146 assertEquals(property3, "John");
147 assertEquals(property4, "FD205490A48D48475607C36B9AD902BF");
148 assertEquals(property5, "defaultValue");
149 assertEquals(property6, "1234");
150 assertEquals(property7, "true");
151 } catch (Exception e) {
160 public final void testGetMsoProperties()
161 throws MsoPropertiesException, InterruptedException, ExecutionException, FileNotFoundException {
163 List<Future<Integer>> list = new ArrayList<>();
164 ExecutorService executor = Executors.newFixedThreadPool(20);
166 for (int i = 0; i <= 100000; i++) {
168 Future<Integer> futureResult = executor.submit(taskRead);
169 list.add(futureResult);
171 futureResult = executor.submit(taskReload);
172 list.add(futureResult);
174 futureResult = executor.submit(taskReadAll);
175 list.add(futureResult);
178 while (!executor.awaitTermination(5, TimeUnit.SECONDS)) {
182 for (Future<Integer> result : list) {
183 assertTrue(result.get().equals(0));