Correction in created message.
[integration.git] / test / mocks / pnfsimulator / src / test / java / org / onap / pnfsimulator / message / MessageProviderTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.integration
4  * ================================================================================
5  * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.pnfsimulator.message;
22
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
26 import static org.onap.pnfsimulator.message.MessageConstants.*;
27
28 import java.util.UUID;
29 import org.json.JSONObject;
30 import org.junit.jupiter.api.BeforeAll;
31 import org.junit.jupiter.api.Test;
32
33 public class MessageProviderTest {
34
35     private static final String testParamsJson =
36         "{\"key1\": \"val1\",\"key2\": \"val2\",\"pnfKey3\": \"pnfVal3\",\"key4\": \"val4\"}";
37
38     private static MessageProvider messageProvider;
39
40     @BeforeAll
41     public static void setup() {
42         messageProvider = MessageProvider.getInstance();
43     }
44
45     @Test
46     public void createMessage_should_throw_when_given_null_argument() {
47         assertThrows(IllegalArgumentException.class,
48             () -> messageProvider.createMessage(null),
49             "Params object cannot be null");
50     }
51
52     @Test
53     public void createMessage_should_create_constant_message_when_no_params_specified() {
54         JSONObject message = messageProvider.createMessage(new JSONObject());
55         JSONObject event = message.getJSONObject(EVENT);
56
57         JSONObject commonEventHeader = event.getJSONObject(COMMON_EVENT_HEADER);
58         JSONObject otherFields = event.getJSONObject(OTHER_FIELDS);
59
60         JSONObject expectedCommonEventHeader = generateConstantCommonEventHeader();
61         JSONObject expectedOtherFields = generateConstantOtherFields();
62
63         expectedCommonEventHeader
64             .toMap()
65             .forEach((key, val) -> assertTrue(commonEventHeader.has(key),
66                 () -> String.format("Key %s is not present", key)));
67
68         expectedOtherFields
69             .toMap()
70             .forEach((key, val) -> assertTrue(otherFields.has(key),
71                 () -> String.format("Key %s is not present", key)));
72     }
73
74
75     @Test
76     public void createMessage_should_add_specified_params_to_valid_subobjects() {
77         JSONObject params = new JSONObject(testParamsJson);
78         JSONObject message = messageProvider.createMessage(params);
79
80         JSONObject event = message.getJSONObject(EVENT);
81         JSONObject commonEventHeader = event.getJSONObject(COMMON_EVENT_HEADER);
82         JSONObject otherFields = event.getJSONObject(OTHER_FIELDS);
83
84         assertEquals("pnfVal3", otherFields.getString("pnfKey3"));
85         assertEquals("val1", commonEventHeader.getString("key1"));
86         assertEquals("val2", commonEventHeader.getString("key2"));
87         assertEquals("val4", commonEventHeader.getString("key4"));
88     }
89
90
91     private JSONObject generateConstantCommonEventHeader() {
92
93         JSONObject commonEventHeader = new JSONObject();
94         long timestamp = System.currentTimeMillis();
95
96         commonEventHeader.put(DOMAIN, "other");
97         commonEventHeader.put(EVENT_ID, UUID.randomUUID() + "-reg");
98         commonEventHeader.put(EVENT_TYPE, "pnfRegistration");
99         commonEventHeader.put(LAST_EPOCH_MICROSEC, timestamp);
100         commonEventHeader.put(PRIORITY, "Normal");
101         commonEventHeader.put(SEQUENCE, 0);
102         commonEventHeader.put(START_EPOCH_MICROSEC, timestamp);
103         commonEventHeader.put(INTERNAL_HEADER_FIELDS, new JSONObject());
104         commonEventHeader.put(VERSION, 3);
105
106         return commonEventHeader;
107     }
108
109     private JSONObject generateConstantOtherFields() {
110         JSONObject otherFields = new JSONObject();
111
112         otherFields.put(OTHER_FIELDS_VERSION, 1);
113         otherFields.put(PNF_LAST_SERVICE_DATE, 1517206400);
114         otherFields.put(PNF_MANUFACTURE_DATE, 1516406400);
115
116         return otherFields;
117     }
118
119 }