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 junit.framework.TestCase;
19 import org.apache.log4j.Logger;
20 import org.junit.Before;
21 import org.junit.Test;
23 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager;
24 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.TestUtil;
25 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.JobManager;
26 import org.springframework.boot.context.event.ApplicationReadyEvent;
27 import org.springframework.context.event.ContextClosedEvent;
28 import org.springframework.test.util.ReflectionTestUtils;
30 import static junit.framework.TestCase.assertEquals;
31 import static junit.framework.TestCase.assertTrue;
32 import static org.mockito.Mockito.verify;
35 public class TestNokiaSvnfmApplication {
37 private SelfRegistrationManager selfRegistrationManager;
39 private JobManager jobManager;
41 private Logger logger;
43 private NokiaSvnfmApplication.SelfRegistrationTrigger selfRegistrationTriggerer;
45 private NokiaSvnfmApplication.SelfDeRegistrationTrigger selfUnregistrationTriggerer;
49 public void initMocks() throws Exception {
50 MockitoAnnotations.initMocks(this);
51 ReflectionTestUtils.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 public void testRegistrationIsCalledAfterComponentIsUp() throws Exception {
71 ApplicationReadyEvent event = Mockito.mock(ApplicationReadyEvent.class);
73 selfRegistrationTriggerer.onApplicationEvent(event);
75 verify(selfRegistrationManager).register();
76 verify(logger).info("Self registration started");
77 verify(logger).info("Self registration finished");
78 // this forces the event to be fired after the servlet is up (prevents refactor)
79 assertTrue(ApplicationReadyEvent.class.isAssignableFrom(event.getClass()));
83 * Assert that the self de-registration process is started after the servlet has been ramped down
86 public void testUnRegistrationIsCalledAfterComponentIsUp() throws Exception {
88 ContextClosedEvent event = Mockito.mock(ContextClosedEvent.class);
90 selfUnregistrationTriggerer.onApplicationEvent(event);
92 InOrder inOrder = Mockito.inOrder(jobManager, selfRegistrationManager);
93 inOrder.verify(jobManager).prepareForShutdown();
94 inOrder.verify(selfRegistrationManager).deRegister();
95 verify(logger).info("Self de-registration started");
96 verify(logger).info("Self de-registration finished");
97 // this forces the event to be fired after the servlet is down (prevents refactor)
98 assertTrue(ContextClosedEvent.class.isAssignableFrom(event.getClass()));
102 * Failures in registration is logged and propagated
105 public void failedRegistration() {
107 RuntimeException expectedException = new RuntimeException();
108 Mockito.doThrow(expectedException).when(selfRegistrationManager).register();
109 ApplicationReadyEvent event = Mockito.mock(ApplicationReadyEvent.class);
112 selfRegistrationTriggerer.onApplicationEvent(event);
115 } catch (RuntimeException e) {
116 assertEquals(e, expectedException);
118 verify(logger).error("Self registration failed", expectedException);
122 * Failures in de-registration is logged and propagated
125 public void failedDeRegistration() {
127 RuntimeException expectedException = new RuntimeException();
128 Mockito.doThrow(expectedException).when(selfRegistrationManager).deRegister();
129 ContextClosedEvent event = Mockito.mock(ContextClosedEvent.class);
132 selfUnregistrationTriggerer.onApplicationEvent(event);
135 } catch (RuntimeException e) {
136 assertEquals(e, expectedException);
138 verify(logger).error("Self de-registration failed", expectedException);
142 * Spring will instantiate using reflection
145 public void testUseStaticWay() throws Exception {
147 //the constructor is public even if has no private fields
148 new NokiaSvnfmApplication();
152 * static entry point calling an other static entry point can not be tested
155 public void useless() throws Exception {
157 NokiaSvnfmApplication.main(null);
158 } catch (Exception e) {