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()));
99 private void useVfc(ApplicationReadyEvent event) {
100 ConfigurableApplicationContext context = Mockito.mock(ConfigurableApplicationContext.class);
101 ConfigurableEnvironment environment = Mockito.mock(ConfigurableEnvironment.class);
102 when(context.getEnvironment()).thenReturn(environment);
103 when(event.getApplicationContext()).thenReturn(context);
104 when(environment.getActiveProfiles()).thenReturn(new String[]{});
107 private void useVfc(ContextClosedEvent event) {
108 ApplicationContext context = Mockito.mock(ApplicationContext.class);
109 when(context.getEnvironment()).thenReturn(environment);
110 when(event.getApplicationContext()).thenReturn(context);
111 when(environment.getActiveProfiles()).thenReturn(new String[]{});
115 * Assert that the self de-registration process is started after the servlet has been ramped down
118 public void testUnRegistrationIsCalledAfterComponentIsUp() throws Exception {
120 ContextClosedEvent event = Mockito.mock(ContextClosedEvent.class);
123 selfUnregistrationTriggerer.onApplicationEvent(event);
125 InOrder inOrder = Mockito.inOrder(jobManagerForVfc, jobManagerForSo, selfRegistrationManagerForVfc);
126 inOrder.verify(jobManagerForVfc).prepareForShutdown();
127 inOrder.verify(jobManagerForSo).prepareForShutdown();
128 inOrder.verify(selfRegistrationManagerForVfc).deRegister();
129 verify(logger).info("Self de-registration started");
130 verify(logger).info("Self de-registration finished");
131 // this forces the event to be fired after the servlet is down (prevents refactor)
132 assertTrue(ContextClosedEvent.class.isAssignableFrom(event.getClass()));
136 * Assert that the self registration process is started after the servlet is up and is able to answer REST requests.
139 public void testPreparingForShutdownDoesNotStartRegistration() throws Exception {
141 ApplicationReadyEvent event = Mockito.mock(ApplicationReadyEvent.class);
142 when(jobManagerForVfc.isPreparingForShutDown()).thenReturn(true);
144 selfRegistrationTriggerer.onApplicationEvent(event);
146 verify(selfRegistrationManagerForVfc, never()).register();
150 * Failures in registration is logged and propagated
153 @SuppressWarnings("squid:S2925") //the execution is asynchronous no other way to wait
154 public void failedFirstRegistration() {
157 Set<RuntimeException> expectedException = new HashSet<>();
158 doAnswer(new Answer() {
160 public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
161 if (expectedException.size() == 0) {
162 RuntimeException e = new RuntimeException();
163 expectedException.add(e);
168 }).when(selfRegistrationManagerForVfc).register();
169 ApplicationReadyEvent event = Mockito.mock(ApplicationReadyEvent.class);
172 selfRegistrationTriggerer.onApplicationEvent(event);
174 //wait for the registration to succeed
175 boolean success = false;
178 verify(logger).info("Self registration finished");
181 } catch (Exception e2) {
185 verify(logger, times(2)).info("Self registration started");
186 verify(logger).error("Self registration failed", expectedException.iterator().next());
190 * Failures in de-registration is logged and propagated
193 public void failedDeRegistration() {
195 RuntimeException expectedException = new RuntimeException();
196 Mockito.doThrow(expectedException).when(selfRegistrationManagerForVfc).deRegister();
197 ContextClosedEvent event = Mockito.mock(ContextClosedEvent.class);
201 selfUnregistrationTriggerer.onApplicationEvent(event);
204 } catch (RuntimeException e) {
205 assertEquals(e, expectedException);
207 verify(logger).error("Self de-registration failed", expectedException);
211 * Spring will instantiate using reflection
214 public void testUseStaticWay() throws Exception {
216 //the constructor is public even if has no private fields
217 new NokiaSvnfmApplication();
221 * test spring application bootstrapping
224 public void useless() throws Exception {
225 String[] args = new String[0];
226 SpringApplication springApplicaiton = Mockito.mock(SpringApplication.class);
227 SystemFunctions systemFunctions = SystemFunctions.systemFunctions();
228 when(this.systemFunctions.newSpringApplication(NokiaSvnfmApplication.class)).thenReturn(springApplicaiton);
230 NokiaSvnfmApplication.main(args);
232 verify(springApplicaiton).run(args);