@@ -610,7 +610,7 @@ void whenActionServiceThrowsDeleteServerAlsoThrows() throws IOException, Interru
610610 }
611611
612612 @ Test
613- @ DisplayName ("When Action returned from Hetzner is Null, server service throws a null pointer exception" )
613+ @ DisplayName ("When Delete Action returned from Hetzner is Null, server service throws a null pointer exception" )
614614 void whenActionReturnedFromHetznerIsNullServerServiceThrowsANullPointer () throws IOException , InterruptedException , IllegalAccessException {
615615 HetznerCloud hetznerCloud = mock (HetznerCloud .class );
616616 HetznerCloudHttpClient hetznerCloudHttpClient = mock (HetznerCloudHttpClient .class );
@@ -669,4 +669,96 @@ void deleteServerFromHetznerHandlesException() throws IOException, InterruptedEx
669669 }
670670 }
671671
672+ @ Test
673+ @ DisplayName ("Shutdown Server calls Hetzner and tracks the action" )
674+ void shutdownServerCallsHetznerAndTracksTheAction () throws IOException , InterruptedException , IllegalAccessException {
675+ HetznerCloud hetznerCloud = mock (HetznerCloud .class );
676+ HetznerCloudHttpClient hetznerCloudHttpClient = mock (HetznerCloudHttpClient .class );
677+ ListenerManager listenerManager = mock (ListenerManager .class );
678+ ServiceManager serviceManager = mock (ServiceManager .class );
679+ ActionService actionService = mock (ActionService .class );
680+
681+ try (MockedStatic <HetznerCloud > hetznerCloudMockedStatic = mockStatic (HetznerCloud .class );
682+ MockedStatic <HetznerCloudHttpClient > hetznerCloudHttpClientMockedStatic = mockStatic (HetznerCloudHttpClient .class )) {
683+
684+ Action action = new Action ();
685+ action .setFinished (Date .from (Instant .now ()).toString ());
686+
687+ hetznerCloudHttpClientMockedStatic .when (HetznerCloudHttpClient ::getInstance ).thenReturn (hetznerCloudHttpClient );
688+ hetznerCloudMockedStatic .when (HetznerCloud ::getInstance ).thenReturn (hetznerCloud );
689+ when (hetznerCloud .getListenerManager ()).thenReturn (listenerManager );
690+ when (hetznerCloud .getHttpDetails ()).thenReturn (List .of ("http://host/" , "key1234" ));
691+ when (serviceManager .getActionService ()).thenReturn (actionService );
692+ when (actionService .waitForActionToComplete (any (Action .class ))).thenReturn (CompletableFuture .completedFuture (action ));
693+
694+ when (hetznerCloudHttpClient .sendHttpRequest (any (), anyString (), any (RequestVerb .class ), anyString (), anyString ())).thenReturn (new ActionWrapper (action ));
695+
696+ ServerService serverService = new ServerService (serviceManager );
697+ serverService .shutdownServer (new Server ());
698+
699+ verify (hetznerCloudHttpClient , times (1 )).sendHttpRequest (any (), anyString (), eq (RequestVerb .POST ), eq ("key1234" ), eq ("" ));
700+ verify (actionService , times (1 )).waitForActionToComplete (any (Action .class ));
701+ }
702+ }
703+
704+ @ Test
705+ @ DisplayName ("When httpclient throws, then shutdown Server also throws a Runtime exception" )
706+ void shutdownServerHandlesException () throws IOException , InterruptedException , IllegalAccessException {
707+ HetznerCloud hetznerCloud = mock (HetznerCloud .class );
708+ HetznerCloudHttpClient hetznerCloudHttpClient = mock (HetznerCloudHttpClient .class );
709+ ListenerManager listenerManager = mock (ListenerManager .class );
710+
711+ try (MockedStatic <HetznerCloud > hetznerCloudMockedStatic = mockStatic (HetznerCloud .class );
712+ MockedStatic <HetznerCloudHttpClient > hetznerCloudHttpClientMockedStatic = mockStatic (HetznerCloudHttpClient .class )) {
713+
714+ hetznerCloudHttpClientMockedStatic .when (HetznerCloudHttpClient ::getInstance ).thenReturn (hetznerCloudHttpClient );
715+ hetznerCloudMockedStatic .when (HetznerCloud ::getInstance ).thenReturn (hetznerCloud );
716+ when (hetznerCloud .getListenerManager ()).thenReturn (listenerManager );
717+ when (hetznerCloud .getHttpDetails ()).thenReturn (List .of ("http://host/" , "key1234" ));
718+
719+ when (hetznerCloudHttpClient .sendHttpRequest (any (), anyString (), any (RequestVerb .class ), anyString (), anyString ())).thenThrow (new IOException ());
720+
721+ ServerService serverService = new ServerService ();
722+
723+ RuntimeException runtimeException = assertThrows (RuntimeException .class , () -> serverService .shutdownServer (new Server ()));
724+
725+ verify (hetznerCloudHttpClient , times (1 )).sendHttpRequest (any (), anyString (), eq (RequestVerb .POST ), eq ("key1234" ), eq ("" ));
726+
727+ assertTrue (runtimeException .getMessage ().contains ("IOException" ));
728+ }
729+ }
730+
731+ @ Test
732+ @ DisplayName ("When Shutdown Action returned from Hetzner is Null, server service throws a null pointer exception" )
733+ void whenShutdownActionReturnedFromHetznerIsNullServerServiceThrowsANullPointer () throws IOException , InterruptedException , IllegalAccessException {
734+ HetznerCloud hetznerCloud = mock (HetznerCloud .class );
735+ HetznerCloudHttpClient hetznerCloudHttpClient = mock (HetznerCloudHttpClient .class );
736+ ListenerManager listenerManager = mock (ListenerManager .class );
737+ ServiceManager serviceManager = mock (ServiceManager .class );
738+ ActionService actionService = mock (ActionService .class );
739+
740+ try (MockedStatic <HetznerCloud > hetznerCloudMockedStatic = mockStatic (HetznerCloud .class );
741+ MockedStatic <HetznerCloudHttpClient > hetznerCloudHttpClientMockedStatic = mockStatic (HetznerCloudHttpClient .class )) {
742+
743+ hetznerCloudHttpClientMockedStatic .when (HetznerCloudHttpClient ::getInstance ).thenReturn (hetznerCloudHttpClient );
744+ hetznerCloudMockedStatic .when (HetznerCloud ::getInstance ).thenReturn (hetznerCloud );
745+ when (hetznerCloud .getListenerManager ()).thenReturn (listenerManager );
746+ when (hetznerCloud .getHttpDetails ()).thenReturn (List .of ("http://host/" , "key1234" ));
747+ when (serviceManager .getActionService ()).thenReturn (actionService );
748+
749+ when (actionService .waitForActionToComplete (any (Action .class ))).thenReturn (CompletableFuture .completedFuture (null ));
750+
751+ when (hetznerCloudHttpClient .sendHttpRequest (any (), anyString (), any (RequestVerb .class ), anyString (), anyString ())).thenReturn (new ActionWrapper (new Action ()));
752+
753+ ServerService serverService = new ServerService (serviceManager );
754+
755+ RuntimeException runtimeException = assertThrows (RuntimeException .class , () -> serverService .shutdownServer (new Server ()));
756+
757+ verify (hetznerCloudHttpClient , times (1 )).sendHttpRequest (any (), anyString (), eq (RequestVerb .POST ), eq ("key1234" ), eq ("" ));
758+ verify (actionService , times (1 )).waitForActionToComplete (any (Action .class ));
759+
760+ assertTrue (runtimeException .getMessage ().contains ("NullPointerException" ));
761+ }
762+ }
763+
672764}
0 commit comments