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