Apply defect and Fortify fixes to config bundle code
[appc.git] / appc-config / appc-flow-controller / provider / src / test / java / org / onap / appc / flow / controller / node / FlowSequenceGeneratorTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2018 Nokia. All rights reserved.
6  * =============================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.appc.flow.controller.node;
21
22 import static org.mockito.Matchers.any;
23 import static org.mockito.Matchers.eq;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27 import static org.onap.appc.flow.controller.node.FlowSequenceGenerator.MODULE;
28 import static org.onap.appc.flow.controller.node.InputParamsCollector.SDNC_CONFIG_DIR_VAR;
29 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.DESINGTIME;
30 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.EXTERNAL;
31 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.FLOW_SEQUENCE;
32 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.GENERATION_NODE;
33 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.RUNTIME;
34 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.SEQUENCE_TYPE;
35 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VNFC_TYPE;
36 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VNF_ID;
37 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VNF_TYPE;
38
39 import java.util.ArrayList;
40 import java.util.HashMap;
41 import java.util.Map;
42 import java.util.Properties;
43 import org.junit.Assert;
44 import org.junit.Before;
45 import org.junit.Rule;
46 import org.junit.Test;
47 import org.junit.rules.ExpectedException;
48 import org.onap.appc.flow.controller.data.Transaction;
49 import org.onap.appc.flow.controller.data.Transactions;
50 import org.onap.appc.flow.controller.dbervices.FlowControlDBService;
51 import org.onap.appc.flow.controller.executorImpl.GraphExecutor;
52 import org.onap.appc.flow.controller.executorImpl.RestExecutor;
53 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
54
55 public class FlowSequenceGeneratorTest {
56
57   private FlowControlDBService dbService;
58   private Map<String, String> inParams;
59   private SvcLogicContext localCtx;
60   private SvcLogicContext ctx;
61   private FlowGenerator flowGenerator;
62   private GraphExecutor graphExecutor;
63   private FlowSequenceGenerator flowSequenceGenerator;
64   private RestExecutor restExecutor;
65
66   @Rule
67   public ExpectedException expectedException = ExpectedException.none();
68
69   @Before
70   public void setUp() {
71     inParams = new HashMap<>();
72     ctx = mock(SvcLogicContext.class);
73     localCtx = mock(SvcLogicContext.class);
74     dbService = mock(FlowControlDBService.class);
75     flowGenerator = mock(FlowGenerator.class);
76     graphExecutor = mock(GraphExecutor.class);
77     restExecutor = mock(RestExecutor.class);
78
79     EnvVariables envVariables = mock(EnvVariables.class);
80
81     when(envVariables.getenv(SDNC_CONFIG_DIR_VAR)).thenReturn("./src/test/resources");
82
83     flowSequenceGenerator = new FlowSequenceGenerator(
84         dbService,
85         flowGenerator,
86         graphExecutor,
87         restExecutor,
88         envVariables
89     );
90   }
91
92   @Test
93   public void sequence_type_is_null() throws Exception {
94
95     Transaction transaction = new Transaction();
96     transaction.setExecutionType("mock-flow-generator");
97     ArrayList<Transaction> transactionList = new ArrayList<>();
98     transactionList.add(transaction);
99     Transactions transactions = new Transactions();
100     transactions.setTransactions(transactionList);
101
102     when(flowGenerator.createSingleStepModel(inParams, ctx)).thenReturn(transactions);
103
104     String seq = flowSequenceGenerator.getFlowSequence(inParams, ctx, localCtx);
105
106     Assert.assertEquals("{\"transactions\":[{\"executionType\":\"mock-flow-generator\",\"uId\":null,\"statusCode\":null,\"pswd\":null,\"executionEndPoint\":null,\"executionModule\":null,\"executionRPC\":null,\"status\":\"PENDING\",\"transaction-id\":0,\"action\":null,\"action-level\":null,\"action-identifier\":null,\"parameters\":null,\"state\":null,\"precheck\":null,\"payload\":null,\"responses\":null}]}", seq);
107   }
108
109   @Test
110   public void sequence_type_is_not_null_generator_and_generator_node_exists() throws Exception {
111
112     when(localCtx.getAttribute(SEQUENCE_TYPE)).thenReturn("some-seq-type");
113     when(localCtx.getAttribute(GENERATION_NODE)).thenReturn("some-gen-node");
114
115     when(graphExecutor.hasGraph(MODULE, "some-gen-node", null,"sync")).thenReturn(true);
116
117     Properties properties = new Properties();
118     properties.setProperty(FLOW_SEQUENCE, "flow-sequence");
119     when(graphExecutor.executeGraph(MODULE, "some-gen-node", null, "sync", null)).thenReturn(properties);
120
121     String seq = flowSequenceGenerator.getFlowSequence(inParams, ctx, localCtx);
122
123     Assert.assertEquals("flow-sequence", seq);
124   }
125
126   @Test
127   public void sequence_type_is_not_null_generator_exists_but_generator_node_not_exists() throws Exception {
128
129     when(localCtx.getAttribute(SEQUENCE_TYPE)).thenReturn("some-seq-type");
130     when(localCtx.getAttribute(GENERATION_NODE)).thenReturn("some-gen-node");
131
132     when(graphExecutor.hasGraph(MODULE, "some-gen-node", null,"sync")).thenReturn(false);
133
134     expectedException.expectMessage("Can not find Custom defined Flow Generator for some-gen-node");
135     flowSequenceGenerator.getFlowSequence(inParams, ctx, localCtx);
136   }
137
138   @Test
139   public void sequence_type_is_design_time() throws Exception {
140
141     when(localCtx.getAttribute(SEQUENCE_TYPE)).thenReturn(DESINGTIME);
142     when(dbService.getDesignTimeFlowModel(localCtx)).thenReturn("flow-sequence");
143
144     String flowSequence = flowSequenceGenerator.getFlowSequence(inParams, ctx, localCtx);
145     verify(localCtx).setAttribute(VNFC_TYPE, ctx.getAttribute(VNFC_TYPE));
146
147     Assert.assertEquals("flow-sequence", flowSequence);
148   }
149
150   @Test
151   public void sequence_type_is_design_time_but_got_null() throws Exception {
152
153     when(localCtx.getAttribute(SEQUENCE_TYPE)).thenReturn(DESINGTIME);
154     when(ctx.getAttribute(VNF_TYPE)).thenReturn("some-vnf-type");
155     when(dbService.getDesignTimeFlowModel(localCtx)).thenReturn(null);
156
157     expectedException.expectMessage("Flow Sequence is not found User Designed VNF some-vnf-type");
158     flowSequenceGenerator.getFlowSequence(inParams, ctx, localCtx);
159     verify(localCtx).setAttribute(VNFC_TYPE, ctx.getAttribute(VNFC_TYPE));
160   }
161
162   @Test
163   public void sequence_type_is_runtime() throws Exception {
164     when(localCtx.getAttribute(SEQUENCE_TYPE)).thenReturn(RUNTIME);
165     when(ctx.getAttribute(VNF_TYPE)).thenReturn("some-vnf-type");
166     when(ctx.getAttribute(VNF_ID)).thenReturn("some-vnf-id");
167
168     Map<String, String> map = new HashMap<>();
169     map.put("restResponse", "{'output':{'transactions':[{'transaction-id':'1','payload':''}]}}".replaceAll("'", "\""));
170     when(restExecutor.execute(any(Transaction.class), eq(localCtx))).thenReturn(map);
171
172     String flowSequence = flowSequenceGenerator.getFlowSequence(inParams, ctx, localCtx);
173
174     Assert.assertEquals("{'transactions':[{'transaction-id':'1','payload':''}]}".replaceAll("'", "\""), flowSequence);
175   }
176
177   @Test
178   public void sequence_type_is_runtime_but_no_transactions_generated() throws Exception {
179     when(localCtx.getAttribute(SEQUENCE_TYPE)).thenReturn(RUNTIME);
180     when(ctx.getAttribute(VNF_TYPE)).thenReturn("some-vnf-type");
181     when(ctx.getAttribute(VNF_ID)).thenReturn("some-vnf-id");
182
183     Map<String, String> map = new HashMap<>();
184     // {"status":{"code":450,"message":"Request is not supported"}}
185     map.put("restResponse", "{'output':{'status':{'code':450,'message':'Request is not supported'}}}".replaceAll("'", "\""));
186     when(restExecutor.execute(any(Transaction.class), eq(localCtx))).thenReturn(map);
187     expectedException.expectMessage("Failed to generate the sequence for this request");
188     
189     String flowSequence = flowSequenceGenerator.getFlowSequence(inParams, ctx, localCtx);
190   }
191
192   @Test
193   public void sequence_type_is_runtime_but_got_null() throws Exception {
194     when(localCtx.getAttribute(SEQUENCE_TYPE)).thenReturn(RUNTIME);
195     when(ctx.getAttribute(VNF_TYPE)).thenReturn("some-vnf-type");
196     when(ctx.getAttribute(VNF_ID)).thenReturn("some-vnf-id");
197
198     Map<String, String> map = new HashMap<>();
199     map.put("restResponse", "{}".replaceAll("'", "\""));
200     when(restExecutor.execute(any(Transaction.class), eq(localCtx))).thenReturn(map);
201
202     expectedException.expectMessage("Failed to get the Flow Sequence runtime for VNF type some-vnf-type");
203     flowSequenceGenerator.getFlowSequence(inParams, ctx, localCtx);
204   }
205
206   @Test
207   public void sequence_type_is_external() throws Exception {
208     when(localCtx.getAttribute(SEQUENCE_TYPE)).thenReturn(EXTERNAL);
209     when(ctx.getAttribute(VNF_TYPE)).thenReturn("some-vnf-type");
210
211     expectedException.expectMessage("Flow Sequence not found for some-vnf-type");
212     flowSequenceGenerator.getFlowSequence(inParams, ctx, localCtx);
213   }
214
215   @Test
216   public void unknown_sequence() throws Exception {
217     when(localCtx.getAttribute(SEQUENCE_TYPE)).thenReturn("some-unknown-type");
218
219     expectedException.expectMessage("No information found for sequence Owner Design-Time Vs Run-Time");
220     flowSequenceGenerator.getFlowSequence(inParams, ctx, localCtx);
221   }
222
223 }