2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.appc.flow.controller.node;
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;
39 import java.util.ArrayList;
40 import java.util.HashMap;
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;
55 public class FlowSequenceGeneratorTest {
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;
67 public ExpectedException expectedException = ExpectedException.none();
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);
79 EnvVariables envVariables = mock(EnvVariables.class);
81 when(envVariables.getenv(SDNC_CONFIG_DIR_VAR)).thenReturn("./src/test/resources");
83 flowSequenceGenerator = new FlowSequenceGenerator(
93 public void sequence_type_is_null() throws Exception {
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);
102 when(flowGenerator.createSingleStepModel(inParams, ctx)).thenReturn(transactions);
104 String seq = flowSequenceGenerator.getFlowSequence(inParams, ctx, localCtx);
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);
110 public void sequence_type_is_not_null_generator_and_generator_node_exists() throws Exception {
112 when(localCtx.getAttribute(SEQUENCE_TYPE)).thenReturn("some-seq-type");
113 when(localCtx.getAttribute(GENERATION_NODE)).thenReturn("some-gen-node");
115 when(graphExecutor.hasGraph(MODULE, "some-gen-node", null,"sync")).thenReturn(true);
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);
121 String seq = flowSequenceGenerator.getFlowSequence(inParams, ctx, localCtx);
123 Assert.assertEquals("flow-sequence", seq);
127 public void sequence_type_is_not_null_generator_exists_but_generator_node_not_exists() throws Exception {
129 when(localCtx.getAttribute(SEQUENCE_TYPE)).thenReturn("some-seq-type");
130 when(localCtx.getAttribute(GENERATION_NODE)).thenReturn("some-gen-node");
132 when(graphExecutor.hasGraph(MODULE, "some-gen-node", null,"sync")).thenReturn(false);
134 expectedException.expectMessage("Can not find Custom defined Flow Generator for some-gen-node");
135 flowSequenceGenerator.getFlowSequence(inParams, ctx, localCtx);
139 public void sequence_type_is_design_time() throws Exception {
141 when(localCtx.getAttribute(SEQUENCE_TYPE)).thenReturn(DESINGTIME);
142 when(dbService.getDesignTimeFlowModel(localCtx)).thenReturn("flow-sequence");
144 String flowSequence = flowSequenceGenerator.getFlowSequence(inParams, ctx, localCtx);
145 verify(localCtx).setAttribute(VNFC_TYPE, ctx.getAttribute(VNFC_TYPE));
147 Assert.assertEquals("flow-sequence", flowSequence);
151 public void sequence_type_is_design_time_but_got_null() throws Exception {
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);
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));
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");
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);
172 String flowSequence = flowSequenceGenerator.getFlowSequence(inParams, ctx, localCtx);
174 Assert.assertEquals("{'transactions':[{'transaction-id':'1','payload':''}]}".replaceAll("'", "\""), flowSequence);
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");
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");
189 String flowSequence = flowSequenceGenerator.getFlowSequence(inParams, ctx, localCtx);
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");
198 Map<String, String> map = new HashMap<>();
199 map.put("restResponse", "{}".replaceAll("'", "\""));
200 when(restExecutor.execute(any(Transaction.class), eq(localCtx))).thenReturn(map);
202 expectedException.expectMessage("Failed to get the Flow Sequence runtime for VNF type some-vnf-type");
203 flowSequenceGenerator.getFlowSequence(inParams, ctx, localCtx);
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");
211 expectedException.expectMessage("Flow Sequence not found for some-vnf-type");
212 flowSequenceGenerator.getFlowSequence(inParams, ctx, localCtx);
216 public void unknown_sequence() throws Exception {
217 when(localCtx.getAttribute(SEQUENCE_TYPE)).thenReturn("some-unknown-type");
219 expectedException.expectMessage("No information found for sequence Owner Design-Time Vs Run-Time");
220 flowSequenceGenerator.getFlowSequence(inParams, ctx, localCtx);