Merge "Update model-loader dependencies"
[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 testDecryptPassword() {
64         String password = "youshallnotpass";
65         ModelLoaderConfig config =
66                 createObfuscatedTestConfig(ModelLoaderConfig.PROP_ML_DISTRIBUTION_PASSWORD, password);
67         assertEquals(password, config.getPassword());
68         
69         config = createUnobfuscatedTestConfig(ModelLoaderConfig.PROP_ML_DISTRIBUTION_PASSWORD, password);
70         assertEquals(password, config.getPassword());
71     }
72
73     @Test
74     public void testDecryptKeystorePassword() {
75         String password = "youshallnotpass";
76         ModelLoaderConfig config =
77                 createObfuscatedTestConfig(ModelLoaderConfig.PROP_ML_DISTRIBUTION_KEYSTORE_PASSWORD, password);
78         assertEquals(password, config.getKeyStorePassword());
79         
80         config = createUnobfuscatedTestConfig(ModelLoaderConfig.PROP_ML_DISTRIBUTION_KEYSTORE_PASSWORD, password);
81         assertEquals(password, config.getKeyStorePassword());
82     }
83
84     @Test
85     public void testDecryptAaiAuthenticationPassword() {
86         String password = "myvoiceismypassword";
87         ModelLoaderConfig config =
88                 createObfuscatedTestConfig(ModelLoaderConfig.PROP_AAI_AUTHENTICATION_PASSWORD, password);
89         assertEquals(password, config.getAaiAuthenticationPassword());
90         
91         config = createUnobfuscatedTestConfig(ModelLoaderConfig.PROP_AAI_AUTHENTICATION_PASSWORD, password);
92         assertEquals(password, config.getAaiAuthenticationPassword());
93     }
94
95     @Test
96     public void testDecryptAaiKeystorePassword() {
97         String password = "myvoiceismypassword";
98         ModelLoaderConfig config = createObfuscatedTestConfig(ModelLoaderConfig.PROP_AAI_KEYSTORE_PASSWORD, password);
99         assertEquals(password, config.getAaiKeyStorePassword());
100         
101         config = createUnobfuscatedTestConfig(ModelLoaderConfig.PROP_AAI_KEYSTORE_PASSWORD, password);
102         assertEquals(password, config.getAaiKeyStorePassword());
103     }
104
105     @Test
106     public void testAaiBaseUrl() {
107         String url = "http://localhost:1234/";
108         Properties props = new Properties();
109         props.put(ModelLoaderConfig.PROP_AAI_BASE_URL, url);
110         ModelLoaderConfig config = new ModelLoaderConfig(props, null);
111         assertEquals(url, config.getAaiBaseUrl());
112     }
113
114     @Test
115     public void testDecryptBabelKeystorePassword() {
116         String password = "babelpassword";
117         ModelLoaderConfig config = createObfuscatedTestConfig(ModelLoaderConfig.PROP_BABEL_KEYSTORE_PASSWORD, password);
118         assertEquals(password, config.getBabelKeyStorePassword());
119         
120         config = createUnobfuscatedTestConfig(ModelLoaderConfig.PROP_BABEL_KEYSTORE_PASSWORD, password);
121         assertEquals(password, config.getBabelKeyStorePassword());
122     }
123
124     @Test
125     public void testBabelKeystorePath() {
126         String root = "path_to_keystore";
127         String path = "relative_keystore_path";
128         Properties props = new Properties();
129         props.put(ModelLoaderConfig.PROP_BABEL_KEYSTORE_FILE, path);
130         ModelLoaderConfig config = new ModelLoaderConfig(props, root);
131         assertEquals(root + File.separator + path, config.getBabelKeyStorePath());
132     }
133
134     @Test
135     public void testBabelBaseUrl() {
136         String url = "http://localhost/";
137         Properties props = new Properties();
138         props.put(ModelLoaderConfig.PROP_BABEL_BASE_URL, url);
139         ModelLoaderConfig config = new ModelLoaderConfig(props, null);
140         assertEquals(url, config.getBabelBaseUrl());
141     }
142
143     @Test
144     public void testBabelGenerateArtifactsUrl() {
145         String url = "/path/to/the/resource";
146         Properties props = new Properties();
147         props.put(ModelLoaderConfig.PROP_BABEL_GENERATE_RESOURCE_URL, url);
148         ModelLoaderConfig config = new ModelLoaderConfig(props, null);
149         assertEquals(url, config.getBabelGenerateArtifactsUrl());
150     }
151
152     @Test
153     public void testMissingAuthenticationProperties() throws IOException {
154         Properties props = new Properties();
155         props.load(new FileInputStream("src/test/resources/model-loader-empty-auth-password.properties"));
156
157         ModelLoaderConfig config = new ModelLoaderConfig(props, null);
158         AaiRestClient aaiClient = new AaiRestClient(config);
159
160         assertFalse("Empty AAI Password should result in no basic authentication", aaiClient.useBasicAuth());
161
162         props.load(new FileInputStream("src/test/resources/model-loader-no-auth-password.properties"));
163         config = new ModelLoaderConfig(props, null);
164         aaiClient = new AaiRestClient(config);
165
166         assertFalse("No AAI Password should result in no basic authentication", aaiClient.useBasicAuth());
167     }
168
169     @Test
170     public void testGetUrls() {
171         Properties props = new Properties();
172         props.put(ModelLoaderConfig.PROP_AAI_MODEL_RESOURCE_URL, "/aai/v*/service-design-and-creation/models/model/");
173         props.put(ModelLoaderConfig.PROP_AAI_NAMED_QUERY_RESOURCE_URL,
174                 "/aai/v*/service-design-and-creation/named-queries/named-query/");
175         ModelLoaderConfig config = new ModelLoaderConfig(props, null);
176
177         assertEquals("/aai/v9/service-design-and-creation/models/model/", config.getAaiModelUrl("v9"));
178         assertEquals("/aai/v10/service-design-and-creation/named-queries/named-query/",
179                 config.getAaiNamedQueryUrl("v10"));
180     }
181
182
183     /**
184      * Create a Model Loader Configuration object from the supplied Property.
185      * 
186      * @param propertyName property key
187      * @param propertyValue value of the property
188      * @return a new ModelLoaderConfig object containing a single obfuscated property value
189      */
190     private ModelLoaderConfig createObfuscatedTestConfig(String propertyName, String propertyValue) {
191         Properties props = new Properties();
192         props.put(propertyName, Password.obfuscate(propertyValue));
193         return new ModelLoaderConfig(props, null);
194     }
195     
196     private ModelLoaderConfig createUnobfuscatedTestConfig(String propertyName, String propertyValue) {
197         Properties props = new Properties();
198         props.put(propertyName, propertyValue);
199         return new ModelLoaderConfig(props, null);
200     }
201 }