Skip to content

Commit

Permalink
Use householdId and groupId if provided
Browse files Browse the repository at this point in the history
  • Loading branch information
Migwel authored and Migwel committed Jan 18, 2024
1 parent cb663ce commit 98e869b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
20 changes: 17 additions & 3 deletions src/main/java/dev/migwel/sts/sonos/FromSonosService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -29,7 +31,8 @@ public boolean isRelevant(Class<?> searchRequestType) {

@Override
public Optional<Song> search(FromSonosRequest searchRequest) {
Optional<Group> activeGroup = getActiveGroup();
Optional<Group> activeGroup =
getActiveGroup(searchRequest.householdId(), searchRequest.groupId());
if (activeGroup.isEmpty()) {
return Optional.empty();
}
Expand All @@ -41,8 +44,12 @@ private Optional<Song> whatsPlaying(String groupId) {
return playbackMetadataParser.parse(playbackMetadataResponse);
}

private Optional<Group> getActiveGroup() {
List<String> households = sonosService.getHouseholds();
private Optional<Group> getActiveGroup(
@CheckForNull String householdId, @CheckForNull String groupId) {
if (groupId != null) {
return Optional.of(new Group(groupId, null, Collections.emptyList()));
}
List<String> households = getHouseholds(householdId);
for (String household : households) {
List<Group> activeGroups =
sonosService.getGroups(household, PlaybackState.PLAYBACK_STATE_PLAYING);
Expand All @@ -52,4 +59,11 @@ private Optional<Group> getActiveGroup() {
}
return Optional.empty();
}

private List<String> getHouseholds(@CheckForNull String householdId) {
if (householdId != null) {
return List.of(householdId);
}
return sonosService.getHouseholds();
}
}
17 changes: 16 additions & 1 deletion src/test/java/dev/migwel/sts/sonos/FromSonosServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}

0 comments on commit 98e869b

Please sign in to comment.