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