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 junit.framework.TestCase;
21 import org.junit.Before;
22 import org.junit.Test;
24 import org.mockito.invocation.InvocationOnMock;
25 import org.mockito.stubbing.Answer;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions;
28 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.TestUtil;
29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.JobManager;
30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
31 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.Useless;
32 import org.slf4j.Logger;
33 import org.springframework.boot.SpringApplication;
34 import org.springframework.boot.context.event.ApplicationReadyEvent;
35 import org.springframework.context.event.ContextClosedEvent;
36 import org.springframework.test.util.ReflectionTestUtils;
38 import static junit.framework.TestCase.assertEquals;
39 import static junit.framework.TestCase.assertTrue;
40 import static junit.framework.TestCase.fail;
41 import static org.mockito.Mockito.*;
42 import static org.springframework.test.util.ReflectionTestUtils.setField;
45 public class TestNokiaSvnfmApplication extends TestBase {
47 private JobManager jobManager;
49 private NokiaSvnfmApplication.SelfRegistrationTrigger selfRegistrationTriggerer;
50 private NokiaSvnfmApplication.SelfDeRegistrationTrigger selfUnregistrationTriggerer;
54 public void initMocks() throws Exception {
55 selfRegistrationTriggerer = new NokiaSvnfmApplication.SelfRegistrationTrigger(selfRegistrationManager, jobManager);
56 selfUnregistrationTriggerer = new NokiaSvnfmApplication.SelfDeRegistrationTrigger(selfRegistrationManager, jobManager);
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);
80 selfRegistrationTriggerer.onApplicationEvent(event);
82 boolean success = false;
86 verify(selfRegistrationManager).register();
87 verify(logger).info("Self registration started");
88 verify(logger).info("Self registration finished");
96 // this forces the event to be fired after the servlet is up (prevents refactor)
97 assertTrue(ApplicationReadyEvent.class.isAssignableFrom(event.getClass()));
101 * Assert that the self de-registration process is started after the servlet has been ramped down
104 public void testUnRegistrationIsCalledAfterComponentIsUp() throws Exception {
106 ContextClosedEvent event = Mockito.mock(ContextClosedEvent.class);
108 selfUnregistrationTriggerer.onApplicationEvent(event);
110 InOrder inOrder = Mockito.inOrder(jobManager, selfRegistrationManager);
111 inOrder.verify(jobManager).prepareForShutdown();
112 inOrder.verify(selfRegistrationManager).deRegister();
113 verify(logger).info("Self de-registration started");
114 verify(logger).info("Self de-registration finished");
115 // this forces the event to be fired after the servlet is down (prevents refactor)
116 assertTrue(ContextClosedEvent.class.isAssignableFrom(event.getClass()));
120 * Assert that the self registration process is started after the servlet is up and is able to answer REST requests.
123 public void testPreparingForShutdownDoesNotStartRegistration() throws Exception {
125 ApplicationReadyEvent event = Mockito.mock(ApplicationReadyEvent.class);
126 when(jobManager.isPreparingForShutDown()).thenReturn(true);
128 selfRegistrationTriggerer.onApplicationEvent(event);
130 verify(selfRegistrationManager, never()).register();
134 * Failures in registration is logged and propagated
137 @SuppressWarnings("squid:S2925") //the execution is asynchronous no other way to wait
138 public void failedFirstRegistration() {
140 Set<RuntimeException> expectedException = new HashSet<>();
141 doAnswer(new Answer() {
143 public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
144 if(expectedException.size() == 0){
145 RuntimeException e = new RuntimeException();
146 expectedException.add(e);
151 }).when(selfRegistrationManager).register();
152 ApplicationReadyEvent event = Mockito.mock(ApplicationReadyEvent.class);
154 selfRegistrationTriggerer.onApplicationEvent(event);
156 //wait for the registration to succeed
157 boolean success = false;
160 verify(logger).info("Self registration finished");
164 catch (Exception e2){}
167 verify(logger, times(2)).info("Self registration started");
168 verify(logger).error("Self registration failed", expectedException.iterator().next());
172 * Failures in de-registration is logged and propagated
175 public void failedDeRegistration() {
177 RuntimeException expectedException = new RuntimeException();
178 Mockito.doThrow(expectedException).when(selfRegistrationManager).deRegister();
179 ContextClosedEvent event = Mockito.mock(ContextClosedEvent.class);
182 selfUnregistrationTriggerer.onApplicationEvent(event);
185 } catch (RuntimeException e) {
186 assertEquals(e, expectedException);
188 verify(logger).error("Self de-registration failed", expectedException);
192 * Spring will instantiate using reflection
195 public void testUseStaticWay() throws Exception {
197 //the constructor is public even if has no private fields
198 new NokiaSvnfmApplication();
202 * test spring application bootstrapping
205 public void useless() throws Exception {
206 String[] args = new String [0];
207 SpringApplication springApplicaiton = Mockito.mock(SpringApplication.class);
208 SystemFunctions systemFunctions = SystemFunctions.systemFunctions();
209 when(this.systemFunctions.newSpringApplication(NokiaSvnfmApplication.class)).thenReturn(springApplicaiton);
211 NokiaSvnfmApplication.main(args);
213 verify(springApplicaiton).run(args);