2 * Copyright 2017 BOCO Corporation. CMCC Technologies Co., Ltd
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.vfc.nfvo.emsdriver.configmgr;
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
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;
31 import java.io.FileInputStream;
32 import java.io.IOException;
33 import java.io.InputStream;
36 import static org.junit.Assert.assertNotNull;
37 import static org.junit.Assert.assertTrue;
39 public class ConfigurationImpTest {
40 //normally an unit test should not log
41 protected static Log log = LogFactory.getLog(ConfigurationImpTest.class);
42 private ConfigurationManager configurationManager;
43 private ConfigurationImp configurationImp;
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);
55 InputStream is = null;
56 Map<String, EMSInfo> tmpcache = new HashMap<String, EMSInfo>();
59 is = new FileInputStream(cfg);
60 Document doc = XmlUtil.getDocument(is);
62 Element root = doc.getRootElement();
64 @SuppressWarnings("unchecked")
65 List<Element> children = root.getChildren();
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)) {
74 emsInfo.setName(name);
76 // tmpcache.put(name, emsInfo);
78 @SuppressWarnings("unchecked")
79 List<Element> collectList = child.getChildren();
80 for (Element collect : collectList) {
82 CollectVo collectVo = new CollectVo();
84 String type = collect.getAttributeValue("type");
85 if ("alarm".equalsIgnoreCase(type)) {
86 boolean iscollect = Boolean.parseBoolean(collect.getAttributeValue("iscollect"));
88 collectVo.setIscollect(iscollect);
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"));
99 String crontab = collect.getAttributeValue("crontab");
100 if (!StringUtil.isBank(type) && !StringUtil.isBank(crontab)) {
101 collectVo.setType(type);
102 collectVo.setCrontab(crontab);
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"));
117 emsInfo.putCollectMap(type, collectVo);
119 tmpcache.put(name, emsInfo);
121 ConfigurationManager.emsInfoCache.putAll(tmpcache);
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");
128 InputStream in = null;
130 ConfigurationManager.properties = new Properties();
131 in = new FileInputStream(file);
132 ConfigurationManager.properties.load(in);
133 } catch (Exception e) {
136 } catch (Exception e) {
137 log.error("load EMSInfo.xml is error " + StringUtil.getStackTrace(e));
145 } catch (Exception e2) {
153 public void setUp() throws IOException {
154 configurationImp = new ConfigurationImp();
155 configurationManager = new ConfigurationManager();
160 public void getAllEMSInfo() {
162 List<EMSInfo> list = configurationImp.getAllEMSInfo();
164 assertTrue(list.size() > 0);
168 public void getCollectVoByEmsNameAndType() {
170 CollectVo collectVo = configurationImp.getCollectVoByEmsNameAndType("1234", "cm");
172 assertNotNull(collectVo);
176 public void getProperties() {
178 Properties properties = configurationImp.getProperties();
180 assertNotNull(properties);