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