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.JobManager;
29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
30 import org.springframework.boot.SpringApplication;
31 import org.springframework.boot.context.event.ApplicationReadyEvent;
32 import org.springframework.context.event.ContextClosedEvent;
34 import static junit.framework.TestCase.*;
35 import static org.mockito.Mockito.*;
36 import static org.springframework.test.util.ReflectionTestUtils.setField;
39 public class TestNokiaSvnfmApplication extends TestBase {
41 private JobManager jobManager;
43 private NokiaSvnfmApplication.SelfRegistrationTrigger selfRegistrationTriggerer;
44 private NokiaSvnfmApplication.SelfDeRegistrationTrigger selfUnregistrationTriggerer;
48 public void initMocks() throws Exception {
49 selfRegistrationTriggerer = new NokiaSvnfmApplication.SelfRegistrationTrigger(selfRegistrationManager, jobManager);
50 selfUnregistrationTriggerer = new NokiaSvnfmApplication.SelfDeRegistrationTrigger(selfRegistrationManager, jobManager);
51 setField(NokiaSvnfmApplication.class, "logger", logger);
55 * Assert that the entry point of the application does not change
58 public void doNotRename() {
60 //1. if the entry point is renamed the main class of spring boot in the driverwar must also be changed
61 //2. all classes that use @Autowrire must be in a subpackage relative to this class
62 assertEquals("org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.NokiaSvnfmApplication", NokiaSvnfmApplication.class.getCanonicalName());
66 * Assert that the self registration process is started after the servlet is up and is able to answer REST requests.
69 @SuppressWarnings("squid:S2925") //the execution is asynchronous no other way to wait
70 public void testRegistrationIsCalledAfterComponentIsUp() throws Exception {
72 ApplicationReadyEvent event = Mockito.mock(ApplicationReadyEvent.class);
74 selfRegistrationTriggerer.onApplicationEvent(event);
76 boolean success = false;
79 verify(selfRegistrationManager).register();
80 verify(logger).info("Self registration started");
81 verify(logger).info("Self registration finished");
88 // this forces the event to be fired after the servlet is up (prevents refactor)
89 assertTrue(ApplicationReadyEvent.class.isAssignableFrom(event.getClass()));
93 * Assert that the self de-registration process is started after the servlet has been ramped down
96 public void testUnRegistrationIsCalledAfterComponentIsUp() throws Exception {
98 ContextClosedEvent event = Mockito.mock(ContextClosedEvent.class);
100 selfUnregistrationTriggerer.onApplicationEvent(event);
102 InOrder inOrder = Mockito.inOrder(jobManager, selfRegistrationManager);
103 inOrder.verify(jobManager).prepareForShutdown();
104 inOrder.verify(selfRegistrationManager).deRegister();
105 verify(logger).info("Self de-registration started");
106 verify(logger).info("Self de-registration finished");
107 // this forces the event to be fired after the servlet is down (prevents refactor)
108 assertTrue(ContextClosedEvent.class.isAssignableFrom(event.getClass()));
112 * Assert that the self registration process is started after the servlet is up and is able to answer REST requests.
115 public void testPreparingForShutdownDoesNotStartRegistration() throws Exception {
117 ApplicationReadyEvent event = Mockito.mock(ApplicationReadyEvent.class);
118 when(jobManager.isPreparingForShutDown()).thenReturn(true);
120 selfRegistrationTriggerer.onApplicationEvent(event);
122 verify(selfRegistrationManager, never()).register();
126 * Failures in registration is logged and propagated
129 @SuppressWarnings("squid:S2925") //the execution is asynchronous no other way to wait
130 public void failedFirstRegistration() {
132 Set<RuntimeException> expectedException = new HashSet<>();
133 doAnswer(new Answer() {
135 public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
136 if (expectedException.size() == 0) {
137 RuntimeException e = new RuntimeException();
138 expectedException.add(e);
143 }).when(selfRegistrationManager).register();
144 ApplicationReadyEvent event = Mockito.mock(ApplicationReadyEvent.class);
146 selfRegistrationTriggerer.onApplicationEvent(event);
148 //wait for the registration to succeed
149 boolean success = false;
152 verify(logger).info("Self registration finished");
155 } catch (Exception e2) {
159 verify(logger, times(2)).info("Self registration started");
160 verify(logger).error("Self registration failed", expectedException.iterator().next());
164 * Failures in de-registration is logged and propagated
167 public void failedDeRegistration() {
169 RuntimeException expectedException = new RuntimeException();
170 Mockito.doThrow(expectedException).when(selfRegistrationManager).deRegister();
171 ContextClosedEvent event = Mockito.mock(ContextClosedEvent.class);
174 selfUnregistrationTriggerer.onApplicationEvent(event);
177 } catch (RuntimeException e) {
178 assertEquals(e, expectedException);
180 verify(logger).error("Self de-registration failed", expectedException);
184 * Spring will instantiate using reflection
187 public void testUseStaticWay() throws Exception {
189 //the constructor is public even if has no private fields
190 new NokiaSvnfmApplication();
194 * test spring application bootstrapping
197 public void useless() throws Exception {
198 String[] args = new String[0];
199 SpringApplication springApplicaiton = Mockito.mock(SpringApplication.class);
200 SystemFunctions systemFunctions = SystemFunctions.systemFunctions();
201 when(this.systemFunctions.newSpringApplication(NokiaSvnfmApplication.class)).thenReturn(springApplicaiton);
203 NokiaSvnfmApplication.main(args);
205 verify(springApplicaiton).run(args);