summaryrefslogtreecommitdiffstats
path: root/components/bbs-event-processor/src/test/java/org/onap/bbs/event/processor/controllers/BbsEventProcessorControllerTest.java
blob: bacb6c3e0bf45922bbf999de566f2578358fc37a (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
/*
 * ============LICENSE_START=======================================================
 * BBS-RELOCATION-CPE-AUTHENTICATION-HANDLER
 * ================================================================================
 * Copyright (C) 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.bbs.event.processor.controllers;

import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.onap.bbs.event.processor.config.ApplicationConfiguration;
import org.onap.bbs.event.processor.pipelines.CpeAuthenticationPipeline;
import org.onap.bbs.event.processor.pipelines.ReRegistrationPipeline;
import org.onap.bbs.event.processor.pipelines.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;

@WebMvcTest(BbsEventProcessorController.class)
@DisplayName("BBS Event Processor Controllers MVC Unit-Tests")
class BbsEventProcessorControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @Autowired
    private ReRegistrationPipeline reRegistrationPipeline;
    @Autowired
    private CpeAuthenticationPipeline cpeAuthenticationPipeline;
    @Autowired
    private Scheduler scheduler;
    @Autowired
    private ApplicationConfiguration configuration;

    @BeforeEach
    void resetInteractions() {
        Mockito.reset(scheduler);
    }

    @Test
    void sendingHeartBeatRestCall_RespondsWithAlive() throws Exception {
        MvcResult heartBeatResult = mockMvc.perform(get("/heartbeat")).andReturn();

        mockMvc.perform(asyncDispatch(heartBeatResult))
                .andExpect(status().isOk())
                .andExpect(content().string("bbs-event-processor is alive\n"));
    }

    @Test
    void sendingReRegistrationSubmissionRestCall_RespondsWithOk() throws Exception {
        MvcResult reregistrationSubmissionResult = mockMvc.perform(post("/poll-reregistration-events")).andReturn();

        mockMvc.perform(asyncDispatch(reregistrationSubmissionResult))
                .andExpect(status().isOk())
                .andExpect(content().string("Request submitted\n"));
        verify(reRegistrationPipeline, timeout(500)).processPnfReRegistrationEvents();
    }

    @Test
    void sendingCpeAuthenticationSubmissionRestCall_RespondsWithOk() throws Exception {
        MvcResult reregistrationSubmissionResult = mockMvc.perform(post("/poll-cpe-authentication-events")).andReturn();

        mockMvc.perform(asyncDispatch(reregistrationSubmissionResult))
                .andExpect(status().isOk())
                .andExpect(content().string("Request submitted\n"));
        verify(cpeAuthenticationPipeline, timeout(500)).processPnfCpeAuthenticationEvents();
    }

    @Test
    void sendingStartTasksRestCall_ifItSucceeds_RespondsWithOk() throws Exception {
        when(scheduler.reScheduleProcessingTasks()).thenReturn(true);
        MvcResult startTasksResult = mockMvc.perform(post("/start-tasks")).andReturn();

        mockMvc.perform(asyncDispatch(startTasksResult))
                .andExpect(status().isOk())
                .andExpect(content().string("Initiation of tasks was successful\n"));
        verify(scheduler).reScheduleProcessingTasks();
    }

    @Test
    void sendingStartTasksRestCall_ifItFails_RespondsWithNotAcceptable() throws Exception {
        when(scheduler.reScheduleProcessingTasks()).thenReturn(false);
        MvcResult startTasksResult = mockMvc.perform(post("/start-tasks")).andReturn();

        mockMvc.perform(asyncDispatch(startTasksResult))
                .andExpect(status().isNotAcceptable())
                .andExpect(content().string("Initiation of tasks failed\n"));
        verify(scheduler).reScheduleProcessingTasks();
    }

    @Test
    void sendingCancelTasksRestCall_ifItSucceeds_RespondsWithOk() throws Exception {
        when(scheduler.cancelScheduledProcessingTasks()).thenReturn(true);
        MvcResult cancellationResult = mockMvc.perform(post("/cancel-tasks")).andReturn();

        mockMvc.perform(asyncDispatch(cancellationResult))
                .andExpect(status().isOk())
                .andExpect(content().string("Cancellation was successful\n"));
        verify(scheduler).cancelScheduledProcessingTasks();
    }

    @Test
    void sendingCancelTasksRestCall_ifItFails_RespondsWithNotAcceptable() throws Exception {
        when(scheduler.cancelScheduledProcessingTasks()).thenReturn(false);
        MvcResult cancellationResult = mockMvc.perform(post("/cancel-tasks")).andReturn();

        mockMvc.perform(asyncDispatch(cancellationResult))
                .andExpect(status().isNotAcceptable())
                .andExpect(content().string("Cancellation failed\n"));
        verify(scheduler).cancelScheduledProcessingTasks();
    }

    @TestConfiguration
    static class ControllerTestConfiguration {
        @Bean
        ReRegistrationPipeline reRegistrationPipeline() {
            return Mockito.mock(ReRegistrationPipeline.class);
        }

        @Bean
        CpeAuthenticationPipeline cpeAuthenticationPipeline() {
            return Mockito.mock(CpeAuthenticationPipeline.class);
        }

        @Bean
        Scheduler scheduler() {
            return Mockito.mock(Scheduler.class);
        }

        @Bean
        ApplicationConfiguration configuration() {
            return Mockito.mock(ApplicationConfiguration.class);
        }
    }
}