3fe4f1abf76269e21d1c8ec80962f5b79eac19d7
[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.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26
27 import java.io.File;
28 import java.io.IOException;
29 import java.nio.charset.StandardCharsets;
30
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation;
35 import org.onap.ccsdk.features.sdnr.wt.database.config.EsConfig;
36 import org.onap.ccsdk.features.sdnr.wt.devicemanager.aaiconnector.impl.config.AaiConfig;
37 import org.onap.ccsdk.features.sdnr.wt.devicemanager.archiveservice.ArchiveCleanService;
38 import org.onap.ccsdk.features.sdnr.wt.devicemanager.dcaeconnector.impl.config.DcaeConfig;
39 import org.onap.ccsdk.features.sdnr.wt.devicemanager.performancemanager.impl.config.PmConfig;
40 import org.onap.ccsdk.features.sdnr.wt.devicemanager.toggleAlarmFilter.conf.ToggleAlarmConfig;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 import com.google.common.io.Files;
45
46 public class TestDevMgrPropertiesFile {
47
48     private static final Logger LOG = LoggerFactory.getLogger(ArchiveCleanService.class);
49
50     private static final File FILENAME = new File("test.properties");
51     private static final File AAIPROP_FILE=new File("aaiclient.properties");
52     private int hasChanged;
53
54     @Before
55     public void init() {
56         //if (! LOG.isDebugEnabled()) {
57                 delete(FILENAME);
58                 delete(AAIPROP_FILE);
59         //}
60     }
61     @After
62     public void deinit() {
63         this.init();
64     }
65
66     @Test
67     public void test1() {
68
69         writeFile(FILENAME, this.getContent1());
70         writeFile(AAIPROP_FILE, this.getAaiPropertiesConfig());
71
72         System.out.println("Read and verify");
73         ConfigurationFileRepresentation cfg = new ConfigurationFileRepresentation(FILENAME.getPath());
74         ConfigurationFileRepresentation cfg2 = cfg;
75
76         AaiConfig aaiConfig = new AaiConfig(cfg2);
77         assertNotNull(aaiConfig);
78         DcaeConfig dcaeConfig = new DcaeConfig(cfg2);
79         assertNotNull(dcaeConfig);
80         PmConfig pmConfig = new PmConfig(cfg2);
81         assertNotNull(pmConfig);
82         EsConfig esConfig = new EsConfig(cfg2);
83         assertNotNull(esConfig);
84         ToggleAlarmConfig toggleAlarmConfig = new ToggleAlarmConfig(cfg2);
85         assertNotNull(toggleAlarmConfig);
86
87         System.out.println("Verify\n"+aaiConfig+"\n");
88         @SuppressWarnings("unused")
89                 boolean res;
90         /*
91         res = cfg.getAai().equals(AaiConfig.getDefaultConfiguration());
92         res = cfg.getDcae().equals(DcaeConfig.getDefaultConfiguration());
93         res = cfg.getPm().equals(PmConfig.getDefaultConfiguration());
94         res = cfg.getEs().equals(EsConfig.getDefaultConfiguration());
95         res = cfg.getToggleAlarm().equals(ToggleAlarmConfig.getDefaultConfiguration());
96
97         res = cfg.getAai().hashCode() == AaiConfig.getDefaultConfiguration().hashCode();
98         res = cfg.getDcae().hashCode() == DcaeConfig.getDefaultConfiguration().hashCode();
99         res = cfg.getPm().hashCode() == PmConfig.getDefaultConfiguration().hashCode();
100         res = cfg.getEs().hashCode() == EsConfig.getDefaultConfiguration().hashCode();
101         res = cfg.getToggleAlarm().hashCode() == ToggleAlarmConfig.getDefaultConfiguration().hashCode();
102         */
103     }
104
105     //-- Observer not working with all testcases, because config does not support different file types.
106     @Test
107     public void test2() {
108
109         hasChanged=0;
110         writeFile(FILENAME, this.getContent1());
111         writeFile(AAIPROP_FILE, this.getAaiPropertiesConfig());
112
113         System.out.println("Read and verify");
114         ConfigurationFileRepresentation cfg2 = new ConfigurationFileRepresentation(FILENAME.getPath());
115
116         AaiConfig aaiConfig = new AaiConfig(cfg2);
117         assertNotNull(aaiConfig);
118         DcaeConfig dcaeConfig = new DcaeConfig(cfg2);
119         assertNotNull(dcaeConfig);
120         PmConfig pmConfig = new PmConfig(cfg2);
121         assertNotNull(pmConfig);
122         EsConfig esConfig = new EsConfig(cfg2);
123         assertNotNull(esConfig);
124         ToggleAlarmConfig toggleAlarmConfig = new ToggleAlarmConfig(cfg2);
125         assertNotNull(toggleAlarmConfig);
126
127         cfg2.registerConfigChangedListener(() -> {
128             hasChanged++;
129             System.out.println("file changed listener triggered: "+hasChanged);
130         });
131
132         sleep(1000);
133         System.out.println("Write new content. Changes "+hasChanged);
134         writeFile(FILENAME, this.getContent2());
135         sleep(1000);
136
137         int i=10;
138         while(hasChanged == 0 && i-- > 0) {
139             System.out.println("Wait for Change indication.");
140             sleep(1000);
141         }
142         System.out.println("Changes "+hasChanged);
143
144         assertTrue("fileChanged counter"+hasChanged, hasChanged > 0);
145         System.out.println("Test done");
146
147     }
148
149
150     private static void sleep(int milliseconds) {
151         try {
152             Thread.sleep(milliseconds);
153         } catch (InterruptedException e) {
154         }
155     }
156
157     public static void writeFile(File f, String content) {
158          try {
159             Files.asCharSink(f, StandardCharsets.UTF_8).write(content);
160         } catch (IOException e) {
161             fail(e.getMessage());
162         };
163         sleep(500);
164     }
165
166     private void delete(File f) {
167         if(f.exists()) {
168             f.delete();
169         }
170     }
171
172
173     private String getContent2() {
174         return "[dcae]\n" +
175                 "dcaeUserCredentials=admin:admin\n" +
176                 "dcaeUrl=http://localhost:45451/abc\n" +
177                 "dcaeHeartbeatPeriodSeconds=120\n" +
178                 "dcaeTestCollector=no\n" +
179                 "\n" +
180                 "[aots]\n" +
181                 "userPassword=passwd\n" +
182                 "soapurladd=off\n" +
183                 "soapaddtimeout=10\n" +
184                 "soapinqtimeout=20\n" +
185                 "userName=user\n" +
186                 "inqtemplate=inqreq.tmpl.xml\n" +
187                 "assignedto=userid\n" +
188                 "addtemplate=addreq.tmpl.xml\n" +
189                 "severitypassthrough=critical,major,minor,warning\n" +
190                 "systemuser=user\n" +
191                 "prt-offset=1200\n" +
192                 "soapurlinq=off\n" +
193                 "#smtpHost=\n" +
194                 "#smtpPort=\n" +
195                 "#smtpUsername=\n" +
196                 "#smtpPassword=\n" +
197                 "#smtpSender=\n" +
198                 "#smtpReceivers=\n" +
199                 "\n" +
200                 "[es]\n" +
201                 "esCluster=sendateodl5\n" +
202                 "\n" +
203                 "[aai]\n" +
204                 "#keep comment\n" +
205                 "aaiHeaders=[\"X-TransactionId: 9999\"]\n" +
206                 "aaiUrl=off\n" +
207                 "aaiUserCredentials=AAI:AAI\n" +
208                 "aaiDeleteOnMountpointRemove=true\n" +
209                 "aaiTrustAllCerts=false\n" +
210                 "aaiApiVersion=aai/v13\n" +
211                 "aaiPropertiesFile=aaiclient.properties\n" +
212                 "aaiApplicationId=SDNR\n" +
213                 "aaiPcks12ClientCertFile=/opt/logs/externals/data/stores/keystore.client.p12\n" +
214                 "aaiPcks12ClientCertPassphrase=adminadmin\n" +
215                 "aaiClientConnectionTimeout=30000\n" +
216                 "\n" +
217                 "[pm]\n" +
218                 "pmCluster=sendateodl5\n" +
219                 "pmEnabled=true\n" +
220                 "[toggleAlarmFilter]\n" +
221                 "taEnabled=true\n" +
222                 "taDelay=5555\n" +
223                 "";
224     }
225
226     private String getContent1() {
227         return "[dcae]\n" +
228                 "dcaeUserCredentials=admin:admin\n" +
229                 "dcaeUrl=http://localhost:45/abc\n" +
230                 "dcaeHeartbeatPeriodSeconds=120\n" +
231                 "dcaeTestCollector=no\n" +
232                 "\n" +
233                 "[aots]\n" +
234                 "userPassword=passwd\n" +
235                 "soapurladd=off\n" +
236                 "soapaddtimeout=10\n" +
237                 "soapinqtimeout=20\n" +
238                 "userName=user\n" +
239                 "inqtemplate=inqreq.tmpl.xml\n" +
240                 "assignedto=userid\n" +
241                 "addtemplate=addreq.tmpl.xml\n" +
242                 "severitypassthrough=critical,major,minor,warning\n" +
243                 "systemuser=user\n" +
244                 "prt-offset=1200\n" +
245                 "soapurlinq=off\n" +
246                 "#smtpHost=\n" +
247                 "#smtpPort=\n" +
248                 "#smtpUsername=\n" +
249                 "#smtpPassword=\n" +
250                 "#smtpSender=\n" +
251                 "#smtpReceivers=\n" +
252                 "\n" +
253                 "[es]\n" +
254                 "esCluster=sendateodl5\n" +
255                 "\n" +
256                 "[aai]\n" +
257                 "#keep comment\n" +
258                 "aaiHeaders=[\"X-TransactionId: 9999\"]\n" +
259                 "aaiUrl=off\n" +
260                 "aaiUserCredentials=AAI:AAI\n" +
261                 "aaiDeleteOnMountpointRemove=true\n" +
262                 "aaiTrustAllCerts=false\n" +
263                 "aaiApiVersion=aai/v13\n" +
264                 "aaiPropertiesFile=aaiclient.properties\n" +
265                 "\n" +
266                 "[pm]\n" +
267                 "pmCluster=sendateodl5\n" +
268                 "pmEnabled=true\n" +
269                 "[toggleAlarmFilter]\n" +
270                 "taEnabled=false\n" +
271                 "taDelay=5555\n" +
272                 "";
273     }
274     private String getAaiPropertiesConfig() {
275         return "org.onap.ccsdk.sli.adaptors.aai.ssl.key=keykey\"\"\n" +
276                 "org.onap.ccsdk.sli.adaptors.aai.ssl.key.psswd=psswdpsswd\"\"\n" +
277                 "org.onap.ccsdk.sli.adaptors.aai.host.certificate.ignore=\"false\"\n" +
278                 "org.onap.ccsdk.sli.adaptors.aai.application=appxyz\"\"\n" +
279                 "org.onap.ccsdk.sli.adaptors.aai.uri=uriu\"\"\n" +
280                 "connection.timeout=60000\n" +
281                 "read.timeout=60000";
282     }
283
284 }