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