From fc148b47f2879b59055bff9e28d027980e7f5937 Mon Sep 17 00:00:00 2001 From: Mirko Ebert Date: Thu, 12 Dec 2024 18:35:03 +0100 Subject: [PATCH] more tests --- .../player/ResilientStreamPlayer.java | 44 +++++++++------- .../player/ResilientStreamPlayerTest.java | 51 +++++++++++++++++++ 2 files changed, 77 insertions(+), 18 deletions(-) create mode 100644 src/test/java/com/mirkoebert/simplejavaradioplayer/player/ResilientStreamPlayerTest.java diff --git a/src/main/java/com/mirkoebert/simplejavaradioplayer/player/ResilientStreamPlayer.java b/src/main/java/com/mirkoebert/simplejavaradioplayer/player/ResilientStreamPlayer.java index 2141714..0ca4197 100644 --- a/src/main/java/com/mirkoebert/simplejavaradioplayer/player/ResilientStreamPlayer.java +++ b/src/main/java/com/mirkoebert/simplejavaradioplayer/player/ResilientStreamPlayer.java @@ -13,38 +13,46 @@ @Service @RequiredArgsConstructor @Slf4j -public class ResilientStreamPlayer { +class ResilientStreamPlayer { private final Mp3StreamPlayer mp3StreamPlayer; private InputStream in; private URL audioStreamUrl; - public void playStream(final URL audioStreamUrl) { + void playStream(final URL audioStreamUrl) { + if (audioStreamUrl == null) { + log.warn("Expect not null audioStreamUrl but was null."); + return; + } this.audioStreamUrl = audioStreamUrl; openNewPlayerWithNewStream(); } @Scheduled(fixedRate = 2000) void watchDog() { - final Status playerStatus = mp3StreamPlayer.getStatus(); - switch (playerStatus) { - case PLAYING, PAUSED -> { - try { - if (in == null) { - log.warn("In stream is null"); - } else if (in.available() > 0) { - log.debug("In stream is ok: {}", in.available()); - // TODO use a 'magic eye tube' input - } else { - log.warn("In stream is not ok"); - openNewPlayerWithNewStream(); + try { + final Status playerStatus = mp3StreamPlayer.getStatus(); + switch (playerStatus) { + case PLAYING, PAUSED -> { + try { + if (in == null) { + log.warn("In stream is null"); + } else if (in.available() > 0) { + log.debug("In stream is ok: {}", in.available()); + // TODO use a 'magic eye tube' input + } else { + log.warn("In stream is not ok"); + openNewPlayerWithNewStream(); + } + } catch (IOException e) { + log.warn("Can't determine stream {}", e.getMessage()); } - } catch (IOException e) { - log.warn("Can't determine stream {}", e.getMessage()); } + case STOPPED, NOT_SPECIFIED -> log.debug("Player state {}, nothing to do", playerStatus); + default -> log.info("Unsupported player state {}", playerStatus); } - case STOPPED, NOT_SPECIFIED -> log.debug("Player state {}, nothing to do", playerStatus); - default -> log.info("Unsupported player state {}", playerStatus); + } catch (Exception e) { + log.error("Watchdog runs into an error", e); } } diff --git a/src/test/java/com/mirkoebert/simplejavaradioplayer/player/ResilientStreamPlayerTest.java b/src/test/java/com/mirkoebert/simplejavaradioplayer/player/ResilientStreamPlayerTest.java new file mode 100644 index 0000000..fa70fab --- /dev/null +++ b/src/test/java/com/mirkoebert/simplejavaradioplayer/player/ResilientStreamPlayerTest.java @@ -0,0 +1,51 @@ +package com.mirkoebert.simplejavaradioplayer.player; + +import com.goxr3plus.streamplayer.enums.Status; +import lombok.SneakyThrows; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Import; +import org.springframework.test.context.bean.override.mockito.MockitoBean; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import java.net.URL; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(SpringExtension.class) +@Import(ResilientStreamPlayer.class) +class ResilientStreamPlayerTest { + + @MockitoBean + private Mp3StreamPlayer mp3StreamPlayer; + @Autowired + private ResilientStreamPlayer cut; + + @SneakyThrows + @Test + void playStream() { + cut.playStream(null); + + //cut.playStream(new URL("malformed url")); + cut.playStream(new URL("http://ebert-p.com")); + verify(mp3StreamPlayer).playStream(any()); + } + + @Test + void watchDogPlayerStatusNull() { + cut.watchDog(); + } + + @Test + void watchDog() { + when(mp3StreamPlayer.getStatus()).thenReturn(Status.PLAYING); + cut.watchDog(); + } + + @Test + void stop() { + } +} \ No newline at end of file