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;
31 import java.util.Optional;
32 import org.json.JSONObject;
33 import org.junit.jupiter.api.BeforeAll;
34 import org.junit.jupiter.api.Test;
36 public class MessageProviderTest {
38 private static final String testParamsPnfRegistration =
39 "{\"pnfKey1\": \"pnfVal1\",\"pnfKey2\": \"pnfVal2\",\"pnfKey3\": \"pnfVal3\",\"pnfKey4\": \"pnfVal4\"}";
41 private static final String testParamsNotification =
42 "{\"notKey1\": \"notVal1\",\"notKey2\": \"notVal2\",\"notKey3\": \"notVal3\",\"notKey4\": \"notVal4\"}";
44 private static MessageProvider messageProvider;
47 public static void setup() {
48 messageProvider = new MessageProvider();
52 public void createMessage_should_throw_when_given_empty_arguments() {
53 assertThrows(IllegalArgumentException.class,
54 () -> messageProvider.createMessage(new JSONObject(), Optional.empty(), Optional.empty()),
55 "Params object cannot be null");
59 public void createMessage_should_create_constant_message_when_no_params_specified() {
60 JSONObject message = messageProvider.createMessage(new JSONObject(), Optional.ofNullable(new JSONObject()),
61 Optional.ofNullable(new JSONObject()));
62 JSONObject event = message.getJSONObject(EVENT);
64 JSONObject commonEventHeader = event.getJSONObject(COMMON_EVENT_HEADER);
65 JSONObject pnfRegistrationFields = event.getJSONObject(PNF_REGISTRATION_FIELDS);
66 JSONObject notificationFields = event.getJSONObject(NOTIFICATION_FIELDS);
68 JSONObject expectedCommonEventHeader = JSONObjectFactory.generateConstantCommonEventHeader();
69 JSONObject expectedPnfRegistrationFields = JSONObjectFactory.generatePnfRegistrationFields();
70 JSONObject expectedNotificationFields = JSONObjectFactory.generateNotificationFields();
72 expectedCommonEventHeader
74 .forEach((key, val) -> assertTrue(commonEventHeader.has(key),
75 () -> String.format("Key %s is not present", key)));
77 expectedPnfRegistrationFields
79 .forEach((key, val) -> assertTrue(pnfRegistrationFields.has(key),
80 () -> String.format("Key %s is not present", key)));
82 expectedNotificationFields
84 .forEach((key, val) -> assertTrue(notificationFields.has(key),
85 () -> String.format("Key %s is not present", key)));
89 public void createMessage_should_throw_exception_when_params_specified_as_empty() {
90 assertThrows(IllegalArgumentException.class,
91 () -> messageProvider.createMessage(new JSONObject(), Optional.empty(),
96 public void createMessage_should_add_specified_params_to_valid_subobjects() {
97 JSONObject message = messageProvider
98 .createMessage(new JSONObject(), Optional.of(new JSONObject(testParamsPnfRegistration)),
99 Optional.of(new JSONObject(testParamsNotification)));
100 JSONObject event = message.getJSONObject(EVENT);
102 JSONObject commonEventHeader = event.getJSONObject(COMMON_EVENT_HEADER);
103 assertEquals(10, commonEventHeader.keySet().size());
105 JSONObject pnfRegistrationFields = event.getJSONObject(PNF_REGISTRATION_FIELDS);
106 assertEquals("pnfVal1", pnfRegistrationFields.getString("pnfKey1"));
107 assertEquals("pnfVal2", pnfRegistrationFields.getString("pnfKey2"));
109 JSONObject notificationFields = event.getJSONObject(NOTIFICATION_FIELDS);
110 assertEquals("notVal1", notificationFields.getString("notKey1"));
111 assertEquals("notVal2", notificationFields.getString("notKey2"));