Added test case for Flow control Node
[appc.git] / appc-config / appc-flow-controller / provider / src / test / java / org / onap / appc / flow / controller / node / FlowControlNodeTest.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 Copyright (C) 2019 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.flow.controller.node;
27
28 import static org.junit.Assert.assertEquals;
29 import static org.mockito.Matchers.anyObject;
30 import static org.mockito.Matchers.eq;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.when;
33
34 import java.util.HashMap;
35 import java.util.Map;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.onap.appc.flow.controller.dbervices.FlowControlDBService;
39 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
40 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
41
42 public class FlowControlNodeTest {
43
44   private FlowControlDBService dbService;
45   private SvcLogicContext ctx;
46   private FlowControlNode flowControlNode;
47   private FlowSequenceGenerator flowSequenceGenerator;
48   private Map<String, String> inParams;
49
50   @Before
51   public void setUp() throws Exception {
52     ctx = new SvcLogicContext();
53     ctx.setAttribute("response.status", "success");
54     dbService = mock(FlowControlDBService.class);
55     flowSequenceGenerator = mock(FlowSequenceGenerator.class);
56     flowControlNode = new FlowControlNode(dbService, flowSequenceGenerator);
57     inParams = new HashMap<>();
58     inParams.put("responsePrefix", "response");
59   }
60
61   @Test
62   public void testProcessFlow() throws Exception {
63     String transactionJson = "{\"transaction-id\": \"1\","
64         + "  \"action\": \"HealthCheck\", \"action-level\": \"vnf\","
65         + "  \"executionModule\": \"APPC\", \"executionRPC\": \"healthcheck\", \"executionType\": \"node\","
66         + "\"precheck\":{\"precheck-operator\":\"any\",\"precheck-options\": ["
67         + "{\"pre-transaction-id\":\"1\",\"param-name\":\"executionType\",\"param-value\":\"node\"}]} }";
68     when(flowSequenceGenerator.getFlowSequence(eq(inParams), eq(ctx), anyObject()))
69         .thenReturn("{\"transactions\" :[" + transactionJson + "] }");
70     flowControlNode.processFlow(inParams, ctx);
71     assertEquals("response.", ctx.getAttribute("response-prefix"));
72   }
73
74   @Test
75   public void testProcessFlowWithoutPrecheck() throws Exception {
76     String transactionJson = "{\"transaction-id\": \"1\","
77         + "  \"action\": \"HealthCheck\", \"action-level\": \"vnf\","
78         + "  \"executionModule\": \"APPC\", \"executionRPC\": \"healthcheck\", \"executionType\": \"node\","
79         + "\"precheck\":{\"precheck-operator\":\"any\",\"precheck-options\": ["
80         + "{\"pre-transaction-id\":\"1\",\"param-name\":\"executionType\",\"param-value\":\"node1\"}]} }";
81     when(flowSequenceGenerator.getFlowSequence(eq(inParams), eq(ctx), anyObject()))
82         .thenReturn("{\"transactions\" :[" + transactionJson + "] }");
83     flowControlNode.processFlow(inParams, ctx);
84     assertEquals("response.", ctx.getAttribute("response-prefix"));
85   }
86
87   @Test(expected = SvcLogicException.class)
88   public void testProcessFlowWithFailure() throws Exception {
89     when(flowSequenceGenerator.getFlowSequence(eq(inParams), eq(ctx), anyObject()))
90         .thenReturn("{\"transactions\" :[] }");
91     ctx.setAttribute("response.status", "fail");
92     flowControlNode.processFlow(inParams, ctx);
93   }
94
95   @Test(expected = SvcLogicException.class)
96   public void testProcessFlowWithNoExecutionType() throws Exception {
97     String transactionJson = "{\"transaction-id\": \"1\","
98         + "  \"action\": \"HealthCheck\", \"action-level\": \"vnf\","
99         + "  \"executionModule\": \"APPC\", \"executionRPC\": \"healthcheck\", \"executionType\": \"other\","
100         + "\"precheck\":{\"precheck-operator\":\"any\",\"precheck-options\": ["
101         + "{\"pre-transaction-id\":\"1\",\"param-name\":\"executionType\",\"param-value\":\"other\"}]} }";
102     when(flowSequenceGenerator.getFlowSequence(eq(inParams), eq(ctx), anyObject()))
103         .thenReturn("{\"transactions\" :[" + transactionJson + "] }");
104     ctx.setAttribute("response.status", "fail");
105     flowControlNode.processFlow(inParams, ctx);
106   }
107 }