5caa137d33455cf2543a68dc3420e50d19d4cb5f
[so.git] / common / src / test / java / org / openecomp / mso / adapter_utils / tests / MsoPropertiesFactoryConcurrencyTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
21 package org.openecomp.mso.adapter_utils.tests;
22
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26
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;
36
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39
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;
44
45 /**
46  * This class implements test methods of the MsoPropertiesFactory features.
47  */
48 public class MsoPropertiesFactoryConcurrencyTest {
49
50     public static MsoPropertiesFactory msoPropertiesFactory = new MsoPropertiesFactory();
51
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);
59
60     /**
61      * This method is called before any test occurs. It creates a fake tree from
62      * scratch
63      *
64      * @throws MsoPropertiesException
65      */
66     @BeforeClass
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);
71     }
72
73     private Callable<Integer> taskReload = new Callable<Integer>() {
74         @Override
75         public Integer call() {
76             try {
77                 if (!msoPropertiesFactory.reloadMsoProperties()) {
78                     return 1;
79                 }
80             } catch (Exception e) {
81                 e.printStackTrace();
82                 return 1;
83             }
84             return 0;
85         }
86     };
87
88     private Callable<Integer> taskRead = new Callable<Integer>() {
89         @Override
90         public Integer call() {
91             try {
92                 MsoJavaProperties msoProperties = msoPropertiesFactory.getMsoJavaProperties(MSO_PROP_ID);
93                 String property1 = msoProperties.getProperty("ecomp.mso.cloud.1.cloudId",
94                         "defaultValue");
95                 String property2 = msoProperties.getProperty("ecomp.mso.cloud.1.keystoneUrl",
96                         "defaultValue");
97                 String property3 = msoProperties.getProperty("ecomp.mso.cloud.1.msoId",
98                         "defaultValue");
99                 String property4 = msoProperties.getProperty("ecomp.mso.cloud.1.publicNetId",
100                         "defaultValue");
101                 String property5 = msoProperties.getProperty("does.not.exist",
102                         "defaultValue");
103                 String property6 = msoProperties.getProperty("ecomp.mso.cloud.1.test",
104                         "defaultValue");
105                 String property7 = msoProperties.getProperty("ecomp.mso.cloud.1.boolean",
106                         "defaultValue");
107
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");
115
116             } catch (MsoPropertiesException e) {
117                 e.printStackTrace();
118                 return 1;
119             }
120             return 0;
121         }
122     };
123
124     private Callable<Integer> taskReadAll = new Callable<Integer>() {
125         @Override
126         public Integer call() {
127             try {
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");
143
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) {
152                 e.printStackTrace();
153                 return 1;
154             }
155             return 0;
156         }
157     };
158
159     @Test
160     public final void testGetMsoProperties()
161             throws MsoPropertiesException, InterruptedException, ExecutionException, FileNotFoundException {
162
163         List<Future<Integer>> list = new ArrayList<>();
164         ExecutorService executor = Executors.newFixedThreadPool(20);
165
166         for (int i = 0; i <= 100000; i++) {
167
168             Future<Integer> futureResult = executor.submit(taskRead);
169             list.add(futureResult);
170
171             futureResult = executor.submit(taskReload);
172             list.add(futureResult);
173
174             futureResult = executor.submit(taskReadAll);
175             list.add(futureResult);
176         }
177         executor.shutdown();
178         while (!executor.awaitTermination(5, TimeUnit.SECONDS)) {
179             ;
180         }
181
182         for (Future<Integer> result : list) {
183             assertTrue(result.get().equals(0));
184         }
185
186     }
187
188 }