bacb6c3e0bf45922bbf999de566f2578358fc37a
[dcaegen2/services.git] /
1 /*
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
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.bbs.event.processor.controllers;
22
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;
31
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;
46
47 @WebMvcTest(BbsEventProcessorController.class)
48 @DisplayName("BBS Event Processor Controllers MVC Unit-Tests")
49 class BbsEventProcessorControllerTest {
50
51     @Autowired
52     private MockMvc mockMvc;
53
54     @Autowired
55     private ReRegistrationPipeline reRegistrationPipeline;
56     @Autowired
57     private CpeAuthenticationPipeline cpeAuthenticationPipeline;
58     @Autowired
59     private Scheduler scheduler;
60     @Autowired
61     private ApplicationConfiguration configuration;
62
63     @BeforeEach
64     void resetInteractions() {
65         Mockito.reset(scheduler);
66     }
67
68     @Test
69     void sendingHeartBeatRestCall_RespondsWithAlive() throws Exception {
70         MvcResult heartBeatResult = mockMvc.perform(get("/heartbeat")).andReturn();
71
72         mockMvc.perform(asyncDispatch(heartBeatResult))
73                 .andExpect(status().isOk())
74                 .andExpect(content().string("bbs-event-processor is alive\n"));
75     }
76
77     @Test
78     void sendingReRegistrationSubmissionRestCall_RespondsWithOk() throws Exception {
79         MvcResult reregistrationSubmissionResult = mockMvc.perform(post("/poll-reregistration-events")).andReturn();
80
81         mockMvc.perform(asyncDispatch(reregistrationSubmissionResult))
82                 .andExpect(status().isOk())
83                 .andExpect(content().string("Request submitted\n"));
84         verify(reRegistrationPipeline, timeout(500)).processPnfReRegistrationEvents();
85     }
86
87     @Test
88     void sendingCpeAuthenticationSubmissionRestCall_RespondsWithOk() throws Exception {
89         MvcResult reregistrationSubmissionResult = mockMvc.perform(post("/poll-cpe-authentication-events")).andReturn();
90
91         mockMvc.perform(asyncDispatch(reregistrationSubmissionResult))
92                 .andExpect(status().isOk())
93                 .andExpect(content().string("Request submitted\n"));
94         verify(cpeAuthenticationPipeline, timeout(500)).processPnfCpeAuthenticationEvents();
95     }
96
97     @Test
98     void sendingStartTasksRestCall_ifItSucceeds_RespondsWithOk() throws Exception {
99         when(scheduler.reScheduleProcessingTasks()).thenReturn(true);
100         MvcResult startTasksResult = mockMvc.perform(post("/start-tasks")).andReturn();
101
102         mockMvc.perform(asyncDispatch(startTasksResult))
103                 .andExpect(status().isOk())
104                 .andExpect(content().string("Initiation of tasks was successful\n"));
105         verify(scheduler).reScheduleProcessingTasks();
106     }
107
108     @Test
109     void sendingStartTasksRestCall_ifItFails_RespondsWithNotAcceptable() throws Exception {
110         when(scheduler.reScheduleProcessingTasks()).thenReturn(false);
111         MvcResult startTasksResult = mockMvc.perform(post("/start-tasks")).andReturn();
112
113         mockMvc.perform(asyncDispatch(startTasksResult))
114                 .andExpect(status().isNotAcceptable())
115                 .andExpect(content().string("Initiation of tasks failed\n"));
116         verify(scheduler).reScheduleProcessingTasks();
117     }
118
119     @Test
120     void sendingCancelTasksRestCall_ifItSucceeds_RespondsWithOk() throws Exception {
121         when(scheduler.cancelScheduledProcessingTasks()).thenReturn(true);
122         MvcResult cancellationResult = mockMvc.perform(post("/cancel-tasks")).andReturn();
123
124         mockMvc.perform(asyncDispatch(cancellationResult))
125                 .andExpect(status().isOk())
126                 .andExpect(content().string("Cancellation was successful\n"));
127         verify(scheduler).cancelScheduledProcessingTasks();
128     }
129
130     @Test
131     void sendingCancelTasksRestCall_ifItFails_RespondsWithNotAcceptable() throws Exception {
132         when(scheduler.cancelScheduledProcessingTasks()).thenReturn(false);
133         MvcResult cancellationResult = mockMvc.perform(post("/cancel-tasks")).andReturn();
134
135         mockMvc.perform(asyncDispatch(cancellationResult))
136                 .andExpect(status().isNotAcceptable())
137                 .andExpect(content().string("Cancellation failed\n"));
138         verify(scheduler).cancelScheduledProcessingTasks();
139     }
140
141     @TestConfiguration
142     static class ControllerTestConfiguration {
143         @Bean
144         ReRegistrationPipeline reRegistrationPipeline() {
145             return Mockito.mock(ReRegistrationPipeline.class);
146         }
147
148         @Bean
149         CpeAuthenticationPipeline cpeAuthenticationPipeline() {
150             return Mockito.mock(CpeAuthenticationPipeline.class);
151         }
152
153         @Bean
154         Scheduler scheduler() {
155             return Mockito.mock(Scheduler.class);
156         }
157
158         @Bean
159         ApplicationConfiguration configuration() {
160             return Mockito.mock(ApplicationConfiguration.class);
161         }
162     }
163 }