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;
28 import static org.onap.appc.listener.TestUtil.JSON_INPUT_BODY_STR;
29 import static org.onap.appc.listener.TestUtil.JSON_OUTPUT_BODY_STR;
30 import static org.onap.appc.listener.TestUtil.buildDmaapIncomingMessage;
31 import static org.onap.appc.listener.TestUtil.buildDmaapOutgoingMessage;
33 import com.fasterxml.jackson.core.JsonProcessingException;
34 import com.fasterxml.jackson.databind.JsonNode;
35 import org.junit.Test;
36 import org.onap.appc.listener.LCM.model.DmaapIncomingMessage;
37 import org.onap.appc.listener.LCM.model.DmaapOutgoingMessage;
38 import org.onap.appc.listener.util.Mapper;
39 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
41 public class ConverterTest {
43 private static final String EXPECTED_DMAAP_OUTGOING_MESSAGE_AS_JSON_STRING =
44 "{\"body\":{\"output\":{\"common-header\":"
45 + "{\"timestamp\":\"2016-08-03T08:50:18.97Z\",\"api-ver\":\"1\",\"flags\":{\"force\":\"TRUE\",\"ttl\":\"9900\"},"
46 + "\"sub-request-id\":\"1\",\"request-id\":\"123\",\"originator-id\":\"1\"},\"locked\":\"test-locked\",\""
47 + "status\":{\"message\":\"test message\",\"code\":200}}},\"cambria.partition\":\"MSO\",\"rpc-name\":\"test\"}";
49 @Test(expected = IllegalArgumentException.class)
50 public void convertJsonNodeToDmaapOutgoingMessage_should_throw_when_given_null_arguments() {
52 Converter.convertJsonNodeToDmaapOutgoingMessage(null, null);
56 public void convertJsonNodeToDmaapOutgoingMessage_should_convert_to_outgoing_message() {
58 DmaapIncomingMessage message = new DmaapIncomingMessage();
59 message.setRpcName("test");
60 message.setCorrelationID("test-1");
61 message.setVersion("v1");
62 JsonNode jsonNode = Mapper.toJsonNodeFromJsonString(JSON_INPUT_BODY_STR);
63 message.setBody(jsonNode);
65 DmaapOutgoingMessage result = Converter.convertJsonNodeToDmaapOutgoingMessage(message, jsonNode);
67 assertEquals("test", result.getRpcName());
68 assertEquals("test-1", result.getCorrelationID());
69 assertEquals("v1", result.getVersion());
70 assertEquals(jsonNode, result.getBody());
73 @Test(expected = IllegalArgumentException.class)
74 public void convertDmaapOutgoingMessageToJsonString_should_throw_when_given_null_arguments()
75 throws JsonProcessingException {
77 Converter.convertDmaapOutgoingMessageToJsonString(null);
81 public void convertDmaapOutgoingMessageToJsonString_should_return_converted_json_string()
82 throws JsonProcessingException {
84 DmaapOutgoingMessage message = new DmaapOutgoingMessage();
85 message.setRpcName("test");
86 JsonNode jsonNode = Mapper.toJsonNodeFromJsonString(JSON_OUTPUT_BODY_STR);
87 message.setBody(jsonNode);
89 assertEquals(EXPECTED_DMAAP_OUTGOING_MESSAGE_AS_JSON_STRING,
90 Converter.convertDmaapOutgoingMessageToJsonString(message));
93 @Test(expected = IllegalArgumentException.class)
94 public void buildDmaapOutgoingMessageWithUnexpectedErrorTest_should_throw_given_null_arguments()
95 throws JsonProcessingException {
97 Converter.buildDmaapOutgoingMessageWithUnexpectedError(null, null);
101 public void buildDmaapOutgoingMessageWithUnexpectedErrorTest_should_build_valid_outgoing_message()
102 throws JsonProcessingException {
104 DmaapIncomingMessage dmaapIncomingMessage = buildDmaapIncomingMessage();
105 String errMsg = "TestException";
106 DmaapOutgoingMessage dmaapOutgoingMessage = Converter
107 .buildDmaapOutgoingMessageWithUnexpectedError(dmaapIncomingMessage, new Exception(errMsg));
108 int code = dmaapOutgoingMessage.getBody().get("output").get("status").get("code").asInt();
109 String value = dmaapOutgoingMessage.getBody().get("output").get("status").get("value").asText();
110 assertEquals(200, code);
111 assertEquals(errMsg, value);
115 @Test(expected = IllegalArgumentException.class)
116 public void extractRequestIdWithSubId_should_throw_given_null_argument() throws SvcLogicException {
118 Converter.extractRequestIdWithSubId(null);
122 public void extractRequestIdWithSubIdTest_should_extract_id_with_subDd() throws SvcLogicException {
123 DmaapIncomingMessage dmaapIncomingMessage = buildDmaapIncomingMessage();
125 String requestIdWithSubId = Converter.extractRequestIdWithSubId(dmaapIncomingMessage.getBody());
126 assertEquals("123-1", requestIdWithSubId);
130 @Test(expected = IllegalArgumentException.class)
131 public void extractStatusCode_should_throw_given_null_argument() {
132 Converter.extractStatusCode(null);
137 public void extractStatusCode_should_extract_valid_status_code() {
138 DmaapOutgoingMessage dmaapOutgoingMessage = buildDmaapOutgoingMessage();
139 Integer statusCode = Converter.extractStatusCode(dmaapOutgoingMessage.getBody());
140 assertEquals(200L, statusCode.longValue());