diff --git a/src/main/java/dev/migwel/sts/sonos/FromSonosService.java b/src/main/java/dev/migwel/sts/sonos/FromSonosService.java index 6fa2609..6362f50 100644 --- a/src/main/java/dev/migwel/sts/sonos/FromSonosService.java +++ b/src/main/java/dev/migwel/sts/sonos/FromSonosService.java @@ -7,6 +7,8 @@ import org.springframework.stereotype.Component; +import javax.annotation.CheckForNull; +import java.util.Collections; import java.util.List; import java.util.Optional; @@ -29,7 +31,8 @@ public boolean isRelevant(Class searchRequestType) { @Override public Optional search(FromSonosRequest searchRequest) { - Optional activeGroup = getActiveGroup(); + Optional activeGroup = + getActiveGroup(searchRequest.householdId(), searchRequest.groupId()); if (activeGroup.isEmpty()) { return Optional.empty(); } @@ -41,8 +44,12 @@ private Optional whatsPlaying(String groupId) { return playbackMetadataParser.parse(playbackMetadataResponse); } - private Optional getActiveGroup() { - List households = sonosService.getHouseholds(); + private Optional getActiveGroup( + @CheckForNull String householdId, @CheckForNull String groupId) { + if (groupId != null) { + return Optional.of(new Group(groupId, null, Collections.emptyList())); + } + List households = getHouseholds(householdId); for (String household : households) { List activeGroups = sonosService.getGroups(household, PlaybackState.PLAYBACK_STATE_PLAYING); @@ -52,4 +59,11 @@ private Optional getActiveGroup() { } return Optional.empty(); } + + private List getHouseholds(@CheckForNull String householdId) { + if (householdId != null) { + return List.of(householdId); + } + return sonosService.getHouseholds(); + } } diff --git a/src/test/java/dev/migwel/sts/sonos/FromSonosServiceTest.java b/src/test/java/dev/migwel/sts/sonos/FromSonosServiceTest.java index 0203c8c..c377e50 100644 --- a/src/test/java/dev/migwel/sts/sonos/FromSonosServiceTest.java +++ b/src/test/java/dev/migwel/sts/sonos/FromSonosServiceTest.java @@ -2,7 +2,7 @@ import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; import dev.migwel.sts.domain.model.FromSonosRequest; import dev.migwel.sts.domain.model.Song; @@ -75,4 +75,19 @@ void search_songFound() { assertTrue(foundSong.isPresent()); assertEquals(song, foundSong.get()); } + + @Test + void search_dontSearchForHouseholdsIfProvided() { + when(sonosService.getGroups(any(), any())).thenReturn(Collections.emptyList()); + fromSonosService.search(new FromSonosRequest("householdId", null)); + verify(sonosService, never()).getHouseholds(); + } + + @Test + void search_dontSearchForGroupsIfProvided() { + when(sonosService.getPlaybackMetadata(any())).thenReturn(null); + fromSonosService.search(new FromSonosRequest(null, "groupId")); + verify(sonosService, never()).getHouseholds(); + verify(sonosService, never()).getGroups(any(), any()); + } }