b4eed27cfd98fb912f92424affad93924dfe09f5
[vfc/nfvo/driver/vnfm/svnfm.git] /
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 java.util.HashSet;
19 import java.util.Set;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.mockito.InOrder;
23 import org.mockito.Mock;
24 import org.mockito.Mockito;
25 import org.mockito.invocation.InvocationOnMock;
26 import org.mockito.stubbing.Answer;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions;
28 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.JobManager;
29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
30 import org.springframework.boot.SpringApplication;
31 import org.springframework.boot.context.event.ApplicationReadyEvent;
32 import org.springframework.context.event.ContextClosedEvent;
33
34 import static junit.framework.TestCase.*;
35 import static org.mockito.Mockito.*;
36 import static org.springframework.test.util.ReflectionTestUtils.setField;
37
38
39 public class TestNokiaSvnfmApplication extends TestBase {
40     @Mock
41     private JobManager jobManager;
42
43     private NokiaSvnfmApplication.SelfRegistrationTrigger selfRegistrationTriggerer;
44     private NokiaSvnfmApplication.SelfDeRegistrationTrigger selfUnregistrationTriggerer;
45
46
47     @Before
48     public void initMocks() throws Exception {
49         selfRegistrationTriggerer = new NokiaSvnfmApplication.SelfRegistrationTrigger(selfRegistrationManager, jobManager);
50         selfUnregistrationTriggerer = new NokiaSvnfmApplication.SelfDeRegistrationTrigger(selfRegistrationManager, jobManager);
51         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     @SuppressWarnings("squid:S2925") //the execution is asynchronous no other way to wait
70     public void testRegistrationIsCalledAfterComponentIsUp() throws Exception {
71         //given
72         ApplicationReadyEvent event = Mockito.mock(ApplicationReadyEvent.class);
73         //when
74         selfRegistrationTriggerer.onApplicationEvent(event);
75         //verify
76         boolean success = false;
77         while (!success) {
78             try {
79                 verify(selfRegistrationManager).register();
80                 verify(logger).info("Self registration started");
81                 verify(logger).info("Self registration finished");
82                 success = true;
83             } catch (Error e) {
84
85             }
86             Thread.sleep(10);
87         }
88         // this forces the event to be fired after the servlet is up (prevents refactor)
89         assertTrue(ApplicationReadyEvent.class.isAssignableFrom(event.getClass()));
90     }
91
92     /**
93      * Assert that the self de-registration process is started after the servlet has been ramped down
94      */
95     @Test
96     public void testUnRegistrationIsCalledAfterComponentIsUp() throws Exception {
97         //given
98         ContextClosedEvent event = Mockito.mock(ContextClosedEvent.class);
99         //when
100         selfUnregistrationTriggerer.onApplicationEvent(event);
101         //verify
102         InOrder inOrder = Mockito.inOrder(jobManager, selfRegistrationManager);
103         inOrder.verify(jobManager).prepareForShutdown();
104         inOrder.verify(selfRegistrationManager).deRegister();
105         verify(logger).info("Self de-registration started");
106         verify(logger).info("Self de-registration finished");
107         // this forces the event to be fired after the servlet is down (prevents refactor)
108         assertTrue(ContextClosedEvent.class.isAssignableFrom(event.getClass()));
109     }
110
111     /**
112      * Assert that the self registration process is started after the servlet is up and is able to answer REST requests.
113      */
114     @Test
115     public void testPreparingForShutdownDoesNotStartRegistration() throws Exception {
116         //given
117         ApplicationReadyEvent event = Mockito.mock(ApplicationReadyEvent.class);
118         when(jobManager.isPreparingForShutDown()).thenReturn(true);
119         //when
120         selfRegistrationTriggerer.onApplicationEvent(event);
121         //verify
122         verify(selfRegistrationManager, never()).register();
123     }
124
125     /**
126      * Failures in registration is logged and propagated
127      */
128     @Test
129     @SuppressWarnings("squid:S2925") //the execution is asynchronous no other way to wait
130     public void failedFirstRegistration() {
131         //given
132         Set<RuntimeException> expectedException = new HashSet<>();
133         doAnswer(new Answer() {
134             @Override
135             public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
136                 if (expectedException.size() == 0) {
137                     RuntimeException e = new RuntimeException();
138                     expectedException.add(e);
139                     throw e;
140                 }
141                 return null;
142             }
143         }).when(selfRegistrationManager).register();
144         ApplicationReadyEvent event = Mockito.mock(ApplicationReadyEvent.class);
145         //when
146         selfRegistrationTriggerer.onApplicationEvent(event);
147         //verify
148         //wait for the registration to succeed
149         boolean success = false;
150         while (!success) {
151             try {
152                 verify(logger).info("Self registration finished");
153                 success = true;
154                 Thread.sleep(10);
155             } catch (Exception e2) {
156             } catch (Error e) {
157             }
158         }
159         verify(logger, times(2)).info("Self registration started");
160         verify(logger).error("Self registration failed", expectedException.iterator().next());
161     }
162
163     /**
164      * Failures in de-registration is logged and propagated
165      */
166     @Test
167     public void failedDeRegistration() {
168         //given
169         RuntimeException expectedException = new RuntimeException();
170         Mockito.doThrow(expectedException).when(selfRegistrationManager).deRegister();
171         ContextClosedEvent event = Mockito.mock(ContextClosedEvent.class);
172         //when
173         try {
174             selfUnregistrationTriggerer.onApplicationEvent(event);
175             //verify
176             fail();
177         } catch (RuntimeException e) {
178             assertEquals(e, expectedException);
179         }
180         verify(logger).error("Self de-registration failed", expectedException);
181     }
182
183     /**
184      * Spring will instantiate using reflection
185      */
186     @Test
187     public void testUseStaticWay() throws Exception {
188         //verify
189         //the constructor is public even if has no private fields
190         new NokiaSvnfmApplication();
191     }
192
193     /**
194      * test spring application bootstrapping
195      */
196     @Test
197     public void useless() throws Exception {
198         String[] args = new String[0];
199         SpringApplication springApplicaiton = Mockito.mock(SpringApplication.class);
200         SystemFunctions systemFunctions = SystemFunctions.systemFunctions();
201         when(this.systemFunctions.newSpringApplication(NokiaSvnfmApplication.class)).thenReturn(springApplicaiton);
202         //when
203         NokiaSvnfmApplication.main(args);
204         //verify
205         verify(springApplicaiton).run(args);
206     }
207 }