Changes needed for MultiStep Actions
[appc.git] / appc-config / appc-flow-controller / provider / src / test / java / org / onap / appc / flow / controller / node / InputParamsCollectorTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2018 Nokia. All rights reserved.
6  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
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.appc.flow.controller.node;
22
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
25 import static org.onap.appc.flow.controller.node.InputParamsCollector.SDNC_CONFIG_DIR_VAR;
26 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.ACTION_LEVEL;
27 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.PAYLOAD;
28 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.REQUEST_ACTION;
29 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VNFC_NAME;
30 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VNF_ID;
31 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VSERVER_ID;
32
33 import com.fasterxml.jackson.core.JsonProcessingException;
34 import com.fasterxml.jackson.databind.ObjectMapper;
35 import java.util.ArrayList;
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39 import org.junit.Assert;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.onap.appc.flow.controller.data.Transaction;
43 import org.onap.appc.flow.controller.dbervices.FlowControlDBService;
44 import org.onap.appc.flow.controller.interfaceData.DependencyInfo;
45 import org.onap.appc.flow.controller.interfaceData.Vnfcs;
46 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
47
48 public class InputParamsCollectorTest {
49
50   private SvcLogicContext ctx;
51   private FlowControlDBService dbService;
52   private EnvVariables envVariables;
53   private InputParamsCollector inputParamsCollector;
54
55   @Before
56   public void setUp() {
57
58     ctx = mock(SvcLogicContext.class);
59     dbService = mock(FlowControlDBService.class);
60     envVariables = mock(EnvVariables.class);
61
62     when(envVariables.getenv(SDNC_CONFIG_DIR_VAR)).thenReturn("./src/test/resources");
63
64     inputParamsCollector = new InputParamsCollector(envVariables, dbService);
65   }
66
67   @Test
68   public void should_collect_input_params() throws Exception {
69
70     when(ctx.getAttribute(VNF_ID)).thenReturn("some-vnf-id");
71     when(ctx.getAttribute(REQUEST_ACTION)).thenReturn("some-request-action");
72     when(ctx.getAttribute(ACTION_LEVEL)).thenReturn("some-action-level");
73     when(ctx.getAttribute(PAYLOAD)).thenReturn("some-payload");
74     when(ctx.getAttribute(VSERVER_ID)).thenReturn("some-vserver-id");
75     when(ctx.getAttribute(VNFC_NAME)).thenReturn("some-vnfc-name");
76
77     when(dbService.getCapabilitiesData(ctx)).thenReturn(
78         "{'vnf':['vnf-1', 'vnf-2'],'vf-module':['vf-module-1', 'vf-module-2'],'vnfc':['vnfc-1', 'vnfc-2'],'vm':['vm-1', 'vm-2']}"
79             .replaceAll("'", "\""));
80     when(dbService.getDependencyInfo(ctx)).thenReturn(dependencyInfoPayload());
81
82     Transaction transaction = inputParamsCollector.collectInputParams(ctx);
83
84     Assert.assertEquals("{\"input\":{\"request-info\":{\"action\":\"some-request-action\",\"payload\":\"some-payload\",\"action-level\":\"some-action-level\",\"action-identifier\":{\"vnf-id\":\"some-vnf-id\",\"vserver-id\":\"some-vserver-id\",\"vnfc-name\":\"some-vnfc-name\"}},\"inventory-info\":{\"vnf-info\":{\"vnf-id\":\"some-vnf-id\",\"identity-url\":\"\",\"vm\":[]}},\"capabilities\":{\"vnf\":[\"vnf-1\",\"vnf-2\"],\"vm\":[\"vm-1\",\"vm-2\"],\"vnfc\":[\"vnfc-1\",\"vnfc-2\"],\"vf-module\":[\"vf-module-1\",\"vf-module-2\"]}}}",
85         transaction.getPayload());
86     Assert.assertEquals("POST", transaction.getExecutionRPC());
87     Assert.assertEquals("seq-generator-uid", transaction.getuId());
88     Assert.assertEquals("some-pswd", transaction.getPswd());
89     Assert.assertEquals("exec-endpoint", transaction.getExecutionEndPoint());
90   }
91
92   @Test
93   public void should_handle_dependency_config() throws Exception {
94
95     Vnfcs vnfcs = new Vnfcs();
96     vnfcs.setVnfcType("some-type");
97     vnfcs.setResilience("some-resilience");
98     vnfcs.setMandatory("some-mandatory");
99     Map<String, List<Vnfcs>> input = new HashMap<>();
100     List<Vnfcs> list = new ArrayList<>();
101     list.add(vnfcs);
102     list.add(vnfcs);
103     input.put("vnfcs", list);
104
105     String jsonPayload = new ObjectMapper().writeValueAsString(input);
106
107     when(dbService.getDependencyInfo(ctx)).thenReturn(jsonPayload);
108
109     DependencyInfo dependencyInfo = inputParamsCollector.getDependencyInfo(ctx);
110
111     Assert.assertEquals(
112         "DependencyInfo [vnfcs=[Vnfcs [vnfcType=some-type, mandatory=some-mandatory, resilience=some-resilience, parents=[]], Vnfcs [vnfcType=some-type, mandatory=some-mandatory, resilience=some-resilience, parents=[]]]]",
113         dependencyInfo.toString());
114   }
115
116   private String dependencyInfoPayload() throws JsonProcessingException {
117     Vnfcs vnfcs = new Vnfcs();
118     vnfcs.setVnfcType("some-type");
119     vnfcs.setResilience("some-resilience");
120     vnfcs.setMandatory("some-mandatory");
121     Map<String, List<Vnfcs>> input = new HashMap<>();
122     List<Vnfcs> list = new ArrayList<>();
123     list.add(vnfcs);
124     list.add(vnfcs);
125     input.put("vnfcs", list);
126
127     return new ObjectMapper().writeValueAsString(input);
128   }
129
130 }