First part of onap rename
[appc.git] / appc-client / client-lib / src / test / java / org / openecomp / appc / client / impl / protocol / APPCMessageReaderWriterTest.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.client.impl.protocol;
26
27 import org.onap.appc.client.impl.core.MessageContext;
28 import org.onap.appc.client.impl.protocol.APPCMessageReaderWriter;
29 import org.onap.appc.client.impl.protocol.ProtocolException;
30
31 import com.fasterxml.jackson.core.JsonFactory;
32 import com.fasterxml.jackson.core.JsonGenerator;
33 import com.fasterxml.jackson.databind.JsonNode;
34 import com.fasterxml.jackson.databind.ObjectMapper;
35 import com.fasterxml.jackson.databind.node.JsonNodeFactory;
36 import com.fasterxml.jackson.databind.node.ObjectNode;
37 import org.junit.AfterClass;
38 import org.junit.Assert;
39 import org.junit.Before;
40 import org.junit.Test;
41
42 import java.io.IOException;
43 import java.io.StringWriter;
44
45 public class APPCMessageReaderWriterTest {
46
47     private APPCMessageReaderWriter messageReaderWriter;
48     private ObjectMapper mapper;
49
50     private static final String VERSION = "2.0";
51     private static final String TYPE = "typeTest";
52     private static final String CORRELATION_ID = "correlationIdTest";
53     private static final String PARTITION = "partitionTest";
54     private static final String RPC = "rpcTest";
55     private static final String PAYLOAD = "{\"key1\":\"val1\",\"key2\":\"val2\",\"key3\":{\"key3.1\":\"val3.1\"}}";
56
57     @Before
58     public void init() throws IOException {
59         mapper = new ObjectMapper();
60         messageReaderWriter = new APPCMessageReaderWriter();
61     }
62
63     @Test
64     public void writeTest() throws IOException, ProtocolException {
65         MessageContext context = new MessageContext();
66         context.setType(TYPE);
67         context.setCorrelationID(CORRELATION_ID);
68         context.setPartiton(PARTITION);
69         context.setRpc(RPC);
70         String payload = PAYLOAD;
71         String message = messageReaderWriter.write(payload, context);
72
73         JsonNode messageJson = mapper.readTree(message);
74         Assert.assertEquals(VERSION, messageJson.get("version").asText());
75         Assert.assertEquals(context.getType(), messageJson.get("type").asText());
76         Assert.assertEquals(context.getCorrelationID(), messageJson.get("correlation-id").asText());
77         Assert.assertEquals(context.getPartiton(), messageJson.get("cambria.partition").asText());
78         Assert.assertEquals(context.getRpc(), messageJson.get("rpc-name").asText());
79         Assert.assertEquals(payload, messageJson.get("body").toString());
80     }
81
82     @Test
83     public void readTest() throws IOException, ProtocolException {
84         ObjectNode node = mapper.createObjectNode();
85         node.put("version", VERSION);
86         node.put("type", TYPE);
87         node.put("correlation-id", CORRELATION_ID);
88         node.put("cambria.partition", PARTITION);
89         node.put("rpc-name", RPC);
90         JsonNode payload = mapper.valueToTree(PAYLOAD);
91         node.set("body", payload);
92         String message = node.toString();
93
94         MessageContext returnContext = new MessageContext();
95         String returnPayload = messageReaderWriter.read(message, returnContext);
96
97         Assert.assertEquals(TYPE, returnContext.getType());
98         Assert.assertEquals(CORRELATION_ID, returnContext.getCorrelationID());
99         Assert.assertEquals(PARTITION, returnContext.getPartiton());
100         Assert.assertEquals(RPC, returnContext.getRpc());
101         Assert.assertEquals(payload.toString(), returnPayload);
102     }
103
104 }