LCM package test classes refactor
[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 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;
32
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;
40
41 public class ConverterTest {
42
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\"}";
48
49     @Test(expected = IllegalArgumentException.class)
50     public void convertJsonNodeToDmaapOutgoingMessage_should_throw_when_given_null_arguments() {
51
52         Converter.convertJsonNodeToDmaapOutgoingMessage(null, null);
53     }
54
55     @Test
56     public void convertJsonNodeToDmaapOutgoingMessage_should_convert_to_outgoing_message() {
57
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);
64
65         DmaapOutgoingMessage result = Converter.convertJsonNodeToDmaapOutgoingMessage(message, jsonNode);
66
67         assertEquals("test", result.getRpcName());
68         assertEquals("test-1", result.getCorrelationID());
69         assertEquals("v1", result.getVersion());
70         assertEquals(jsonNode, result.getBody());
71     }
72
73     @Test(expected = IllegalArgumentException.class)
74     public void convertDmaapOutgoingMessageToJsonString_should_throw_when_given_null_arguments()
75         throws JsonProcessingException {
76
77         Converter.convertDmaapOutgoingMessageToJsonString(null);
78     }
79
80     @Test
81     public void convertDmaapOutgoingMessageToJsonString_should_return_converted_json_string()
82         throws JsonProcessingException {
83
84         DmaapOutgoingMessage message = new DmaapOutgoingMessage();
85         message.setRpcName("test");
86         JsonNode jsonNode = Mapper.toJsonNodeFromJsonString(JSON_OUTPUT_BODY_STR);
87         message.setBody(jsonNode);
88
89         assertEquals(EXPECTED_DMAAP_OUTGOING_MESSAGE_AS_JSON_STRING,
90             Converter.convertDmaapOutgoingMessageToJsonString(message));
91     }
92
93     @Test(expected = IllegalArgumentException.class)
94     public void buildDmaapOutgoingMessageWithUnexpectedErrorTest_should_throw_given_null_arguments()
95         throws JsonProcessingException {
96
97         Converter.buildDmaapOutgoingMessageWithUnexpectedError(null, null);
98     }
99
100     @Test
101     public void buildDmaapOutgoingMessageWithUnexpectedErrorTest_should_build_valid_outgoing_message()
102         throws JsonProcessingException {
103
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);
112     }
113
114
115     @Test(expected = IllegalArgumentException.class)
116     public void extractRequestIdWithSubId_should_throw_given_null_argument() throws SvcLogicException {
117
118         Converter.extractRequestIdWithSubId(null);
119     }
120
121     @Test
122     public void extractRequestIdWithSubIdTest_should_extract_id_with_subDd() throws SvcLogicException {
123         DmaapIncomingMessage dmaapIncomingMessage = buildDmaapIncomingMessage();
124
125         String requestIdWithSubId = Converter.extractRequestIdWithSubId(dmaapIncomingMessage.getBody());
126         assertEquals("123-1", requestIdWithSubId);
127     }
128
129
130     @Test(expected = IllegalArgumentException.class)
131     public void extractStatusCode_should_throw_given_null_argument() {
132         Converter.extractStatusCode(null);
133     }
134
135
136     @Test
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());
141     }
142
143
144 }