037f91a6e708bfa3b0bd9b84593137ff9c73b2f7
[appc.git] / services / appc-dmaap-service / 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-2018 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  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.listener.LCM.conv;
25
26 import static org.junit.Assert.assertEquals;
27 import static org.onap.appc.listener.TestUtil.JSON_INPUT_BODY_STR;
28 import static org.onap.appc.listener.TestUtil.JSON_OUTPUT_BODY_STR;
29 import static org.onap.appc.listener.TestUtil.buildDmaapIncomingMessage;
30 import static org.onap.appc.listener.TestUtil.buildDmaapOutgoingMessage;
31
32 import com.fasterxml.jackson.core.JsonProcessingException;
33 import com.fasterxml.jackson.databind.JsonNode;
34 import org.junit.Test;
35 import org.onap.appc.listener.LCM.model.DmaapIncomingMessage;
36 import org.onap.appc.listener.LCM.model.DmaapOutgoingMessage;
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 EXPECTED_DMAAP_OUTGOING_MESSAGE_AS_JSON_STRING =
43         "{\"body\":{\"output\":{\"common-header\":"
44             + "{\"timestamp\":\"2016-08-03T08:50:18.97Z\",\"api-ver\":\"1\",\"flags\":{\"force\":\"TRUE\",\"ttl\":\"9900\"},"
45             + "\"sub-request-id\":\"1\",\"request-id\":\"123\",\"originator-id\":\"1\"},\"locked\":\"test-locked\",\""
46             + "status\":{\"message\":\"test message\",\"code\":200}}},\"cambria.partition\":\"MSO\",\"rpc-name\":\"test\"}";
47
48     @Test(expected = IllegalArgumentException.class)
49     public void convertJsonNodeToDmaapOutgoingMessage_should_throw_when_given_null_arguments() {
50
51         Converter.convertJsonNodeToDmaapOutgoingMessage(null, null);
52     }
53
54     @Test
55     public void convertJsonNodeToDmaapOutgoingMessage_should_convert_to_outgoing_message() {
56
57         DmaapIncomingMessage message = new DmaapIncomingMessage();
58         message.setRpcName("test");
59         message.setCorrelationID("test-1");
60         message.setVersion("v1");
61         JsonNode jsonNode = Mapper.toJsonNodeFromJsonString(JSON_INPUT_BODY_STR);
62         message.setBody(jsonNode);
63
64         DmaapOutgoingMessage result = Converter.convertJsonNodeToDmaapOutgoingMessage(message, jsonNode);
65
66         assertEquals("test", result.getRpcName());
67         assertEquals("test-1", result.getCorrelationID());
68         assertEquals("v1", result.getVersion());
69         assertEquals(jsonNode, result.getBody());
70     }
71
72     @Test(expected = IllegalArgumentException.class)
73     public void convertDmaapOutgoingMessageToJsonString_should_throw_when_given_null_arguments()
74         throws JsonProcessingException {
75
76         Converter.convertDmaapOutgoingMessageToJsonString(null);
77     }
78
79     @Test
80     public void convertDmaapOutgoingMessageToJsonString_should_return_converted_json_string()
81         throws JsonProcessingException {
82
83         DmaapOutgoingMessage message = new DmaapOutgoingMessage();
84         message.setRpcName("test");
85         JsonNode jsonNode = Mapper.toJsonNodeFromJsonString(JSON_OUTPUT_BODY_STR);
86         message.setBody(jsonNode);
87
88         assertEquals(EXPECTED_DMAAP_OUTGOING_MESSAGE_AS_JSON_STRING,
89             Converter.convertDmaapOutgoingMessageToJsonString(message));
90     }
91
92     @Test(expected = IllegalArgumentException.class)
93     public void buildDmaapOutgoingMessageWithUnexpectedErrorTest_should_throw_given_null_arguments()
94         throws JsonProcessingException {
95
96         Converter.buildDmaapOutgoingMessageWithUnexpectedError(null, null);
97     }
98
99     @Test
100     public void buildDmaapOutgoingMessageWithUnexpectedErrorTest_should_build_valid_outgoing_message()
101         throws JsonProcessingException {
102
103         DmaapIncomingMessage dmaapIncomingMessage = buildDmaapIncomingMessage();
104         String errMsg = "TestException";
105         DmaapOutgoingMessage dmaapOutgoingMessage = Converter
106             .buildDmaapOutgoingMessageWithUnexpectedError(dmaapIncomingMessage, new Exception(errMsg));
107         int code = dmaapOutgoingMessage.getBody().get("output").get("status").get("code").asInt();
108         String value = dmaapOutgoingMessage.getBody().get("output").get("status").get("value").asText();
109         assertEquals(200, code);
110         assertEquals(errMsg, value);
111     }
112
113
114     @Test(expected = IllegalArgumentException.class)
115     public void extractRequestIdWithSubId_should_throw_given_null_argument() throws SvcLogicException {
116
117         Converter.extractRequestIdWithSubId(null);
118     }
119
120     @Test
121     public void extractRequestIdWithSubIdTest_should_extract_id_with_subDd() throws SvcLogicException {
122         DmaapIncomingMessage dmaapIncomingMessage = buildDmaapIncomingMessage();
123
124         String requestIdWithSubId = Converter.extractRequestIdWithSubId(dmaapIncomingMessage.getBody());
125         assertEquals("123-1", requestIdWithSubId);
126     }
127
128
129     @Test(expected = IllegalArgumentException.class)
130     public void extractStatusCode_should_throw_given_null_argument() {
131         Converter.extractStatusCode(null);
132     }
133
134
135     @Test
136     public void extractStatusCode_should_extract_valid_status_code() {
137         DmaapOutgoingMessage dmaapOutgoingMessage = buildDmaapOutgoingMessage();
138         Integer statusCode = Converter.extractStatusCode(dmaapOutgoingMessage.getBody());
139         assertEquals(200L, statusCode.longValue());
140     }
141
142
143 }