-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package dev.migwel.sts.model; | ||
|
||
public record RadioSearchRequest(String url) implements SearchRequest {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package dev.migwel.sts.model; | ||
|
||
public sealed interface SearchRequest permits SonosSearchRequest, RadioSearchRequest {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package dev.migwel.sts.model; | ||
|
||
public record Song(String artist, String title) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package dev.migwel.sts.model; | ||
|
||
public record SonosSearchRequest(String householdId, String groupId) implements SearchRequest {} |
22 changes: 22 additions & 0 deletions
22
src/main/java/dev/migwel/sts/radio/RadioSearchService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package dev.migwel.sts.radio; | ||
|
||
import dev.migwel.sts.model.RadioSearchRequest; | ||
import dev.migwel.sts.model.Song; | ||
import dev.migwel.sts.service.SearchService; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Optional; | ||
|
||
@Component | ||
public class RadioSearchService implements SearchService<RadioSearchRequest> { | ||
|
||
@Override | ||
public boolean isRelevant(Class<?> searchRequestType) { | ||
return searchRequestType.isAssignableFrom(RadioSearchRequest.class); | ||
} | ||
|
||
@Override | ||
public Optional<Song> search(RadioSearchRequest searchRequest) { | ||
return Optional.empty(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package dev.migwel.sts.service; | ||
|
||
import dev.migwel.sts.model.SearchRequest; | ||
import dev.migwel.sts.model.Song; | ||
|
||
import java.util.Optional; | ||
|
||
public interface SearchService<T extends SearchRequest> { | ||
boolean isRelevant(Class<?> searchRequestType); | ||
|
||
Optional<Song> search(T searchRequest); | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/dev/migwel/sts/service/SearchServiceFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package dev.migwel.sts.service; | ||
|
||
import dev.migwel.sts.model.SearchRequest; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.List; | ||
|
||
@Component | ||
public class SearchServiceFactory { | ||
|
||
private final List<SearchService<?>> searchServices; | ||
|
||
@Autowired | ||
public SearchServiceFactory(List<SearchService<?>> searchServices) { | ||
this.searchServices = List.copyOf(searchServices); | ||
} | ||
|
||
public <T extends SearchRequest> SearchService<T> getSearchService(T searchRequest) { | ||
for (SearchService<?> searchService : searchServices) { | ||
if (searchService.isRelevant(searchRequest.getClass())) { | ||
return (SearchService<T>) searchService; | ||
} | ||
} | ||
throw new IllegalArgumentException("No search service available for this search request"); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/dev/migwel/sts/sonos/SonosSearchService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package dev.migwel.sts.sonos; | ||
|
||
import dev.migwel.sts.model.Song; | ||
import dev.migwel.sts.model.SonosSearchRequest; | ||
import dev.migwel.sts.service.SearchService; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Optional; | ||
|
||
@Component | ||
public class SonosSearchService implements SearchService<SonosSearchRequest> { | ||
@Override | ||
public boolean isRelevant(Class<?> searchRequestType) { | ||
return searchRequestType.isAssignableFrom(SonosSearchRequest.class); | ||
} | ||
|
||
@Override | ||
public Optional<Song> search(SonosSearchRequest searchRequest) { | ||
return Optional.empty(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package dev.migwel.sts; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import dev.migwel.sts.service.SearchService; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
import java.util.List; | ||
|
||
@SpringBootTest | ||
class ApplicationTest { | ||
|
||
private final List<SearchService<?>> searchServices; | ||
|
||
@Autowired | ||
public ApplicationTest(List<SearchService<?>> searchServices) { | ||
this.searchServices = searchServices; | ||
} | ||
|
||
@Test | ||
void loadContexts() { | ||
assertNotNull(searchServices); | ||
assertFalse(searchServices.isEmpty()); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/test/java/dev/migwel/sts/service/SearchServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package dev.migwel.sts.service; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import dev.migwel.sts.Application; | ||
import dev.migwel.sts.model.SearchRequest; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; | ||
|
||
import java.util.List; | ||
|
||
@SpringJUnitConfig(Application.class) | ||
class SearchServiceTest { | ||
|
||
private final List<SearchService<?>> searchServices; | ||
|
||
@Autowired | ||
public SearchServiceTest(List<SearchService<?>> searchServices) { | ||
this.searchServices = searchServices; | ||
} | ||
|
||
@Test | ||
void isRelevant_onlyOneRelevantServicePerRequestType() { | ||
Class<?>[] permittedSearchRequests = SearchRequest.class.getPermittedSubclasses(); | ||
for (Class<?> permittedSearchRequest : permittedSearchRequests) { | ||
int nbRelevantSearchServices = 0; | ||
for (SearchService<?> searchService : searchServices) { | ||
if (searchService.isRelevant(permittedSearchRequest)) { | ||
nbRelevantSearchServices += 1; | ||
} | ||
} | ||
assertEquals( | ||
1, | ||
nbRelevantSearchServices, | ||
"Didn't find exactly one search service for request " | ||
+ permittedSearchRequest.getName()); | ||
} | ||
} | ||
} |