aboutsummaryrefslogtreecommitdiffstats
path: root/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/simulator/SimulatorTest.java
blob: 7ed9f04c2d1ab8a3c7e9e0ab71fbf17778fcf06c (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
 * ============LICENSE_START=======================================================
 * PNF-REGISTRATION-HANDLER
 * ================================================================================
 * Copyright (C) 2018 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.simulator;

import static org.junit.Assert.assertNull;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTimeout;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.onap.pnfsimulator.simulator.TestMessages.INVALID_NOTIFICATION_PARAMS;
import static org.onap.pnfsimulator.simulator.TestMessages.INVALID_PNF_REGISTRATION_PARAMS_1;
import static org.onap.pnfsimulator.simulator.TestMessages.INVALID_PNF_REGISTRATION_PARAMS_2;
import static org.onap.pnfsimulator.simulator.TestMessages.INVALID_PNF_REGISTRATION_PARAMS_3;
import static org.onap.pnfsimulator.simulator.TestMessages.VALID_COMMON_EVENT_HEADER_PARAMS;
import static org.onap.pnfsimulator.simulator.TestMessages.VALID_NOTIFICATION_PARAMS;
import static org.onap.pnfsimulator.simulator.TestMessages.VALID_PNF_REGISTRATION_PARAMS;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import org.mockito.Mockito;
import org.onap.pnfsimulator.FileProvider;
import org.onap.pnfsimulator.simulator.client.HttpClientAdapter;
import org.onap.pnfsimulator.simulator.validation.NoRopFilesException;
import org.onap.pnfsimulator.simulator.validation.ValidationException;

public class SimulatorTest {

    private static final String TEST_VES_URL = "http://localhost:10000/eventListener/v7";
    private static final String TEST_XNF_URL = "sftp://onap:pano@10.11.0.68" + "/";
    private FileProvider fileProvider = mock(FileProvider.class);

    private void createSampleFileList() {
        List<String> fileList = new ArrayList<>();
        fileList.add("A20190401.1608+0000-1622+0000_excl-eeiwbue-perf-large-pnf-sim-lw-1.xml.gz");
        fileList.add("A20190401.1623+0000-1637+0000_excl-eeiwbue-perf-large-pnf-sim-lw-1.xml.gz");

        try {
            doReturn(fileList).when(fileProvider).getFiles();
        } catch (NoRopFilesException e) {
            e.printStackTrace();
        }
    }

    @Test
    void builder_should_create_endless_simulator_when_duration_not_specified() {
        Simulator simulator = Simulator
            .builder()
            .withDuration(Duration.ofSeconds(1))
            .withVesUrl(TEST_VES_URL).build();

        assertFalse(simulator.isEndless());

        simulator = Simulator
            .builder()
            .withVesUrl(TEST_VES_URL).build();

        assertTrue(simulator.isEndless());
    }

    @Test
    void simulator_should_stop_when_interrupted() {
        createSampleFileList();

        HttpClientAdapter httpClientMock = Mockito.mock(HttpClientAdapter.class);
        Simulator simulator = Simulator.builder()
            .withInterval(Duration.ofSeconds(1))
            .withCustomHttpClientAdapter(httpClientMock)
            .withCommonEventHeaderParams(VALID_COMMON_EVENT_HEADER_PARAMS)
            .withPnfRegistrationParams(Optional.empty())
            .withNotificationParams(VALID_NOTIFICATION_PARAMS)
            .withVesUrl(TEST_VES_URL)
            .withXnfUrl(TEST_XNF_URL)
            .withCustomHttpClientAdapter(httpClientMock)
            .withFileProvider(fileProvider).build();

        simulator.start();
        simulator.interrupt();

        assertTimeout(Duration.ofSeconds(1), (Executable) simulator::join);
    }

    @Test
    void should_throw_noropfiles_exception_given_empty_filelist() {
        Simulator simulator = Simulator.builder()
                .withDuration(Duration.ofMillis(100))
                .withInterval(Duration.ofMillis(10))
                .withCommonEventHeaderParams(VALID_COMMON_EVENT_HEADER_PARAMS)
                .withPnfRegistrationParams(VALID_PNF_REGISTRATION_PARAMS)
                .withNotificationParams(Optional.empty())
                .withVesUrl(TEST_VES_URL)
                .withXnfUrl(TEST_XNF_URL)
                .withFileProvider(new FileProvider()).build();
        simulator.run();
        Exception e = simulator.getThrownException();
        assertTrue(e instanceof NoRopFilesException);
    }

    @Test
    void should_throw_validation_exception_given_invalid_params() {
        createSampleFileList();

        Simulator simulator = Simulator.builder()
                .withDuration(Duration.ofMillis(100))
                .withInterval(Duration.ofMillis(10))
                .withCommonEventHeaderParams(VALID_COMMON_EVENT_HEADER_PARAMS)
                .withPnfRegistrationParams(INVALID_PNF_REGISTRATION_PARAMS_1)
                .withNotificationParams(Optional.empty())
                .withVesUrl(TEST_VES_URL)
                .withXnfUrl(TEST_XNF_URL)
                .withFileProvider(fileProvider).build();
        simulator.run();
        Exception e = simulator.getThrownException();
        assertTrue(e instanceof ValidationException);

        simulator = Simulator.builder()
                .withDuration(Duration.ofMillis(100))
                .withInterval(Duration.ofMillis(10))
                .withCommonEventHeaderParams(VALID_COMMON_EVENT_HEADER_PARAMS)
                .withPnfRegistrationParams(INVALID_PNF_REGISTRATION_PARAMS_2)
                .withNotificationParams(Optional.empty())
                .withVesUrl(TEST_VES_URL)
                .withXnfUrl(TEST_XNF_URL)
                .withFileProvider(fileProvider).build();
        simulator.run();
        e = simulator.getThrownException();
        assertTrue(e instanceof ValidationException);

        simulator = Simulator.builder()
                .withDuration(Duration.ofMillis(100))
                .withInterval(Duration.ofMillis(10))
                .withCommonEventHeaderParams(VALID_COMMON_EVENT_HEADER_PARAMS)
                .withPnfRegistrationParams(INVALID_PNF_REGISTRATION_PARAMS_3)
                .withNotificationParams(Optional.empty())
                .withVesUrl(TEST_VES_URL)
                .withXnfUrl(TEST_XNF_URL)
                .withFileProvider(fileProvider).build();
        simulator.run();
        e = simulator.getThrownException();
        assertTrue(e instanceof ValidationException);

        simulator = Simulator.builder()
                .withDuration(Duration.ofMillis(100))
                .withInterval(Duration.ofMillis(10))
                .withCommonEventHeaderParams(VALID_COMMON_EVENT_HEADER_PARAMS)
                .withPnfRegistrationParams(VALID_PNF_REGISTRATION_PARAMS)
                .withNotificationParams(INVALID_NOTIFICATION_PARAMS)
                .withVesUrl(TEST_VES_URL)
                .withXnfUrl(TEST_XNF_URL)
                .withFileProvider(fileProvider).build();
        simulator.run();
        e = simulator.getThrownException();
        assertTrue(e instanceof ValidationException);
    }

    @Test
    void simulator_should_send_fileready_message() {
        createSampleFileList();

        HttpClientAdapter httpClientMock = Mockito.mock(HttpClientAdapter.class);
        Simulator simulator = Simulator.builder()
                .withDuration(Duration.ofMillis(100))
                .withInterval(Duration.ofMillis(10))
                .withCommonEventHeaderParams(VALID_COMMON_EVENT_HEADER_PARAMS)
                .withPnfRegistrationParams(Optional.empty())
                .withNotificationParams(VALID_NOTIFICATION_PARAMS)
                .withVesUrl(TEST_VES_URL)
                .withXnfUrl(TEST_XNF_URL)
                .withCustomHttpClientAdapter(httpClientMock)
                .withFileProvider(fileProvider).build();
        simulator.run();
        Exception e = simulator.getThrownException();
        assertNull(e);

        assertTimeout(Duration.ofMillis(150), (Executable) simulator::join);
        verify(httpClientMock, times(1)).send(anyString(), eq(TEST_VES_URL));
    }
}