Added Some Tools to GsonUtil
[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         assertThat(obj.getDefaultRules().size(), equalTo(1));\r
47         assertThat(obj.get("collector.keystore.alias"), equalTo("dynamically generated"));\r
48         assertThat(((SecurityInfo) obj.getPubSecInfo("sec_measurement")).getAafPassword(), equalTo("aaf_password"));\r
49         assertThat(((SecurityInfo) obj.getPubSecInfo("sec_measurement")).getDmaapInfo().getLocation(), equalTo("mtl5"));\r
50     }\r
51 \r
52     @Test\r
53     public void parse_with_empty_contents_excption() throws CorrelationException {\r
54         thrown.expect(CorrelationException.class);\r
55         thrown.expectMessage("Can not resolve configurations from DCAE. The configuration string is empty.");\r
56         DcaeConfigurationParser.parse("");\r
57     }\r
58 \r
59     @Test\r
60     public void parse_with_illegal_dcae_response_excption() throws CorrelationException {\r
61         thrown.expect(CorrelationException.class);\r
62         DcaeConfigurationParser.parse("This is an ordinary string.");\r
63     }\r
64 \r
65     private String readConfigurationsFromFile(String fileName) throws URISyntaxException, FileNotFoundException {\r
66         URL url = DcaeConfigurationParserTest.class.getClassLoader().getResource(fileName);\r
67         File configFile = new File(new URI(url.toString()).getPath());\r
68         BufferedReader br = new BufferedReader(new FileReader(configFile));\r
69 \r
70         final StringBuilder sb = new StringBuilder();\r
71         br.lines().forEach(line -> {\r
72             sb.append(line);\r
73         });\r
74         try {\r
75             br.close();\r
76         } catch (IOException e) {\r
77             // Do nothing\r
78         }\r
79         return sb.toString();\r
80     }\r
81 \r
82 }