2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.listener.LCM.conv;
27 import static org.junit.Assert.assertEquals;
29 import com.fasterxml.jackson.core.JsonProcessingException;
30 import com.fasterxml.jackson.databind.JsonNode;
31 import org.junit.Assert;
32 import org.junit.Test;
33 import org.onap.appc.listener.LCM.model.DmaapIncomingMessage;
34 import org.onap.appc.listener.LCM.model.DmaapMessage;
35 import org.onap.appc.listener.LCM.model.DmaapOutgoingMessage;
36 import org.onap.appc.listener.demo.model.OutgoingMessage;
37 import org.onap.appc.listener.util.Mapper;
38 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
40 public class ConverterTest {
42 private static final String jsonInputBodyStr = "{\"input\":{ \"common-header\": { \"timestamp\": \"2016-08-03T08:50:18.97Z\", "
43 + "\"api-ver\": \"1\", \"originator-id\": \"1\", \"request-id\": \"123\", \"sub-request-id\": \"1\", "
44 + "\"flags\": { \"force\":\"TRUE\", \"ttl\":\"9900\" } }, \"action\": \"Stop\", "
45 + "\"action-identifiers\": { \"vnf-id\": \"TEST\" } }}";
47 private static final String jsonOutputBodyStr = "{\"output\":{\"common-header\":{\"timestamp\":\"2016-08-03T08:50:18.97Z\","
48 + "\"api-ver\":\"1\",\"flags\":{\"force\":\"TRUE\",\"ttl\":\"9900\"},\"sub-request-id\":\"1\","
49 + "\"request-id\":\"123\",\"originator-id\":\"1\"},\"status\":{\"value\":\"TestException\",\"code\":200}}}";
51 private static final String expectedDmaapOutgoingMessageAsJsonString = "{\"body\":{\"output\":{\"common-header\":"
52 + "{\"timestamp\":\"2016-08-03T08:50:18.97Z\",\"api-ver\":\"1\",\"flags\":{\"force\":\"TRUE\",\"ttl\":\"9900\"},"
53 + "\"sub-request-id\":\"1\",\"request-id\":\"123\",\"originator-id\":\"1\"},\"status\":"
54 + "{\"value\":\"TestException\",\"code\":200}}},\"cambria.partition\":\"MSO\",\"rpc-name\":\"test\"}";
57 @Test(expected = IllegalArgumentException.class)
58 public void convertJsonNodeToDmaapOutgoingMessage_should_throw_when_given_null_arguments() {
60 Converter.convertJsonNodeToDmaapOutgoingMessage(null, null);
64 public void convertJsonNodeToDmaapOutgoingMessage_should_convert_to_outgoing_message() {
66 DmaapIncomingMessage message = new DmaapIncomingMessage();
67 message.setRpcName("test");
68 message.setCorrelationID("test-1");
69 message.setVersion("v1");
70 JsonNode jsonNode = Mapper.toJsonNodeFromJsonString(jsonInputBodyStr);
71 message.setBody(jsonNode);
73 DmaapOutgoingMessage result = Converter.convertJsonNodeToDmaapOutgoingMessage(message, jsonNode);
75 assertEquals("test", result.getRpcName());
76 assertEquals("test-1", result.getCorrelationID());
77 assertEquals("v1", result.getVersion());
78 assertEquals(jsonNode, result.getBody());
81 @Test(expected = IllegalArgumentException.class)
82 public void convertDmaapOutgoingMessageToJsonString_should_throw_when_given_null_arguments()
83 throws JsonProcessingException {
85 Converter.convertDmaapOutgoingMessageToJsonString(null);
89 public void convertDmaapOutgoingMessageToJsonString_should_return_converted_json_string()
90 throws JsonProcessingException {
92 DmaapOutgoingMessage message = new DmaapOutgoingMessage();
93 message.setRpcName("test");
94 JsonNode jsonNode = Mapper.toJsonNodeFromJsonString(jsonOutputBodyStr);
95 message.setBody(jsonNode);
97 assertEquals(expectedDmaapOutgoingMessageAsJsonString,
98 Converter.convertDmaapOutgoingMessageToJsonString(message));
101 @Test(expected = IllegalArgumentException.class)
102 public void buildDmaapOutgoingMessageWithUnexpectedErrorTest_should_throw_given_null_arguments()
103 throws JsonProcessingException {
105 Converter.buildDmaapOutgoingMessageWithUnexpectedError(null, null);
109 public void buildDmaapOutgoingMessageWithUnexpectedErrorTest_should_build_valid_outgoing_message()
110 throws JsonProcessingException {
112 DmaapIncomingMessage dmaapIncomingMessage = buildDmaapIncomingMessage();
113 String errMsg = "TestException";
114 DmaapOutgoingMessage dmaapOutgoingMessage = Converter
115 .buildDmaapOutgoingMessageWithUnexpectedError(dmaapIncomingMessage, new Exception(errMsg));
116 int code = dmaapOutgoingMessage.getBody().get("output").get("status").get("code").asInt();
117 String value = dmaapOutgoingMessage.getBody().get("output").get("status").get("value").asText();
118 assertEquals(200, code);
119 assertEquals(errMsg, value);
123 @Test(expected = IllegalArgumentException.class)
124 public void extractRequestIdWithSubId_should_throw_given_null_argument() throws SvcLogicException {
126 Converter.extractRequestIdWithSubId(null);
130 public void extractRequestIdWithSubIdTest_should_extract_id_with_subDd() throws SvcLogicException {
131 DmaapIncomingMessage dmaapIncomingMessage = buildDmaapIncomingMessage();
133 String requestIdWithSubId = Converter.extractRequestIdWithSubId(dmaapIncomingMessage.getBody());
134 assertEquals("123-1", requestIdWithSubId);
139 @Test(expected = IllegalArgumentException.class)
140 public void extractStatusCode_should_throw_given_null_argument() {
141 Converter.extractStatusCode(null);
146 public void extractStatusCode_should_extract_valid_status_code() {
147 DmaapOutgoingMessage dmaapOutgoingMessage = buildDmaapOutgoingMessage();
148 Integer statusCode = Converter.extractStatusCode(dmaapOutgoingMessage.getBody());
149 assertEquals(200L, statusCode.longValue());
152 private DmaapIncomingMessage buildDmaapIncomingMessage() {
153 DmaapIncomingMessage dmaapIncomingMessage = new DmaapIncomingMessage();
154 dmaapIncomingMessage.setRpcName("test");
155 JsonNode jsonNode = Mapper.toJsonNodeFromJsonString(jsonInputBodyStr);
156 dmaapIncomingMessage.setBody(jsonNode);
157 return dmaapIncomingMessage;
161 private DmaapOutgoingMessage buildDmaapOutgoingMessage() {
162 DmaapOutgoingMessage dmaapOutgoingMessage = new DmaapOutgoingMessage();
163 dmaapOutgoingMessage.setRpcName("test");
164 JsonNode jsonNode = Mapper.toJsonNodeFromJsonString(jsonOutputBodyStr);
165 dmaapOutgoingMessage.setBody(jsonNode);
166 return dmaapOutgoingMessage;