4ed79c077e57ebe170abe2efd1bd1a7aba9d3617
[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.ActivateNESwPayload;
38 import org.onap.so.client.sdnc.lcm.beans.payload.DownloadNESwPayload;
39 import org.onap.so.client.sdnc.lcm.beans.payload.UpgradePostCheckPayload;
40 import org.onap.so.client.sdnc.lcm.beans.payload.UpgradePreCheckPayload;
41
42 @RunWith(SpringJUnit4ClassRunner.class)
43 public class SDNCLcmPayloadBuilderTest {
44     private static Logger logger = LoggerFactory.getLogger(SDNCLcmPayloadBuilderTest.class);
45
46     protected static String payload = "{" + "\"ipaddressV4Oam\": \"192.168.1.10\"," + "\"oldSwVersion\": \"v1\","
47             + "\"preCheckRuleName\": \"r101\"," + "\"preCheckAdditionalData\": \"{}\","
48             + "\"preCheckPlaybook\": \"precheck_playbook\"," + "\"swToBeDownloaded\": \"[{"
49             + "\\\"swLocation\\\": \\\"http://192.168.1.20/test.zip\\\"," + "\\\"swFileSize\\\": 123456,"
50             + "\\\"swFileCompression\\\": \\\"ZIP\\\"," + "\\\"swFileFormat\\\": \\\"binary\\\"}]\","
51             + "\"downloadNESwPlaybook\": \"downloadnesw_playbook\","
52             + "\"activateNESwPlaybook\": \"activatenesw_playbook\"," + "\"postCheckRuleName\": \"r102\","
53             + "\"postCheckAdditionalData\": \"{}\"," + "\"postCheckPlaybook\": \"postcheck_playbook\"" + "}";
54     protected String targetSoftwareVersion = "v2";
55
56     @MockBean
57     private DelegateExecution execution;
58
59     public static String getRequestPayload() {
60         return payload;
61     }
62
63     @Before
64     public void setUp() {
65         when(execution.getVariable(REQUEST_PAYLOAD)).thenReturn(payload);
66         when(execution.getVariable(SoPropertyConstants.TARGET_SOFTWARE_VERSION)).thenReturn(targetSoftwareVersion);
67     }
68
69
70     @Test
71     public final void testBuildActivateNESwPayload() {
72         String expectedPayload = "{" + "\"ipaddress-v4-oam\":\"192.168.1.10\","
73                 + "\"playbook-name\":\"activatenesw_playbook\"," + "\"swVersionToBeActivated\":\"v2\"" + "}";
74
75         ActivateNESwPayload activateNESwPayload = SDNCLcmPayloadBuilder.buildActivateNESwPayload(execution);
76         try {
77             String payload = SDNCLcmPayloadBuilder.convertToSting(activateNESwPayload);
78             logger.debug("ActivateNESwPayload:\n" + payload);
79
80             assertEquals(expectedPayload, payload);
81         } catch (JsonProcessingException e) {
82             fail("Convert ActivateNESwPayload to String error: " + e.toString());
83         }
84     }
85
86     @Test
87     public final void testBuildDownloadNESwPayload() {
88         String expectedSwToBeDownloadedElement = "{" + "\"swLocation\":\"http://192.168.1.20/test.zip\","
89                 + "\"swFileSize\":123456," + "\"swFileCompression\":\"ZIP\"," + "\"swFileFormat\":\"binary\"" + "}";
90         String expectedPayload =
91                 "{" + "\"ipaddress-v4-oam\":\"192.168.1.10\"," + "\"playbook-name\":\"downloadnesw_playbook\","
92                         + "\"swToBeDownloaded\":[" + expectedSwToBeDownloadedElement + "]" + "}";
93
94         DownloadNESwPayload downloadNESwPayload = SDNCLcmPayloadBuilder.buildDownloadNESwPayload(execution);
95         try {
96             String payload = SDNCLcmPayloadBuilder.convertToSting(downloadNESwPayload);
97             logger.debug("DownloadNESwPayload:\n" + payload);
98
99             assertEquals(expectedPayload, payload);
100         } catch (JsonProcessingException e) {
101             fail("Convert DownloadNESwPayload to String error: " + e.toString());
102         }
103     }
104
105     @Test
106     public final void testBuildUpgradePostCheckPayload() {
107         String expectedPayload = "{" + "\"ipaddress-v4-oam\":\"192.168.1.10\","
108                 + "\"playbook-name\":\"postcheck_playbook\"," + "\"oldSwVersion\":\"v1\","
109                 + "\"targetSwVersion\":\"v2\"," + "\"ruleName\":\"r102\"," + "\"additionalData\":\"{}\"" + "}";
110
111         UpgradePostCheckPayload upgradePostCheckPayload = SDNCLcmPayloadBuilder.buildUpgradePostCheckPayload(execution);
112         try {
113             String payload = SDNCLcmPayloadBuilder.convertToSting(upgradePostCheckPayload);
114             logger.debug("UpgradePostCheckPayload:\n" + payload);
115
116             assertEquals(expectedPayload, payload);
117         } catch (JsonProcessingException e) {
118             fail("Convert UpgradePostCheckPayload to String error: " + e.toString());
119         }
120     }
121
122     @Test
123     public final void testBuildUpgradePreCheckPayload() {
124         String expectedPayload = "{" + "\"ipaddress-v4-oam\":\"192.168.1.10\","
125                 + "\"playbook-name\":\"precheck_playbook\"," + "\"oldSwVersion\":\"v1\","
126                 + "\"targetSwVersion\":\"v2\"," + "\"ruleName\":\"r101\"," + "\"additionalData\":\"{}\"" + "}";
127
128         UpgradePreCheckPayload upgradePreCheckPayload = SDNCLcmPayloadBuilder.buildUpgradePreCheckPayload(execution);
129         try {
130             String payload = SDNCLcmPayloadBuilder.convertToSting(upgradePreCheckPayload);
131             logger.debug("UpgradePreCheckPayload:\n" + payload);
132
133             assertEquals(expectedPayload, payload);
134         } catch (JsonProcessingException e) {
135             fail("Convert UpgradePreCheckPayload to String error: " + e.toString());
136         }
137     }
138 }