InputBody and OutputBody unit tests
[appc.git] / appc-event-listener / appc-event-listener-bundle / src / test / java / org / onap / appc / listener / LCM / model / OutputBodyTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2018 Nokia Solutions and Networks
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 package org.onap.appc.listener.LCM.model;
25
26 import static junit.framework.TestCase.assertNotNull;
27 import static org.junit.Assert.assertEquals;
28
29 import java.util.HashMap;
30 import java.util.Map;
31 import org.json.JSONObject;
32 import org.junit.Before;
33 import org.junit.Test;
34
35 public class OutputBodyTest {
36
37     private OutputBody outputBody;
38
39     @Before
40     public void setup() {
41         outputBody = new OutputBody();
42     }
43
44     @Test
45     public void should_set_properties() {
46
47         CommonHeader testCommonHeader = buildCommonHeader();
48         ResponseStatus testResponseStatus = new ResponseStatus(200, "OK");
49
50         outputBody.setHeader(testCommonHeader);
51         outputBody.setStatus(testResponseStatus);
52         outputBody.setLocked("test-locked");
53         outputBody.setPayload("{\"payload\": \"value\"");
54
55         assertEquals(testCommonHeader, outputBody.getHeader());
56         assertEquals(testResponseStatus, outputBody.getStatus());
57         assertEquals("test-locked", outputBody.getLocked());
58         assertEquals("{\"payload\": \"value\"", outputBody.getPayload());
59     }
60
61
62     @Test
63     public void should_inherit_input_body_header_when_initialized_from_constructor() {
64
65         InputBody testInputBody = new InputBody();
66         CommonHeader testCommonHeader = buildCommonHeader();
67         testInputBody.setCommonHeader(testCommonHeader);
68
69         outputBody = new OutputBody(testInputBody);
70
71         assertNotNull(outputBody.getHeader());
72         assertEquals(testCommonHeader.getFlags(), outputBody.getHeader().getFlags());
73         assertEquals(testCommonHeader.getSubRequestId(), outputBody.getHeader().getSubRequestId());
74         assertEquals(testCommonHeader.getRequestID(), outputBody.getHeader().getRequestID());
75         assertEquals(testCommonHeader.getOriginatorId(), outputBody.getHeader().getOriginatorId());
76         assertEquals(testCommonHeader.getApiVer(), outputBody.getHeader().getApiVer());
77     }
78
79     @Test
80     public void toResponse_should_convert_to_json_object() {
81         CommonHeader testCommonHeader = buildCommonHeader();
82         ResponseStatus testResponseStatus = new ResponseStatus(200, "OK");
83
84         outputBody.setHeader(testCommonHeader);
85         outputBody.setStatus(testResponseStatus);
86         outputBody.setLocked("test-locked");
87         outputBody.setPayload("{\"payload\": \"value\"");
88
89         JSONObject response = outputBody.toResponse();
90         assertNotNull(response);
91
92         assertEquals("test-locked", response.get("locked"));
93         assertEquals("{\"payload\": \"value\"", response.get("payload"));
94     }
95
96     private CommonHeader buildCommonHeader() {
97
98         CommonHeader commonHeader = new CommonHeader();
99         commonHeader.setTimeStamp("test-timestamp");
100         commonHeader.setApiVer("test-api-version");
101         commonHeader.setOriginatorId("test-originator-id");
102         commonHeader.setRequestID("test-request-id");
103         commonHeader.setSubRequestId("test-subrequest-id");
104
105         Map<String, String> flags = new HashMap<>();
106         flags.put("key1", "flag1");
107         flags.put("key2", "flag2");
108         flags.put("key3", "flag3");
109
110         commonHeader.setFlags(flags);
111         return commonHeader;
112     }
113 }