DmaapMessage and its subclasses unit tests
[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.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;
36
37 public class ConverterTest {
38
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\" } }}";
44
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}}}";
49
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\"}";
54
55
56     @Test(expected = IllegalArgumentException.class)
57     public void convertJsonNodeToDmaapOutgoingMessage_should_throw_when_given_null_arguments() {
58
59         Converter.convertJsonNodeToDmaapOutgoingMessage(null, null);
60     }
61
62     @Test
63     public void convertJsonNodeToDmaapOutgoingMessage_should_convert_to_outgoing_message() {
64
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);
71
72         DmaapOutgoingMessage result = Converter.convertJsonNodeToDmaapOutgoingMessage(message, jsonNode);
73
74         assertEquals("test", result.getRpcName());
75         assertEquals("test-1", result.getCorrelationID());
76         assertEquals("v1", result.getVersion());
77         assertEquals(jsonNode, result.getBody());
78     }
79
80     @Test(expected = IllegalArgumentException.class)
81     public void convertDmaapOutgoingMessageToJsonString_should_throw_when_given_null_arguments()
82         throws JsonProcessingException {
83
84         Converter.convertDmaapOutgoingMessageToJsonString(null);
85     }
86
87     @Test
88     public void convertDmaapOutgoingMessageToJsonString_should_return_converted_json_string()
89         throws JsonProcessingException {
90
91         DmaapOutgoingMessage message = new DmaapOutgoingMessage();
92         message.setRpcName("test");
93         JsonNode jsonNode = Mapper.toJsonNodeFromJsonString(jsonOutputBodyStr);
94         message.setBody(jsonNode);
95
96         assertEquals(expectedDmaapOutgoingMessageAsJsonString,
97             Converter.convertDmaapOutgoingMessageToJsonString(message));
98     }
99
100     @Test(expected = IllegalArgumentException.class)
101     public void buildDmaapOutgoingMessageWithUnexpectedErrorTest_should_throw_given_null_arguments()
102         throws JsonProcessingException {
103
104         Converter.buildDmaapOutgoingMessageWithUnexpectedError(null, null);
105     }
106
107     @Test
108     public void buildDmaapOutgoingMessageWithUnexpectedErrorTest_should_build_valid_outgoing_message()
109         throws JsonProcessingException {
110
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);
119     }
120
121
122     @Test(expected = IllegalArgumentException.class)
123     public void extractRequestIdWithSubId_should_throw_given_null_argument() throws SvcLogicException {
124
125         Converter.extractRequestIdWithSubId(null);
126     }
127
128     @Test
129     public void extractRequestIdWithSubIdTest_should_extract_id_with_subDd() throws SvcLogicException {
130         DmaapIncomingMessage dmaapIncomingMessage = buildDmaapIncomingMessage();
131
132         String requestIdWithSubId = Converter.extractRequestIdWithSubId(dmaapIncomingMessage.getBody());
133         assertEquals("123-1", requestIdWithSubId);
134     }
135
136
137     @Test(expected = IllegalArgumentException.class)
138     public void extractStatusCode_should_throw_given_null_argument() {
139         Converter.extractStatusCode(null);
140     }
141
142
143     @Test
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());
148     }
149
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;
156
157     }
158
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;
165
166     }
167
168 }