74d2d2263cca2b5b245ceaf4cda4a6ecbbef2e61
[ccsdk/features.git] /
1 /*******************************************************************************
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk feature sdnr wt
4  *  ================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  ******************************************************************************/
21 package org.onap.ccsdk.features.sdnr.wt.devicemanager.test;
22
23 import static org.junit.Assert.*;
24
25 import java.io.File;
26 import java.io.IOException;
27 import java.nio.charset.StandardCharsets;
28
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.HtDevicemanagerConfiguration;
33 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.impl.AaiConfig;
34 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.impl.DcaeConfig;
35 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.impl.EsConfig;
36 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.impl.PmConfig;
37 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.impl.ToggleAlarmConfig;
38
39 import com.google.common.io.Files;
40
41 public class TestDevMgrPropertiesFile {
42
43     private static final File FILENAME = new File("test.properties");
44     private static final File AAIPROP_FILE=new File("aaiclient.properties");
45     protected int hasChanged;
46
47     @Before
48     public void init() {
49         delete(FILENAME);
50         delete(AAIPROP_FILE);
51     }
52     @After
53     public void deinit() {
54         this.init();
55     }
56
57     @Test
58     public void test1() {
59
60         writeFile(FILENAME, this.getContent1());
61         writeFile(AAIPROP_FILE, this.getAaiPropertiesConfig());
62
63         System.out.println("Read and verify");
64         HtDevicemanagerConfiguration cfg=HtDevicemanagerConfiguration.getTestConfiguration(FILENAME.getPath());
65
66         assertNotNull(cfg.getAai());
67         assertNotNull(cfg.getDcae());
68         assertNotNull(cfg.getPm());
69         assertNotNull(cfg.getEs());
70         assertNotNull(cfg.getToggleAlarm());
71         assertTrue(AaiConfig.isInstantiated());
72         assertTrue(DcaeConfig.isInstantiated());
73         assertTrue(PmConfig.isInstantiated());
74         assertTrue(EsConfig.isInstantiated());
75         assertTrue(ToggleAlarmConfig.isInstantiated());
76
77         System.out.println("Verify\n"+cfg.getAai()+"\n"+AaiConfig.getDefaultConfiguration());
78         boolean res;
79         res = cfg.getAai().equals(AaiConfig.getDefaultConfiguration());
80         res = cfg.getDcae().equals(DcaeConfig.getDefaultConfiguration());
81         res = cfg.getPm().equals(PmConfig.getDefaultConfiguration());
82         res = cfg.getEs().equals(EsConfig.getDefaultConfiguration());
83         res = cfg.getToggleAlarm().equals(ToggleAlarmConfig.getDefaultConfiguration());
84
85         res = cfg.getAai().hashCode() == AaiConfig.getDefaultConfiguration().hashCode();
86         res = cfg.getDcae().hashCode() == DcaeConfig.getDefaultConfiguration().hashCode();
87         res = cfg.getPm().hashCode() == PmConfig.getDefaultConfiguration().hashCode();
88         res = cfg.getEs().hashCode() == EsConfig.getDefaultConfiguration().hashCode();
89         res = cfg.getToggleAlarm().hashCode() == ToggleAlarmConfig.getDefaultConfiguration().hashCode();
90
91     }
92
93     //-- Observer not working with all testcases, because config does not support different file types.
94     //@Test
95     public void test2() {
96
97         hasChanged=0;
98         writeFile(FILENAME, this.getContent1());
99         writeFile(AAIPROP_FILE, this.getAaiPropertiesConfig());
100
101
102         System.out.println("Read and verify");
103         HtDevicemanagerConfiguration cfg=HtDevicemanagerConfiguration.getTestConfiguration(FILENAME.getPath());
104
105         assertNotNull(cfg.getAai());
106         assertNotNull(cfg.getDcae());
107         assertNotNull(cfg.getPm());
108         assertNotNull(cfg.getEs());
109         assertNotNull(cfg.getToggleAlarm());
110         assertTrue(AaiConfig.isInstantiated());
111         assertTrue(DcaeConfig.isInstantiated());
112         assertTrue(PmConfig.isInstantiated());
113         assertTrue(EsConfig.isInstantiated());
114         assertTrue(ToggleAlarmConfig.isInstantiated());
115
116         System.out.println("Verify456\n"+cfg.getAai()+"\n"+AaiConfig.getDefaultConfiguration());
117         cfg.getAai().equals(AaiConfig.getDefaultConfiguration());
118         cfg.getDcae().equals(DcaeConfig.getDefaultConfiguration());
119         cfg.getPm().equals(PmConfig.getDefaultConfiguration());
120         cfg.getEs().equals(EsConfig.getDefaultConfiguration());
121         cfg.getToggleAlarm().equals(ToggleAlarmConfig.getDefaultConfiguration());
122
123         cfg.registerConfigChangedListener(() -> {
124             hasChanged++;
125             System.out.println("file changed listener triggered: "+hasChanged);
126             AaiConfig.reload();
127             DcaeConfig.reload();
128             PmConfig.reload();
129             EsConfig.reload();
130             ToggleAlarmConfig.reload();
131
132         });
133         System.out.println("Listerner registered.");
134         System.out.println(cfg.getAai().toString());
135         System.out.println(cfg.getDcae().toString());
136         System.out.println(cfg.getPm().toString());
137         System.out.println(cfg.getEs().toString());
138         System.out.println(cfg.getToggleAlarm().toString());
139
140         sleep(5000);
141         System.out.println("Write new content. Changes "+hasChanged);
142         writeFile(FILENAME, this.getContent2());
143         sleep(5000);
144
145         int i=10;
146         while(hasChanged == 0 && i-- > 0) {
147             System.out.println("Wait for Change indication.");
148             sleep(1000);
149         }
150         System.out.println("Changes "+hasChanged);
151
152         assertTrue("fileChanged counter"+hasChanged, hasChanged > 0);
153         assertFalse(cfg.getAai().hashCode()==AaiConfig.getDefaultConfiguration().hashCode());
154         assertFalse(cfg.getDcae().hashCode()==DcaeConfig.getDefaultConfiguration().hashCode());
155         assertFalse(cfg.getPm().hashCode()==PmConfig.getDefaultConfiguration().hashCode());
156         assertFalse(cfg.getEs().hashCode()==EsConfig.getDefaultConfiguration().hashCode());
157         assertFalse(cfg.getToggleAlarm().hashCode()==ToggleAlarmConfig.getDefaultConfiguration().hashCode());
158
159         HtDevicemanagerConfiguration.clear();
160
161         System.out.println("Test done");
162
163     }
164
165
166     private void sleep(int milliseconds) {
167         try {
168             Thread.sleep(milliseconds);
169         } catch (InterruptedException e) {
170         }
171     }
172
173     private void writeFile(File f, String content) {
174          try {
175             Files.asCharSink(f, StandardCharsets.UTF_8).write(content);
176         } catch (IOException e) {
177             fail(e.getMessage());
178         };
179         sleep(500);
180     }
181
182     private void delete(File f) {
183         if(f.exists()) {
184             f.delete();
185         }
186     }
187
188
189     private String getContent2() {
190         return "[dcae]\n" +
191                 "dcaeUserCredentials=admin:admin\n" +
192                 "dcaeUrl=http://localhost:45451/abc\n" +
193                 "dcaeHeartbeatPeriodSeconds=120\n" +
194                 "dcaeTestCollector=no\n" +
195                 "\n" +
196                 "[aots]\n" +
197                 "userPassword=passwd\n" +
198                 "soapurladd=off\n" +
199                 "soapaddtimeout=10\n" +
200                 "soapinqtimeout=20\n" +
201                 "userName=user\n" +
202                 "inqtemplate=inqreq.tmpl.xml\n" +
203                 "assignedto=userid\n" +
204                 "addtemplate=addreq.tmpl.xml\n" +
205                 "severitypassthrough=critical,major,minor,warning\n" +
206                 "systemuser=user\n" +
207                 "prt-offset=1200\n" +
208                 "soapurlinq=off\n" +
209                 "#smtpHost=\n" +
210                 "#smtpPort=\n" +
211                 "#smtpUsername=\n" +
212                 "#smtpPassword=\n" +
213                 "#smtpSender=\n" +
214                 "#smtpReceivers=\n" +
215                 "\n" +
216                 "[es]\n" +
217                 "esCluster=sendateodl5\n" +
218                 "\n" +
219                 "[aai]\n" +
220                 "#keep comment\n" +
221                 "aaiHeaders=[\"X-TransactionId: 9999\"]\n" +
222                 "aaiUrl=off\n" +
223                 "aaiUserCredentials=AAI:AAI\n" +
224                 "aaiDeleteOnMountpointRemove=true\n" +
225                 "aaiTrustAllCerts=false\n" +
226                 "aaiApiVersion=aai/v13\n" +
227                 "aaiPropertiesFile=aaiclient.properties\n" +
228                 "aaiApplicationId=SDNR\n" +
229                 "aaiPcks12ClientCertFile=/opt/logs/externals/data/stores/keystore.client.p12\n" +
230                 "aaiPcks12ClientCertPassphrase=adminadmin\n" +
231                 "aaiClientConnectionTimeout=30000\n" +
232                 "\n" +
233                 "[pm]\n" +
234                 "pmCluster=sendateodl5\n" +
235                 "pmEnabled=true\n" +
236                 "[toggleAlarmFilter]\n" +
237                 "taEnabled=true\n" +
238                 "taDelay=5555\n" +
239                 "";
240     }
241
242     private String getContent1() {
243         return "[dcae]\n" +
244                 "dcaeUserCredentials=admin:admin\n" +
245                 "dcaeUrl=http://localhost:45/abc\n" +
246                 "dcaeHeartbeatPeriodSeconds=120\n" +
247                 "dcaeTestCollector=no\n" +
248                 "\n" +
249                 "[aots]\n" +
250                 "userPassword=passwd\n" +
251                 "soapurladd=off\n" +
252                 "soapaddtimeout=10\n" +
253                 "soapinqtimeout=20\n" +
254                 "userName=user\n" +
255                 "inqtemplate=inqreq.tmpl.xml\n" +
256                 "assignedto=userid\n" +
257                 "addtemplate=addreq.tmpl.xml\n" +
258                 "severitypassthrough=critical,major,minor,warning\n" +
259                 "systemuser=user\n" +
260                 "prt-offset=1200\n" +
261                 "soapurlinq=off\n" +
262                 "#smtpHost=\n" +
263                 "#smtpPort=\n" +
264                 "#smtpUsername=\n" +
265                 "#smtpPassword=\n" +
266                 "#smtpSender=\n" +
267                 "#smtpReceivers=\n" +
268                 "\n" +
269                 "[es]\n" +
270                 "esCluster=sendateodl5\n" +
271                 "\n" +
272                 "[aai]\n" +
273                 "#keep comment\n" +
274                 "aaiHeaders=[\"X-TransactionId: 9999\"]\n" +
275                 "aaiUrl=off\n" +
276                 "aaiUserCredentials=AAI:AAI\n" +
277                 "aaiDeleteOnMountpointRemove=true\n" +
278                 "aaiTrustAllCerts=false\n" +
279                 "aaiApiVersion=aai/v13\n" +
280                 "aaiPropertiesFile=aaiclient.properties\n" +
281                 "\n" +
282                 "[pm]\n" +
283                 "pmCluster=sendateodl5\n" +
284                 "pmEnabled=true\n" +
285                 "[toggleAlarmFilter]\n" +
286                 "taEnabled=false\n" +
287                 "taDelay=5555\n" +
288                 "";
289     }
290     private String getAaiPropertiesConfig() {
291         return "org.onap.ccsdk.sli.adaptors.aai.ssl.key=\"\"\n" +
292                 "org.onap.ccsdk.sli.adaptors.aai.ssl.key.psswd=\"\"\n" +
293                 "org.onap.ccsdk.sli.adaptors.aai.host.certificate.ignore=\"false\"\n" +
294                 "org.onap.ccsdk.sli.adaptors.aai.application=\"\"\n" +
295                 "org.onap.ccsdk.sli.adaptors.aai.uri=\"\"\n" +
296                 "connection.timeout=60000\n" +
297                 "read.timeout=60000";
298     }
299
300 }