2 * ============LICENSE_START=======================================================
3 * PNF-REGISTRATION-HANDLER
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.COMMON_EVENT_HEADER;
27 import static org.onap.pnfsimulator.message.MessageConstants.EVENT;
28 import static org.onap.pnfsimulator.message.MessageConstants.NOTIFICATION_FIELDS;
29 import static org.onap.pnfsimulator.message.MessageConstants.PNF_REGISTRATION_FIELDS;
30 import java.util.Optional;
31 import org.json.JSONObject;
32 import org.junit.jupiter.api.BeforeAll;
33 import org.junit.jupiter.api.Test;
35 public class MessageProviderTest {
37 private static final String testParamsPnfRegistration =
38 "{\"pnfKey1\": \"pnfVal1\",\"pnfKey2\": \"pnfVal2\",\"pnfKey3\": \"pnfVal3\",\"pnfKey4\": \"pnfVal4\"}";
40 private static final String testParamsNotification =
41 "{\"notKey1\": \"notVal1\",\"notKey2\": \"notVal2\",\"notKey3\": \"notVal3\",\"notKey4\": \"notVal4\"}";
43 private static MessageProvider messageProvider;
46 public static void setup() {
47 messageProvider = new MessageProvider();
51 public void createMessage_should_throw_when_given_empty_arguments() {
52 assertThrows(IllegalArgumentException.class,
53 () -> messageProvider.createMessage(new JSONObject(), Optional.empty(), Optional.empty()),
54 "Params object cannot be null");
58 public void createMessage_should_create_constant_message_when_no_params_specified() {
59 JSONObject message = messageProvider.createMessage(new JSONObject(), Optional.ofNullable(new JSONObject()),
60 Optional.ofNullable(new JSONObject()));
61 JSONObject event = message.getJSONObject(EVENT);
63 JSONObject commonEventHeader = event.getJSONObject(COMMON_EVENT_HEADER);
64 JSONObject pnfRegistrationFields = event.getJSONObject(PNF_REGISTRATION_FIELDS);
65 JSONObject notificationFields = event.getJSONObject(NOTIFICATION_FIELDS);
67 JSONObject expectedCommonEventHeader = JSONObjectFactory.generateConstantCommonEventHeader();
68 JSONObject expectedPnfRegistrationFields = JSONObjectFactory.generatePnfRegistrationFields();
69 JSONObject expectedNotificationFields = JSONObjectFactory.generateNotificationFields();
71 expectedCommonEventHeader
73 .forEach((key, val) -> assertTrue(commonEventHeader.has(key),
74 () -> String.format("Key %s is not present", key)));
76 expectedPnfRegistrationFields
78 .forEach((key, val) -> assertTrue(pnfRegistrationFields.has(key),
79 () -> String.format("Key %s is not present", key)));
81 expectedNotificationFields
83 .forEach((key, val) -> assertTrue(notificationFields.has(key),
84 () -> String.format("Key %s is not present", key)));
88 public void createMessage_should_throw_exception_when_params_specified_as_empty() {
89 assertThrows(IllegalArgumentException.class,
90 () -> messageProvider.createMessage(new JSONObject(), Optional.empty(),
95 public void createMessage_should_add_specified_params_to_valid_subobjects_with_event_pnf_registration() {
96 JSONObject message = messageProvider
97 .createMessage(new JSONObject(), Optional.of(new JSONObject(testParamsPnfRegistration)), Optional.empty());
98 JSONObject event = message.getJSONObject(EVENT);
100 JSONObject commonEventHeader = event.getJSONObject(COMMON_EVENT_HEADER);
101 assertEquals(13, commonEventHeader.keySet().size());
103 JSONObject pnfRegistrationFields = event.getJSONObject(PNF_REGISTRATION_FIELDS);
104 assertEquals("pnfVal1", pnfRegistrationFields.getString("pnfKey1"));
105 assertEquals("pnfVal2", pnfRegistrationFields.getString("pnfKey2"));
109 public void createMessage_should_add_specified_params_to_valid_subobjects_with_event_notification() {
110 JSONObject message = messageProvider
111 .createMessage(new JSONObject(), Optional.empty(), Optional.of(new JSONObject(testParamsNotification)));
112 JSONObject event = message.getJSONObject(EVENT);
114 JSONObject commonEventHeader = event.getJSONObject(COMMON_EVENT_HEADER);
115 assertEquals(12, commonEventHeader.keySet().size());
117 JSONObject notificationFields = event.getJSONObject(NOTIFICATION_FIELDS);
118 assertEquals("notVal1", notificationFields.getString("notKey1"));
119 assertEquals("notVal2", notificationFields.getString("notKey2"));