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.junit.Before;
20 import org.junit.Test;
22 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager;
23 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.JobManager;
24 import org.slf4j.Logger;
25 import org.springframework.boot.context.event.ApplicationReadyEvent;
26 import org.springframework.context.event.ContextClosedEvent;
27 import org.springframework.test.util.ReflectionTestUtils;
29 import static junit.framework.TestCase.assertEquals;
30 import static junit.framework.TestCase.assertTrue;
31 import static org.mockito.Mockito.verify;
34 public class TestNokiaSvnfmApplication {
36 private SelfRegistrationManager selfRegistrationManager;
38 private JobManager jobManager;
40 private Logger logger;
42 private NokiaSvnfmApplication.SelfRegistrationTrigger selfRegistrationTriggerer;
44 private NokiaSvnfmApplication.SelfDeRegistrationTrigger selfUnregistrationTriggerer;
48 public void initMocks() throws Exception {
49 MockitoAnnotations.initMocks(this);
50 ReflectionTestUtils.setField(NokiaSvnfmApplication.class, "logger", logger);
54 * Assert that the entry point of the application does not change
57 public void doNotRename() {
59 //1. if the entry point is renamed the main class of spring boot in the driverwar must also be changed
60 //2. all classes that use @Autowrire must be in a subpackage relative to this class
61 assertEquals("org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.NokiaSvnfmApplication", NokiaSvnfmApplication.class.getCanonicalName());
65 * Assert that the self registration process is started after the servlet is up and is able to answer REST requests.
68 public void testRegistrationIsCalledAfterComponentIsUp() throws Exception {
70 ApplicationReadyEvent event = Mockito.mock(ApplicationReadyEvent.class);
72 selfRegistrationTriggerer.onApplicationEvent(event);
74 verify(selfRegistrationManager).register();
75 verify(logger).info("Self registration started");
76 verify(logger).info("Self registration finished");
77 // this forces the event to be fired after the servlet is up (prevents refactor)
78 assertTrue(ApplicationReadyEvent.class.isAssignableFrom(event.getClass()));
82 * Assert that the self de-registration process is started after the servlet has been ramped down
85 public void testUnRegistrationIsCalledAfterComponentIsUp() throws Exception {
87 ContextClosedEvent event = Mockito.mock(ContextClosedEvent.class);
89 selfUnregistrationTriggerer.onApplicationEvent(event);
91 InOrder inOrder = Mockito.inOrder(jobManager, selfRegistrationManager);
92 inOrder.verify(jobManager).prepareForShutdown();
93 inOrder.verify(selfRegistrationManager).deRegister();
94 verify(logger).info("Self de-registration started");
95 verify(logger).info("Self de-registration finished");
96 // this forces the event to be fired after the servlet is down (prevents refactor)
97 assertTrue(ContextClosedEvent.class.isAssignableFrom(event.getClass()));
101 * Failures in registration is logged and propagated
104 public void failedRegistration() {
106 RuntimeException expectedException = new RuntimeException();
107 Mockito.doThrow(expectedException).when(selfRegistrationManager).register();
108 ApplicationReadyEvent event = Mockito.mock(ApplicationReadyEvent.class);
111 selfRegistrationTriggerer.onApplicationEvent(event);
114 } catch (RuntimeException e) {
115 assertEquals(e, expectedException);
117 verify(logger).error("Self registration failed", expectedException);
121 * Failures in de-registration is logged and propagated
124 public void failedDeRegistration() {
126 RuntimeException expectedException = new RuntimeException();
127 Mockito.doThrow(expectedException).when(selfRegistrationManager).deRegister();
128 ContextClosedEvent event = Mockito.mock(ContextClosedEvent.class);
131 selfUnregistrationTriggerer.onApplicationEvent(event);
134 } catch (RuntimeException e) {
135 assertEquals(e, expectedException);
137 verify(logger).error("Self de-registration failed", expectedException);
141 * Spring will instantiate using reflection
144 public void testUseStaticWay() throws Exception {
146 //the constructor is public even if has no private fields
147 new NokiaSvnfmApplication();
151 * static entry point calling an other static entry point can not be tested
154 public void useless() throws Exception {
156 NokiaSvnfmApplication.main(null);
157 } catch (Exception e) {