Junit test case coverage for some classes in music-rest
[music.git] / music-rest / src / main / java / org / onap / music / main / PropertiesLoader.java
1 /*
2  * ============LICENSE_START==========================================
3  * org.onap.music
4  * ===================================================================
5  *  Copyright (c) 2017 AT&T Intellectual Property
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  * 
19  * ============LICENSE_END=============================================
20  * ====================================================================
21  */
22
23 package org.onap.music.main;
24
25 import java.util.HashSet;
26 import java.util.Properties;
27
28 import org.onap.music.eelf.logging.EELFLoggerDelegate;
29 import org.springframework.beans.factory.InitializingBean;
30 import org.springframework.beans.factory.annotation.Value;
31 import org.springframework.context.annotation.Bean;
32 import org.springframework.context.annotation.PropertySource;
33 import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
34 import org.springframework.stereotype.Component;
35
36 @PropertySource(value = {"file:/opt/app/music/etc/music.properties", "classpath:/project.properties","file:/opt/app/music/etc/key.properties"})
37 @Component
38 public class PropertiesLoader implements InitializingBean {
39
40     @Value("${cassandra.host}")
41     public String cassandraHost;
42
43     @Value("${debug}")
44     public String debug;
45
46     @Value("${version}")
47     public String version;
48
49     @Value("${build}")
50     public String build;
51
52     @Value("${music.properties}")
53     public String musicProperties;
54
55     @Value("${lock.lease.period}")
56     public String lockLeasePeriod;
57
58     @Value("${cassandra.user}")
59     public String cassandraUser;
60
61     @Value("${cassandra.password}")
62     public String cassandraPassword;
63
64     @Value("${cassandra.port}")
65     public String cassandraPort;
66
67     @Value("${cassandra.connecttimeoutms}")
68     public String cassandraConnectTimeOutMS;
69
70     @Value("${cassandra.readtimeoutms}")
71     public String cassandraReadTimeOutMS;
72
73     @Value("${cadi}")
74     public String isCadi;
75
76     @Value("${keyspace.active}")
77     public String isKeyspaceActive;
78
79     @Value("${retry.count}")
80     public String rertryCount;
81     
82     @Value("${lock.daemon.sleeptime.ms}")
83     public String lockDaemonSleeptimems;
84
85     @Value("${keyspaces.for.lock.cleanup}")
86     public String keyspacesForLockCleanup;
87
88     @Value("${create.lock.wait.period.ms}")
89     private long createLockWaitPeriod;
90
91     @Value("${create.lock.wait.increment.ms}")
92     private int createLockWaitIncrement;
93
94     @Value("${transId.header.prefix}")
95     private String transIdPrefix;
96
97     @Value("${conversation.header.prefix}")
98     private String conversationIdPrefix;
99
100     @Value("${clientId.header.prefix}")
101     private String clientIdPrefix;
102
103     @Value("${messageId.header.prefix}")
104     private String messageIdPrefix;    
105
106     @Value("${transId.header.required}")
107     private Boolean transIdRequired;
108
109     @Value("${conversation.header.required}")
110     private Boolean conversationIdRequired;
111
112     @Value("${clientId.header.required}")
113     private Boolean clientIdRequired;
114
115     @Value("${messageId.header.required}")
116     private Boolean messageIdRequired;
117
118     @Value("${music.aaf.ns}")
119     private String musicAafNs;
120
121     @Value("${cipher.enc.key}")
122     private String cipherEncKey;
123
124     @SuppressWarnings("unused")
125     private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PropertiesLoader.class);
126
127     @Bean
128     public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
129
130         PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
131         pspc.setIgnoreResourceNotFound(true);
132         pspc.setIgnoreUnresolvablePlaceholders(true);
133         return pspc;
134     }
135
136     /**
137      * .
138      */
139     public void loadProperties() {
140         if(cipherEncKey != null) {
141             MusicUtil.setCipherEncKey(cipherEncKey);
142         }
143         if (musicAafNs != null) {
144             MusicUtil.setMusicAafNs(musicAafNs);
145         }
146         if (cassandraPort != null && !cassandraPort.equals("${cassandra.port}")) {
147             MusicUtil.setCassandraPort(Integer.parseInt(cassandraPort));
148         }
149         if (cassandraUser != null && !cassandraUser.equals("${cassandra.user}")) {
150             MusicUtil.setCassName(cassandraUser);
151         }
152         if (cassandraPassword != null && !cassandraPassword.equals("${cassandra.password}")) {
153             MusicUtil.setCassPwd(cassandraPassword);
154         }
155         if (cassandraConnectTimeOutMS != null && !cassandraConnectTimeOutMS.equals("${cassandra.connecttimeoutms}")) {
156             MusicUtil.setCassandraConnectTimeOutMS(Integer.parseInt(cassandraConnectTimeOutMS));
157         }
158         if (cassandraReadTimeOutMS != null && !cassandraReadTimeOutMS.equals("${cassandra.readtimeoutms}")) {
159             MusicUtil.setCassandraReadTimeOutMS(Integer.parseInt(cassandraReadTimeOutMS));
160         }
161         if (debug != null && !debug.equals("${debug}")) {
162             MusicUtil.setDebug(Boolean.parseBoolean(debug));
163         }
164         if (lockLeasePeriod != null && !lockLeasePeriod.equals("${lock.lease.period}")) {
165             MusicUtil.setDefaultLockLeasePeriod(Long.parseLong(lockLeasePeriod));
166         }
167         if (musicProperties != null && !musicProperties.equals("${music.properties}")) {
168             MusicUtil.setMusicPropertiesFilePath(musicProperties);
169         }
170         if (cassandraHost != null && !cassandraHost.equals("${cassandra.host}")) {
171             MusicUtil.setMyCassaHost(cassandraHost);
172         }
173         if (version != null && !version.equals("${version}")) {
174             MusicUtil.setVersion(version);
175         }
176         if (build != null && !version.equals("${build}")) {
177             MusicUtil.setBuild(build);
178         }
179         if (isCadi != null && !isCadi.equals("${cadi}")) {
180             MusicUtil.setIsCadi(Boolean.parseBoolean(isCadi));
181         }
182         if (rertryCount != null && !rertryCount.equals("${retry.count}")) {
183             MusicUtil.setRetryCount(Integer.parseInt(rertryCount));
184         }
185         if (isKeyspaceActive != null && !isKeyspaceActive.equals("${keyspace.active}")) {
186             MusicUtil.setKeyspaceActive(Boolean.parseBoolean(isKeyspaceActive));
187         }
188         if (lockDaemonSleeptimems != null && !lockDaemonSleeptimems.equals("${lock.daemon.sleeptime.ms}")) {
189             MusicUtil.setLockDaemonSleepTimeMs(Long.parseLong(lockDaemonSleeptimems));
190         }
191         if (keyspacesForLockCleanup !=null && !keyspacesForLockCleanup.equals("${keyspaces.for.lock.cleanup}")) {
192             HashSet<String> keyspaces = new HashSet<>();
193             for (String keyspace: keyspacesForLockCleanup.split(",")) {
194                 keyspaces.add(keyspace);
195             }
196             MusicUtil.setKeyspacesToCleanLocks(keyspaces);
197         }
198         if(transIdPrefix!=null) {
199             MusicUtil.setTransIdPrefix(transIdPrefix);
200         }
201
202         if(conversationIdPrefix!=null) {
203             MusicUtil.setConversationIdPrefix(conversationIdPrefix);
204         }
205
206         if(clientIdPrefix!=null) {
207             MusicUtil.setClientIdPrefix(clientIdPrefix);
208         }
209
210         if(messageIdPrefix!=null) {
211             MusicUtil.setMessageIdPrefix(messageIdPrefix);
212         }
213
214         if(transIdRequired!=null) {
215             MusicUtil.setTransIdRequired(transIdRequired);
216         }
217
218         if(conversationIdRequired!=null) {
219             MusicUtil.setConversationIdRequired(conversationIdRequired);
220         }
221
222         if(clientIdRequired!=null) {
223             MusicUtil.setClientIdRequired(clientIdRequired);
224         }
225
226         if(messageIdRequired!=null) {
227             MusicUtil.setMessageIdRequired(messageIdRequired);
228         }
229         
230         if(createLockWaitPeriod!=0) {
231             MusicUtil.setCreateLockWaitPeriod(createLockWaitPeriod);
232         }
233
234         if(createLockWaitIncrement!=0) {
235             MusicUtil.setCreateLockWaitIncrement(createLockWaitIncrement);
236         }
237     }
238
239     public static void loadProperties(Properties properties) {
240         if (properties.getProperty("cassandra.host")!=null) {
241             MusicUtil.setMyCassaHost(properties.getProperty("cassandra.host"));
242         }
243
244         if (properties.getProperty("cassandra.port")!=null) {
245             MusicUtil.setCassandraPort(Integer.parseInt(properties.getProperty("cassandra.port")));
246         }
247
248         if (properties.getProperty("cassandra.user")!=null) {
249             MusicUtil.setCassName(properties.getProperty("cassandra.user"));
250         }
251
252         if (properties.getProperty("cassandra.password")!=null) {
253             MusicUtil.setCassPwd(properties.getProperty("cassandra.password"));
254         }
255
256         if(properties.getProperty("cassandra.connectTimeOutMS")!=null) {
257             MusicUtil.setCassandraConnectTimeOutMS(Integer.parseInt(properties.getProperty("cassandra.connecttimeoutms")));
258         }
259
260         if(properties.getProperty("cassandra.readTimeOutMS")!=null) {
261             MusicUtil.setCassandraReadTimeOutMS(Integer.parseInt(properties.getProperty("cassandra.readtimeoutms")));
262         }
263
264         if (properties.getProperty("music.properties")!=null) {
265             MusicUtil.setMusicPropertiesFilePath(properties.getProperty("music.properties"));
266         }
267
268         if (properties.getProperty("debug")!=null) {
269             MusicUtil.setDebug(Boolean.parseBoolean(properties.getProperty("debug")));
270         }
271
272         if (properties.getProperty("version")!=null) {
273             MusicUtil.setVersion(properties.getProperty("version"));
274         }
275
276         if (properties.getProperty("build")!=null) {
277             MusicUtil.setBuild(properties.getProperty("build"));
278         }
279
280         if (properties.getProperty("lock.lease.period")!=null) {
281             MusicUtil.setDefaultLockLeasePeriod(Long.parseLong(properties.getProperty("lock.lease.period")));
282         }
283
284         if (properties.getProperty("cadi")!=null) {
285             MusicUtil.setIsCadi(Boolean.parseBoolean(properties.getProperty("cadi")));
286         }
287
288         if (properties.getProperty("keyspace.active")!=null) {
289             MusicUtil.setKeyspaceActive(Boolean.parseBoolean(properties.getProperty("keyspace.active")));
290         }
291
292         if (properties.getProperty("retry.count")!=null) {
293             MusicUtil.setRetryCount(Integer.parseInt(properties.getProperty("retry.count")));
294         }
295
296         if (properties.getProperty("transId.header.prefix")!=null) {
297             MusicUtil.setTransIdPrefix(properties.getProperty("transId.header.prefix"));
298         }
299
300         if (properties.getProperty("conversation.header.prefix")!=null) {
301             MusicUtil.setConversationIdPrefix(properties.getProperty("conversation.header.prefix"));
302         }
303
304         if (properties.getProperty("clientId.header.prefix")!=null) {
305             MusicUtil.setClientIdPrefix(properties.getProperty("clientId.header.prefix"));
306         }
307
308         if (properties.getProperty("messageId.header.prefix")!=null) {
309             MusicUtil.setMessageIdPrefix(properties.getProperty("messageId.header.prefix"));
310         }
311
312         if (properties.getProperty("transId.header.required")!=null) {
313             MusicUtil.setTransIdRequired(Boolean.parseBoolean(properties.getProperty("transId.header.required")));
314         }
315
316         if (properties.getProperty("conversation.header.required")!=null) {
317             MusicUtil.setConversationIdRequired(Boolean.parseBoolean(properties.getProperty("conversation.header.required")));
318         }
319
320         if (properties.getProperty("clientId.header.required")!=null) {
321             MusicUtil.setClientIdRequired(Boolean.parseBoolean(properties.getProperty("clientId.header.required")));
322         }
323
324         if (properties.getProperty("messageId.header.required")!=null) {
325             MusicUtil.setMessageIdRequired(Boolean.parseBoolean(properties.getProperty("messageId.header.required")));
326         }
327
328         if (properties.getProperty("music.aaf.ns")!=null) {
329             MusicUtil.setMusicAafNs(properties.getProperty("music.aaf.ns"));
330         }
331
332         if (properties.getProperty("cipher.enc.key")!=null) {
333             MusicUtil.setCipherEncKey(properties.getProperty("cipher.enc.key"));
334         }
335
336     }
337
338     @Override
339     public void afterPropertiesSet() throws Exception {
340         // TODO Auto-generated method stub
341
342     }
343     
344     /* For unit testing purpose only*/
345     protected void setProperties() {
346         cassandraHost = "127.0.0.1";
347         debug = "true";
348         version = "x.x.x";
349         build = "y.y";
350         musicProperties = "property";
351         lockLeasePeriod = "5000";
352         cassandraUser = "user";
353         cassandraPassword = "password";
354         cassandraPort = "8007";
355         cassandraConnectTimeOutMS = "1000";
356         cassandraReadTimeOutMS = "1000";
357         isCadi = "true";
358         isKeyspaceActive = "true";
359         rertryCount = "20";
360         transIdPrefix = "transId-";
361         conversationIdPrefix = "conversation-";
362         clientIdPrefix = "clientId-";
363         messageIdPrefix = "messageId-";    
364         transIdRequired = true;
365         conversationIdRequired = true;
366         clientIdRequired = true;
367         messageIdRequired = true;
368         musicAafNs = "ns";
369         cipherEncKey = "key";
370     }
371
372 }