aboutsummaryrefslogtreecommitdiffstats
path: root/test/mocks/pnfsimulator/src/test/java/org/onap/pnfsimulator/message/MessageProviderTest.java
blob: 4524b96c3fb7bfff32b43552b85d115222a0a2b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * ============LICENSE_START=======================================================
 * PNF-REGISTRATION-HANDLER
 * ================================================================================
 * Copyright (C) 2018-2019 NOKIA Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.pnfsimulator.message;

import org.json.JSONObject;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.onap.pnfsimulator.simulator.ResourceReader;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.data.MapEntry.entry;
import static org.onap.pnfsimulator.message.MessageConstants.COMMON_EVENT_HEADER;
import static org.onap.pnfsimulator.message.MessageConstants.EVENT;
import static org.onap.pnfsimulator.message.MessageConstants.NOTIFICATION_FIELDS;
import static org.onap.pnfsimulator.message.MessageConstants.PNF_REGISTRATION_FIELDS;

public class MessageProviderTest {

    private static String testParamsPnfRegistration;

    private static String testParamsNotification;

    private MessageProvider messageProvider = new MessageProvider();

    @BeforeAll
    public static void setup() {
        ResourceReader reader = new ResourceReader("org/onap/pnfsimulator/message/MessageProviderTest/");
        testParamsPnfRegistration = reader.readResource("pnfRegistrationParams.json");
        testParamsNotification = reader.readResource("notificationParams.json");
    }

    @Test
    public void createMessageWithPnfRegistration_should_create_constant_message_when_no_params_specified() {
        JSONObject message = messageProvider.createMessageWithPnfRegistration(new JSONObject(), new JSONObject());
        JSONObject event = message.getJSONObject(EVENT);

        JSONObject commonEventHeader = event.getJSONObject(COMMON_EVENT_HEADER);
        JSONObject pnfRegistrationFields = event.getJSONObject(PNF_REGISTRATION_FIELDS);

        JSONObject expectedCommonEventHeader = JSONObjectFactory.generateConstantCommonEventHeader();
        JSONObject expectedPnfRegistrationFields = JSONObjectFactory.generatePnfRegistrationFields();

        assertThat(commonEventHeader.keySet())
                .containsAll(expectedCommonEventHeader.keySet());
        assertThat(pnfRegistrationFields.keySet())
                .containsAll(expectedPnfRegistrationFields.keySet());
    }

    @Test
    public void createMessageWithNotification_should_create_constant_message_when_no_params_specified() {
        JSONObject message = messageProvider.createMessageWithNotification(new JSONObject(),
                new JSONObject());
        JSONObject event = message.getJSONObject(EVENT);

        JSONObject commonEventHeader = event.getJSONObject(COMMON_EVENT_HEADER);
        JSONObject notificationFields = event.getJSONObject(NOTIFICATION_FIELDS);

        JSONObject expectedCommonEventHeader = JSONObjectFactory.generateConstantCommonEventHeader();
        JSONObject expectedNotificationFields = JSONObjectFactory.generateNotificationFields();

        assertThat(commonEventHeader.keySet())
                .containsAll(expectedCommonEventHeader.keySet());

        assertThat(notificationFields.keySet())
                .containsAll(expectedNotificationFields.keySet());
    }

    @Test
    public void createMessageWithPnfRegistration_should_add_specified_params_to_valid_subobjects() {
        JSONObject message = messageProvider
                .createMessageWithPnfRegistration(new JSONObject(), new JSONObject(testParamsPnfRegistration));
        JSONObject event = message.getJSONObject(EVENT);

        JSONObject commonEventHeader = event.getJSONObject(COMMON_EVENT_HEADER);

        JSONObject pnfRegistrationFields = event.getJSONObject(PNF_REGISTRATION_FIELDS);
        assertThat(pnfRegistrationFields.toMap()).contains(
                entry("pnfKey1", "pnfVal1"),
                entry("pnfKey2", "pnfVal2"),
                entry("pnfKey3", "pnfVal3"),
                entry("pnfKey4", "pnfVal4")
        );
    }

    @Test
    public void createMessageWithNotification_should_add_specified_params_to_valid_subobjects() {
        JSONObject message = messageProvider
                .createMessageWithNotification(new JSONObject(),
                        new JSONObject(testParamsNotification));
        JSONObject event = message.getJSONObject(EVENT);

        JSONObject notificationFields = event.getJSONObject(NOTIFICATION_FIELDS);
        assertThat(notificationFields.toMap()).contains(
                entry("notKey1", "notVal1"),
                entry("notKey2", "notVal2"),
                entry("notKey3", "notVal3"),
                entry("notKey4", "notVal4")
        );

    }

}