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