Add DMaaP plugin support
[dcaegen2/services/pm-mapper.git] / src / test / java / org / onap / dcaegen2 / services / pmmapper / utils / EnvironmentConfigTest.java
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  *  Copyright (C) 2019 Nordix Foundation.\r
4  * ================================================================================\r
5  * Licensed under the Apache License, Version 2.0 (the "License");\r
6  * you may not use this file except in compliance with the License.\r
7  * You may obtain a copy of the License at\r
8  *\r
9  *      http://www.apache.org/licenses/LICENSE-2.0\r
10  *\r
11  * Unless required by applicable law or agreed to in writing, software\r
12  * distributed under the License is distributed on an "AS IS" BASIS,\r
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14  * See the License for the specific language governing permissions and\r
15  * limitations under the License.\r
16  *\r
17  * SPDX-License-Identifier: Apache-2.0\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 \r
21 package org.onap.dcaegen2.services.pmmapper.utils;\r
22 import static org.junit.jupiter.api.Assertions.assertThrows;\r
23 import static org.junit.Assert.assertEquals;\r
24 import org.junit.Before;\r
25 import org.junit.Test;\r
26 import org.junit.runner.RunWith;\r
27 import org.onap.dcaegen2.services.pmmapper.exceptions.EnvironmentConfigException;\r
28 import org.powermock.api.mockito.PowerMockito;\r
29 import org.powermock.core.classloader.annotations.PrepareForTest;\r
30 import org.powermock.modules.junit4.PowerMockRunner;\r
31 \r
32 @RunWith(PowerMockRunner.class)\r
33 @PrepareForTest(EnvironmentConfig.class)\r
34 public class EnvironmentConfigTest {\r
35     private EnvironmentConfig objUnderTest;\r
36 \r
37     @Before\r
38     public void before() throws Exception {\r
39         PowerMockito.mockStatic(System.class);\r
40         objUnderTest = new EnvironmentConfig();\r
41     }\r
42 \r
43     @Test\r
44     public void environmentConfig_is_present_success() throws EnvironmentConfigException {\r
45         String CBS_HOST = "cbs_host";\r
46         PowerMockito.when(System.getenv(EnvironmentConfig.ENV_CBS_HOST_KEY)).thenReturn(CBS_HOST);\r
47         assertEquals(CBS_HOST, objUnderTest.getCBSHostName());\r
48     }\r
49 \r
50     @Test\r
51     public void environmentConfig_host_not_present() throws EnvironmentConfigException {\r
52         PowerMockito.when(System.getenv(EnvironmentConfig.ENV_CBS_HOST_KEY)).thenReturn(null);\r
53         assertThrows(EnvironmentConfigException.class, objUnderTest::getCBSHostName);\r
54     }\r
55 \r
56     @Test\r
57     public void environmentConfig_hostname_present() throws EnvironmentConfigException {\r
58         PowerMockito.when(System.getenv(EnvironmentConfig.ENV_SERVICE_NAME_KEY)).thenCallRealMethod();\r
59         assertThrows(EnvironmentConfigException.class, objUnderTest::getCBSHostName);\r
60     }\r
61 \r
62     @Test\r
63     public void environmentConfig_default_port_is_used() throws EnvironmentConfigException {\r
64         PowerMockito.when(System.getenv(EnvironmentConfig.ENV_CBS_PORT_KEY)).thenReturn(null);\r
65         assertEquals(Integer.valueOf(EnvironmentConfig.DEFAULT_CBS_PORT), objUnderTest.getCBSPort());\r
66     }\r
67 \r
68     @Test\r
69     public void environmentConfig_port_invalid() throws EnvironmentConfigException {\r
70         PowerMockito.when(System.getenv(EnvironmentConfig.ENV_CBS_PORT_KEY)).thenReturn("Invalid_port number");\r
71         assertThrows(EnvironmentConfigException.class, objUnderTest::getCBSHostName);\r
72     }\r
73 \r
74     @Test\r
75     public void environmentConfig_service_name_missing() {\r
76         PowerMockito.when(System.getenv(EnvironmentConfig.ENV_SERVICE_NAME_KEY)).thenReturn(null);\r
77         assertThrows(EnvironmentConfigException.class, objUnderTest::getServiceName);\r
78     }\r
79     @Test\r
80     public void environmentConfig_service_name_success() throws EnvironmentConfigException {\r
81         String serviceName = "we the best service";\r
82         PowerMockito.when(System.getenv(EnvironmentConfig.ENV_SERVICE_NAME_KEY)).thenReturn(serviceName);\r
83         assertEquals(serviceName, objUnderTest.getServiceName());\r
84     }\r
85 }\r