f51a6a4d7aab47917141234cc30e9c8cb709ce5c
[appc.git] / appc-event-listener / appc-event-listener-bundle / src / test / java / org / onap / appc / listener / LCM / conv / ConverterTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.listener.LCM.conv;
26
27 import static org.junit.Assert.assertEquals;
28
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;
39
40 public class ConverterTest {
41
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\" } }}";
46
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}}}";
50
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\"}";
55
56
57     @Test(expected = IllegalArgumentException.class)
58     public void convertJsonNodeToDmaapOutgoingMessage_should_throw_when_given_null_arguments() {
59
60         Converter.convertJsonNodeToDmaapOutgoingMessage(null, null);
61     }
62
63     @Test
64     public void convertJsonNodeToDmaapOutgoingMessage_should_convert_to_outgoing_message() {
65
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);
72
73         DmaapOutgoingMessage result = Converter.convertJsonNodeToDmaapOutgoingMessage(message, jsonNode);
74
75         assertEquals("test", result.getRpcName());
76         assertEquals("test-1", result.getCorrelationID());
77         assertEquals("v1", result.getVersion());
78         assertEquals(jsonNode, result.getBody());
79     }
80
81     @Test(expected = IllegalArgumentException.class)
82     public void convertDmaapOutgoingMessageToJsonString_should_throw_when_given_null_arguments()
83         throws JsonProcessingException {
84
85         Converter.convertDmaapOutgoingMessageToJsonString(null);
86     }
87
88     @Test
89     public void convertDmaapOutgoingMessageToJsonString_should_return_converted_json_string()
90         throws JsonProcessingException {
91
92         DmaapOutgoingMessage message = new DmaapOutgoingMessage();
93         message.setRpcName("test");
94         JsonNode jsonNode = Mapper.toJsonNodeFromJsonString(jsonOutputBodyStr);
95         message.setBody(jsonNode);
96
97         assertEquals(expectedDmaapOutgoingMessageAsJsonString,
98             Converter.convertDmaapOutgoingMessageToJsonString(message));
99     }
100
101     @Test(expected = IllegalArgumentException.class)
102     public void buildDmaapOutgoingMessageWithUnexpectedErrorTest_should_throw_given_null_arguments()
103         throws JsonProcessingException {
104
105         Converter.buildDmaapOutgoingMessageWithUnexpectedError(null, null);
106     }
107
108     @Test
109     public void buildDmaapOutgoingMessageWithUnexpectedErrorTest_should_build_valid_outgoing_message()
110         throws JsonProcessingException {
111
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);
120     }
121
122
123     @Test(expected = IllegalArgumentException.class)
124     public void extractRequestIdWithSubId_should_throw_given_null_argument() throws SvcLogicException {
125
126         Converter.extractRequestIdWithSubId(null);
127     }
128
129     @Test
130     public void extractRequestIdWithSubIdTest_should_extract_id_with_subDd() throws SvcLogicException {
131         DmaapIncomingMessage dmaapIncomingMessage = buildDmaapIncomingMessage();
132
133         String requestIdWithSubId = Converter.extractRequestIdWithSubId(dmaapIncomingMessage.getBody());
134         assertEquals("123-1", requestIdWithSubId);
135     }
136
137
138
139     @Test(expected = IllegalArgumentException.class)
140     public void extractStatusCode_should_throw_given_null_argument() {
141         Converter.extractStatusCode(null);
142     }
143
144
145     @Test
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());
150     }
151
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;
158
159     }
160
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;
167
168     }
169
170 }