Improved the UT coverage
[holmes/common.git] / holmes-actions / src / test / java / org / onap / holmes / common / dcae / utils / DcaeConfigurationParserTest.java
1 /*\r
2  * Copyright 2017 ZTE Corporation.\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  * http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 package org.onap.holmes.common.dcae.utils;\r
18 \r
19 import static org.hamcrest.core.IsEqual.equalTo;\r
20 import static org.junit.Assert.assertThat;\r
21 \r
22 import java.io.BufferedReader;\r
23 import java.io.File;\r
24 import java.io.FileNotFoundException;\r
25 import java.io.FileReader;\r
26 import java.io.IOException;\r
27 import java.net.URI;\r
28 import java.net.URISyntaxException;\r
29 import java.net.URL;\r
30 \r
31 import org.junit.Rule;\r
32 import org.junit.Test;\r
33 import org.junit.rules.ExpectedException;\r
34 import org.onap.holmes.common.dcae.entity.DcaeConfigurations;\r
35 import org.onap.holmes.common.dcae.entity.SecurityInfo;\r
36 import org.onap.holmes.common.exception.CorrelationException;\r
37 \r
38 public class DcaeConfigurationParserTest {\r
39 \r
40     @Rule\r
41     public ExpectedException thrown = ExpectedException.none();\r
42 \r
43     @Test\r
44     public void parse() throws Exception {\r
45         DcaeConfigurations obj = DcaeConfigurationParser.parse(readConfigurationsFromFile("dcae.config.json"));\r
46 \r
47         assertThat(obj.getDefaultRules().size(), equalTo(1));\r
48         assertThat(obj.get("collector.keystore.alias"), equalTo("dynamically generated"));\r
49         assertThat(((SecurityInfo) obj.getPubSecInfo("sec_measurement")).getAafPassword(), equalTo("aaf_password"));\r
50         assertThat(((SecurityInfo) obj.getPubSecInfo("sec_measurement")).getDmaapInfo().getLocation(), equalTo("mtl5"));\r
51     }\r
52 \r
53     @Test\r
54     public void parse_with_empty_contents_excption() throws CorrelationException {\r
55         thrown.expect(CorrelationException.class);\r
56         thrown.expectMessage("Can not resolve configurations from DCAE. The configuration string is empty.");\r
57         DcaeConfigurationParser.parse("");\r
58     }\r
59 \r
60     @Test\r
61     public void parse_with_illegal_dcae_response_excption() throws CorrelationException {\r
62         thrown.expect(CorrelationException.class);\r
63         DcaeConfigurationParser.parse("This is an ordinary string.");\r
64     }\r
65 \r
66     private String readConfigurationsFromFile(String fileName) throws URISyntaxException, FileNotFoundException {\r
67         URL url = DcaeConfigurationParserTest.class.getClassLoader().getResource(fileName);\r
68         File configFile = new File(new URI(url.toString()).getPath());\r
69         BufferedReader br = new BufferedReader(new FileReader(configFile));\r
70 \r
71         final StringBuilder sb = new StringBuilder();\r
72         br.lines().forEach(line -> {\r
73             sb.append(line);\r
74         });\r
75         try {\r
76             br.close();\r
77         } catch (IOException e) {\r
78             // Do nothing\r
79         }\r
80         return sb.toString();\r
81     }\r
82 \r
83 }