fed3fd22ba35abc8c0755450372beed47cf055d7
[dcaegen2/analytics/tca.git] / dcae-analytics-model / src / test / java / org / openecomp / dcae / apod / analytics / model / util / AnalyticsModelIOUtilsTest.java
1 /*\r
2  * ===============================LICENSE_START======================================\r
3  *  dcae-analytics\r
4  * ================================================================================\r
5  *    Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  *  Licensed under the Apache License, Version 2.0 (the "License");\r
8  *  you may not use this file except in compliance with the License.\r
9  *   You may obtain a copy of the License at\r
10  *\r
11  *          http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  *  Unless required by applicable law or agreed to in writing, software\r
14  *  distributed under the License is distributed on an "AS IS" BASIS,\r
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  *  See the License for the specific language governing permissions and\r
17  *  limitations under the License.\r
18  *  ============================LICENSE_END===========================================\r
19  */\r
20 \r
21 package org.openecomp.dcae.apod.analytics.model.util;\r
22 \r
23 \r
24 import org.junit.Test;\r
25 import org.mockito.Mockito;\r
26 import org.openecomp.dcae.apod.analytics.model.BaseAnalyticsModelUnitTest;\r
27 \r
28 import java.io.IOException;\r
29 import java.io.InputStream;\r
30 import java.util.Properties;\r
31 \r
32 import static org.hamcrest.CoreMatchers.is;\r
33 import static org.junit.Assert.assertEquals;\r
34 import static org.junit.Assert.assertThat;\r
35 import static org.mockito.ArgumentMatchers.any;\r
36 import static org.mockito.Mockito.doThrow;\r
37 \r
38 /**\r
39  * @author Rajiv Singla . Creation Date: 10/17/2016.\r
40  */\r
41 public class AnalyticsModelIOUtilsTest extends BaseAnalyticsModelUnitTest {\r
42 \r
43     private static final String TEST_CONFIG_FILE_LOCATION = "data/json/config/testAppConfig.json";\r
44     private static final String INVALID_TEST_CONFIG_FILE_LOCATION = "data/json/config/invalidJsonConfig.json";\r
45     private static final String TEST_PROPERTIES_FILE_LOCATION = "data/properties/testApp.properties";\r
46 \r
47     @Test\r
48     public void testConvertToJsonObjectWhenFileLocationIsValid() throws Exception {\r
49         ConfigHolder configHolder =\r
50                 AnalyticsModelIOUtils.convertToJsonObject(TEST_CONFIG_FILE_LOCATION, ConfigHolder.class);\r
51         String appName = configHolder.getConfig().getAppName();\r
52         assertEquals("App Name must match with json settings file value", "TestAppName", appName);\r
53         String appDescription = configHolder.getConfig().getAppDescription();\r
54         assertEquals("App Description much with json settings file value", "Test App Description", appDescription);\r
55     }\r
56 \r
57     @Test(expected = IOException.class)\r
58     public void testConvertToJsonObjectWhenFileLocationIsInvValid() throws Exception {\r
59         AnalyticsModelIOUtils.convertToJsonObject("InvalidFileLocation", ConfigHolder.class);\r
60     }\r
61 \r
62     @Test(expected = IOException.class)\r
63     public void testConvertToJsonObjectWhenJsonFileHasInvalidJson() throws Exception {\r
64         AnalyticsModelIOUtils.convertToJsonObject(INVALID_TEST_CONFIG_FILE_LOCATION, ConfigHolder.class);\r
65     }\r
66 \r
67     @Test\r
68     public void testValidPropertiesFileLoading() throws Exception {\r
69         final Properties properties =\r
70                 AnalyticsModelIOUtils.loadPropertiesFile(TEST_PROPERTIES_FILE_LOCATION, new Properties());\r
71         assertThat("Properties File must contain 2 properties", properties.size(), is(2));\r
72     }\r
73 \r
74     @Test(expected = RuntimeException.class)\r
75     public void testNonExistingPropertiesFileLoading() throws Exception {\r
76         AnalyticsModelIOUtils.loadPropertiesFile("InvalidPropertiesFileLocation", new Properties());\r
77     }\r
78 \r
79     @Test(expected = RuntimeException.class)\r
80     public void testLoadPropertiesFileWhenIOException() throws Exception {\r
81         final Properties mockProperties = Mockito.mock(Properties.class);\r
82         doThrow(new IOException()).when(mockProperties).load(any(InputStream.class));\r
83         AnalyticsModelIOUtils.loadPropertiesFile(TEST_PROPERTIES_FILE_LOCATION, mockProperties);\r
84     }\r
85 }\r
86 \r