2 * ============LICENSE_START=======================================================
3 * BBS-RELOCATION-CPE-AUTHENTICATION-HANDLER
4 * ================================================================================
5 * Copyright (C) 2019 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.bbs.event.processor.controllers;
23 import static org.mockito.Mockito.timeout;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch;
27 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
28 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
29 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
30 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
32 import org.junit.jupiter.api.BeforeEach;
33 import org.junit.jupiter.api.DisplayName;
34 import org.junit.jupiter.api.Test;
35 import org.mockito.Mockito;
36 import org.onap.bbs.event.processor.config.ApplicationConfiguration;
37 import org.onap.bbs.event.processor.pipelines.CpeAuthenticationPipeline;
38 import org.onap.bbs.event.processor.pipelines.ReRegistrationPipeline;
39 import org.onap.bbs.event.processor.pipelines.Scheduler;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
42 import org.springframework.boot.test.context.TestConfiguration;
43 import org.springframework.context.annotation.Bean;
44 import org.springframework.test.web.servlet.MockMvc;
45 import org.springframework.test.web.servlet.MvcResult;
47 @WebMvcTest(BbsEventProcessorController.class)
48 @DisplayName("BBS Event Processor Controllers MVC Unit-Tests")
49 class BbsEventProcessorControllerTest {
52 private MockMvc mockMvc;
55 private ReRegistrationPipeline reRegistrationPipeline;
57 private CpeAuthenticationPipeline cpeAuthenticationPipeline;
59 private Scheduler scheduler;
61 private ApplicationConfiguration configuration;
64 void resetInteractions() {
65 Mockito.reset(scheduler);
69 void sendingHeartBeatRestCall_RespondsWithAlive() throws Exception {
70 MvcResult heartBeatResult = mockMvc.perform(get("/heartbeat")).andReturn();
72 mockMvc.perform(asyncDispatch(heartBeatResult))
73 .andExpect(status().isOk())
74 .andExpect(content().string("bbs-event-processor is alive\n"));
78 void sendingReRegistrationSubmissionRestCall_RespondsWithOk() throws Exception {
79 MvcResult reregistrationSubmissionResult = mockMvc.perform(post("/poll-reregistration-events")).andReturn();
81 mockMvc.perform(asyncDispatch(reregistrationSubmissionResult))
82 .andExpect(status().isOk())
83 .andExpect(content().string("Request submitted\n"));
84 verify(reRegistrationPipeline, timeout(500)).processPnfReRegistrationEvents();
88 void sendingCpeAuthenticationSubmissionRestCall_RespondsWithOk() throws Exception {
89 MvcResult reregistrationSubmissionResult = mockMvc.perform(post("/poll-cpe-authentication-events")).andReturn();
91 mockMvc.perform(asyncDispatch(reregistrationSubmissionResult))
92 .andExpect(status().isOk())
93 .andExpect(content().string("Request submitted\n"));
94 verify(cpeAuthenticationPipeline, timeout(500)).processPnfCpeAuthenticationEvents();
98 void sendingStartTasksRestCall_ifItSucceeds_RespondsWithOk() throws Exception {
99 when(scheduler.reScheduleProcessingTasks()).thenReturn(true);
100 MvcResult startTasksResult = mockMvc.perform(post("/start-tasks")).andReturn();
102 mockMvc.perform(asyncDispatch(startTasksResult))
103 .andExpect(status().isOk())
104 .andExpect(content().string("Initiation of tasks was successful\n"));
105 verify(scheduler).reScheduleProcessingTasks();
109 void sendingStartTasksRestCall_ifItFails_RespondsWithNotAcceptable() throws Exception {
110 when(scheduler.reScheduleProcessingTasks()).thenReturn(false);
111 MvcResult startTasksResult = mockMvc.perform(post("/start-tasks")).andReturn();
113 mockMvc.perform(asyncDispatch(startTasksResult))
114 .andExpect(status().isNotAcceptable())
115 .andExpect(content().string("Initiation of tasks failed\n"));
116 verify(scheduler).reScheduleProcessingTasks();
120 void sendingCancelTasksRestCall_ifItSucceeds_RespondsWithOk() throws Exception {
121 when(scheduler.cancelScheduledProcessingTasks()).thenReturn(true);
122 MvcResult cancellationResult = mockMvc.perform(post("/cancel-tasks")).andReturn();
124 mockMvc.perform(asyncDispatch(cancellationResult))
125 .andExpect(status().isOk())
126 .andExpect(content().string("Cancellation was successful\n"));
127 verify(scheduler).cancelScheduledProcessingTasks();
131 void sendingCancelTasksRestCall_ifItFails_RespondsWithNotAcceptable() throws Exception {
132 when(scheduler.cancelScheduledProcessingTasks()).thenReturn(false);
133 MvcResult cancellationResult = mockMvc.perform(post("/cancel-tasks")).andReturn();
135 mockMvc.perform(asyncDispatch(cancellationResult))
136 .andExpect(status().isNotAcceptable())
137 .andExpect(content().string("Cancellation failed\n"));
138 verify(scheduler).cancelScheduledProcessingTasks();
142 static class ControllerTestConfiguration {
144 ReRegistrationPipeline reRegistrationPipeline() {
145 return Mockito.mock(ReRegistrationPipeline.class);
149 CpeAuthenticationPipeline cpeAuthenticationPipeline() {
150 return Mockito.mock(CpeAuthenticationPipeline.class);
154 Scheduler scheduler() {
155 return Mockito.mock(Scheduler.class);
159 ApplicationConfiguration configuration() {
160 return Mockito.mock(ApplicationConfiguration.class);