Update ves-agent dependency
[vfc/nfvo/driver/ems.git] / ems / boco / src / test / java / org / onap / vfc / nfvo / emsdriver / configmgr / ConfigurationImpTest.java
1 /*
2  * Copyright 2017 BOCO Corporation.  CMCC Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.vfc.nfvo.emsdriver.configmgr;
17
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20 import org.jdom.Document;
21 import org.jdom.Element;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.onap.vfc.nfvo.emsdriver.commons.constant.Constant;
25 import org.onap.vfc.nfvo.emsdriver.commons.model.CollectVo;
26 import org.onap.vfc.nfvo.emsdriver.commons.model.EMSInfo;
27 import org.onap.vfc.nfvo.emsdriver.commons.utils.StringUtil;
28 import org.onap.vfc.nfvo.emsdriver.commons.utils.XmlUtil;
29
30 import java.io.File;
31 import java.io.FileInputStream;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.util.*;
35
36 import static org.junit.Assert.assertNotNull;
37 import static org.junit.Assert.assertTrue;
38
39 public class ConfigurationImpTest {
40     //normally an unit test should not log
41     protected static Logger log = LoggerFactory.getLogger(ConfigurationImpTest.class);
42     private ConfigurationManager configurationManager;
43     private ConfigurationImp configurationImp;
44
45     public static void readcfg() {
46         String path = Constant.SYS_CFG + "EMSInfo.xml";
47         File cfg = new File(path);
48         log.debug("start loading " + path);
49         if (!cfg.exists() || !cfg.isFile()) {
50             log.debug("not exists " + path);
51             return;
52         }
53
54
55         InputStream is = null;
56         Map<String, EMSInfo> tmpcache = new HashMap<String, EMSInfo>();
57
58         try {
59             is = new FileInputStream(cfg);
60             Document doc = XmlUtil.getDocument(is);
61
62             Element root = doc.getRootElement();
63
64             @SuppressWarnings("unchecked")
65             List<Element> children = root.getChildren();
66
67             for (Iterator<Element> it = children.iterator(); it.hasNext(); ) {
68                 EMSInfo emsInfo = new EMSInfo();
69                 Element child = it.next();
70                 String name = child.getAttributeValue("name");
71                 if (StringUtil.isBank(name)) {
72                     continue;
73                 }
74                 emsInfo.setName(name);
75
76 //                              tmpcache.put(name, emsInfo);
77
78                 @SuppressWarnings("unchecked")
79                 List<Element> collectList = child.getChildren();
80                 for (Element collect : collectList) {
81
82                     CollectVo collectVo = new CollectVo();
83
84                     String type = collect.getAttributeValue("type");
85                     if ("alarm".equalsIgnoreCase(type)) {
86                         boolean iscollect = Boolean.parseBoolean(collect.getAttributeValue("iscollect"));
87                         if (iscollect) {
88                             collectVo.setIscollect(iscollect);
89                         } else {
90                             continue;
91                         }
92                         collectVo.setType(type);
93                         collectVo.setIP(collect.getChildText("ip"));
94                         collectVo.setPort(collect.getChildText("port"));
95                         collectVo.setUser(collect.getChildText("user"));
96                         collectVo.setPassword(collect.getChildText("password"));
97                         collectVo.setReadTimeout(collect.getChildText("readtimeout"));
98                     } else {
99                         String crontab = collect.getAttributeValue("crontab");
100                         if (!StringUtil.isBank(type) && !StringUtil.isBank(crontab)) {
101                             collectVo.setType(type);
102                             collectVo.setCrontab(crontab);
103                         } else {
104                             continue;
105                         }
106                         collectVo.setIP(collect.getChildText("ip"));
107                         collectVo.setPort(collect.getChildText("port"));
108                         collectVo.setUser(collect.getChildText("user"));
109                         collectVo.setPassword(collect.getChildText("password"));
110                         collectVo.setRemotepath(collect.getChildText("remotepath"));
111                         collectVo.setMatch(collect.getChildText("match"));
112                         collectVo.setPassive(collect.getChildText("passive"));
113                         collectVo.setFtptype(collect.getChildText("ftptype"));
114                         collectVo.setGranularity(collect.getChildText("granularity"));
115                     }
116
117                     emsInfo.putCollectMap(type, collectVo);
118                 }
119                 tmpcache.put(name, emsInfo);
120             }
121             ConfigurationManager.emsInfoCache.putAll(tmpcache);
122
123             File file = new File(ConfigurationManager.CONFIG_PROPERTIES_LOCATION);
124             if (!file.exists() || !file.isFile()) {
125                 log.error("cacheFilePath " + ConfigurationManager.CONFIG_PROPERTIES_LOCATION + " not exist or is not File");
126                 return;
127             }
128             InputStream in = null;
129             try {
130                 ConfigurationManager.properties = new Properties();
131                 in = new FileInputStream(file);
132                 ConfigurationManager.properties.load(in);
133             } catch (Exception e) {
134                 e.printStackTrace();
135             }
136         } catch (Exception e) {
137             log.error("load EMSInfo.xml is error " + StringUtil.getStackTrace(e));
138         } finally {
139             tmpcache.clear();
140             try {
141                 if (is != null) {
142                     is.close();
143                     is = null;
144                 }
145             } catch (Exception e2) {
146             }
147             cfg = null;
148         }
149     }
150
151
152     @Before
153     public void setUp() throws IOException {
154         configurationImp = new ConfigurationImp();
155         configurationManager = new ConfigurationManager();
156         readcfg();
157     }
158
159     @Test
160     public void getAllEMSInfo() {
161
162         List<EMSInfo> list = configurationImp.getAllEMSInfo();
163
164         assertTrue(list.size() > 0);
165     }
166
167     @Test
168     public void getCollectVoByEmsNameAndType() {
169
170         CollectVo collectVo = configurationImp.getCollectVoByEmsNameAndType("1234", "cm");
171
172         assertNotNull(collectVo);
173     }
174
175     @Test
176     public void getProperties() {
177
178         Properties properties = configurationImp.getProperties();
179
180         assertNotNull(properties);
181     }
182
183 }