Merge "switch drools pdp image to new one"
[integration.git] / test / mocks / mass-pnf-sim / 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
31 import java.util.Optional;
32 import org.json.JSONObject;
33 import org.junit.jupiter.api.BeforeAll;
34 import org.junit.jupiter.api.Test;
35
36 public class MessageProviderTest {
37
38     private static final String testParamsPnfRegistration =
39         "{\"pnfKey1\": \"pnfVal1\",\"pnfKey2\": \"pnfVal2\",\"pnfKey3\": \"pnfVal3\",\"pnfKey4\": \"pnfVal4\"}";
40
41     private static final String testParamsNotification =
42         "{\"notKey1\": \"notVal1\",\"notKey2\": \"notVal2\",\"notKey3\": \"notVal3\",\"notKey4\": \"notVal4\"}";
43
44     private static MessageProvider messageProvider;
45
46     @BeforeAll
47     public static void setup() {
48         messageProvider = new MessageProvider();
49     }
50
51     @Test
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");
56     }
57
58     @Test
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);
63
64         JSONObject commonEventHeader = event.getJSONObject(COMMON_EVENT_HEADER);
65         JSONObject pnfRegistrationFields = event.getJSONObject(PNF_REGISTRATION_FIELDS);
66         JSONObject notificationFields = event.getJSONObject(NOTIFICATION_FIELDS);
67
68         JSONObject expectedCommonEventHeader = JSONObjectFactory.generateConstantCommonEventHeader();
69         JSONObject expectedPnfRegistrationFields = JSONObjectFactory.generatePnfRegistrationFields();
70         JSONObject expectedNotificationFields = JSONObjectFactory.generateNotificationFields();
71
72         expectedCommonEventHeader
73             .toMap()
74             .forEach((key, val) -> assertTrue(commonEventHeader.has(key),
75                 () -> String.format("Key %s is not present", key)));
76
77         expectedPnfRegistrationFields
78             .toMap()
79             .forEach((key, val) -> assertTrue(pnfRegistrationFields.has(key),
80                 () -> String.format("Key %s is not present", key)));
81
82         expectedNotificationFields
83             .toMap()
84             .forEach((key, val) -> assertTrue(notificationFields.has(key),
85                 () -> String.format("Key %s is not present", key)));
86     }
87
88     @Test
89     public void createMessage_should_throw_exception_when_params_specified_as_empty() {
90         assertThrows(IllegalArgumentException.class,
91             () -> messageProvider.createMessage(new JSONObject(), Optional.empty(),
92                 Optional.empty()));
93     }
94
95     @Test
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);
101
102         JSONObject commonEventHeader = event.getJSONObject(COMMON_EVENT_HEADER);
103         assertEquals(10, commonEventHeader.keySet().size());
104
105         JSONObject pnfRegistrationFields = event.getJSONObject(PNF_REGISTRATION_FIELDS);
106         assertEquals("pnfVal1", pnfRegistrationFields.getString("pnfKey1"));
107         assertEquals("pnfVal2", pnfRegistrationFields.getString("pnfKey2"));
108
109         JSONObject notificationFields = event.getJSONObject(NOTIFICATION_FIELDS);
110         assertEquals("notVal1", notificationFields.getString("notKey1"));
111         assertEquals("notVal2", notificationFields.getString("notKey2"));
112
113     }
114
115 }