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.
50 public class MsoPropertiesFactoryConcurrencyTest {
52 public static MsoPropertiesFactory msoPropertiesFactory = new MsoPropertiesFactory();
54 public static final String MSO_PROP_ID = "TEST_PROP";
55 public static final String PATH_MSO_PROP1 = MsoJavaProperties.class.getClassLoader().getResource("mso.properties")
56 .toString().substring(5);
57 public static final String PATH_MSO_PROP2 = MsoJavaProperties.class.getClassLoader().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", "defaultValue");
94 String property2 = msoProperties.getProperty("ecomp.mso.cloud.1.keystoneUrl", "defaultValue");
95 String property3 = msoProperties.getProperty("ecomp.mso.cloud.1.msoId", "defaultValue");
96 String property4 = msoProperties.getProperty("ecomp.mso.cloud.1.publicNetId", "defaultValue");
97 String property5 = msoProperties.getProperty("does.not.exist", "defaultValue");
98 String property6 = msoProperties.getProperty("ecomp.mso.cloud.1.test", "defaultValue");
99 String property7 = msoProperties.getProperty("ecomp.mso.cloud.1.boolean", "defaultValue");
101 assertEquals(property1, "MT");
102 assertEquals(property2, "http://localhost:5000/v2.0");
103 assertEquals(property3, "John");
104 assertEquals(property4, "FD205490A48D48475607C36B9AD902BF");
105 assertEquals(property5, "defaultValue");
106 assertEquals(property6, "1234");
107 assertEquals(property7, "true");
109 } catch (MsoPropertiesException e) {
110 e.printStackTrace ();
117 private Callable<Integer> taskReadAll = new Callable<Integer>() {
119 public Integer call() {
121 List<AbstractMsoProperties> msoPropertiesList = msoPropertiesFactory.getAllMsoProperties();
122 String property1 = ((MsoJavaProperties)msoPropertiesList.get(0)).getProperty("ecomp.mso.cloud.1.cloudId", "defaultValue");
123 String property2 = ((MsoJavaProperties)msoPropertiesList.get(0)).getProperty("ecomp.mso.cloud.1.keystoneUrl", "defaultValue");
124 String property3 = ((MsoJavaProperties)msoPropertiesList.get(0)).getProperty("ecomp.mso.cloud.1.msoId", "defaultValue");
125 String property4 = ((MsoJavaProperties)msoPropertiesList.get(0)).getProperty("ecomp.mso.cloud.1.publicNetId", "defaultValue");
126 String property5 = ((MsoJavaProperties)msoPropertiesList.get(0)).getProperty("does.not.exist", "defaultValue");
127 String property6 = ((MsoJavaProperties)msoPropertiesList.get(0)).getProperty("ecomp.mso.cloud.1.test", "defaultValue");
128 String property7 = ((MsoJavaProperties)msoPropertiesList.get(0)).getProperty("ecomp.mso.cloud.1.boolean", "defaultValue");
130 assertEquals(property1, "MT");
131 assertEquals(property2, "http://localhost:5000/v2.0");
132 assertEquals(property3, "John");
133 assertEquals(property4, "FD205490A48D48475607C36B9AD902BF");
134 assertEquals(property5, "defaultValue");
135 assertEquals(property6, "1234");
136 assertEquals(property7, "true");
137 } catch (Exception e) {
138 e.printStackTrace ();
146 public final void testGetMsoProperties()
147 throws MsoPropertiesException, InterruptedException, ExecutionException, FileNotFoundException {
149 List<Future<Integer>> list = new ArrayList<Future<Integer>>();
150 ExecutorService executor = Executors.newFixedThreadPool(20);
152 for (int i = 0; i <= 100000; i++) {
154 Future<Integer> futureResult = executor.submit(taskRead);
155 list.add(futureResult);
157 futureResult = executor.submit(taskReload);
158 list.add(futureResult);
160 futureResult = executor.submit(taskReadAll);
161 list.add(futureResult);
164 while (!executor.awaitTermination(5, TimeUnit.SECONDS)) {
168 for (Future<Integer> result : list) {
169 assertTrue(result.get().equals(0));