summaryrefslogtreecommitdiffstats
path: root/appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/node/FlowSequenceGeneratorTest.java
blob: 533de58404cea3b27593aa0340d13caa8055f035 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
 * ============LICENSE_START=======================================================
 * ONAP : APPC
 * ================================================================================
 * Copyright (C) 2018 Nokia. All rights reserved.
 * =============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.appc.flow.controller.node;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.onap.appc.flow.controller.node.FlowSequenceGenerator.MODULE;
import static org.onap.appc.flow.controller.node.InputParamsCollector.SDNC_CONFIG_DIR_VAR;
import static org.onap.appc.flow.controller.utils.FlowControllerConstants.DESINGTIME;
import static org.onap.appc.flow.controller.utils.FlowControllerConstants.EXTERNAL;
import static org.onap.appc.flow.controller.utils.FlowControllerConstants.FLOW_SEQUENCE;
import static org.onap.appc.flow.controller.utils.FlowControllerConstants.GENERATION_NODE;
import static org.onap.appc.flow.controller.utils.FlowControllerConstants.RUNTIME;
import static org.onap.appc.flow.controller.utils.FlowControllerConstants.SEQUENCE_TYPE;
import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VNFC_TYPE;
import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VNF_ID;
import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VNF_TYPE;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.onap.appc.flow.controller.data.Transaction;
import org.onap.appc.flow.controller.data.Transactions;
import org.onap.appc.flow.controller.dbervices.FlowControlDBService;
import org.onap.appc.flow.controller.executorImpl.GraphExecutor;
import org.onap.appc.flow.controller.executorImpl.RestExecutor;
import org.onap.ccsdk.sli.core.sli.SvcLogicContext;

public class FlowSequenceGeneratorTest {

  private FlowControlDBService dbService;
  private Map<String, String> inParams;
  private SvcLogicContext localCtx;
  private SvcLogicContext ctx;
  private FlowGenerator flowGenerator;
  private GraphExecutor graphExecutor;
  private FlowSequenceGenerator flowSequenceGenerator;
  private RestExecutor restExecutor;

  @Rule
  public ExpectedException expectedException = ExpectedException.none();

  @Before
  public void setUp() {
    inParams = new HashMap<>();
    ctx = mock(SvcLogicContext.class);
    localCtx = mock(SvcLogicContext.class);
    dbService = mock(FlowControlDBService.class);
    flowGenerator = mock(FlowGenerator.class);
    graphExecutor = mock(GraphExecutor.class);
    restExecutor = mock(RestExecutor.class);

    EnvVariables envVariables = mock(EnvVariables.class);

    when(envVariables.getenv(SDNC_CONFIG_DIR_VAR)).thenReturn("./src/test/resources");

    flowSequenceGenerator = new FlowSequenceGenerator(
        dbService,
        flowGenerator,
        graphExecutor,
        restExecutor,
        envVariables
    );
  }

