2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.pnfsimulator.message;
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.*;
28 import java.util.UUID;
29 import org.json.JSONObject;
30 import org.junit.jupiter.api.BeforeAll;
31 import org.junit.jupiter.api.Test;
33 public class MessageProviderTest {
35 private static final String testParamsJson =
36 "{\"key1\": \"val1\",\"key2\": \"val2\",\"pnfKey3\": \"pnfVal3\",\"key4\": \"val4\"}";
38 private static MessageProvider messageProvider;
41 public static void setup() {
42 messageProvider = MessageProvider.getInstance();
46 public void createMessage_should_throw_when_given_null_argument() {
47 assertThrows(IllegalArgumentException.class,
48 () -> messageProvider.createMessage(null),
49 "Params object cannot be null");
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);
57 JSONObject commonEventHeader = event.getJSONObject(COMMON_EVENT_HEADER);
58 JSONObject otherFields = event.getJSONObject(OTHER_FIELDS);
60 JSONObject expectedCommonEventHeader = generateConstantCommonEventHeader();
61 JSONObject expectedOtherFields = generateConstantOtherFields();
63 expectedCommonEventHeader
65 .forEach((key, val) -> assertTrue(commonEventHeader.has(key),
66 () -> String.format("Key %s is not present", key)));
70 .forEach((key, val) -> assertTrue(otherFields.has(key),
71 () -> String.format("Key %s is not present", key)));
76 public void createMessage_should_add_specified_params_to_valid_subobjects() {
77 JSONObject params = new JSONObject(testParamsJson);
78 JSONObject message = messageProvider.createMessage(params);
80 JSONObject event = message.getJSONObject(EVENT);
81 JSONObject commonEventHeader = event.getJSONObject(COMMON_EVENT_HEADER);
82 JSONObject otherFields = event.getJSONObject(OTHER_FIELDS);
84 assertEquals("pnfVal3", otherFields.getString("pnfKey3"));
85 assertEquals("val1", commonEventHeader.getString("key1"));
86 assertEquals("val2", commonEventHeader.getString("key2"));
87 assertEquals("val4", commonEventHeader.getString("key4"));
91 private JSONObject generateConstantCommonEventHeader() {
93 JSONObject commonEventHeader = new JSONObject();
94 long timestamp = System.currentTimeMillis();
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);
106 return commonEventHeader;
109 private JSONObject generateConstantOtherFields() {
110 JSONObject otherFields = new JSONObject();
112 otherFields.put(OTHER_FIELDS_VERSION, 1);
113 otherFields.put(PNF_LAST_SERVICE_DATE, 1517206400);
114 otherFields.put(PNF_MANUFACTURE_DATE, 1516406400);