2 * Copyright 2016-2017, Nokia Corporation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia;
18 import java.util.HashSet;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.mockito.InOrder;
23 import org.mockito.Mock;
24 import org.mockito.Mockito;
25 import org.mockito.invocation.InvocationOnMock;
26 import org.mockito.stubbing.Answer;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions;
28 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.JobManagerForSo;
29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.JobManagerForVfc;
30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
31 import org.springframework.boot.SpringApplication;
32 import org.springframework.boot.context.event.ApplicationReadyEvent;
33 import org.springframework.context.ApplicationContext;
34 import org.springframework.context.ConfigurableApplicationContext;
35 import org.springframework.context.event.ContextClosedEvent;
36 import org.springframework.core.env.ConfigurableEnvironment;
38 import static junit.framework.TestCase.*;
39 import static org.mockito.Mockito.*;
40 import static org.springframework.test.util.ReflectionTestUtils.setField;
43 public class TestNokiaSvnfmApplication extends TestBase {
45 private JobManagerForVfc jobManagerForVfc;
47 private JobManagerForSo jobManagerForSo;
49 private NokiaSvnfmApplication.SelfRegistrationTrigger selfRegistrationTriggerer;
50 private NokiaSvnfmApplication.SelfDeRegistrationTrigger selfUnregistrationTriggerer;
54 public void initMocks() throws Exception {
55 selfRegistrationTriggerer = new NokiaSvnfmApplication.SelfRegistrationTrigger(selfRegistrationManagerForVfc, selfRegistrationManagerForSo, jobManagerForSo, jobManagerForVfc);
56 selfUnregistrationTriggerer = new NokiaSvnfmApplication.SelfDeRegistrationTrigger(selfRegistrationManagerForVfc, selfRegistrationManagerForSo, jobManagerForSo, jobManagerForVfc);
57 setField(NokiaSvnfmApplication.class, "logger", logger);
61 * Assert that the entry point of the application does not change
64 public void doNotRename() {
66 //1. if the entry point is renamed the main class of spring boot in the driverwar must also be changed
67 //2. all classes that use @Autowrire must be in a subpackage relative to this class
68 assertEquals("org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.NokiaSvnfmApplication", NokiaSvnfmApplication.class.getCanonicalName());
72 * Assert that the self registration process is started after the servlet is up and is able to answer REST requests.
75 @SuppressWarnings("squid:S2925") //the execution is asynchronous no other way to wait
76 public void testRegistrationIsCalledAfterComponentIsUp() throws Exception {
78 ApplicationReadyEvent event = Mockito.mock(ApplicationReadyEvent.class);
81 selfRegistrationTriggerer.onApplicationEvent(event);
83 boolean success = false;
86 verify(selfRegistrationManagerForVfc).register();
87 verify(logger).info("Self registration started");
88 verify(logger).info("Self registration finished");
95 // this forces the event to be fired after the servlet is up (prevents refactor)
96 assertTrue(ApplicationReadyEvent.class.isAssignableFrom(event.getClass()));
100 * Assert that the self registration process is started after the servlet is up and is able to answer REST requests.
103 @SuppressWarnings("squid:S2925") //the execution is asynchronous no other way to wait
104 public void testRegistrationIsNotCalledIfPreparingForShutdown() throws Exception {
106 ApplicationReadyEvent event = Mockito.mock(ApplicationReadyEvent.class);
108 when(jobManagerForSo.isPreparingForShutDown()).thenReturn(true);
110 selfRegistrationTriggerer.onApplicationEvent(event);
112 boolean success = false;
115 verify(logger).warn("Component is preparing for shutdown giving up component start");
122 verify(selfRegistrationManagerForSo, never()).register();
123 // this forces the event to be fired after the servlet is up (prevents refactor)
124 assertTrue(ApplicationReadyEvent.class.isAssignableFrom(event.getClass()));
129 * Assert that the self registration process is started after the servlet is up and is able to answer REST requests.
132 @SuppressWarnings("squid:S2925") //the execution is asynchronous no other way to wait
133 public void testRegistrationIsCalledAfterComponentIsUpForSo() throws Exception {
135 ApplicationReadyEvent event = Mockito.mock(ApplicationReadyEvent.class);
138 selfRegistrationTriggerer.onApplicationEvent(event);
140 boolean success = false;
143 verify(selfRegistrationManagerForSo).register();
144 verify(logger).info("Self registration started");
145 verify(logger).info("Self registration finished");
152 // this forces the event to be fired after the servlet is up (prevents refactor)
153 assertTrue(ApplicationReadyEvent.class.isAssignableFrom(event.getClass()));
157 * Failuires in the self registration process is retried for SO
160 @SuppressWarnings("squid:S2925") //the execution is asynchronous no other way to wait
161 public void testFailuresInRegistrationIsRetriedForSo() throws Exception {
163 ApplicationReadyEvent event = Mockito.mock(ApplicationReadyEvent.class);
164 RuntimeException e2 = new RuntimeException();
166 Set<Boolean> calls = new HashSet<>();
167 doAnswer(new Answer() {
169 public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
170 if (calls.size() == 0) {
176 }).when(selfRegistrationManagerForSo).register();
178 selfRegistrationTriggerer.onApplicationEvent(event);
180 boolean success = false;
183 verify(logger).info("Self registration finished");
190 verify(selfRegistrationManagerForSo, times(2)).register();
191 verify(systemFunctions).sleep(5000);
192 verify(logger, times(2)).info("Self registration started");
193 verify(logger).error("Self registration failed", e2);
194 // this forces the event to be fired after the servlet is up (prevents refactor)
195 assertTrue(ApplicationReadyEvent.class.isAssignableFrom(event.getClass()));
199 * Failuires in the self registration process is retried for VFC
202 @SuppressWarnings("squid:S2925") //the execution is asynchronous no other way to wait
203 public void testFailuresInRegistrationIsRetriedForVfc() throws Exception {
205 ApplicationReadyEvent event = Mockito.mock(ApplicationReadyEvent.class);
206 RuntimeException e2 = new RuntimeException();
208 Set<Boolean> calls = new HashSet<>();
209 doAnswer(new Answer() {
211 public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
212 if (calls.size() == 0) {
218 }).when(selfRegistrationManagerForVfc).register();
220 selfRegistrationTriggerer.onApplicationEvent(event);
222 boolean success = false;
225 verify(logger).info("Self registration finished");
232 verify(selfRegistrationManagerForVfc, times(2)).register();
233 verify(systemFunctions).sleep(5000);
234 verify(logger, times(2)).info("Self registration started");
235 verify(logger).error("Self registration failed", e2);
236 // this forces the event to be fired after the servlet is up (prevents refactor)
237 assertTrue(ApplicationReadyEvent.class.isAssignableFrom(event.getClass()));
240 private void useSo(ApplicationReadyEvent event) {
241 ConfigurableApplicationContext context = Mockito.mock(ConfigurableApplicationContext.class);
242 ConfigurableEnvironment environment = Mockito.mock(ConfigurableEnvironment.class);
243 when(context.getEnvironment()).thenReturn(environment);
244 when(event.getApplicationContext()).thenReturn(context);
245 when(environment.getActiveProfiles()).thenReturn(new String[]{"direct"});
248 private void useVfc(ApplicationReadyEvent event) {
249 ConfigurableApplicationContext context = Mockito.mock(ConfigurableApplicationContext.class);
250 ConfigurableEnvironment environment = Mockito.mock(ConfigurableEnvironment.class);
251 when(context.getEnvironment()).thenReturn(environment);
252 when(event.getApplicationContext()).thenReturn(context);
253 when(environment.getActiveProfiles()).thenReturn(new String[]{});
256 private void useVfc(ContextClosedEvent event) {
257 ApplicationContext context = Mockito.mock(ApplicationContext.class);
258 when(context.getEnvironment()).thenReturn(environment);
259 when(event.getApplicationContext()).thenReturn(context);
260 when(environment.getActiveProfiles()).thenReturn(new String[]{});
263 private void useSo(ContextClosedEvent event) {
264 ApplicationContext context = Mockito.mock(ApplicationContext.class);
265 when(context.getEnvironment()).thenReturn(environment);
266 when(event.getApplicationContext()).thenReturn(context);
267 when(environment.getActiveProfiles()).thenReturn(new String[]{"direct"});
271 * Assert that the self de-registration process is started after the servlet has been ramped down
274 public void testUnRegistrationIsCalledAfterComponentIsUp() throws Exception {
276 ContextClosedEvent event = Mockito.mock(ContextClosedEvent.class);
279 selfUnregistrationTriggerer.onApplicationEvent(event);
281 InOrder inOrder = Mockito.inOrder(jobManagerForVfc, jobManagerForSo, selfRegistrationManagerForVfc);
282 inOrder.verify(jobManagerForVfc).prepareForShutdown();
283 inOrder.verify(jobManagerForSo).prepareForShutdown();
284 inOrder.verify(selfRegistrationManagerForVfc).deRegister();
285 verify(logger).info("Self de-registration started");
286 verify(logger).info("Self de-registration finished");
287 // this forces the event to be fired after the servlet is down (prevents refactor)
288 assertTrue(ContextClosedEvent.class.isAssignableFrom(event.getClass()));
292 * Assert that the self de-registration process is started after the servlet has been ramped down
295 public void testUnRegistrationIsCalledAfterComponentIsUpForSo() throws Exception {
297 ContextClosedEvent event = Mockito.mock(ContextClosedEvent.class);
300 selfUnregistrationTriggerer.onApplicationEvent(event);
302 InOrder inOrder = Mockito.inOrder(jobManagerForVfc, jobManagerForSo, selfRegistrationManagerForSo);
303 inOrder.verify(jobManagerForVfc).prepareForShutdown();
304 inOrder.verify(jobManagerForSo).prepareForShutdown();
305 inOrder.verify(selfRegistrationManagerForSo).deRegister();
306 verify(logger).info("Self de-registration started");
307 verify(logger).info("Self de-registration finished");
308 // this forces the event to be fired after the servlet is down (prevents refactor)
309 assertTrue(ContextClosedEvent.class.isAssignableFrom(event.getClass()));
313 * Assert that the self registration process is started after the servlet is up and is able to answer REST requests.
316 public void testPreparingForShutdownDoesNotStartRegistration() throws Exception {
318 ApplicationReadyEvent event = Mockito.mock(ApplicationReadyEvent.class);
319 when(jobManagerForVfc.isPreparingForShutDown()).thenReturn(true);
321 selfRegistrationTriggerer.onApplicationEvent(event);
323 verify(selfRegistrationManagerForVfc, never()).register();
327 * Failures in registration is logged and propagated
330 @SuppressWarnings("squid:S2925") //the execution is asynchronous no other way to wait
332 public void failedFirstRegistration() {
334 Set<RuntimeException> expectedException = new HashSet<>();
335 doAnswer(new Answer() {
337 public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
338 if (expectedException.size() == 0) {
339 RuntimeException e = new RuntimeException();
340 expectedException.add(e);
345 }).when(selfRegistrationManagerForVfc).register();
346 ApplicationReadyEvent event = Mockito.mock(ApplicationReadyEvent.class);
349 selfRegistrationTriggerer.onApplicationEvent(event);
351 //wait for the registration to succeed
352 boolean success = false;
355 verify(logger).info("Self registration finished");
358 } catch (Exception e2) {
362 verify(logger, times(2)).info("Self registration started");
363 verify(logger).error("Self registration failed", expectedException.iterator().next());
367 * Failures in de-registration is logged and propagated
370 public void failedDeRegistration() {
372 RuntimeException expectedException = new RuntimeException();
373 Mockito.doThrow(expectedException).when(selfRegistrationManagerForVfc).deRegister();
374 ContextClosedEvent event = Mockito.mock(ContextClosedEvent.class);
378 selfUnregistrationTriggerer.onApplicationEvent(event);
381 } catch (RuntimeException e) {
382 assertEquals(e, expectedException);
384 verify(logger).error("Self de-registration failed", expectedException);
388 * Spring will instantiate using reflection
391 public void testUseStaticWay() throws Exception {
393 //the constructor is public even if has no private fields
394 new NokiaSvnfmApplication();
398 * test spring application bootstrapping
401 public void useless() throws Exception {
402 String[] args = new String[0];
403 SpringApplication springApplicaiton = Mockito.mock(SpringApplication.class);
404 SystemFunctions systemFunctions = SystemFunctions.systemFunctions();
405 when(this.systemFunctions.newSpringApplication(NokiaSvnfmApplication.class)).thenReturn(springApplicaiton);
407 NokiaSvnfmApplication.main(args);
409 verify(springApplicaiton).run(args);