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.Test;
32 import org.onap.appc.listener.LCM.model.DmaapIncomingMessage;
33 import org.onap.appc.listener.LCM.model.DmaapOutgoingMessage;
34 import org.onap.appc.listener.util.Mapper;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
37 public class ConverterTest {
39 private static final String jsonInputBodyStr =
40 "{\"input\":{ \"common-header\": { \"timestamp\": \"2016-08-03T08:50:18.97Z\", "
41 + "\"api-ver\": \"1\", \"originator-id\": \"1\", \"request-id\": \"123\", \"sub-request-id\": \"1\", "
42 + "\"flags\": { \"force\":\"TRUE\", \"ttl\":\"9900\" } }, \"action\": \"Stop\", "
43 + "\"action-identifiers\": { \"vnf-id\": \"TEST\" } }}";
45 private static final String jsonOutputBodyStr =
46 "{\"output\":{\"common-header\":{\"timestamp\":\"2016-08-03T08:50:18.97Z\","
47 + "\"api-ver\":\"1\",\"flags\":{\"force\":\"TRUE\",\"ttl\":\"9900\"},\"sub-request-id\":\"1\","
48 + "\"request-id\":\"123\",\"originator-id\":\"1\"},\"status\":{\"value\":\"TestException\",\"code\":200}}}";
50 private static final String expectedDmaapOutgoingMessageAsJsonString = "{\"body\":{\"output\":{\"common-header\":"
51 + "{\"timestamp\":\"2016-08-03T08:50:18.97Z\",\"api-ver\":\"1\",\"flags\":{\"force\":\"TRUE\",\"ttl\":\"9900\"},"
52 + "\"sub-request-id\":\"1\",\"request-id\":\"123\",\"originator-id\":\"1\"},\"status\":"
53 + "{\"value\":\"TestException\",\"code\":200}}},\"cambria.partition\":\"MSO\",\"rpc-name\":\"test\"}";
56 @Test(expected = IllegalArgumentException.class)
57 public void convertJsonNodeToDmaapOutgoingMessage_should_throw_when_given_null_arguments() {
59 Converter.convertJsonNodeToDmaapOutgoingMessage(null, null);
63 public void convertJsonNodeToDmaapOutgoingMessage_should_convert_to_outgoing_message() {
65 DmaapIncomingMessage message = new DmaapIncomingMessage();
66 message.setRpcName("test");
67 message.setCorrelationID("test-1");
68 message.setVersion("v1");
69 JsonNode jsonNode = Mapper.toJsonNodeFromJsonString(jsonInputBodyStr);
70 message.setBody(jsonNode);
72 DmaapOutgoingMessage result = Converter.convertJsonNodeToDmaapOutgoingMessage(message, jsonNode);
74 assertEquals("test", result.getRpcName());
75 assertEquals("test-1", result.getCorrelationID());
76 assertEquals("v1", result.getVersion());
77 assertEquals(jsonNode, result.getBody());
80 @Test(expected = IllegalArgumentException.class)
81 public void convertDmaapOutgoingMessageToJsonString_should_throw_when_given_null_arguments()
82 throws JsonProcessingException {
84 Converter.convertDmaapOutgoingMessageToJsonString(null);
88 public void convertDmaapOutgoingMessageToJsonString_should_return_converted_json_string()
89 throws JsonProcessingException {
91 DmaapOutgoingMessage message = new DmaapOutgoingMessage();
92 message.setRpcName("test");
93 JsonNode jsonNode = Mapper.toJsonNodeFromJsonString(jsonOutputBodyStr);
94 message.setBody(jsonNode);
96 assertEquals(expectedDmaapOutgoingMessageAsJsonString,
97 Converter.convertDmaapOutgoingMessageToJsonString(message));
100 @Test(expected = IllegalArgumentException.class)
101 public void buildDmaapOutgoingMessageWithUnexpectedErrorTest_should_throw_given_null_arguments()
102 throws JsonProcessingException {
104 Converter.buildDmaapOutgoingMessageWithUnexpectedError(null, null);
108 public void buildDmaapOutgoingMessageWithUnexpectedErrorTest_should_build_valid_outgoing_message()
109 throws JsonProcessingException {
111 DmaapIncomingMessage dmaapIncomingMessage = buildDmaapIncomingMessage();
112 String errMsg = "TestException";
113 DmaapOutgoingMessage dmaapOutgoingMessage = Converter
114 .buildDmaapOutgoingMessageWithUnexpectedError(dmaapIncomingMessage, new Exception(errMsg));
115 int code = dmaapOutgoingMessage.getBody().get("output").get("status").get("code").asInt();
116 String value = dmaapOutgoingMessage.getBody().get("output").get("status").get("value").asText();
117 assertEquals(200, code);
118 assertEquals(errMsg, value);
122 @Test(expected = IllegalArgumentException.class)
123 public void extractRequestIdWithSubId_should_throw_given_null_argument() throws SvcLogicException {
125 Converter.extractRequestIdWithSubId(null);
129 public void extractRequestIdWithSubIdTest_should_extract_id_with_subDd() throws SvcLogicException {
130 DmaapIncomingMessage dmaapIncomingMessage = buildDmaapIncomingMessage();
132 String requestIdWithSubId = Converter.extractRequestIdWithSubId(dmaapIncomingMessage.getBody());
133 assertEquals("123-1", requestIdWithSubId);
137 @Test(expected = IllegalArgumentException.class)
138 public void extractStatusCode_should_throw_given_null_argument() {
139 Converter.extractStatusCode(null);
144 public void extractStatusCode_should_extract_valid_status_code() {
145 DmaapOutgoingMessage dmaapOutgoingMessage = buildDmaapOutgoingMessage();
146 Integer statusCode = Converter.extractStatusCode(dmaapOutgoingMessage.getBody());
147 assertEquals(200L, statusCode.longValue());
150 private DmaapIncomingMessage buildDmaapIncomingMessage() {
151 DmaapIncomingMessage dmaapIncomingMessage = new DmaapIncomingMessage();
152 dmaapIncomingMessage.setRpcName("test");
153 JsonNode jsonNode = Mapper.toJsonNodeFromJsonString(jsonInputBodyStr);
154 dmaapIncomingMessage.setBody(jsonNode);
155 return dmaapIncomingMessage;
159 private DmaapOutgoingMessage buildDmaapOutgoingMessage() {
160 DmaapOutgoingMessage dmaapOutgoingMessage = new DmaapOutgoingMessage();
161 dmaapOutgoingMessage.setRpcName("test");
162 JsonNode jsonNode = Mapper.toJsonNodeFromJsonString(jsonOutputBodyStr);
163 dmaapOutgoingMessage.setBody(jsonNode);
164 return dmaapOutgoingMessage;