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