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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.so.bpmn.infrastructure.decisionpoint.impl.buildingblock.controller.sdnc.prepare;
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;
32 public class PrepareSdncUpgradePreCheckPnfBBTest {
34 private PrepareSdncUpgradePreCheckPnfBB testedObject;
38 testedObject = new PrepareSdncUpgradePreCheckPnfBB();
42 public void understandTrue() {
43 ControllerContext<BuildingBlockExecution> controllerContext =
44 createControllerContext("sdnc", "UpgradePreCheck", "pnf");
45 boolean result = testedObject.understand(controllerContext);
46 assertThat(result).isTrue();
50 public void understandFalse() {
51 ControllerContext<BuildingBlockExecution> controllerContext =
52 createControllerContext("actor1", "action1", "scope1");
53 boolean result = testedObject.understand(controllerContext);
54 assertThat(result).isFalse();
58 public void prepareJson_payloadWithoutAction() {
59 String payloadWithoutActionArray = "{\"json name\": \"test1\"}";
60 ControllerContext<BuildingBlockExecution> controllerContext =
61 createControllerContext(payloadWithoutActionArray, "action1");
62 testedObject.prepare(controllerContext);
64 assertThat((String) controllerContext.getExecution().getVariable("payload"))
65 .isEqualTo(payloadWithoutActionArray);
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);
76 testedObject.prepare(controllerContext);
78 assertThat((String) controllerContext.getExecution().getVariable("payload")).isEqualTo(jsonActionObject);
82 public void prepareJson_payloadWithActionJsonObjectButDifferentKey() {
83 String payloadWithActionArray = ("{\"json name\":\"test1\",\"action\": [{\"action1\":\"act1\"}]}");
84 ControllerContext<BuildingBlockExecution> controllerContext =
85 createControllerContext(payloadWithActionArray, "otherAction");
87 testedObject.prepare(controllerContext);
89 assertThat((String) controllerContext.getExecution().getVariable("payload")).isEqualTo(payloadWithActionArray);
92 private ControllerContext<BuildingBlockExecution> createControllerContext(String actor, String action,
94 ControllerContext<BuildingBlockExecution> controllerContext = new ControllerContext<>();
95 controllerContext.setControllerActor(actor);
96 controllerContext.setControllerAction(action);
97 controllerContext.setControllerScope(scope);
98 return controllerContext;
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;
108 private BuildingBlockExecution prepareBuildingBlockExecution(String payload) {
109 DelegateExecution execution = new DelegateExecutionFake();
110 execution.setVariable("payload", payload);
111 return new DelegateExecutionImpl(execution);