2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Amdocs
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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.appc.listener.LCM;
25 import com.fasterxml.jackson.core.JsonProcessingException;
26 import com.fasterxml.jackson.databind.JsonNode;
27 import org.junit.Assert;
28 import org.junit.Test;
29 import org.openecomp.appc.listener.LCM.conv.Converter;
30 import org.openecomp.appc.listener.LCM.model.DmaapIncomingMessage;
31 import org.openecomp.appc.listener.LCM.model.DmaapOutgoingMessage;
32 import org.openecomp.appc.listener.util.Mapper;
34 public class TestConverter {
36 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\" } }}";
37 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}}}";
40 public void buildDmaapOutgoingMessageWithUnexpectedErrorTest() throws JsonProcessingException {
41 DmaapIncomingMessage dmaapIncomingMessage = buildDmaapIncomingMessage();
42 String errMsg = "TestException";
43 DmaapOutgoingMessage dmaapOutgoingMessage = Converter.buildDmaapOutgoingMessageWithUnexpectedError(dmaapIncomingMessage, new Exception(errMsg));
44 int code = dmaapOutgoingMessage.getBody().get("output").get("status").get("code").asInt();
45 String value = dmaapOutgoingMessage.getBody().get("output").get("status").get("value").asText();
46 Assert.assertEquals(200,code);
47 Assert.assertEquals(errMsg,value);
50 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\"}";
52 public void convDmaapOutgoingMessageToJsonStringTest() throws JsonProcessingException {
53 DmaapOutgoingMessage dmaapOutgoingMessage = buildDmaapOutgoingMessage();
54 String dmaapOutgoingMessageAsJsonString = Converter.convDmaapOutgoingMessageToJsonString(dmaapOutgoingMessage);
55 // Assert.assertEquals(dmaapOutgoingMessageAsJsonString,dmaapOutgoingMessageAsJsonString);
56 Assert.assertEquals(expectedDmaapOutgoingMessageAsJsonString,dmaapOutgoingMessageAsJsonString);
59 private DmaapIncomingMessage buildDmaapIncomingMessage() {
60 DmaapIncomingMessage dmaapIncomingMessage = new DmaapIncomingMessage();
61 dmaapIncomingMessage.setRpcName("test");
62 JsonNode jsonNode = Mapper.toJsonNodeFromJsonString(jsonInputBodyStr);
63 dmaapIncomingMessage.setBody(jsonNode);
64 return dmaapIncomingMessage;
68 private DmaapOutgoingMessage buildDmaapOutgoingMessage() {
69 DmaapOutgoingMessage dmaapOutgoingMessage = new DmaapOutgoingMessage();
70 dmaapOutgoingMessage.setRpcName("test");
71 JsonNode jsonNode = Mapper.toJsonNodeFromJsonString(jsonOutputBodyStr);
72 dmaapOutgoingMessage.setBody(jsonNode);
73 return dmaapOutgoingMessage;
79 public void extractRequestIdWithSubIdTest() {
80 DmaapIncomingMessage dmaapIncomingMessage = buildDmaapIncomingMessage();
81 String equestIdWithSubId = Converter.extractRequestIdWithSubId(dmaapIncomingMessage.getBody());
82 Assert.assertEquals("123-1",equestIdWithSubId);
86 public void extractStatusCodeTest() {
87 DmaapOutgoingMessage dmaapOutgoingMessage = buildDmaapOutgoingMessage();
88 Integer statusCode = Converter.extractStatusCode(dmaapOutgoingMessage.getBody());
89 Assert.assertEquals(200L,statusCode.longValue());