48aecf4adc4fce5434b4b94bf177755fe4277ee2
[sdc.git] / common / onap-common-configuration-management / onap-configuration-management-core / src / test / java / org / onap / config / test / CliFallbackAndLookupTest.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.config.test;
18
19 import java.io.IOException;
20 import java.lang.management.ManagementFactory;
21 import java.util.HashMap;
22 import java.util.Map;
23 import javax.management.JMX;
24 import javax.management.MBeanServerConnection;
25 import javax.management.ObjectName;
26 import org.junit.After;
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.onap.config.Constants;
31 import org.onap.config.api.ConfigurationManager;
32 import org.onap.config.util.ConfigTestConstant;
33 import org.onap.config.util.TestUtil;
34
35
36 /**
37  * Created by sheetalm on 10/18/2016.
38  * Scenario 21, Scenario 23
39  * 21 - Verify the CLI fetches only the current value unless the fallback option is specified
40  * 23 - Fetch value using CLI for a key with underlying resource
41  */
42 public class CliFallbackAndLookupTest {
43
44     private static final String NAMESPACE = "CLIFallback";
45     private static final String TENANT = "OPENECOMP";
46
47     @Before
48     public void setUp() throws IOException {
49         String data = "{name:\"SCM\"}";
50         TestUtil.writeFile(data);
51     }
52
53     @Test
54     public void testCliFallbackAndLookup() throws Exception {
55
56         //Verify without fallback
57         Map<String, Object> input = new HashMap<>();
58         input.put("ImplClass", "org.onap.config.type.ConfigurationQuery");
59         input.put("tenant", TENANT);
60         input.put("namespace", NAMESPACE);
61         input.put("key", ConfigTestConstant.ARTIFACT_MAXSIZE);
62
63         MBeanServerConnection mbsc = ManagementFactory.getPlatformMBeanServer();
64         ObjectName mbeanName = new ObjectName(Constants.MBEAN_NAME);
65         ConfigurationManager conf = JMX.newMBeanProxy(mbsc, mbeanName, ConfigurationManager.class, true);
66         String maxSizeWithNoFallback = conf.getConfigurationValue(input);
67         Assert.assertEquals("", maxSizeWithNoFallback);
68
69         //Verify underlying resource without lookup switch
70         input.put("key", ConfigTestConstant.ARTIFACT_JSON_SCHEMA);
71         String jsonSchema = conf.getConfigurationValue(input);
72         System.out.println("jsonSchema==" + jsonSchema);
73         Assert.assertEquals("@" + System.getProperty("user.home") + "/TestResources/GeneratorsList.json", jsonSchema);
74
75         //Verify underlying resource with lookup switch
76         input.put("externalLookup", true);
77         jsonSchema = conf.getConfigurationValue(input);
78         System.out.println("jsonSchema==" + jsonSchema);
79         Assert.assertEquals("{name:\"SCM\"}", jsonSchema);
80
81         //Verify with fallback
82         Map<String, Object> fallbackInput = new HashMap<>();
83         fallbackInput.put("ImplClass", "org.onap.config.type.ConfigurationQuery");
84         fallbackInput.put("fallback", true);
85         fallbackInput.put("tenant", TENANT);
86         fallbackInput.put("namespace", NAMESPACE);
87         fallbackInput.put("key", ConfigTestConstant.ARTIFACT_MAXSIZE);
88
89         String maxSizeWithFallback = conf.getConfigurationValue(fallbackInput);
90         Assert.assertEquals("1024", maxSizeWithFallback);
91     }
92
93     @After
94     public void tearDown() throws Exception {
95         TestUtil.cleanUp();
96     }
97 }