Update install-feature for pre-install
[appc.git] / appc-outbound / appc-network-inventory-client / provider / src / test / java / org / onap / appc / instar / node / TestInstarClientNode.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * ================================================================================
9  * Modifications Copyrigh (C) 2018 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.instar.node;
27
28 import java.util.HashMap;
29 import java.util.Map;
30 import org.jline.utils.Log;
31 import org.junit.Assert;
32 import org.junit.Rule;
33 import org.junit.Test;
34 import org.junit.rules.ExpectedException;
35 import org.powermock.api.mockito.PowerMockito;
36 import org.mockito.Mockito;
37 import org.onap.appc.aai.utils.AaiClientConstant;
38 import org.onap.appc.instar.interfaceImpl.InstarRestClientImpl;
39 import org.onap.appc.instar.interfaceImpl.InterfaceIpAddressImpl;
40 import org.onap.appc.instar.interfaces.RuleHandlerInterface;
41 import org.onap.appc.instar.utils.InstarClientConstant;
42 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
43 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
44
45
46 public class TestInstarClientNode {
47
48     //ONAP Migration
49
50     @Rule
51     public ExpectedException expectedEx = ExpectedException.none();
52
53     @Test
54     public void testInstarClientNode() throws Exception {
55         SvcLogicContext ctx = new SvcLogicContext();
56         String key_content = "{\"name\":\"LOCAL_ACCESS_IP_ADDR\",\"description\":\"this is the node0 tacplus server IP address\",\"type\":"
57                 + "\"ipv4_address\",\"required\":true,\"source\":\"INSTAR\",\"rule-type\":\"interface-ip-address\",\"default\":null,\"request-keys\":"
58                 + "null,\"response-keys\":[{\"unique-key-name\":\"addressfqdn\",\"unique-key-value\":\"00000000000000\",\"field-key-name\":\"ipaddress-v4\"}]}";
59         Map<String, String> inParams = new HashMap<String, String>();
60         inParams.put(InstarClientConstant.INPUT_PARAM_RESPONSE_PRIFIX, "");
61         inParams.put(InstarClientConstant.INSTAR_KEYS, "LOCAL_ACCESS_IP_ADDR");
62         inParams.put("operationName", InstarClientConstant.OPERATION_GET_IPADDRESS_BY_VNF_NAME);
63         ctx.setAttribute("INSTAR.LOCAL_ACCESS_IP_ADDR", key_content);
64         ctx.setAttribute(InstarClientConstant.INPUT_PARAM_RESPONSE_PRIFIX, "");
65         ctx.setAttribute(InstarClientConstant.VNF_NAME, "basx0003v");
66         InstarClientNode icn  = PowerMockito.spy(new InstarClientNode());
67         RuleHandlerInterface mockRuleHandler = Mockito.mock(InterfaceIpAddressImpl.class);
68         Mockito.doReturn(mockRuleHandler).when(icn).createHandler(Mockito.any(), Mockito.any());
69         Mockito.doNothing().when(mockRuleHandler).processRule();
70         icn.getInstarInfo(inParams, ctx);
71         Log.info(ctx.getStatus());
72         Assert.assertEquals(InstarClientConstant.OUTPUT_STATUS_SUCCESS, ctx.getAttribute("" + InstarClientConstant.OUTPUT_PARAM_STATUS));
73     }
74
75     @Test
76     public void testInstarClientNodeFailure() throws Exception {
77         SvcLogicContext ctx = new SvcLogicContext();
78         String key_content = "{\"name\":\"LOCAL_ACCESS_IP_ADDR\",\"description\":\"this is the node0 tacplus server IP address\",\"type\":"
79                 + "\"ipv4_address\",\"required\":true,\"source\":\"INSTAR\",\"rule-type\":\"NON_INTERFACE_IP_ADDRESS\",\"default\":null,\"request-keys\":"
80                 + "null,\"response-keys\":[{\"unique-key-name\":\"addressfqdn\",\"unique-key-value\":\"00000000000000\",\"field-key-name\":\"ipaddress-v4\"}]}";
81         Log.info(key_content);
82         Map<String, String> inParams = new HashMap<String, String>();
83         inParams.put(InstarClientConstant.INPUT_PARAM_RESPONSE_PRIFIX, "TEST");
84         inParams.put(InstarClientConstant.INSTAR_KEYS, "LOCAL_ACCESS_IP_ADDR");
85         inParams.put("operationName", InstarClientConstant.OPERATION_GET_IPADDRESS_BY_VNF_NAME);
86         ctx.setAttribute("INSTAR.LOCAL_ACCESS_IP_ADDR", key_content);
87         ctx.setAttribute(InstarClientConstant.INPUT_PARAM_RESPONSE_PRIFIX, "TEST");
88         ctx.setAttribute(InstarClientConstant.VNF_NAME, "basx0003v");
89         InstarClientNode icn  = new InstarClientNode();
90         expectedEx.expect(SvcLogicException.class);
91         expectedEx.expectMessage("No Rule Defined to process :");
92         icn.getInstarInfo(inParams, ctx);
93     }
94
95     @Test
96     public void testInstarData() throws Exception {
97          InstarClientNode icn  = Mockito.spy(new InstarClientNode());
98          SvcLogicContext ctx  = new SvcLogicContext ();
99          Map<String, String> inParams = new HashMap<String, String>();
100          InstarRestClientImpl mockRestClient = Mockito.mock(InstarRestClientImpl.class);
101          Mockito.doReturn(mockRestClient).when(icn).createRestClientInterface(Mockito.any());
102          Mockito.doReturn("Test-data").when(mockRestClient).sendRequest(Mockito.anyString());
103          inParams.put(InstarClientConstant.VNF_NAME, "basx0003v");
104          inParams.put("operationName", InstarClientConstant.OPERATION_GET_IPADDRESS_BY_VNF_NAME);
105          icn.getInstarData(inParams, ctx);
106          Assert.assertEquals(InstarClientConstant.OUTPUT_STATUS_SUCCESS, ctx.getAttribute("" + InstarClientConstant.OUTPUT_PARAM_STATUS));
107          Assert.assertEquals("Test-data", ctx.getAttribute(InstarClientConstant.INSTAR_KEY_VALUES));
108     }
109
110     @Test
111     public void testInstarDataFailure() throws Exception {
112          InstarClientNode icn  = new InstarClientNode();
113          SvcLogicContext ctx  = new SvcLogicContext ();
114          Map<String, String> inParams = new HashMap<String, String>();
115          inParams.put(InstarClientConstant.VNF_NAME, "basx0003v");
116          inParams.put("operationName", InstarClientConstant.OPERATION_GET_IPADDRESS_BY_VNF_NAME);
117          expectedEx.expect(SvcLogicException.class);
118          expectedEx.expectMessage("Cannot find Property file -SDNC_CONFIG_DIR");
119          icn.getInstarData(inParams, ctx);
120     }
121
122     @Test
123     public void  TestGetAaiInfo() throws Exception {
124         InstarClientNode aaiNode=new InstarClientNode();
125         Map<String, String> inParams=new HashMap<String, String> ();
126         SvcLogicContext ctx=new SvcLogicContext();
127         String keyVals = new String("[\"vnf_name\"]");
128         inParams.put("aaiKeys",keyVals);
129         inParams.put("responsePrefix","test");
130         String parameterString="{\"name\":\"vnf_name\",\"description\":null,\"type\":null,\"required\":false,\"source\":\"A&AI\","
131                 + "\"rule-type\":\"vnf-name\",\"default\":null,\"request-keys\":null,\"response-keys\":[{\"unique-key-name\":\"parent-name\","
132                 + "\"unique-key-value\":\"vnf\",\"field-key-name\":\"vnf-name\",\"filter-by-field\":null,\"filter-by-value\":null}]}";
133         ctx.setAttribute(AaiClientConstant.SOURCE_SYSTEM_AAI + "." +  "vnf_name",parameterString);
134         aaiNode.getAaiInfo(inParams, ctx);
135         Assert.assertEquals(InstarClientConstant.OUTPUT_STATUS_SUCCESS, ctx.getAttribute("test." + InstarClientConstant.OUTPUT_PARAM_STATUS));
136     }
137
138     @Test
139     public void  TestGetAaiInfoFailure() throws Exception {
140         InstarClientNode aaiNode=new InstarClientNode();
141         Map<String, String> inParams= Mockito.spy(new HashMap<String, String> ());
142         inParams.put(InstarClientConstant.INPUT_PARAM_RESPONSE_PRIFIX, "");
143         Mockito.doThrow(new RuntimeException("Test-exception")).when(inParams).get(AaiClientConstant.AAI_KEYS);
144         SvcLogicContext ctx=new SvcLogicContext();
145         expectedEx.expect(SvcLogicException.class);
146         expectedEx.expectMessage("Test-exception");
147         aaiNode.getAaiInfo(inParams, ctx);
148     }
149 }