Applying license changes to all files
[appc.git] / appc-event-listener / appc-event-listener-bundle / src / test / java / org / openecomp / appc / listener / LCM / TestConverter.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.openecomp.appc.listener.LCM;
26
27 import com.fasterxml.jackson.core.JsonProcessingException;
28 import com.fasterxml.jackson.databind.JsonNode;
29 import org.junit.Assert;
30 import org.junit.Test;
31 import org.openecomp.appc.listener.LCM.conv.Converter;
32 import org.openecomp.appc.listener.LCM.model.DmaapIncomingMessage;
33 import org.openecomp.appc.listener.LCM.model.DmaapOutgoingMessage;
34 import org.openecomp.appc.listener.util.Mapper;
35
36 public class TestConverter {
37
38     private String jsonInputBodyStr ="{\"input\":{ \"common-header\": { \"timestamp\": \"2016-08-03T08:50:18.97Z\", \"api-ver\": \"1\", \"originator-id\": \"1\", \"request-id\": \"123\", \"sub-request-id\": \"1\", \"flags\": { \"force\":\"TRUE\", \"ttl\":\"9900\" } }, \"action\": \"Stop\", \"action-identifiers\": { \"vnf-id\": \"TEST\" } }}";
39     private String jsonOutputBodyStr ="{\"output\":{\"common-header\":{\"timestamp\":\"2016-08-03T08:50:18.97Z\",\"api-ver\":\"1\",\"flags\":{\"force\":\"TRUE\",\"ttl\":\"9900\"},\"sub-request-id\":\"1\",\"request-id\":\"123\",\"originator-id\":\"1\"},\"status\":{\"value\":\"TestException\",\"code\":200}}}";
40
41     @Test
42     public void buildDmaapOutgoingMessageWithUnexpectedErrorTest() throws JsonProcessingException {
43         DmaapIncomingMessage dmaapIncomingMessage = buildDmaapIncomingMessage();
44         String errMsg = "TestException";
45         DmaapOutgoingMessage dmaapOutgoingMessage = Converter.buildDmaapOutgoingMessageWithUnexpectedError(dmaapIncomingMessage, new Exception(errMsg));
46         int code = dmaapOutgoingMessage.getBody().get("output").get("status").get("code").asInt();
47         String value = dmaapOutgoingMessage.getBody().get("output").get("status").get("value").asText();
48         Assert.assertEquals(200,code);
49         Assert.assertEquals(errMsg,value);
50     }
51
52     private  static String expectedDmaapOutgoingMessageAsJsonString = "{\"body\":{\"output\":{\"common-header\":{\"timestamp\":\"2016-08-03T08:50:18.97Z\",\"api-ver\":\"1\",\"flags\":{\"force\":\"TRUE\",\"ttl\":\"9900\"},\"sub-request-id\":\"1\",\"request-id\":\"123\",\"originator-id\":\"1\"},\"status\":{\"value\":\"TestException\",\"code\":200}}},\"cambria.partition\":\"MSO\",\"rpc-name\":\"test\"}";
53     @Test
54     public void convDmaapOutgoingMessageToJsonStringTest() throws JsonProcessingException {
55         DmaapOutgoingMessage dmaapOutgoingMessage = buildDmaapOutgoingMessage();
56         String dmaapOutgoingMessageAsJsonString = Converter.convDmaapOutgoingMessageToJsonString(dmaapOutgoingMessage);
57 //        Assert.assertEquals(dmaapOutgoingMessageAsJsonString,dmaapOutgoingMessageAsJsonString);
58         Assert.assertEquals(expectedDmaapOutgoingMessageAsJsonString,dmaapOutgoingMessageAsJsonString);
59     }
60
61     private DmaapIncomingMessage buildDmaapIncomingMessage() {
62         DmaapIncomingMessage dmaapIncomingMessage = new DmaapIncomingMessage();
63         dmaapIncomingMessage.setRpcName("test");
64         JsonNode jsonNode = Mapper.toJsonNodeFromJsonString(jsonInputBodyStr);
65         dmaapIncomingMessage.setBody(jsonNode);
66         return dmaapIncomingMessage;
67
68     }
69
70     private DmaapOutgoingMessage buildDmaapOutgoingMessage() {
71         DmaapOutgoingMessage dmaapOutgoingMessage = new DmaapOutgoingMessage();
72         dmaapOutgoingMessage.setRpcName("test");
73         JsonNode jsonNode = Mapper.toJsonNodeFromJsonString(jsonOutputBodyStr);
74         dmaapOutgoingMessage.setBody(jsonNode);
75         return dmaapOutgoingMessage;
76
77     }
78
79
80     @Test
81     public void extractRequestIdWithSubIdTest() {
82         DmaapIncomingMessage dmaapIncomingMessage = buildDmaapIncomingMessage();
83         String equestIdWithSubId = Converter.extractRequestIdWithSubId(dmaapIncomingMessage.getBody());
84         Assert.assertEquals("123-1",equestIdWithSubId);
85     }
86
87     @Test
88     public void extractStatusCodeTest() {
89         DmaapOutgoingMessage dmaapOutgoingMessage = buildDmaapOutgoingMessage();
90         Integer statusCode = Converter.extractStatusCode(dmaapOutgoingMessage.getBody());
91         Assert.assertEquals(200L,statusCode.longValue());
92     }
93
94 }