  @Test
  public void sequence_type_is_null() throws Exception {

    Transaction transaction = new Transaction();
    transaction.setExecutionType("mock-flow-generator");
    ArrayList<Transaction> transactionList = new ArrayList<>();
    transactionList.add(transaction);
    Transactions transactions = new Transactions();
    transactions.setTransactions(transactionList);

    when(flowGenerator.createSingleStepModel(inParams, ctx)).thenReturn(transactions);

    String seq = flowSequenceGenerator.getFlowSequence(inParams, ctx, localCtx);

    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);
  }

  @Test
  public void sequence_type_is_not_null_generator_and_generator_node_exists() throws Exception {

    when(localCtx.getAttribute(SEQUENCE_TYPE)).thenReturn("some-seq-type");
    when(localCtx.getAttribute(GENERATION_NODE)).thenReturn("some-gen-node");

    when(graphExecutor.hasGraph(MODULE, "some-gen-node", null,"sync")).thenReturn(true);

    Properties properties = new Properties();
    properties.setProperty(FLOW_SEQUENCE, "flow-sequence");
    when(graphExecutor.executeGraph(MODULE, "some-gen-node", null, "sync", null)).thenReturn(properties);

    String seq = flowSequenceGenerator.getFlowSequence(inParams, ctx, localCtx);

    Assert.assertEquals("flow-sequence", seq);
  }

  @Test
  public void sequence_type_is_not_null_generator_exists_but_generator_node_not_exists() throws Exception {

    when(localCtx.getAttribute(SEQUENCE_TYPE)).thenReturn("some-seq-type");
    when(localCtx.getAttribute(GENERATION_NODE)).thenReturn("some-gen-node");

    when(graphExecutor.hasGraph(MODULE, "some-gen-node", null,"sync")).thenReturn(false);

    expectedException.expectMessage("Can not find Custom defined Flow Generator for some-gen-node");
    flowSequenceGenerator.getFlowSequence(inParams, ctx, localCtx);
  }

  @Test
  public void sequence_type_is_design_time() throws Exception {

    when(localCtx.getAttribute(SEQUENCE_TYPE)).thenReturn(DESINGTIME);
    when(dbService.getDesignTimeFlowModel(localCtx)).thenReturn("flow-sequence");

    String flowSequence = flowSequenceGenerator.getFlowSequence(inParams, ctx, localCtx);
    verify(localCtx).setAttribute(VNFC_TYPE, ctx.getAttribute(VNFC_TYPE));

    Assert.assertEquals("flow-sequence", flowSequence);
  }

  @Test
  public void sequence_type_is_design_time_but_got_null() throws Exception {

    when(localCtx.getAttribute(SEQUENCE_TYPE)).thenReturn(DESINGTIME);
    when(ctx.getAttribute(VNF_TYPE)).thenReturn("some-vnf-type");
    when(dbService.getDesignTimeFlowModel(localCtx)).thenReturn(null);

    expectedException.expectMessage("Flow Sequence is not found User Designed VNF some-vnf-type");
    flowSequenceGenerator.getFlowSequence(inParams, ctx, localCtx);
    verify(localCtx).setAttribute(VNFC_TYPE, ctx.getAttribute(VNFC_TYPE));
  }

  @Test
  public void sequence_type_is_runtime() throws Exception {
    when(localCtx.getAttribute(SEQUENCE_TYPE)).thenReturn(RUNTIME);
    when(ctx.getAttribute(VNF_TYPE)).thenReturn("some-vnf-type");
    when(ctx.getAttribute(VNF_ID)).thenReturn("some-vnf-id");

    Map<String, String> map = new HashMap<>();
    map.put("restResponse", "{'output':{'dummy-json-object':'some-param'}}".replaceAll("'", "\""));
    when(restExecutor.execute(any(Transaction.class), eq(localCtx))).thenReturn(map);

    String flowSequence = flowSequenceGenerator.getFlowSequence(inParams, ctx, localCtx);

    Assert.assertEquals("{'dummy-json-object':'some-param'}".replaceAll("'", "\""), flowSequence);
  }

  @Test
  public void sequence_type_is_runtime_but_got_null() throws Exception {
    when(localCtx.getAttribute(SEQUENCE_TYPE)).thenReturn(RUNTIME);
    when(ctx.getAttribute(VNF_TYPE)).thenReturn("some-vnf-type");
    when(ctx.getAttribute(VNF_ID)).thenReturn("some-vnf-id");

    Map<String, String> map = new HashMap<>();
    map.put("restResponse", "{}".replaceAll("'", "\""));
    when(restExecutor.execute(any(Transaction.class), eq(localCtx))).thenReturn(map);

    expectedException.expectMessage("Failed to get the Flow Sequence runtime for VNF type some-vnf-type");
    flowSequenceGenerator.getFlowSequence(inParams, ctx, localCtx);
  }

  @Test
  public void sequence_type_is_external() throws Exception {
    when(localCtx.getAttribute(SEQUENCE_TYPE)).thenReturn(EXTERNAL);
    when(ctx.getAttribute(VNF_TYPE)).thenReturn("some-vnf-type");

    expectedException.expectMessage("Flow Sequence not found for some-vnf-type");
    flowSequenceGenerator.getFlowSequence(inParams, ctx, localCtx);
  }

  @Test
  public void unknown_sequence() throws Exception {
    when(localCtx.getAttribute(SEQUENCE_TYPE)).thenReturn("some-unknown-type");

    expectedException.expectMessage("No information found for sequence Owner Design-Time Vs Run-Time");
    flowSequenceGenerator.getFlowSequence(inParams, ctx, localCtx);
  }

}