diff options
Diffstat (limited to 'nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/TestNokiaSvnfmApplication.java')
-rw-r--r-- | nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/TestNokiaSvnfmApplication.java | 115 |
1 files changed, 83 insertions, 32 deletions
diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/TestNokiaSvnfmApplication.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/TestNokiaSvnfmApplication.java index 444660b1..c6586c28 100644 --- a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/TestNokiaSvnfmApplication.java +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/TestNokiaSvnfmApplication.java @@ -15,40 +15,46 @@ */ package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia; +import java.util.HashSet; +import java.util.Set; import junit.framework.TestCase; import org.junit.Before; import org.junit.Test; import org.mockito.*; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.TestUtil; import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.JobManager; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase; import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.Useless; import org.slf4j.Logger; +import org.springframework.boot.SpringApplication; import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.context.event.ContextClosedEvent; import org.springframework.test.util.ReflectionTestUtils; import static junit.framework.TestCase.assertEquals; import static junit.framework.TestCase.assertTrue; -import static org.mockito.Mockito.verify; +import static junit.framework.TestCase.fail; +import static org.mockito.Mockito.*; +import static org.springframework.test.util.ReflectionTestUtils.setField; -public class TestNokiaSvnfmApplication { - @Mock - private SelfRegistrationManager selfRegistrationManager; +public class TestNokiaSvnfmApplication extends TestBase { @Mock private JobManager jobManager; - @Mock - private Logger logger; - @InjectMocks + private NokiaSvnfmApplication.SelfRegistrationTrigger selfRegistrationTriggerer; - @InjectMocks private NokiaSvnfmApplication.SelfDeRegistrationTrigger selfUnregistrationTriggerer; @Before public void initMocks() throws Exception { - MockitoAnnotations.initMocks(this); - ReflectionTestUtils.setField(NokiaSvnfmApplication.class, "logger", logger); + selfRegistrationTriggerer = new NokiaSvnfmApplication.SelfRegistrationTrigger(selfRegistrationManager, jobManager); + selfUnregistrationTriggerer = new NokiaSvnfmApplication.SelfDeRegistrationTrigger(selfRegistrationManager, jobManager); + setField(NokiaSvnfmApplication.class, "logger", logger); } /** @@ -66,15 +72,27 @@ public class TestNokiaSvnfmApplication { * Assert that the self registration process is started after the servlet is up and is able to answer REST requests. */ @Test + @SuppressWarnings("squid:S2925") //the execution is asynchronous no other way to wait public void testRegistrationIsCalledAfterComponentIsUp() throws Exception { //given ApplicationReadyEvent event = Mockito.mock(ApplicationReadyEvent.class); //when selfRegistrationTriggerer.onApplicationEvent(event); //verify - verify(selfRegistrationManager).register(); - verify(logger).info("Self registration started"); - verify(logger).info("Self registration finished"); + boolean success = false; + while(!success) + { + try { + verify(selfRegistrationManager).register(); + verify(logger).info("Self registration started"); + verify(logger).info("Self registration finished"); + success = true; + } + catch (Error e){ + + } + Thread.sleep(10); + } // this forces the event to be fired after the servlet is up (prevents refactor) assertTrue(ApplicationReadyEvent.class.isAssignableFrom(event.getClass())); } @@ -99,23 +117,55 @@ public class TestNokiaSvnfmApplication { } /** + * Assert that the self registration process is started after the servlet is up and is able to answer REST requests. + */ + @Test + public void testPreparingForShutdownDoesNotStartRegistration() throws Exception { + //given + ApplicationReadyEvent event = Mockito.mock(ApplicationReadyEvent.class); + when(jobManager.isPreparingForShutDown()).thenReturn(true); + //when + selfRegistrationTriggerer.onApplicationEvent(event); + //verify + verify(selfRegistrationManager, never()).register(); + } + + /** * Failures in registration is logged and propagated */ @Test - public void failedRegistration() { + @SuppressWarnings("squid:S2925") //the execution is asynchronous no other way to wait + public void failedFirstRegistration() { //given - RuntimeException expectedException = new RuntimeException(); - Mockito.doThrow(expectedException).when(selfRegistrationManager).register(); + Set<RuntimeException> expectedException = new HashSet<>(); + doAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocationOnMock) throws Throwable { + if(expectedException.size() == 0){ + RuntimeException e = new RuntimeException(); + expectedException.add(e); + throw e; + } + return null; + } + }).when(selfRegistrationManager).register(); ApplicationReadyEvent event = Mockito.mock(ApplicationReadyEvent.class); //when - try { - selfRegistrationTriggerer.onApplicationEvent(event); - //verify - TestCase.fail(); - } catch (RuntimeException e) { - assertEquals(e, expectedException); + selfRegistrationTriggerer.onApplicationEvent(event); + //verify + //wait for the registration to succeed + boolean success = false; + while(!success){ + try{ + verify(logger).info("Self registration finished"); + success = true; + Thread.sleep(10); + } + catch (Exception e2){} + catch (Error e){} } - verify(logger).error("Self registration failed", expectedException); + verify(logger, times(2)).info("Self registration started"); + verify(logger).error("Self registration failed", expectedException.iterator().next()); } /** @@ -131,7 +181,7 @@ public class TestNokiaSvnfmApplication { try { selfUnregistrationTriggerer.onApplicationEvent(event); //verify - TestCase.fail(); + fail(); } catch (RuntimeException e) { assertEquals(e, expectedException); } @@ -149,16 +199,17 @@ public class TestNokiaSvnfmApplication { } /** - * static entry point calling an other static entry point can not be tested - * only using powermock + * test spring application bootstrapping */ @Test - @Useless public void useless() throws Exception { - try { - NokiaSvnfmApplication.main(null); - } catch (Exception e) { - - } + String[] args = new String [0]; + SpringApplication springApplicaiton = Mockito.mock(SpringApplication.class); + SystemFunctions systemFunctions = SystemFunctions.systemFunctions(); + when(this.systemFunctions.newSpringApplication(NokiaSvnfmApplication.class)).thenReturn(springApplicaiton); + //when + NokiaSvnfmApplication.main(args); + //verify + verify(springApplicaiton).run(args); } } |