444660b1cf6fcb46360c148cfeccc4621e4431d0
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / test / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / TestNokiaSvnfmApplication.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia;
17
18 import junit.framework.TestCase;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.*;
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.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.Useless;
25 import org.slf4j.Logger;
26 import org.springframework.boot.context.event.ApplicationReadyEvent;
27 import org.springframework.context.event.ContextClosedEvent;
28 import org.springframework.test.util.ReflectionTestUtils;
29
30 import static junit.framework.TestCase.assertEquals;
31 import static junit.framework.TestCase.assertTrue;
32 import static org.mockito.Mockito.verify;
33
34
35 public class TestNokiaSvnfmApplication {
36     @Mock
37     private SelfRegistrationManager selfRegistrationManager;
38     @Mock
39     private JobManager jobManager;
40     @Mock
41     private Logger logger;
42     @InjectMocks
43     private NokiaSvnfmApplication.SelfRegistrationTrigger selfRegistrationTriggerer;
44     @InjectMocks
45     private NokiaSvnfmApplication.SelfDeRegistrationTrigger selfUnregistrationTriggerer;
46
47
48     @Before
49     public void initMocks() throws Exception {
50         MockitoAnnotations.initMocks(this);
51         ReflectionTestUtils.setField(NokiaSvnfmApplication.class, "logger", logger);
52     }
53
54     /**
55      * Assert that the entry point of the application does not change
56      */
57     @Test
58     public void doNotRename() {
59         //verify
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());
63     }
64
65     /**
66      * Assert that the self registration process is started after the servlet is up and is able to answer REST requests.
67      */
68     @Test
69     public void testRegistrationIsCalledAfterComponentIsUp() throws Exception {
70         //given
71         ApplicationReadyEvent event = Mockito.mock(ApplicationReadyEvent.class);
72         //when
73         selfRegistrationTriggerer.onApplicationEvent(event);
74         //verify
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()));
80     }
81
82     /**
83      * Assert that the self de-registration process is started after the servlet has been ramped down
84      */
85     @Test
86     public void testUnRegistrationIsCalledAfterComponentIsUp() throws Exception {
87         //given
88         ContextClosedEvent event = Mockito.mock(ContextClosedEvent.class);
89         //when
90         selfUnregistrationTriggerer.onApplicationEvent(event);
91         //verify
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()));
99     }
100
101     /**
102      * Failures in registration is logged and propagated
103      */
104     @Test
105     public void failedRegistration() {
106         //given
107         RuntimeException expectedException = new RuntimeException();
108         Mockito.doThrow(expectedException).when(selfRegistrationManager).register();
109         ApplicationReadyEvent event = Mockito.mock(ApplicationReadyEvent.class);
110         //when
111         try {
112             selfRegistrationTriggerer.onApplicationEvent(event);
113             //verify
114             TestCase.fail();
115         } catch (RuntimeException e) {
116             assertEquals(e, expectedException);
117         }
118         verify(logger).error("Self registration failed", expectedException);
119     }
120
121     /**
122      * Failures in de-registration is logged and propagated
123      */
124     @Test
125     public void failedDeRegistration() {
126         //given
127         RuntimeException expectedException = new RuntimeException();
128         Mockito.doThrow(expectedException).when(selfRegistrationManager).deRegister();
129         ContextClosedEvent event = Mockito.mock(ContextClosedEvent.class);
130         //when
131         try {
132             selfUnregistrationTriggerer.onApplicationEvent(event);
133             //verify
134             TestCase.fail();
135         } catch (RuntimeException e) {
136             assertEquals(e, expectedException);
137         }
138         verify(logger).error("Self de-registration failed", expectedException);
139     }
140
141     /**
142      * Spring will instantiate using reflection
143      */
144     @Test
145     public void testUseStaticWay() throws Exception {
146         //verify
147         //the constructor is public even if has no private fields
148         new NokiaSvnfmApplication();
149     }
150
151     /**
152      * static entry point calling an other static entry point can not be tested
153      * only using powermock
154      */
155     @Test
156     @Useless
157     public void useless() throws Exception {
158         try {
159             NokiaSvnfmApplication.main(null);
160         } catch (Exception e) {
161
162         }
163     }
164 }