summaryrefslogtreecommitdiffstats
path: root/ems/boco/src/test/java/org/onap/vfc/nfvo/emsdriver/configmgr/ConfigurationImpTest.java
blob: 15284daf6636ae6043e52766acc301d430dcd858 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * Copyright 2017 BOCO Corporation.  CMCC Technologies Co., Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onap.vfc.nfvo.emsdriver.configmgr;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdom.Document;
import org.jdom.Element;
import org.junit.Before;
import org.junit.Test;
import org.onap.vfc.nfvo.emsdriver.commons.constant.Constant;
import org.onap.vfc.nfvo.emsdriver.commons.model.CollectVo;
import org.onap.vfc.nfvo.emsdriver.commons.model.EMSInfo;
import org.onap.vfc.nfvo.emsdriver.commons.utils.StringUtil;
import org.onap.vfc.nfvo.emsdriver.commons.utils.XmlUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

public class ConfigurationImpTest {
    //normally an unit test should not log
    protected static Log log = LogFactory.getLog(ConfigurationImpTest.class);
    private ConfigurationManager configurationManager;
    private ConfigurationImp configurationImp;

    public static void readcfg() {
        String path = Constant.SYS_CFG + "EMSInfo.xml";
        File cfg = new File(path);
        log.debug("start loading " + path);
        if (!cfg.exists() || !cfg.isFile()) {
            log.debug("not exists " + path);
            return;
        }


        InputStream is = null;
        Map<String, EMSInfo> tmpcache = new HashMap<String, EMSInfo>();

        try {
            is = new FileInputStream(cfg);
            Document doc = XmlUtil.getDocument(is);

            Element root = doc.getRootElement();

            @SuppressWarnings("unchecked")
            List<Element> children = root.getChildren();

            for (Iterator<Element> it = children.iterator(); it.hasNext(); ) {
                EMSInfo emsInfo = new EMSInfo();
                Element child = it.next();
                String name = child.getAttributeValue("name");
                if (StringUtil.isBank(name)) {
                    continue;
                }
                emsInfo.setName(name);

//				tmpcache.put(name, emsInfo);

                @SuppressWarnings("unchecked")
                List<Element> collectList = child.getChildren();
                for (Element collect : collectList) {

                    CollectVo collectVo = new CollectVo();

                    String type = collect.getAttributeValue("type");
                    if ("alarm".equalsIgnoreCase(type)) {
                        boolean iscollect = Boolean.parseBoolean(collect.getAttributeValue("iscollect"));
                        if (iscollect) {
                            collectVo.setIscollect(iscollect);
                        } else {
                            continue;
                        }
                        collectVo.setType(type);
                        collectVo.setIP(collect.getChildText("ip"));
                        collectVo.setPort(collect.getChildText("port"));
                        collectVo.setUser(collect.getChildText("user"));
                        collectVo.setPassword(collect.getChildText("password"));
                        collectVo.setRead_timeout(collect.getChildText("readtimeout"));
                    } else {
                        String crontab = collect.getAttributeValue("crontab");
                        if (!StringUtil.isBank(type) && !StringUtil.isBank(crontab)) {
                            collectVo.setType(type);
                            collectVo.setCrontab(crontab);
                        } else {
                            continue;
                        }
                        collectVo.setIP(collect.getChildText("ip"));
                        collectVo.setPort(collect.getChildText("port"));
                        collectVo.setUser(collect.getChildText("user"));
                        collectVo.setPassword(collect.getChildText("password"));
                        collectVo.setRemotepath(collect.getChildText("remotepath"));
                        collectVo.setMatch(collect.getChildText("match"));
                        collectVo.setPassive(collect.getChildText("passive"));
                        collectVo.setFtptype(collect.getChildText("ftptype"));
                        collectVo.setGranularity(collect.getChildText("granularity"));
                    }

                    emsInfo.putCollectMap(type, collectVo);
                }
                tmpcache.put(name, emsInfo);
            }
            ConfigurationManager.emsInfoCache.putAll(tmpcache);

            File file = new File(ConfigurationManager.CONFIG_PROPERTIES_LOCATION);
            if (!file.exists() || !file.isFile()) {
                log.error("cacheFilePath " + ConfigurationManager.CONFIG_PROPERTIES_LOCATION + " not exist or is not File");
                return;
            }
            InputStream in = null;
            try {
                ConfigurationManager.properties = new Properties();
                in = new FileInputStream(file);
                ConfigurationManager.properties.load(in);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            log.error("load EMSInfo.xml is error " + StringUtil.getStackTrace(e));
        } finally {
            tmpcache.clear();
            try {
                if (is != null) {
                    is.close();
                    is = null;
                }
            } catch (Exception e2) {
            }
            cfg = null;
        }
    }


    @Before
    public void setUp() throws IOException {
        configurationImp = new ConfigurationImp();
        configurationManager = new ConfigurationManager();
        readcfg();
    }

    @Test
    public void getAllEMSInfo() {

        List<EMSInfo> list = configurationImp.getAllEMSInfo();

        assertTrue(list.size() > 0);
    }

    @Test
    public void getCollectVoByEmsNameAndType() {

        CollectVo collectVo = configurationImp.getCollectVoByEmsNameAndType("1234", "cm");

        assertNotNull(collectVo);
    }

    @Test
    public void getProperties() {

        Properties properties = configurationImp.getProperties();

        assertNotNull(properties);
    }

}