2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.openecomp.appc.requesthandler;
24 import com.fasterxml.jackson.core.JsonProcessingException;
25 import org.junit.Assert;
26 import org.junit.Test;
27 import org.openecomp.appc.domainmodel.lcm.*;
28 import org.openecomp.appc.executor.objects.LCMCommandStatus;
29 import org.openecomp.appc.requesthandler.conv.Converter;
31 import javax.ws.rs.container.AsyncResponse;
32 import java.text.ParseException;
33 import java.util.Date;
34 import java.util.HashMap;
37 public class TestConverter {
38 private String expectedJsonBodyStr ="{\"output\":{\"common-header\":{\"api-ver\":\"2.0.0\",\"flags\":{},\"originator-id\":\"oid\",\"request-id\":\"reqid\",\"timestamp\":\"1970-01-01T00:00:01.000Z\"},\"status\":{\"code\":400,\"message\":\"SUCCESS - request has been processed successfully\"}}}";
39 private String expectedDmaapOutgoingMessageJsonStringTest ="{\"body\":{\"output\":{\"common-header\":{\"api-ver\":\"2.0.0\",\"flags\":{},\"originator-id\":\"oid\",\"request-id\":\"reqid\",\"timestamp\":\"1970-01-01T00:00:01.000Z\"},\"status\":{\"code\":400,\"message\":\"SUCCESS - request has been processed successfully\"}}},\"cambria.partition\":\"MSO\",\"rpc-name\":\"test\"}";
40 private String expectedDmaapOutgoingMessageJsonStringRollback ="{\"body\":{\"output\":{\"common-header\":{\"api-ver\":\"2.0.0\",\"flags\":{},\"originator-id\":\"oid\",\"request-id\":\"reqid\",\"timestamp\":\"1970-01-01T00:00:01.000Z\"},\"status\":{\"code\":400,\"message\":\"SUCCESS - request has been processed successfully\"}}},\"cambria.partition\":\"MSO\",\"rpc-name\":\"rollback\"}";
41 private String expectedDmaapOutgoingMessageJsonStringSnapshot ="{\"body\":{\"output\":{\"common-header\":{\"api-ver\":\"2.0.0\",\"flags\":{},\"originator-id\":\"oid\",\"request-id\":\"reqid\",\"timestamp\":\"1970-01-01T00:00:01.000Z\"},\"status\":{\"code\":400,\"message\":\"SUCCESS - request has been processed successfully\"}}},\"cambria.partition\":\"MSO\",\"rpc-name\":\"snapshot\"}";
42 private String expectedDmaapOutgoingMessageJsonStringAudit ="{\"body\":{\"output\":{\"common-header\":{\"api-ver\":\"2.0.0\",\"flags\":{},\"originator-id\":\"oid\",\"request-id\":\"reqid\",\"timestamp\":\"1970-01-01T00:00:01.000Z\"},\"status\":{\"code\":400,\"message\":\"SUCCESS - request has been processed successfully\"}}},\"cambria.partition\":\"MSO\",\"rpc-name\":\"audit\"}";
43 private String expectedDmaapOutgoingMessageJsonStringHealthCheck ="{\"body\":{\"output\":{\"common-header\":{\"api-ver\":\"2.0.0\",\"flags\":{},\"originator-id\":\"oid\",\"request-id\":\"reqid\",\"timestamp\":\"1970-01-01T00:00:01.000Z\"},\"status\":{\"code\":400,\"message\":\"SUCCESS - request has been processed successfully\"}}},\"cambria.partition\":\"MSO\",\"rpc-name\":\"health-check\"}";
44 private String expectedDmaapOutgoingMessageJsonStringLiveUpgrade ="{\"body\":{\"output\":{\"common-header\":{\"api-ver\":\"2.0.0\",\"flags\":{},\"originator-id\":\"oid\",\"request-id\":\"reqid\",\"timestamp\":\"1970-01-01T00:00:01.000Z\"},\"status\":{\"code\":400,\"message\":\"SUCCESS - request has been processed successfully\"}}},\"cambria.partition\":\"MSO\",\"rpc-name\":\"live-upgrade\"}";
45 private String expectedDmaapOutgoingMessageJsonStringLock ="{\"body\":{\"output\":{\"common-header\":{\"api-ver\":\"2.0.0\",\"flags\":{},\"originator-id\":\"oid\",\"request-id\":\"reqid\",\"timestamp\":\"1970-01-01T00:00:01.000Z\"},\"status\":{\"code\":400,\"message\":\"SUCCESS - request has been processed successfully\"}}},\"cambria.partition\":\"MSO\",\"rpc-name\":\"lock\"}";
46 private String expectedDmaapOutgoingMessageJsonStringModifyConfig ="{\"body\":{\"output\":{\"common-header\":{\"api-ver\":\"2.0.0\",\"flags\":{},\"originator-id\":\"oid\",\"request-id\":\"reqid\",\"timestamp\":\"1970-01-01T00:00:01.000Z\"},\"status\":{\"code\":400,\"message\":\"SUCCESS - request has been processed successfully\"}}},\"cambria.partition\":\"MSO\",\"rpc-name\":\"modify-config\"}";
47 private String expectedDmaapOutgoingMessageJsonStringSoftwareUpload ="{\"body\":{\"output\":{\"common-header\":{\"api-ver\":\"2.0.0\",\"flags\":{},\"originator-id\":\"oid\",\"request-id\":\"reqid\",\"timestamp\":\"1970-01-01T00:00:01.000Z\"},\"status\":{\"code\":400,\"message\":\"SUCCESS - request has been processed successfully\"}}},\"cambria.partition\":\"MSO\",\"rpc-name\":\"software-upload\"}";
48 private String expectedDmaapOutgoingMessageJsonStringStop ="{\"body\":{\"output\":{\"common-header\":{\"api-ver\":\"2.0.0\",\"flags\":{},\"originator-id\":\"oid\",\"request-id\":\"reqid\",\"timestamp\":\"1970-01-01T00:00:01.000Z\"},\"status\":{\"code\":400,\"message\":\"SUCCESS - request has been processed successfully\"}}},\"cambria.partition\":\"MSO\",\"rpc-name\":\"stop\"}";
49 private String expectedDmaapOutgoingMessageJsonStringSync ="{\"body\":{\"output\":{\"common-header\":{\"api-ver\":\"2.0.0\",\"flags\":{},\"originator-id\":\"oid\",\"request-id\":\"reqid\",\"timestamp\":\"1970-01-01T00:00:01.000Z\"},\"status\":{\"code\":400,\"message\":\"SUCCESS - request has been processed successfully\"}}},\"cambria.partition\":\"MSO\",\"rpc-name\":\"sync\"}";
50 private String expectedDmaapOutgoingMessageJsonStringTerminate ="{\"body\":{\"output\":{\"common-header\":{\"api-ver\":\"2.0.0\",\"flags\":{},\"originator-id\":\"oid\",\"request-id\":\"reqid\",\"timestamp\":\"1970-01-01T00:00:01.000Z\"},\"status\":{\"code\":400,\"message\":\"SUCCESS - request has been processed successfully\"}}},\"cambria.partition\":\"MSO\",\"rpc-name\":\"terminate\"}";
51 private String expectedDmaapOutgoingMessageJsonStringUnlock ="{\"body\":{\"output\":{\"common-header\":{\"api-ver\":\"2.0.0\",\"flags\":{},\"originator-id\":\"oid\",\"request-id\":\"reqid\",\"timestamp\":\"1970-01-01T00:00:01.000Z\"},\"status\":{\"code\":400,\"message\":\"SUCCESS - request has been processed successfully\"}}},\"cambria.partition\":\"MSO\",\"rpc-name\":\"unlock\"}";
54 public void convDateToZuluStringTest(){
55 String dateToZuluString = Converter.convDateToZuluString(new Date(0L));
56 Assert.assertEquals("1970-01-01T00:00:00.000Z", dateToZuluString);
60 public void convAsyncResponseToBuilderTestTest() throws JsonProcessingException {
61 ResponseContext asyncResponse = buildAsyncResponse();
62 VNFOperation action = VNFOperation.Test;
63 String rpcName = action.name().toLowerCase();
64 String jsonStr = Converter.convAsyncResponseToJsonStringBody(action, rpcName, asyncResponse);
65 Assert.assertEquals(expectedJsonBodyStr,jsonStr);
69 public void convAsyncResponseToDmaapOutgoingMessageJsonStringTestTest() throws JsonProcessingException {
70 ResponseContext asyncResponse = buildAsyncResponse();
71 VNFOperation action = VNFOperation.Test;
72 String rpcName = action.name().toLowerCase();
73 String jsonStr = Converter.convAsyncResponseToDmaapOutgoingMessageJsonString(action, rpcName, asyncResponse);
74 System.out.println("jsonStr = " + jsonStr);
75 Assert.assertEquals(expectedDmaapOutgoingMessageJsonStringTest,jsonStr);
79 public void convPayloadObjectToJsonStringTest() throws JsonProcessingException, ParseException {
80 String jsonString = Converter.convPayloadObjectToJsonString("any valid JSON string value");
81 Assert.assertEquals("any valid JSON string value", jsonString);
83 HashMap<String, String> hashMap = new HashMap<>();
84 hashMap.put("key","value");
85 jsonString = Converter.convPayloadObjectToJsonString(hashMap);
86 Assert.assertEquals("{\"key\":\"value\"}", jsonString);
90 public void convAsyncResponseToBuilderRollbackTest() throws JsonProcessingException {
91 ResponseContext asyncResponse = buildAsyncResponse();
92 VNFOperation action = VNFOperation.Rollback;
93 String rpcName = action.name().toLowerCase();
94 String jsonStr = Converter.convAsyncResponseToJsonStringBody(action, rpcName, asyncResponse);
95 Assert.assertEquals(expectedJsonBodyStr,jsonStr);
99 public void convAsyncResponseToDmaapOutgoingMessageJsonStringRollbackTest() throws JsonProcessingException {
100 ResponseContext asyncResponse = buildAsyncResponse();
101 VNFOperation action = VNFOperation.Rollback;
102 String rpcName = action.name().toLowerCase();
103 String jsonStr = Converter.convAsyncResponseToDmaapOutgoingMessageJsonString(action, rpcName, asyncResponse);
104 System.out.println("jsonStr = " + jsonStr);
105 Assert.assertEquals(expectedDmaapOutgoingMessageJsonStringRollback,jsonStr);
109 public void convAsyncResponseToBuilderSnapshotTest() throws JsonProcessingException {
110 ResponseContext asyncResponse = buildAsyncResponse();
111 VNFOperation action = VNFOperation.Snapshot;
112 String rpcName = action.name().toLowerCase();
113 String jsonStr = Converter.convAsyncResponseToJsonStringBody(action, rpcName, asyncResponse);
114 Assert.assertEquals(expectedJsonBodyStr,jsonStr);
118 public void convAsyncResponseToDmaapOutgoingMessageJsonStringSnapshotTest() throws JsonProcessingException {
119 ResponseContext asyncResponse = buildAsyncResponse();
120 VNFOperation action = VNFOperation.Snapshot;
121 String rpcName = action.name().toLowerCase();
122 String jsonStr = Converter.convAsyncResponseToDmaapOutgoingMessageJsonString(action, rpcName, asyncResponse);
123 System.out.println("jsonStr = " + jsonStr);
124 Assert.assertEquals(expectedDmaapOutgoingMessageJsonStringSnapshot,jsonStr);
127 public void convAsyncResponseToBuilderAuditTest() throws JsonProcessingException {
128 ResponseContext asyncResponse = buildAsyncResponse();
129 VNFOperation action = VNFOperation.Audit;
130 String rpcName = action.name().toLowerCase();
131 String jsonStr = Converter.convAsyncResponseToJsonStringBody(action, rpcName, asyncResponse);
132 Assert.assertEquals(expectedJsonBodyStr,jsonStr);
136 public void convAsyncResponseToDmaapOutgoingMessageJsonStringAuditTest() throws JsonProcessingException {
137 ResponseContext asyncResponse = buildAsyncResponse();
138 VNFOperation action = VNFOperation.Audit;
139 String rpcName = action.name().toLowerCase();
140 String jsonStr = Converter.convAsyncResponseToDmaapOutgoingMessageJsonString(action, rpcName, asyncResponse);
141 System.out.println("jsonStr = " + jsonStr);
142 Assert.assertEquals(expectedDmaapOutgoingMessageJsonStringAudit,jsonStr);
145 public void convAsyncResponseToBuilderHealthCheckTest() throws JsonProcessingException {
146 ResponseContext asyncResponse = buildAsyncResponse();
147 VNFOperation action = VNFOperation.HealthCheck;
148 String rpcName = convertActionNameToUrl(action.name());
149 String jsonStr = Converter.convAsyncResponseToJsonStringBody(action, rpcName, asyncResponse);
150 Assert.assertEquals(expectedJsonBodyStr,jsonStr);
154 public void convAsyncResponseToDmaapOutgoingMessageJsonStringHealthCheckTest() throws JsonProcessingException {
155 ResponseContext asyncResponse = buildAsyncResponse();
156 VNFOperation action = VNFOperation.HealthCheck;
157 String rpcName = convertActionNameToUrl(action.name());
158 String jsonStr = Converter.convAsyncResponseToDmaapOutgoingMessageJsonString(action, rpcName, asyncResponse);
159 System.out.println("jsonStr = " + jsonStr);
160 Assert.assertEquals(expectedDmaapOutgoingMessageJsonStringHealthCheck,jsonStr);
163 public void convAsyncResponseToBuilderLiveUpgradeTest() throws JsonProcessingException {
164 ResponseContext asyncResponse = buildAsyncResponse();
165 VNFOperation action = VNFOperation.LiveUpgrade;
166 String rpcName = convertActionNameToUrl(action.name());
167 String jsonStr = Converter.convAsyncResponseToJsonStringBody(action, rpcName, asyncResponse);
168 Assert.assertEquals(expectedJsonBodyStr,jsonStr);
172 public void convAsyncResponseToDmaapOutgoingMessageJsonStringLiveUpgradeTest() throws JsonProcessingException {
173 ResponseContext asyncResponse = buildAsyncResponse();
174 VNFOperation action = VNFOperation.LiveUpgrade;
175 String rpcName = convertActionNameToUrl(action.name());
176 String jsonStr = Converter.convAsyncResponseToDmaapOutgoingMessageJsonString(action, rpcName, asyncResponse);
177 System.out.println("jsonStr = " + jsonStr);
178 Assert.assertEquals(expectedDmaapOutgoingMessageJsonStringLiveUpgrade,jsonStr);
181 public void convAsyncResponseToBuilderLockTest() throws JsonProcessingException {
182 ResponseContext asyncResponse = buildAsyncResponse();
183 VNFOperation action = VNFOperation.Lock;
184 String rpcName = convertActionNameToUrl(action.name());
186 String jsonStr = Converter.convAsyncResponseToJsonStringBody(action, rpcName, asyncResponse);
187 Assert.assertEquals(expectedJsonBodyStr,jsonStr);
191 public void convAsyncResponseToDmaapOutgoingMessageJsonStringLockTest() throws JsonProcessingException {
192 ResponseContext asyncResponse = buildAsyncResponse();
193 VNFOperation action = VNFOperation.Lock;
194 String rpcName = convertActionNameToUrl(action.name());
195 String jsonStr = Converter.convAsyncResponseToDmaapOutgoingMessageJsonString(action, rpcName, asyncResponse);
196 System.out.println("jsonStr = " + jsonStr);
197 Assert.assertEquals(expectedDmaapOutgoingMessageJsonStringLock,jsonStr);
200 public void convAsyncResponseToBuilderModifyConfigTest() throws JsonProcessingException {
201 ResponseContext asyncResponse = buildAsyncResponse();
202 VNFOperation action = VNFOperation.ModifyConfig;
203 String rpcName = convertActionNameToUrl(action.name());
204 String jsonStr = Converter.convAsyncResponseToJsonStringBody(action, rpcName, asyncResponse);
205 Assert.assertEquals(expectedJsonBodyStr,jsonStr);
209 public void convAsyncResponseToDmaapOutgoingMessageJsonStringModifyConfigTest() throws JsonProcessingException {
210 ResponseContext asyncResponse = buildAsyncResponse();
211 VNFOperation action = VNFOperation.ModifyConfig;
212 String rpcName = convertActionNameToUrl(action.name());
213 String jsonStr = Converter.convAsyncResponseToDmaapOutgoingMessageJsonString(action, rpcName, asyncResponse);
214 System.out.println("jsonStr = " + jsonStr);
215 Assert.assertEquals(expectedDmaapOutgoingMessageJsonStringModifyConfig,jsonStr);
218 public void convAsyncResponseToBuilderSoftwareUploadTest() throws JsonProcessingException {
219 ResponseContext asyncResponse = buildAsyncResponse();
220 VNFOperation action = VNFOperation.SoftwareUpload;
221 String rpcName = convertActionNameToUrl(action.name());
223 String jsonStr = Converter.convAsyncResponseToJsonStringBody(action, rpcName, asyncResponse);
224 Assert.assertEquals(expectedJsonBodyStr,jsonStr);
228 public void convAsyncResponseToDmaapOutgoingMessageJsonStringSoftwareUploadTest() throws JsonProcessingException {
229 ResponseContext asyncResponse = buildAsyncResponse();
230 VNFOperation action = VNFOperation.SoftwareUpload;
231 String rpcName = convertActionNameToUrl(action.name());
232 String jsonStr = Converter.convAsyncResponseToDmaapOutgoingMessageJsonString(action, rpcName, asyncResponse);
233 System.out.println("jsonStr = " + jsonStr);
234 Assert.assertEquals(expectedDmaapOutgoingMessageJsonStringSoftwareUpload,jsonStr);
237 public void convAsyncResponseToBuilderStopTest() throws JsonProcessingException {
238 ResponseContext asyncResponse = buildAsyncResponse();
239 VNFOperation action = VNFOperation.Stop;
240 String rpcName = convertActionNameToUrl(action.name());
241 String jsonStr = Converter.convAsyncResponseToJsonStringBody(action, rpcName, asyncResponse);
242 Assert.assertEquals(expectedJsonBodyStr,jsonStr);
246 public void convAsyncResponseToDmaapOutgoingMessageJsonStringStopTest() throws JsonProcessingException {
247 ResponseContext asyncResponse = buildAsyncResponse();
248 VNFOperation action = VNFOperation.Stop;
249 String rpcName = convertActionNameToUrl(action.name());
250 String jsonStr = Converter.convAsyncResponseToDmaapOutgoingMessageJsonString(action, rpcName, asyncResponse);
251 System.out.println("jsonStr = " + jsonStr);
252 Assert.assertEquals(expectedDmaapOutgoingMessageJsonStringStop,jsonStr);
255 public void convAsyncResponseToBuilderSync() throws JsonProcessingException {
256 ResponseContext asyncResponse = buildAsyncResponse();
257 VNFOperation action = VNFOperation.Sync;
258 String rpcName = convertActionNameToUrl(action.name());
260 String jsonStr = Converter.convAsyncResponseToJsonStringBody(action, rpcName, asyncResponse);
261 Assert.assertEquals(expectedJsonBodyStr,jsonStr);
265 public void convAsyncResponseToDmaapOutgoingMessageJsonStringSync() throws JsonProcessingException {
266 ResponseContext asyncResponse = buildAsyncResponse();
267 VNFOperation action = VNFOperation.Sync;
268 String rpcName = convertActionNameToUrl(action.name());
269 String jsonStr = Converter.convAsyncResponseToDmaapOutgoingMessageJsonString(action, rpcName, asyncResponse);
270 System.out.println("jsonStr = " + jsonStr);
271 Assert.assertEquals(expectedDmaapOutgoingMessageJsonStringSync,jsonStr);
274 public void convAsyncResponseToBuilderTerminateTest() throws JsonProcessingException {
275 ResponseContext asyncResponse = buildAsyncResponse();
276 VNFOperation action = VNFOperation.Sync;
277 String rpcName = convertActionNameToUrl(action.name());
278 String jsonStr = Converter.convAsyncResponseToJsonStringBody(action, rpcName, asyncResponse);
279 Assert.assertEquals(expectedJsonBodyStr,jsonStr);
283 public void convAsyncResponseToDmaapOutgoingMessageJsonStringTerminateTest() throws JsonProcessingException {
284 ResponseContext asyncResponse = buildAsyncResponse();
285 VNFOperation action = VNFOperation.Terminate;
286 String rpcName = convertActionNameToUrl(action.name());
287 String jsonStr = Converter.convAsyncResponseToDmaapOutgoingMessageJsonString(action, rpcName, asyncResponse);
288 System.out.println("jsonStr = " + jsonStr);
289 Assert.assertEquals(expectedDmaapOutgoingMessageJsonStringTerminate,jsonStr);
292 public void convAsyncResponseToBuilderUnlockTest() throws JsonProcessingException {
293 ResponseContext asyncResponse = buildAsyncResponse();
294 VNFOperation action = VNFOperation.Unlock;
295 String rpcName = convertActionNameToUrl(action.name());
296 String jsonStr = Converter.convAsyncResponseToJsonStringBody(action, rpcName, asyncResponse);
297 Assert.assertEquals(expectedJsonBodyStr,jsonStr);
301 public void convAsyncResponseToDmaapOutgoingMessageJsonStringUnlockTest() throws JsonProcessingException {
302 ResponseContext asyncResponse = buildAsyncResponse();
303 VNFOperation action = VNFOperation.Unlock;
304 String rpcName = convertActionNameToUrl(action.name());
305 String jsonStr = Converter.convAsyncResponseToDmaapOutgoingMessageJsonString(action, rpcName, asyncResponse);
306 System.out.println("jsonStr = " + jsonStr);
307 Assert.assertEquals(expectedDmaapOutgoingMessageJsonStringUnlock,jsonStr);
310 public void convAsyncResponseToBuilderTest() throws JsonProcessingException {
311 AsyncResponse asyncResponse = buildAsyncResponse();
312 String jsonStr = Converter.convAsyncResponseToJsonStringBody(asyncResponse);
313 Assert.assertEquals(expectedJsonBodyStr,jsonStr);
317 public void convAsyncResponseToDmaapOutgoingMessageJsonStringTest() throws JsonProcessingException {
318 AsyncResponse asyncResponse = buildAsyncResponse();
319 String jsonStr = Converter.convAsyncResponseToDmaapOutgoingMessageJsonString(asyncResponse);
320 System.out.println("jsonStr = " + jsonStr);
321 Assert.assertEquals(expectedDmaapOutgoingMessageJsonString,jsonStr);
325 private ResponseContext buildAsyncResponse() {
326 ResponseContext asyncResponse = createResponseContextWithSubObjects();
327 asyncResponse.getStatus().setCode(LCMCommandStatus.SUCCESS.getResponseCode());
328 asyncResponse.getStatus().setMessage(LCMCommandStatus.SUCCESS.getResponseMessage());
329 asyncResponse.getCommonHeader().setOriginatorId("oid");
330 asyncResponse.getCommonHeader().setApiVer("2.0.0");
331 asyncResponse.getCommonHeader().setRequestId("reqid");
332 asyncResponse.getCommonHeader().setTimestamp(new Date(1000L));
333 asyncResponse.setPayload("any valid JSON string value. Json escape characters need to be added to make it a valid json string value");
334 return asyncResponse;
337 private ResponseContext createResponseContextWithSubObjects() {
339 ResponseContext responseContext = new ResponseContext();
340 CommonHeader commonHeader = new CommonHeader();
341 Flags flags = new Flags();
342 Status status = new Status();
343 responseContext.setCommonHeader(commonHeader);
344 responseContext.setStatus(status);
345 commonHeader.setFlags(flags);
346 return responseContext;
349 private String convertActionNameToUrl(String action) {
350 String regex = "([a-z])([A-Z]+)";
351 String replacement = "$1-$2";
352 return action.replaceAll(regex, replacement)