Remove outdated doc for A1 Adaptor
[integration.git] / test / mocks / masspnfsim / pnf-sim-lightweight / src / test / java / org / onap / pnfsimulator / message / MessageProviderTest.java
1 /*
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
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.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;
34
35 public class MessageProviderTest {
36
37     private static final String testParamsPnfRegistration =
38         "{\"pnfKey1\": \"pnfVal1\",\"pnfKey2\": \"pnfVal2\",\"pnfKey3\": \"pnfVal3\",\"pnfKey4\": \"pnfVal4\"}";
39
40     private static final String testParamsNotification =
41         "{\"notKey1\": \"notVal1\",\"notKey2\": \"notVal2\",\"notKey3\": \"notVal3\",\"notKey4\": \"notVal4\"}";
42
43     private static MessageProvider messageProvider;
44
45     @BeforeAll
46     public static void setup() {
47         messageProvider = new MessageProvider();
48     }
49
50     @Test
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");
55     }
56
57     @Test
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);
62
63         JSONObject commonEventHeader = event.getJSONObject(COMMON_EVENT_HEADER);
64         JSONObject pnfRegistrationFields = event.getJSONObject(PNF_REGISTRATION_FIELDS);
65         JSONObject notificationFields = event.getJSONObject(NOTIFICATION_FIELDS);
66
67         JSONObject expectedCommonEventHeader = JSONObjectFactory.generateConstantCommonEventHeader();
68         JSONObject expectedPnfRegistrationFields = JSONObjectFactory.generatePnfRegistrationFields();
69         JSONObject expectedNotificationFields = JSONObjectFactory.generateNotificationFields();
70
71         expectedCommonEventHeader
72             .toMap()
73             .forEach((key, val) -> assertTrue(commonEventHeader.has(key),
74                 () -> String.format("Key %s is not present", key)));
75
76         expectedPnfRegistrationFields
77             .toMap()
78             .forEach((key, val) -> assertTrue(pnfRegistrationFields.has(key),
79                 () -> String.format("Key %s is not present", key)));
80
81         expectedNotificationFields
82             .toMap()
83             .forEach((key, val) -> assertTrue(notificationFields.has(key),
84                 () -> String.format("Key %s is not present", key)));
85     }
86
87     @Test
88     public void createMessage_should_throw_exception_when_params_specified_as_empty() {
89         assertThrows(IllegalArgumentException.class,
90             () -> messageProvider.createMessage(new JSONObject(), Optional.empty(),
91                 Optional.empty()));
92     }
93
94     @Test
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);
99
100         JSONObject commonEventHeader = event.getJSONObject(COMMON_EVENT_HEADER);
101         assertEquals(13, commonEventHeader.keySet().size());
102
103         JSONObject pnfRegistrationFields = event.getJSONObject(PNF_REGISTRATION_FIELDS);
104         assertEquals("pnfVal1", pnfRegistrationFields.getString("pnfKey1"));
105         assertEquals("pnfVal2", pnfRegistrationFields.getString("pnfKey2"));
106     }
107
108     @Test
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);
113
114         JSONObject commonEventHeader = event.getJSONObject(COMMON_EVENT_HEADER);
115         assertEquals(12, commonEventHeader.keySet().size());
116
117         JSONObject notificationFields = event.getJSONObject(NOTIFICATION_FIELDS);
118         assertEquals("notVal1", notificationFields.getString("notKey1"));
119         assertEquals("notVal2", notificationFields.getString("notKey2"));
120     }
121
122 }