5242bac1c5fccd1c664f69b36863a6c572f1fbf1
[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         @SuppressWarnings("unused")
79                 boolean res;
80         res = cfg.getAai().equals(AaiConfig.getDefaultConfiguration());
81         res = cfg.getDcae().equals(DcaeConfig.getDefaultConfiguration());
82         res = cfg.getPm().equals(PmConfig.getDefaultConfiguration());
83         res = cfg.getEs().equals(EsConfig.getDefaultConfiguration());
84         res = cfg.getToggleAlarm().equals(ToggleAlarmConfig.getDefaultConfiguration());
85
86         res = cfg.getAai().hashCode() == AaiConfig.getDefaultConfiguration().hashCode();
87         res = cfg.getDcae().hashCode() == DcaeConfig.getDefaultConfiguration().hashCode();
88         res = cfg.getPm().hashCode() == PmConfig.getDefaultConfiguration().hashCode();
89         res = cfg.getEs().hashCode() == EsConfig.getDefaultConfiguration().hashCode();
90         res = cfg.getToggleAlarm().hashCode() == ToggleAlarmConfig.getDefaultConfiguration().hashCode();
91
92     }
93
94     //-- Observer not working with all testcases, because config does not support different file types.
95     //@Test
96     public void test2() {
97
98         hasChanged=0;
99         writeFile(FILENAME, this.getContent1());
100         writeFile(AAIPROP_FILE, this.getAaiPropertiesConfig());
101
102
103         System.out.println("Read and verify");
104         HtDevicemanagerConfiguration cfg=HtDevicemanagerConfiguration.getTestConfiguration(FILENAME.getPath());
105
106         assertNotNull(cfg.getAai());
107         assertNotNull(cfg.getDcae());
108         assertNotNull(cfg.getPm());
109         assertNotNull(cfg.getEs());
110         assertNotNull(cfg.getToggleAlarm());
111         assertTrue(AaiConfig.isInstantiated());
112         assertTrue(DcaeConfig.isInstantiated());
113         assertTrue(PmConfig.isInstantiated());
114         assertTrue(EsConfig.isInstantiated());
115         assertTrue(ToggleAlarmConfig.isInstantiated());
116
117         System.out.println("Verify456\n"+cfg.getAai()+"\n"+AaiConfig.getDefaultConfiguration());
118         cfg.getAai().equals(AaiConfig.getDefaultConfiguration());
119         cfg.getDcae().equals(DcaeConfig.getDefaultConfiguration());
120         cfg.getPm().equals(PmConfig.getDefaultConfiguration());
121         cfg.getEs().equals(EsConfig.getDefaultConfiguration());
122         cfg.getToggleAlarm().equals(ToggleAlarmConfig.getDefaultConfiguration());
123
124         cfg.registerConfigChangedListener(() -> {
125             hasChanged++;
126             System.out.println("file changed listener triggered: "+hasChanged);
127             AaiConfig.reload();
128             DcaeConfig.reload();
129             PmConfig.reload();
130             EsConfig.reload();
131             ToggleAlarmConfig.reload();
132
133         });
134         System.out.println("Listerner registered.");
135         System.out.println(cfg.getAai().toString());
136         System.out.println(cfg.getDcae().toString());
137         System.out.println(cfg.getPm().toString());
138         System.out.println(cfg.getEs().toString());
139         System.out.println(cfg.getToggleAlarm().toString());
140
141         sleep(5000);
142         System.out.println("Write new content. Changes "+hasChanged);
143         writeFile(FILENAME, this.getContent2());
144         sleep(5000);
145
146         int i=10;
147         while(hasChanged == 0 && i-- > 0) {
148             System.out.println("Wait for Change indication.");
149             sleep(1000);
150         }
151         System.out.println("Changes "+hasChanged);
152
153         assertTrue("fileChanged counter"+hasChanged, hasChanged > 0);
154         assertFalse(cfg.getAai().hashCode()==AaiConfig.getDefaultConfiguration().hashCode());
155         assertFalse(cfg.getDcae().hashCode()==DcaeConfig.getDefaultConfiguration().hashCode());
156         assertFalse(cfg.getPm().hashCode()==PmConfig.getDefaultConfiguration().hashCode());
157         assertFalse(cfg.getEs().hashCode()==EsConfig.getDefaultConfiguration().hashCode());
158         assertFalse(cfg.getToggleAlarm().hashCode()==ToggleAlarmConfig.getDefaultConfiguration().hashCode());
159
160         HtDevicemanagerConfiguration.clear();
161
162         System.out.println("Test done");
163
164     }
165
166
167     private static void sleep(int milliseconds) {
168         try {
169             Thread.sleep(milliseconds);
170         } catch (InterruptedException e) {
171         }
172     }
173
174     public static void writeFile(File f, String content) {
175          try {
176             Files.asCharSink(f, StandardCharsets.UTF_8).write(content);
177         } catch (IOException e) {
178             fail(e.getMessage());
179         };
180         sleep(500);
181     }
182
183     private void delete(File f) {
184         if(f.exists()) {
185             f.delete();
186         }
187     }
188
189
190     private String getContent2() {
191         return "[dcae]\n" +
192                 "dcaeUserCredentials=admin:admin\n" +
193                 "dcaeUrl=http://localhost:45451/abc\n" +
194                 "dcaeHeartbeatPeriodSeconds=120\n" +
195                 "dcaeTestCollector=no\n" +
196                 "\n" +
197                 "[aots]\n" +
198                 "userPassword=passwd\n" +
199                 "soapurladd=off\n" +
200                 "soapaddtimeout=10\n" +
201                 "soapinqtimeout=20\n" +
202                 "userName=user\n" +
203                 "inqtemplate=inqreq.tmpl.xml\n" +
204                 "assignedto=userid\n" +
205                 "addtemplate=addreq.tmpl.xml\n" +
206                 "severitypassthrough=critical,major,minor,warning\n" +
207                 "systemuser=user\n" +
208                 "prt-offset=1200\n" +
209                 "soapurlinq=off\n" +
210                 "#smtpHost=\n" +
211                 "#smtpPort=\n" +
212                 "#smtpUsername=\n" +
213                 "#smtpPassword=\n" +
214                 "#smtpSender=\n" +
215                 "#smtpReceivers=\n" +
216                 "\n" +
217                 "[es]\n" +
218                 "esCluster=sendateodl5\n" +
219                 "\n" +
220                 "[aai]\n" +
221                 "#keep comment\n" +
222                 "aaiHeaders=[\"X-TransactionId: 9999\"]\n" +
223                 "aaiUrl=off\n" +
224                 "aaiUserCredentials=AAI:AAI\n" +
225                 "aaiDeleteOnMountpointRemove=true\n" +
226                 "aaiTrustAllCerts=false\n" +
227                 "aaiApiVersion=aai/v13\n" +
228                 "aaiPropertiesFile=aaiclient.properties\n" +
229                 "aaiApplicationId=SDNR\n" +
230                 "aaiPcks12ClientCertFile=/opt/logs/externals/data/stores/keystore.client.p12\n" +
231                 "aaiPcks12ClientCertPassphrase=adminadmin\n" +
232                 "aaiClientConnectionTimeout=30000\n" +
233                 "\n" +
234                 "[pm]\n" +
235                 "pmCluster=sendateodl5\n" +
236                 "pmEnabled=true\n" +
237                 "[toggleAlarmFilter]\n" +
238                 "taEnabled=true\n" +
239                 "taDelay=5555\n" +
240                 "";
241     }
242
243     private String getContent1() {
244         return "[dcae]\n" +
245                 "dcaeUserCredentials=admin:admin\n" +
246                 "dcaeUrl=http://localhost:45/abc\n" +
247                 "dcaeHeartbeatPeriodSeconds=120\n" +
248                 "dcaeTestCollector=no\n" +
249                 "\n" +
250                 "[aots]\n" +
251                 "userPassword=passwd\n" +
252                 "soapurladd=off\n" +
253                 "soapaddtimeout=10\n" +
254                 "soapinqtimeout=20\n" +
255                 "userName=user\n" +
256                 "inqtemplate=inqreq.tmpl.xml\n" +
257                 "assignedto=userid\n" +
258                 "addtemplate=addreq.tmpl.xml\n" +
259                 "severitypassthrough=critical,major,minor,warning\n" +
260                 "systemuser=user\n" +
261                 "prt-offset=1200\n" +
262                 "soapurlinq=off\n" +
263                 "#smtpHost=\n" +
264                 "#smtpPort=\n" +
265                 "#smtpUsername=\n" +
266                 "#smtpPassword=\n" +
267                 "#smtpSender=\n" +
268                 "#smtpReceivers=\n" +
269                 "\n" +
270                 "[es]\n" +
271                 "esCluster=sendateodl5\n" +
272                 "\n" +
273                 "[aai]\n" +
274                 "#keep comment\n" +
275                 "aaiHeaders=[\"X-TransactionId: 9999\"]\n" +
276                 "aaiUrl=off\n" +
277                 "aaiUserCredentials=AAI:AAI\n" +
278                 "aaiDeleteOnMountpointRemove=true\n" +
279                 "aaiTrustAllCerts=false\n" +
280                 "aaiApiVersion=aai/v13\n" +
281                 "aaiPropertiesFile=aaiclient.properties\n" +
282                 "\n" +
283                 "[pm]\n" +
284                 "pmCluster=sendateodl5\n" +
285                 "pmEnabled=true\n" +
286                 "[toggleAlarmFilter]\n" +
287                 "taEnabled=false\n" +
288                 "taDelay=5555\n" +
289                 "";
290     }
291     private String getAaiPropertiesConfig() {
292         return "org.onap.ccsdk.sli.adaptors.aai.ssl.key=\"\"\n" +
293                 "org.onap.ccsdk.sli.adaptors.aai.ssl.key.psswd=\"\"\n" +
294                 "org.onap.ccsdk.sli.adaptors.aai.host.certificate.ignore=\"false\"\n" +
295                 "org.onap.ccsdk.sli.adaptors.aai.application=\"\"\n" +
296                 "org.onap.ccsdk.sli.adaptors.aai.uri=\"\"\n" +
297                 "connection.timeout=60000\n" +
298                 "read.timeout=60000";
299     }
300
301 }