Change the header to SO
[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  *
49  */
50 public class MsoPropertiesFactoryConcurrencyTest {
51
52         public static MsoPropertiesFactory msoPropertiesFactory = new MsoPropertiesFactory();
53
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);
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", "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");
100
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");
108
109                         } catch (MsoPropertiesException e) {
110                 e.printStackTrace ();
111                                 return 1;
112                         }
113                         return 0;
114                 }
115         };
116
117         private Callable<Integer> taskReadAll = new Callable<Integer>() {
118                 @Override
119                 public Integer call() {
120                         try {
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");
129
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 ();
139                                 return 1;
140                         }
141                         return 0;
142                 }
143         };
144
145         @Test
146         public final void testGetMsoProperties()
147                         throws MsoPropertiesException, InterruptedException, ExecutionException, FileNotFoundException {
148
149                 List<Future<Integer>> list = new ArrayList<Future<Integer>>();
150                 ExecutorService executor = Executors.newFixedThreadPool(20);
151
152                 for (int i = 0; i <= 100000; i++) {
153
154                         Future<Integer> futureResult = executor.submit(taskRead);
155                         list.add(futureResult);
156
157                         futureResult = executor.submit(taskReload);
158                         list.add(futureResult);
159
160                         futureResult = executor.submit(taskReadAll);
161                         list.add(futureResult);
162                 }
163                 executor.shutdown();
164                 while (!executor.awaitTermination(5, TimeUnit.SECONDS)) {
165             ;
166         }
167
168                 for (Future<Integer> result : list) {
169                         assertTrue(result.get().equals(0));
170                 }
171
172         }
173
174 }