99e7d7bf9014ad34692e67d2049b3beaedcf860d
[ccsdk/features.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 com.google.common.io.Files;
25 import java.io.File;
26 import java.io.FileNotFoundException;
27 import java.io.IOException;
28 import java.nio.charset.StandardCharsets;
29 import org.junit.After;
30 import org.junit.Test;
31 import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation;
32 import org.onap.ccsdk.features.sdnr.wt.devicemanager.vescollectorconnector.impl.VESCollectorClient;
33
34 public class TestVESCollectorClient {
35
36     private static final String TESTCONFIG_CONTENT_NO_AUTH = "[VESCollector]\n" + "VES_COLLECTOR_IP=127.0.0.1\n"
37             + "VES_COLLECTOR_PORT=8080\n" + "VES_COLLECTOR_TLS_ENABLED=false\n" + "VES_COLLECTOR_USERNAME=sample1\n"
38             + "VES_COLLECTOR_PASSWORD=sample1\n" + "VES_COLLECTOR_VERSION=v7\n" + "REPORTING_ENTITY_NAME=ONAP SDN-R\n" + "";
39
40     private static final String TESTCONFIG_CONTENT_AUTH = "[VESCollector]\n" + "VES_COLLECTOR_IP=127.0.0.1\n"
41             + "VES_COLLECTOR_PORT=8080\n" + "VES_COLLECTOR_TLS_ENABLED=true\n" + "VES_COLLECTOR_USERNAME=sample1\n"
42             + "VES_COLLECTOR_PASSWORD=sample1\n" + "VES_COLLECTOR_VERSION=v7\n" + "REPORTING_ENTITY_NAME=ONAP SDN-R\n" + "";
43
44     private static final String message = "Test Message";
45     private static final String CONFIG_FILE = "test.properties";
46
47     @Test
48     public void testNoAuth() throws Exception {
49         ConfigurationFileRepresentation vesCfg;
50         VESCollectorClient vesClient;
51
52         Files.asCharSink(new File(CONFIG_FILE), StandardCharsets.UTF_8).write(TESTCONFIG_CONTENT_NO_AUTH);
53         vesCfg = new ConfigurationFileRepresentation(CONFIG_FILE);
54         vesClient = new VESCollectorClient(vesCfg);
55
56         vesClient.publishVESMessage(message);
57         vesClient.close();
58
59     }
60
61     @Test
62     public void testAuth() throws Exception {
63         ConfigurationFileRepresentation vesCfg;
64         VESCollectorClient vesClient;
65
66         Files.asCharSink(new File("test.properties"), StandardCharsets.UTF_8).write(TESTCONFIG_CONTENT_AUTH);
67         vesCfg = new ConfigurationFileRepresentation("test.properties");
68
69         vesClient = new VESCollectorClient(vesCfg);
70         vesClient.publishVESMessage(message);
71         vesClient.close();
72     }
73
74     @After
75     public void after() throws InterruptedException, IOException {
76
77         delete(new File(CONFIG_FILE));
78
79     }
80
81     private static void delete(File f) throws IOException {
82         if (f.isDirectory()) {
83             for (File c : f.listFiles()) {
84                 delete(c);
85             }
86         }
87         if (!f.delete()) {
88             throw new FileNotFoundException("Failed to delete file: " + f);
89         }
90     }
91 }