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.listener.LCM;
24 import com.fasterxml.jackson.core.JsonProcessingException;
25 import com.fasterxml.jackson.databind.JsonNode;
26 import org.junit.Assert;
27 import org.junit.Test;
28 import org.openecomp.appc.listener.LCM.conv.Converter;
29 import org.openecomp.appc.listener.LCM.model.DmaapIncomingMessage;
30 import org.openecomp.appc.listener.LCM.model.DmaapOutgoingMessage;
31 import org.openecomp.appc.listener.util.Mapper;
33 public class TestConverter {
35 private String jsonInputBodyStr ="{\"input\":{ \"common-header\": { \"timestamp\": \"2016-08-03T08:50:18.97Z\", \"api-ver\": \"1\", \"originator-id\": \"1\", \"request-id\": \"123\", \"sub-request-id\": \"1\", \"flags\": { \"force\":\"TRUE\", \"ttl\":\"9900\" } }, \"action\": \"Stop\", \"action-identifiers\": { \"vnf-id\": \"TEST\" } }}";
36 private String jsonOutputBodyStr ="{\"output\":{\"common-header\":{\"timestamp\":\"2016-08-03T08:50:18.97Z\",\"api-ver\":\"1\",\"flags\":{\"force\":\"TRUE\",\"ttl\":\"9900\"},\"sub-request-id\":\"1\",\"request-id\":\"123\",\"originator-id\":\"1\"},\"status\":{\"value\":\"TestException\",\"code\":200}}}";
39 public void buildDmaapOutgoingMessageWithUnexpectedErrorTest() throws JsonProcessingException {
40 DmaapIncomingMessage dmaapIncomingMessage = buildDmaapIncomingMessage();
41 String errMsg = "TestException";
42 DmaapOutgoingMessage dmaapOutgoingMessage = Converter.buildDmaapOutgoingMessageWithUnexpectedError(dmaapIncomingMessage.getBody(), "test", new Exception(errMsg));
43 int code = dmaapOutgoingMessage.getBody().get("output").get("status").get("code").asInt();
44 String value = dmaapOutgoingMessage.getBody().get("output").get("status").get("value").asText();
45 Assert.assertEquals(200,code);
46 Assert.assertEquals(errMsg,value);
49 private static String expectedDmaapOutgoingMessageAsJsonString = "{\"body\":{\"output\":{\"common-header\":{\"timestamp\":\"2016-08-03T08:50:18.97Z\",\"api-ver\":\"1\",\"flags\":{\"force\":\"TRUE\",\"ttl\":\"9900\"},\"sub-request-id\":\"1\",\"request-id\":\"123\",\"originator-id\":\"1\"},\"status\":{\"value\":\"TestException\",\"code\":200}}},\"cambria.partition\":\"MSO\",\"rpc-name\":\"test\"}";
51 public void convDmaapOutgoingMessageToJsonStringTest() throws JsonProcessingException {
52 DmaapOutgoingMessage dmaapOutgoingMessage = buildDmaapOutgoingMessage();
53 String dmaapOutgoingMessageAsJsonString = Converter.convDmaapOutgoingMessageToJsonString(dmaapOutgoingMessage);
54 // Assert.assertEquals(dmaapOutgoingMessageAsJsonString,dmaapOutgoingMessageAsJsonString);
55 Assert.assertEquals(expectedDmaapOutgoingMessageAsJsonString,dmaapOutgoingMessageAsJsonString);
58 private DmaapIncomingMessage buildDmaapIncomingMessage() {
59 DmaapIncomingMessage dmaapIncomingMessage = new DmaapIncomingMessage();
60 dmaapIncomingMessage.setRpcName("test");
61 JsonNode jsonNode = Mapper.toJsonNodeFromJsonString(jsonInputBodyStr);
62 dmaapIncomingMessage.setBody(jsonNode);
63 return dmaapIncomingMessage;
67 private DmaapOutgoingMessage buildDmaapOutgoingMessage() {
68 DmaapOutgoingMessage dmaapOutgoingMessage = new DmaapOutgoingMessage();
69 dmaapOutgoingMessage.setRpcName("test");
70 JsonNode jsonNode = Mapper.toJsonNodeFromJsonString(jsonOutputBodyStr);
71 dmaapOutgoingMessage.setBody(jsonNode);
72 return dmaapOutgoingMessage;
78 public void extractRequestIdWithSubIdTest() {
79 DmaapIncomingMessage dmaapIncomingMessage = buildDmaapIncomingMessage();
80 String equestIdWithSubId = Converter.extractRequestIdWithSubId(dmaapIncomingMessage.getBody());
81 Assert.assertEquals("123-1",equestIdWithSubId);
85 public void extractStatusCodeTest() {
86 DmaapOutgoingMessage dmaapOutgoingMessage = buildDmaapOutgoingMessage();
87 Integer statusCode = Converter.extractStatusCode(dmaapOutgoingMessage.getBody());
88 Assert.assertEquals(200L,statusCode.longValue());