Revisions made to the Model Loader to use Babel
[aai/model-loader.git] / src / test / java / org / onap / aai / modelloader / config / ModelLoaderConfigTest.java
1 /**\r
2  * ============LICENSE_START==========================================\r
3  * org.onap.aai\r
4  * ===================================================================\r
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.\r
6  * Copyright © 2017-2018 Amdocs\r
7  * ===================================================================\r
8  * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * you may not use this file except in compliance with the License.\r
10  * You may obtain a copy of the License at\r
11  *\r
12  *        http://www.apache.org/licenses/LICENSE-2.0\r
13  *\r
14  * Unless required by applicable law or agreed to in writing, software\r
15  * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * See the License for the specific language governing permissions and\r
18  * limitations under the License.\r
19  * ============LICENSE_END============================================\r
20  */\r
21 package org.onap.aai.modelloader.config;\r
22 \r
23 import static org.junit.Assert.assertEquals;\r
24 import static org.junit.Assert.assertFalse;\r
25 \r
26 import java.io.File;\r
27 import java.io.FileInputStream;\r
28 import java.io.IOException;\r
29 import java.util.List;\r
30 import java.util.Properties;\r
31 \r
32 import org.eclipse.jetty.util.security.Password;\r
33 import org.junit.Test;\r
34 import org.onap.aai.modelloader.restclient.AaiRestClient;\r
35 import org.openecomp.sdc.utils.ArtifactTypeEnum;\r
36 \r
37 /**\r
38  * Tests for ModelLoaderConfig class\r
39  *\r
40  */\r
41 public class ModelLoaderConfigTest {\r
42 \r
43     @Test\r
44     public void testYangModelArtifactType() {\r
45         Properties props = new Properties();\r
46         props.setProperty("ml.distribution.ARTIFACT_TYPES", "MODEL_INVENTORY_PROFILE,MODEL_QUERY_SPEC,VNF_CATALOG");\r
47         ModelLoaderConfig config = new ModelLoaderConfig(props, null);\r
48 \r
49         List<String> types = config.getRelevantArtifactTypes();\r
50 \r
51         System.out.println("ArtifactType: " + types.get(0));\r
52         assertEquals(0, types.get(0).compareToIgnoreCase(ArtifactTypeEnum.MODEL_INVENTORY_PROFILE.toString()));\r
53 \r
54         System.out.println("ArtifactType: " + types.get(1));\r
55         assertEquals(0, types.get(1).compareToIgnoreCase(ArtifactTypeEnum.MODEL_QUERY_SPEC.toString()));\r
56 \r
57         System.out.println("ArtifactType: " + types.get(2));\r
58         assertEquals(0, types.get(2).compareToIgnoreCase(ArtifactTypeEnum.VNF_CATALOG.toString()));\r
59 \r
60         assertEquals(3, types.size());\r
61     }\r
62     \r
63     @Test\r
64     public void testMsgBusAddrs() {\r
65         Properties props = new Properties();\r
66         props.setProperty("ml.distribution.MSG_BUS_ADDRESSES", "host1.onap.com:3904,host2.onap.com:3904");\r
67         ModelLoaderConfig config = new ModelLoaderConfig(props, null);\r
68 \r
69         List<String> addrs = config.getMsgBusAddress();\r
70 \r
71         assertEquals(2, addrs.size());\r
72         assertEquals(0, addrs.get(0).compareToIgnoreCase("host1.onap.com:3904"));\r
73         assertEquals(0, addrs.get(1).compareToIgnoreCase("host2.onap.com:3904"));\r
74     }\r
75 \r
76     @Test\r
77     public void testDecryptPassword() {\r
78         String password = "youshallnotpass";\r
79         ModelLoaderConfig config =\r
80                 createObfuscatedTestConfig(ModelLoaderConfig.PROP_ML_DISTRIBUTION_PASSWORD, password);\r
81         assertEquals(password, config.getPassword());\r
82     }\r
83 \r
84     @Test\r
85     public void testDecryptKeystorePassword() {\r
86         String password = "youshallnotpass";\r
87         ModelLoaderConfig config =\r
88                 createObfuscatedTestConfig(ModelLoaderConfig.PROP_ML_DISTRIBUTION_KEYSTORE_PASSWORD, password);\r
89         assertEquals(password, config.getKeyStorePassword());\r
90     }\r
91 \r
92     @Test\r
93     public void testDecryptAAIAuthenticationPassword() {\r
94         String password = "myvoiceismypassword";\r
95         ModelLoaderConfig config =\r
96                 createObfuscatedTestConfig(ModelLoaderConfig.PROP_AAI_AUTHENTICATION_PASSWORD, password);\r
97         assertEquals(password, config.getAaiAuthenticationPassword());\r
98     }\r
99 \r
100     @Test\r
101     public void testDecryptAAIKeystorePassword() {\r
102         String password = "myvoiceismypassword";\r
103         ModelLoaderConfig config = createObfuscatedTestConfig(ModelLoaderConfig.PROP_AAI_KEYSTORE_PASSWORD, password);\r
104         assertEquals(password, config.getAaiKeyStorePassword());\r
105     }\r
106 \r
107     @Test\r
108     public void testAaiBaseUrl() {\r
109         String url = "http://localhost:1234/";\r
110         Properties props = new Properties();\r
111         props.put(ModelLoaderConfig.PROP_AAI_BASE_URL, url);\r
112         ModelLoaderConfig config = new ModelLoaderConfig(props, null);\r
113         assertEquals(url, config.getAaiBaseUrl());\r
114     }\r
115 \r
116     @Test\r
117     public void testDecryptBabelKeystorePassword() {\r
118         String password = "babelpassword";\r
119         ModelLoaderConfig config = createObfuscatedTestConfig(ModelLoaderConfig.PROP_BABEL_KEYSTORE_PASSWORD, password);\r
120         assertEquals(password, config.getBabelKeyStorePassword());\r
121     }\r
122 \r
123     @Test\r
124     public void testBabelKeystorePath() {\r
125         String root = "path_to_keystore";\r
126         String path = "relative_keystore_path";\r
127         Properties props = new Properties();\r
128         props.put(ModelLoaderConfig.PROP_BABEL_KEYSTORE_FILE, path);\r
129         ModelLoaderConfig config = new ModelLoaderConfig(props, root);\r
130         assertEquals(root + File.separator + path, config.getBabelKeyStorePath());\r
131     }\r
132 \r
133     @Test\r
134     public void testBabelBaseUrl() {\r
135         String url = "http://localhost/";\r
136         Properties props = new Properties();\r
137         props.put(ModelLoaderConfig.PROP_BABEL_BASE_URL, url);\r
138         ModelLoaderConfig config = new ModelLoaderConfig(props, null);\r
139         assertEquals(url, config.getBabelBaseUrl());\r
140     }\r
141 \r
142     @Test\r
143     public void testBabelGenerateArtifactsUrl() {\r
144         String url = "/path/to/the/resource";\r
145         Properties props = new Properties();\r
146         props.put(ModelLoaderConfig.PROP_BABEL_GENERATE_RESOURCE_URL, url);\r
147         ModelLoaderConfig config = new ModelLoaderConfig(props, null);\r
148         assertEquals(url, config.getBabelGenerateArtifactsUrl());\r
149     }\r
150 \r
151     @Test\r
152     public void testNoAAIAuth() throws IOException {\r
153 \r
154         Properties props = new Properties();\r
155         props.load(new FileInputStream("src/test/resources/model-loader-empty-auth-password.properties"));\r
156 \r
157         ModelLoaderConfig config = new ModelLoaderConfig(props, null);\r
158         AaiRestClient aaiClient = new AaiRestClient(config);\r
159 \r
160         assertFalse("Empty AAI Password should result in no basic authentication", aaiClient.useBasicAuth());\r
161 \r
162         props.load(new FileInputStream("src/test/resources/model-loader-no-auth-password.properties"));\r
163         config = new ModelLoaderConfig(props, null);\r
164         aaiClient = new AaiRestClient(config);\r
165 \r
166         assertFalse("No AAI Password should result in no basic authentication", aaiClient.useBasicAuth());\r
167     }\r
168 \r
169     @Test\r
170     public void testGetUrls() {\r
171         Properties props = new Properties();\r
172         props.put(ModelLoaderConfig.PROP_AAI_MODEL_RESOURCE_URL, "/aai/v*/service-design-and-creation/models/model/");\r
173         props.put(ModelLoaderConfig.PROP_AAI_NAMED_QUERY_RESOURCE_URL,\r
174                 "/aai/v*/service-design-and-creation/named-queries/named-query/");\r
175         ModelLoaderConfig config = new ModelLoaderConfig(props, null);\r
176 \r
177         assertEquals("/aai/v9/service-design-and-creation/models/model/", config.getAaiModelUrl("v9"));\r
178         assertEquals("/aai/v10/service-design-and-creation/named-queries/named-query/",\r
179                 config.getAaiNamedQueryUrl("v10"));\r
180     }\r
181 \r
182 \r
183     /**\r
184      * @param propertyName\r
185      * @param propertyValue\r
186      * @return a new ModelLoaderConfig object containing a single obfuscated property value\r
187      */\r
188     private ModelLoaderConfig createObfuscatedTestConfig(String propertyName, String propertyValue) {\r
189         Properties props = new Properties();\r
190         props.put(propertyName, Password.obfuscate(propertyValue));\r
191         return new ModelLoaderConfig(props, null);\r
192     }\r
193 }\r