61f2ee34329f9ea18b4c5f9f2f34712b54e90c99
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2020 Huawei Technologies Co., Ltd. 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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.bpmn.infrastructure.decisionpoint.impl.camunda.controller.sdnc;
22
23 import com.fasterxml.jackson.core.JsonProcessingException;
24 import org.camunda.bpm.engine.delegate.DelegateExecution;
25 import org.junit.Before;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.junit.runner.RunWith;
29 import org.junit.Test;
30 import static org.junit.Assert.assertEquals;
31 import static org.junit.Assert.fail;
32 import static org.mockito.Mockito.when;
33 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.REQUEST_PAYLOAD;
34 import org.springframework.boot.test.mock.mockito.MockBean;
35 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
36 import org.onap.so.bpmn.infrastructure.decisionpoint.impl.camunda.controller.common.SoPropertyConstants;
37 import org.onap.so.client.sdnc.lcm.beans.payload.*;
38
39 @RunWith(SpringJUnit4ClassRunner.class)
40 public class SDNCLcmPayloadBuilderTest {
41     private static Logger logger = LoggerFactory.getLogger(SDNCLcmPayloadBuilderTest.class);
42
43     protected static String payload = "{" + "\"ipaddressV4Oam\": \"192.168.1.10\"," + "\"oldSwVersion\": \"v1\","
44             + "\"preCheckRuleName\": \"r101\"," + "\"preCheckAdditionalData\": \"{}\","
45             + "\"preCheckPlaybook\": \"precheck_playbook\"," + "\"swToBeDownloaded\": \"[{"
46             + "\\\"swLocation\\\": \\\"http://192.168.1.20/test.zip\\\"," + "\\\"swFileSize\\\": 123456,"
47             + "\\\"swFileCompression\\\": \\\"ZIP\\\"," + "\\\"swFileFormat\\\": \\\"binary\\\"}]\","
48             + "\"downloadNESwPlaybook\": \"downloadnesw_playbook\","
49             + "\"activateNESwPlaybook\": \"activatenesw_playbook\"," + "\"postCheckRuleName\": \"r102\","
50             + "\"postCheckAdditionalData\": \"{}\"," + "\"postCheckPlaybook\": \"postcheck_playbook\"" + "}";
51     protected String targetSoftwareVersion = "v2";
52
53     @MockBean
54     private DelegateExecution execution;
55
56     public static String getRequestPayload() {
57         return payload;
58     }
59
60     @Before
61     public void setUp() {
62         when(execution.getVariable(REQUEST_PAYLOAD)).thenReturn(payload);
63         when(execution.getVariable(SoPropertyConstants.TARGET_SOFTWARE_VERSION)).thenReturn(targetSoftwareVersion);
64     }
65
66
67     @Test
68     public final void testBuildActivateNESwPayload() {
69         String expectedPayload = "{" + "\"ipaddress-v4-oam\":\"192.168.1.10\","
70                 + "\"playbook-name\":\"activatenesw_playbook\"," + "\"swVersionToBeActivated\":\"v2\"" + "}";
71
72         ActivateNESwPayload activateNESwPayload = SDNCLcmPayloadBuilder.buildActivateNESwPayload(execution);
73         try {
74             String payload = SDNCLcmPayloadBuilder.convertToSting(activateNESwPayload);
75             logger.debug("ActivateNESwPayload:\n" + payload);
76
77             assertEquals(expectedPayload, payload);
78         } catch (JsonProcessingException e) {
79             fail("Convert ActivateNESwPayload to String error: " + e.toString());
80         }
81     }
82
83     @Test
84     public final void testBuildDownloadNESwPayload() {
85         String expectedSwToBeDownloadedElement = "{" + "\"swLocation\":\"http://192.168.1.20/test.zip\","
86                 + "\"swFileSize\":123456," + "\"swFileCompression\":\"ZIP\"," + "\"swFileFormat\":\"binary\"" + "}";
87         String expectedPayload =
88                 "{" + "\"ipaddress-v4-oam\":\"192.168.1.10\"," + "\"playbook-name\":\"downloadnesw_playbook\","
89                         + "\"swToBeDownloaded\":[" + expectedSwToBeDownloadedElement + "]" + "}";
90
91         DownloadNESwPayload downloadNESwPayload = SDNCLcmPayloadBuilder.buildDownloadNESwPayload(execution);
92         try {
93             String payload = SDNCLcmPayloadBuilder.convertToSting(downloadNESwPayload);
94             logger.debug("DownloadNESwPayload:\n" + payload);
95
96             assertEquals(expectedPayload, payload);
97         } catch (JsonProcessingException e) {
98             fail("Convert DownloadNESwPayload to String error: " + e.toString());
99         }
100     }
101
102     @Test
103     public final void testBuildUpgradePostCheckPayload() {
104         String expectedPayload = "{" + "\"ipaddress-v4-oam\":\"192.168.1.10\","
105                 + "\"playbook-name\":\"postcheck_playbook\"," + "\"oldSwVersion\":\"v1\","
106                 + "\"targetSwVersion\":\"v2\"," + "\"ruleName\":\"r102\"," + "\"additionalData\":\"{}\"" + "}";
107
108         UpgradePostCheckPayload upgradePostCheckPayload = SDNCLcmPayloadBuilder.buildUpgradePostCheckPayload(execution);
109         try {
110             String payload = SDNCLcmPayloadBuilder.convertToSting(upgradePostCheckPayload);
111             logger.debug("UpgradePostCheckPayload:\n" + payload);
112
113             assertEquals(expectedPayload, payload);
114         } catch (JsonProcessingException e) {
115             fail("Convert UpgradePostCheckPayload to String error: " + e.toString());
116         }
117     }
118
119     @Test
120     public final void testBuildUpgradePreCheckPayload() {
121         String expectedPayload = "{" + "\"ipaddress-v4-oam\":\"192.168.1.10\","
122                 + "\"playbook-name\":\"precheck_playbook\"," + "\"oldSwVersion\":\"v1\","
123                 + "\"targetSwVersion\":\"v2\"," + "\"ruleName\":\"r101\"," + "\"additionalData\":\"{}\"" + "}";
124
125         UpgradePreCheckPayload upgradePreCheckPayload = SDNCLcmPayloadBuilder.buildUpgradePreCheckPayload(execution);
126         try {
127             String payload = SDNCLcmPayloadBuilder.convertToSting(upgradePreCheckPayload);
128             logger.debug("UpgradePreCheckPayload:\n" + payload);
129
130             assertEquals(expectedPayload, payload);
131         } catch (JsonProcessingException e) {
132             fail("Convert UpgradePreCheckPayload to String error: " + e.toString());
133         }
134     }
135 }