72ff9b706365d93b0524f85f201474e1c64f0f31
[oam/oam-controller.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2020 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  */
22 package org.onap.ccsdk.features.sdnr.wt.devicemanager.test;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26
27 import com.google.common.io.Files;
28 import java.io.File;
29 import java.io.FileNotFoundException;
30 import java.io.IOException;
31 import java.nio.charset.StandardCharsets;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation;
36 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESMessage;
37 import org.onap.ccsdk.features.sdnr.wt.devicemanager.vescollectorconnector.impl.VESCollectorServiceImpl;
38 import org.onap.ccsdk.features.sdnr.wt.devicemanager.vescollectorconnector.impl.config.VESCollectorCfgImpl;
39
40 public class TestVESCollectorClient {
41
42     private static final String TESTCONFIG_CONTENT_NO_AUTH = "[VESCollector]\n" + "VES_COLLECTOR_IP=127.0.0.1\n"
43             + "VES_COLLECTOR_PORT=8080\n" + "VES_COLLECTOR_TLS_ENABLED=false\n" + "VES_COLLECTOR_USERNAME=sample1\n"
44             + "VES_COLLECTOR_PASSWORD=sample1\n" + "VES_COLLECTOR_VERSION=v7\n" + "REPORTING_ENTITY_NAME=ONAP SDN-R\n" + "";
45
46     private static final String TESTCONFIG_CONTENT_AUTH = "[VESCollector]\n" + "VES_COLLECTOR_IP=127.0.0.1\n"
47             + "VES_COLLECTOR_PORT=8080\n" + "VES_COLLECTOR_TLS_ENABLED=true\n" + "VES_COLLECTOR_USERNAME=sample1\n"
48             + "VES_COLLECTOR_PASSWORD=sample1\n" + "VES_COLLECTOR_VERSION=v7\n" + "REPORTING_ENTITY_NAME=ONAP SDN-R\n" + "";
49
50     private static final VESMessage message = new VESMessage("Test Message");
51     private static final String CONFIG_FILE = "test.properties";
52     private static final String CONFIG_FILE2 = "test2.properties";
53
54     @Test
55     public void testNoAuth() throws Exception {
56         ConfigurationFileRepresentation vesCfg;
57         VESCollectorServiceImpl vesClient;
58
59         Files.asCharSink(new File(CONFIG_FILE), StandardCharsets.UTF_8).write(TESTCONFIG_CONTENT_NO_AUTH);
60         vesCfg = new ConfigurationFileRepresentation(CONFIG_FILE);
61         vesClient = new VESCollectorServiceImpl(vesCfg);
62
63         vesClient.publishVESMessage(message);
64         vesClient.close();
65
66     }
67
68     @Test
69     public void testAuth() throws Exception {
70         ConfigurationFileRepresentation vesCfg;
71         VESCollectorServiceImpl vesClient;
72
73         Files.asCharSink(new File("test.properties"), StandardCharsets.UTF_8).write(TESTCONFIG_CONTENT_AUTH);
74         vesCfg = new ConfigurationFileRepresentation("test.properties");
75
76         vesClient = new VESCollectorServiceImpl(vesCfg);
77         vesClient.publishVESMessage(message);
78         vesClient.close();
79     }
80
81     @Test
82     public void testDefaultConfigValues() throws IOException {
83         Files.asCharSink(new File(CONFIG_FILE2), StandardCharsets.UTF_8).write("");
84         ConfigurationFileRepresentation cfg = new ConfigurationFileRepresentation(CONFIG_FILE2);
85         VESCollectorCfgImpl vesConfig = new VESCollectorCfgImpl(cfg);
86         assertEquals("ONAP SDN-R", vesConfig.getReportingEntityName());
87         assertEquals("SHORT", vesConfig.getEventLogMsgDetail());
88         assertEquals("v7",vesConfig.getVersion());
89         assertFalse(vesConfig.isVESCollectorEnabled());
90         assertFalse(vesConfig.isTrustAllCerts());
91
92     }
93
94
95     @Before
96     @After
97     public void after() throws InterruptedException, IOException {
98
99         delete(new File(CONFIG_FILE));
100         delete(new File(CONFIG_FILE2));
101
102     }
103
104     private static void delete(File f) throws IOException {
105         if (f.isDirectory()) {
106             for (File c : f.listFiles()) {
107                 delete(c);
108             }
109         }
110         if(f.exists()) {
111             if (!f.delete()) {
112                 throw new FileNotFoundException("Failed to delete file: " + f);
113             }
114         }
115     }
116 }