add junit coverage
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / infrastructure / decisionpoint / impl / buildingblock / controller / sdnc / prepare / PrepareSdncUpgradePreCheckPnfBBTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nokia
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.bpmn.infrastructure.decisionpoint.impl.buildingblock.controller.sdnc.prepare;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import org.camunda.bpm.engine.delegate.DelegateExecution;
25 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.onap.so.bpmn.common.BuildingBlockExecution;
29 import org.onap.so.bpmn.common.DelegateExecutionImpl;
30 import org.onap.so.bpmn.infrastructure.decisionpoint.api.ControllerContext;
31
32 public class PrepareSdncUpgradePreCheckPnfBBTest {
33
34     private PrepareSdncUpgradePreCheckPnfBB testedObject;
35
36     @Before
37     public void setup() {
38         testedObject = new PrepareSdncUpgradePreCheckPnfBB();
39     }
40
41     @Test
42     public void understandTrue() {
43         ControllerContext<BuildingBlockExecution> controllerContext =
44                 createControllerContext("sdnc", "UpgradePreCheck", "pnf");
45         boolean result = testedObject.understand(controllerContext);
46         assertThat(result).isTrue();
47     }
48
49     @Test
50     public void understandFalse() {
51         ControllerContext<BuildingBlockExecution> controllerContext =
52                 createControllerContext("actor1", "action1", "scope1");
53         boolean result = testedObject.understand(controllerContext);
54         assertThat(result).isFalse();
55     }
56
57     @Test
58     public void prepareJson_payloadWithoutAction() {
59         String payloadWithoutActionArray = "{\"json name\": \"test1\"}";
60         ControllerContext<BuildingBlockExecution> controllerContext =
61                 createControllerContext(payloadWithoutActionArray, "action1");
62         testedObject.prepare(controllerContext);
63
64         assertThat((String) controllerContext.getExecution().getVariable("payload"))
65                 .isEqualTo(payloadWithoutActionArray);
66     }
67
68     @Test
69     public void prepareJson_payloadWithActionJsonObject() {
70         String jsonActionObjectKey = "action1";
71         String jsonActionObject = String.format("{\"%s\":\"act1\"}", jsonActionObjectKey);
72         String payloadWithActionArray = String.format("{\"json name\":\"test1\",\"action\": [%s]}", jsonActionObject);
73         ControllerContext<BuildingBlockExecution> controllerContext =
74                 createControllerContext(payloadWithActionArray, jsonActionObjectKey);
75
76         testedObject.prepare(controllerContext);
77
78         assertThat((String) controllerContext.getExecution().getVariable("payload")).isEqualTo(jsonActionObject);
79     }
80
81     @Test
82     public void prepareJson_payloadWithActionJsonObjectButDifferentKey() {
83         String payloadWithActionArray = ("{\"json name\":\"test1\",\"action\": [{\"action1\":\"act1\"}]}");
84         ControllerContext<BuildingBlockExecution> controllerContext =
85                 createControllerContext(payloadWithActionArray, "otherAction");
86
87         testedObject.prepare(controllerContext);
88
89         assertThat((String) controllerContext.getExecution().getVariable("payload")).isEqualTo(payloadWithActionArray);
90     }
91
92     private ControllerContext<BuildingBlockExecution> createControllerContext(String actor, String action,
93             String scope) {
94         ControllerContext<BuildingBlockExecution> controllerContext = new ControllerContext<>();
95         controllerContext.setControllerActor(actor);
96         controllerContext.setControllerAction(action);
97         controllerContext.setControllerScope(scope);
98         return controllerContext;
99     }
100
101     private ControllerContext<BuildingBlockExecution> createControllerContext(String payload, String action) {
102         ControllerContext<BuildingBlockExecution> controllerContext = new ControllerContext<>();
103         controllerContext.setExecution(prepareBuildingBlockExecution(payload));
104         controllerContext.setControllerAction(action);
105         return controllerContext;
106     }
107
108     private BuildingBlockExecution prepareBuildingBlockExecution(String payload) {
109         DelegateExecution execution = new DelegateExecutionFake();
110         execution.setVariable("payload", payload);
111         return new DelegateExecutionImpl(execution);
112     }
113 }