[PORTAL-7] Rebase
[portal.git] / ecomp-portal-BE-common / src / test / java / org / openecomp / portalapp / portal / controller / SharedContextTestProperties.java
1 /*-\r
2  * ================================================================================\r
3  * ECOMP Portal\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property\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  * ================================================================================\r
19  */\r
20 package org.openecomp.portalapp.portal.controller;\r
21 \r
22 import java.io.IOException;\r
23 import java.io.InputStream;\r
24 import java.util.Properties;\r
25 \r
26 /**\r
27  * Trivial extension of Properties that populates itself from a known source.\r
28  */\r
29 public class SharedContextTestProperties extends Properties {\r
30 \r
31         private static final long serialVersionUID = -4064100267979036550L;\r
32 \r
33         // property names\r
34         public static final String HOSTNAME = "hostname";\r
35         public static final String PORT = "port";\r
36         public static final String SECURE = "secure";\r
37         public static final String APPNAME = "appname";\r
38         public static final String RESTPATH = "restpath";\r
39         public static final String UEBKEY = "uebkey";\r
40         public static final String USERNAME = "username";\r
41         public static final String PASSWORD = "password";\r
42 \r
43         /**\r
44          * Expected on the classpath\r
45          */\r
46         private static final String propertiesFileName = "shared-context-test.properties";\r
47 \r
48         /**\r
49          * Constructor populates itself from properties file found in same package.\r
50          * \r
51          * @throws Exception\r
52          */\r
53         public SharedContextTestProperties() throws IOException {\r
54                 InputStream inStream = getClass().getResourceAsStream(propertiesFileName);\r
55                 if (inStream == null)\r
56                         throw new IOException("Failed to find file on classpath: " + propertiesFileName);\r
57                 super.load(inStream);\r
58                 inStream.close();\r
59         }\r
60 \r
61         public int getProperty(final String name, final int defVal) throws NumberFormatException {\r
62                 String prop = getProperty(name);\r
63                 if (prop == null)\r
64                         return defVal;\r
65                 return Integer.parseInt(prop);\r
66         }\r
67         \r
68         public boolean getProperty(final String name, final boolean defVal) {\r
69                 String prop = getProperty(name);\r
70                 if (prop == null)\r
71                         return false;\r
72                 return Boolean.parseBoolean(prop);\r
73         }\r
74         \r
75         // Test this class\r
76         public static void main(String[] args) throws Exception {\r
77                 SharedContextTestProperties p = new SharedContextTestProperties();\r
78                 System.out.println("Property " + SharedContextTestProperties.HOSTNAME + " = "\r
79                                 + p.getProperty(SharedContextTestProperties.HOSTNAME));\r
80         }\r
81 }\r