Compare commits
4 Commits
435bd04b87
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 334d7a834e | |||
| 0766b6352e | |||
| 03a2aa2987 | |||
| ba82c0b6ff |
@@ -11,7 +11,6 @@ RUN mvn package -q -DskipTests
|
||||
# ─── 2. Runtime ───────────────────────────────────
|
||||
FROM eclipse-temurin:21-jre-alpine
|
||||
WORKDIR /app
|
||||
RUN apk add --no-cache sqlite
|
||||
COPY --from=build /build/target/quarkus-app/ /app/
|
||||
EXPOSE 8080
|
||||
|
||||
|
||||
@@ -60,6 +60,12 @@ docker run -p 8080:8080 \
|
||||
-e DISC_DROP_BASE_URL=http://localhost:8080 \
|
||||
-v discdrop-data:/app/data \
|
||||
discdrop
|
||||
|
||||
### Docker Compose
|
||||
|
||||
```bash
|
||||
docker compose up -d
|
||||
```
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
version: "3.8"
|
||||
services:
|
||||
discdrop:
|
||||
build: .
|
||||
container_name: discdrop
|
||||
restart: unless-stopped
|
||||
ports:
|
||||
- "8080:8080"
|
||||
volumes:
|
||||
- discdrop-data:/app/data
|
||||
environment:
|
||||
- DISC_DROP_BASE_URL=http://localhost:8080
|
||||
- DISC_DROP_CACHE_TTL=8
|
||||
- DISC_DROP_DB_PATH=/app/data/discdrop.db
|
||||
- DISC_DROP_USER_AGENT=DiscDrop/1.0 (https://gitea.t3du.com/droideparanoico/discdrop)
|
||||
|
||||
volumes:
|
||||
discdrop-data:
|
||||
@@ -30,22 +30,22 @@
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<!-- REST server (resteasy-reactive successor) -->
|
||||
<!-- REST server (reactive) -->
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-resteasy</artifactId>
|
||||
<artifactId>quarkus-rest</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Qute templating -->
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-resteasy-qute</artifactId>
|
||||
<artifactId>quarkus-rest-qute</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- REST Client -->
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-resteasy-client</artifactId>
|
||||
<artifactId>quarkus-rest-client</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- CDI -->
|
||||
@@ -75,7 +75,7 @@
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-resteasy-client-jackson</artifactId>
|
||||
<artifactId>quarkus-rest-client-jackson</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Test -->
|
||||
@@ -90,10 +90,37 @@
|
||||
<version>5.5.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.jacoco</groupId>
|
||||
<artifactId>jacoco-maven-plugin</artifactId>
|
||||
<version>0.8.12</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>prepare-agent</id>
|
||||
<goals><goal>prepare-agent</goal></goals>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>report</id>
|
||||
<phase>verify</phase>
|
||||
<goals><goal>report</goal></goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>com/discdrop/client/dto/*</exclude>
|
||||
<exclude>com/discdrop/client/MusicBrainzClient.class</exclude>
|
||||
<exclude>com/discdrop/client/UserAgentFilter.class</exclude>
|
||||
<exclude>com/discdrop/service/ArtistService.class</exclude>
|
||||
<exclude>com/discdrop/service/ReleaseGroupService.class</exclude>
|
||||
<exclude>com/discdrop/web/ArtistController.class</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-maven-plugin</artifactId>
|
||||
|
||||
@@ -3,10 +3,15 @@ package com.discdrop.db;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.sql.*;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import org.sqlite.SQLiteDataSource;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
@@ -84,7 +89,7 @@ public class DatabaseService {
|
||||
|
||||
public List<Record.TrackedArtist> findAllArtists() {
|
||||
var artists = new ArrayList<Record.TrackedArtist>();
|
||||
var sql = "SELECT * FROM tracked_artist ORDER BY name";
|
||||
var sql = "SELECT mbid, name, disambiguation, area_name, album_monitored, single_monitored, ep_monitored, broadcast_monitored, other_monitored, created_at, updated_at FROM tracked_artist ORDER BY name";
|
||||
try (Connection conn = getConnection();
|
||||
Statement stmt = conn.createStatement();
|
||||
ResultSet rs = stmt.executeQuery(sql)) {
|
||||
@@ -184,6 +189,29 @@ public class DatabaseService {
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteArtistCascading(String mbid) {
|
||||
var sqlRg = "DELETE FROM release_group_cache WHERE artist_mbid = ?";
|
||||
var sqlAr = "DELETE FROM tracked_artist WHERE mbid = ?";
|
||||
try (Connection conn = getConnection()) {
|
||||
conn.setAutoCommit(false);
|
||||
try (PreparedStatement ps1 = conn.prepareStatement(sqlRg);
|
||||
PreparedStatement ps2 = conn.prepareStatement(sqlAr)) {
|
||||
ps1.setString(1, mbid);
|
||||
ps1.executeUpdate();
|
||||
ps2.setString(1, mbid);
|
||||
ps2.executeUpdate();
|
||||
conn.commit();
|
||||
} catch (SQLException e) {
|
||||
conn.rollback();
|
||||
throw e;
|
||||
} finally {
|
||||
conn.setAutoCommit(true);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
// --- release_group_cache DAO ---
|
||||
|
||||
public void upsertReleaseGroups(List<Record.ReleaseGroup> groups) {
|
||||
@@ -213,16 +241,24 @@ public class DatabaseService {
|
||||
|
||||
public record FeedResult(List<Record.ReleaseGroup> entries, boolean hasMore) {}
|
||||
|
||||
public FeedResult getFeed(List<String> artistMbids, List<String> primaryTypes, int limit, int offset) {
|
||||
public FeedResult getFeed(Map<String, List<String>> artistTypes, int limit, int offset) {
|
||||
var groups = new ArrayList<Record.ReleaseGroup>();
|
||||
if (artistMbids.isEmpty()) return new FeedResult(groups, false);
|
||||
if (artistTypes.isEmpty()) return new FeedResult(groups, false);
|
||||
|
||||
var placeholders = String.join(",", artistMbids.stream().map(m -> "?").toList());
|
||||
var sql = "SELECT * FROM release_group_cache WHERE artist_mbid IN (" + placeholders + ")";
|
||||
if (!primaryTypes.isEmpty()) {
|
||||
var typePlaceholders = String.join(",", primaryTypes.stream().map(t -> "?").toList());
|
||||
sql += " AND primary_type IN (" + typePlaceholders + ")";
|
||||
var conditions = new ArrayList<String>();
|
||||
var params = new ArrayList<Object>();
|
||||
for (var entry : artistTypes.entrySet()) {
|
||||
var types = entry.getValue();
|
||||
if (types.isEmpty()) continue;
|
||||
var placeholders = String.join(",", types.stream().map(t -> "?").toList());
|
||||
conditions.add("(artist_mbid = ? AND primary_type IN (" + placeholders + "))");
|
||||
params.add(entry.getKey());
|
||||
params.addAll(types);
|
||||
}
|
||||
|
||||
if (conditions.isEmpty()) return new FeedResult(groups, false);
|
||||
|
||||
var sql = "SELECT * FROM release_group_cache WHERE " + String.join(" OR ", conditions);
|
||||
sql += " ORDER BY (CASE WHEN first_release_date IS NULL OR first_release_date = '' THEN 1 ELSE 0 END), first_release_date DESC";
|
||||
|
||||
boolean useLimit = limit > 0;
|
||||
@@ -231,8 +267,10 @@ public class DatabaseService {
|
||||
try (Connection conn = getConnection();
|
||||
PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
int idx = 1;
|
||||
for (var mbid : artistMbids) ps.setString(idx++, mbid);
|
||||
for (var type : primaryTypes) ps.setString(idx++, type);
|
||||
for (var p : params) {
|
||||
if (p instanceof String s) ps.setString(idx++, s);
|
||||
else ps.setObject(idx++, p);
|
||||
}
|
||||
if (useLimit) {
|
||||
ps.setInt(idx++, limit + 1);
|
||||
ps.setInt(idx, offset);
|
||||
@@ -291,6 +329,41 @@ public class DatabaseService {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
public java.util.Map<String, Boolean> getStaleness(List<String> artistMbids, long ttlHours) {
|
||||
var result = new java.util.LinkedHashMap<String, Boolean>();
|
||||
if (artistMbids.isEmpty()) return result;
|
||||
|
||||
var placeholders = String.join(",", artistMbids.stream().map(m -> "?").toList());
|
||||
var sql = "SELECT artist_mbid, COUNT(*) as cnt, MAX(cached_at) as last_cached FROM release_group_cache WHERE artist_mbid IN (" + placeholders + ") GROUP BY artist_mbid";
|
||||
try (Connection conn = getConnection();
|
||||
PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
int idx = 1;
|
||||
for (var mbid : artistMbids) ps.setString(idx++, mbid);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
var now = Instant.now();
|
||||
var cached = new java.util.HashMap<String, Long>();
|
||||
var lastCached = new java.util.HashMap<String, Instant>();
|
||||
while (rs.next()) {
|
||||
var mbid = rs.getString("artist_mbid");
|
||||
cached.put(mbid, rs.getLong("cnt"));
|
||||
var ts = rs.getString("last_cached");
|
||||
if (ts != null) {
|
||||
lastCached.put(mbid, Instant.parse(ts.replace(" ", "T") + "Z"));
|
||||
}
|
||||
}
|
||||
for (var mbid : artistMbids) {
|
||||
var cnt = cached.getOrDefault(mbid, 0L);
|
||||
var lc = lastCached.get(mbid);
|
||||
boolean stale = cnt == 0 || lc == null || lc.plus(ttlHours, java.time.temporal.ChronoUnit.HOURS).isBefore(now);
|
||||
result.put(mbid, stale);
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// --- settings DAO ---
|
||||
|
||||
public String getSetting(String key, String defaultValue) {
|
||||
|
||||
@@ -84,8 +84,7 @@ public class ArtistService {
|
||||
}
|
||||
|
||||
public void removeArtist(String mbid) {
|
||||
db.deleteReleaseGroupsForArtist(mbid);
|
||||
db.deleteArtist(mbid);
|
||||
db.deleteArtistCascading(mbid);
|
||||
}
|
||||
|
||||
public void updateMonitoring(String mbid, MonitoringFlags flags) {
|
||||
|
||||
@@ -5,7 +5,10 @@ import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ApplicationScoped
|
||||
public class CacheService {
|
||||
@@ -25,12 +28,15 @@ public class CacheService {
|
||||
public boolean isStale(String artistMbid) {
|
||||
Optional<Instant> lastCached = db.getLastCachedAt(artistMbid);
|
||||
if (lastCached.isEmpty()) return true;
|
||||
|
||||
long ttlHours = getTtlHours();
|
||||
return lastCached.get().plus(ttlHours, ChronoUnit.HOURS).isBefore(Instant.now());
|
||||
return lastCached.get().plus(getTtlHours(), ChronoUnit.HOURS).isBefore(Instant.now());
|
||||
}
|
||||
|
||||
public boolean needsRefresh(String artistMbid) {
|
||||
return db.countReleaseGroupsForArtist(artistMbid) == 0 || isStale(artistMbid);
|
||||
}
|
||||
|
||||
public Map<String, Boolean> needsRefresh(List<String> artistMbids) {
|
||||
var staleness = db.getStaleness(artistMbids, getTtlHours());
|
||||
return staleness;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,8 +6,11 @@ import com.discdrop.db.Record.ReleaseGroup;
|
||||
import com.discdrop.db.Record.TrackedArtist;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
|
||||
@ApplicationScoped
|
||||
public class FeedService {
|
||||
@@ -29,55 +32,36 @@ public class FeedService {
|
||||
public List<ReleaseGroup> getAllFeedEntries() {
|
||||
var artists = artistService.findAll();
|
||||
if (artists.isEmpty()) return List.of();
|
||||
|
||||
var artistMbids = artistMbidsWithRefresh(artists);
|
||||
var allTypes = List.of("Album", "Single", "EP", "Broadcast", "Other");
|
||||
var allEntries = db.getFeed(artistMbids, allTypes, 0, 0).entries();
|
||||
return filterByArtistMonitoring(allEntries, artists);
|
||||
refreshStaleCaches(artists);
|
||||
return db.getFeed(buildArtistTypesMap(artists), 0, 0).entries();
|
||||
}
|
||||
|
||||
public FeedResult getFeedEntries(int page) {
|
||||
var artists = artistService.findAll();
|
||||
if (artists.isEmpty()) return new FeedResult(List.of(), false);
|
||||
|
||||
var artistMbids = artistMbidsWithRefresh(artists);
|
||||
var allTypes = List.of("Album", "Single", "EP", "Broadcast", "Other");
|
||||
var allEntries = db.getFeed(artistMbids, allTypes, 0, 0).entries();
|
||||
var filtered = filterByArtistMonitoring(allEntries, artists);
|
||||
|
||||
var fromIndex = (page - 1) * PAGE_SIZE;
|
||||
if (fromIndex >= filtered.size()) return new FeedResult(List.of(), false);
|
||||
var toIndex = Math.min(fromIndex + PAGE_SIZE, filtered.size());
|
||||
var pageEntries = filtered.subList(fromIndex, toIndex);
|
||||
var hasMore = toIndex < filtered.size();
|
||||
return new FeedResult(pageEntries, hasMore);
|
||||
refreshStaleCaches(artists);
|
||||
var offset = (page - 1) * PAGE_SIZE;
|
||||
return db.getFeed(buildArtistTypesMap(artists), PAGE_SIZE, offset);
|
||||
}
|
||||
|
||||
private List<String> artistMbidsWithRefresh(List<TrackedArtist> artists) {
|
||||
var allTypes = List.of("Album", "Single", "EP", "Broadcast", "Other");
|
||||
var mbids = new ArrayList<String>();
|
||||
private Map<String, List<String>> buildArtistTypesMap(List<TrackedArtist> artists) {
|
||||
var map = new LinkedHashMap<String, List<String>>();
|
||||
for (var a : artists) {
|
||||
if (cacheService.needsRefresh(a.mbid())) {
|
||||
var types = getMonitoredTypes(a);
|
||||
if (!types.isEmpty()) map.put(a.mbid(), types);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
private void refreshStaleCaches(List<TrackedArtist> artists) {
|
||||
var allTypes = List.of("Album", "Single", "EP", "Broadcast", "Other");
|
||||
var mbids = artists.stream().map(TrackedArtist::mbid).toList();
|
||||
var staleness = cacheService.needsRefresh(mbids);
|
||||
for (var a : artists) {
|
||||
if (staleness.getOrDefault(a.mbid(), false)) {
|
||||
releaseGroupService.fetchAndCache(a.mbid(), a.name(), allTypes);
|
||||
}
|
||||
mbids.add(a.mbid());
|
||||
}
|
||||
return mbids;
|
||||
}
|
||||
|
||||
private List<ReleaseGroup> filterByArtistMonitoring(List<ReleaseGroup> entries, List<TrackedArtist> artists) {
|
||||
var flags = new java.util.HashMap<String, java.util.Set<String>>();
|
||||
for (var a : artists) {
|
||||
flags.put(a.mbid(), java.util.Set.copyOf(getMonitoredTypes(a)));
|
||||
}
|
||||
var result = new ArrayList<ReleaseGroup>();
|
||||
for (var e : entries) {
|
||||
var artistFlags = flags.get(e.artistMbid());
|
||||
if (artistFlags != null && artistFlags.contains(e.primaryType())) {
|
||||
result.add(e);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private List<String> getMonitoredTypes(TrackedArtist a) {
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
package com.discdrop.util;
|
||||
|
||||
import com.discdrop.db.Record.ReleaseGroup;
|
||||
import com.rometools.rome.feed.synd.*;
|
||||
import com.rometools.rome.feed.synd.SyndCategoryImpl;
|
||||
import com.rometools.rome.feed.synd.SyndContentImpl;
|
||||
import com.rometools.rome.feed.synd.SyndEntry;
|
||||
import com.rometools.rome.feed.synd.SyndEntryImpl;
|
||||
import com.rometools.rome.feed.synd.SyndFeedImpl;
|
||||
import com.rometools.rome.feed.synd.SyndLinkImpl;
|
||||
import com.rometools.rome.io.SyndFeedOutput;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import org.eclipse.microprofile.config.inject.ConfigProperty;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.discdrop.web;
|
||||
|
||||
import com.discdrop.db.DatabaseService.FeedResult;
|
||||
import com.discdrop.db.Record.MonitoringFlags;
|
||||
import com.discdrop.db.Record.TrackedArtist;
|
||||
import com.discdrop.service.ArtistService;
|
||||
@@ -11,7 +10,18 @@ import io.quarkus.qute.Location;
|
||||
import io.quarkus.qute.Template;
|
||||
import io.quarkus.qute.TemplateInstance;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.*;
|
||||
import jakarta.ws.rs.Consumes;
|
||||
import jakarta.ws.rs.DELETE;
|
||||
import jakarta.ws.rs.DefaultValue;
|
||||
import jakarta.ws.rs.FormParam;
|
||||
import jakarta.ws.rs.GET;
|
||||
import jakarta.ws.rs.NotFoundException;
|
||||
import jakarta.ws.rs.PATCH;
|
||||
import jakarta.ws.rs.POST;
|
||||
import jakarta.ws.rs.Path;
|
||||
import jakarta.ws.rs.PathParam;
|
||||
import jakarta.ws.rs.Produces;
|
||||
import jakarta.ws.rs.QueryParam;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import java.util.ArrayList;
|
||||
@@ -103,12 +113,15 @@ public class ArtistController {
|
||||
@QueryParam("type") String type) {
|
||||
var existing = artistService.findByMbid(mbid)
|
||||
.orElseThrow(() -> new NotFoundException("Artist not found"));
|
||||
if (type == null || type.isBlank()) {
|
||||
throw new jakarta.ws.rs.BadRequestException("type parameter is required");
|
||||
}
|
||||
var flags = new MonitoringFlags(
|
||||
type.equals("album") ? !existing.albumMonitored() : existing.albumMonitored(),
|
||||
type.equals("single") ? !existing.singleMonitored() : existing.singleMonitored(),
|
||||
type.equals("ep") ? !existing.epMonitored() : existing.epMonitored(),
|
||||
type.equals("broadcast") ? !existing.broadcastMonitored() : existing.broadcastMonitored(),
|
||||
type.equals("other") ? !existing.otherMonitored() : existing.otherMonitored()
|
||||
"album".equals(type) ? !existing.albumMonitored() : existing.albumMonitored(),
|
||||
"single".equals(type) ? !existing.singleMonitored() : existing.singleMonitored(),
|
||||
"ep".equals(type) ? !existing.epMonitored() : existing.epMonitored(),
|
||||
"broadcast".equals(type) ? !existing.broadcastMonitored() : existing.broadcastMonitored(),
|
||||
"other".equals(type) ? !existing.otherMonitored() : existing.otherMonitored()
|
||||
);
|
||||
artistService.updateMonitoring(mbid, flags);
|
||||
var allArtists = artistService.findAll();
|
||||
|
||||
@@ -5,7 +5,13 @@ import io.quarkus.qute.Location;
|
||||
import io.quarkus.qute.Template;
|
||||
import io.quarkus.qute.TemplateInstance;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.*;
|
||||
import jakarta.ws.rs.Consumes;
|
||||
import jakarta.ws.rs.DefaultValue;
|
||||
import jakarta.ws.rs.FormParam;
|
||||
import jakarta.ws.rs.GET;
|
||||
import jakarta.ws.rs.PATCH;
|
||||
import jakarta.ws.rs.Path;
|
||||
import jakarta.ws.rs.Produces;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import java.util.List;
|
||||
@@ -36,6 +42,18 @@ public class SettingsController {
|
||||
public Response saveSettings(
|
||||
@FormParam("cache_ttl_hours") @DefaultValue("8") String cacheTtl,
|
||||
@FormParam("default_types") List<String> defaultTypes) {
|
||||
try {
|
||||
var ttl = Integer.parseInt(cacheTtl);
|
||||
if (ttl < 1 || ttl > 168) {
|
||||
return Response.status(400)
|
||||
.entity("<div class=\"alert alert-error\">Cache TTL must be between 1 and 168 hours</div>")
|
||||
.build();
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
return Response.status(400)
|
||||
.entity("<div class=\"alert alert-error\">Invalid cache TTL value</div>")
|
||||
.build();
|
||||
}
|
||||
db.setSetting("cache_ttl_hours", cacheTtl);
|
||||
db.setSetting("default_types", defaultTypes != null ? String.join(",", defaultTypes) : "Album");
|
||||
return Response.ok("<div class=\"alert alert-success\">Settings saved</div>")
|
||||
|
||||
@@ -17,4 +17,4 @@ discdrop.cache.ttl-hours=${DISC_DROP_CACHE_TTL:8}
|
||||
discdrop.base-url=${DISC_DROP_BASE_URL:http://localhost:8080}
|
||||
|
||||
# User-Agent sent to MusicBrainz API (env: DISC_DROP_USER_AGENT)
|
||||
discdrop.user-agent=${DISC_DROP_USER_AGENT:DiscDrop/0.1 (https://https://github.com/droideparanoico/discdrop)}
|
||||
discdrop.user-agent=${DISC_DROP_USER_AGENT:DiscDrop/1.0 (https://github.com/droideparanoico/discdrop)}
|
||||
|
||||
@@ -77,7 +77,7 @@
|
||||
hx-delete="/api/artists/{artist.mbid}"
|
||||
hx-target="#artist-list"
|
||||
hx-swap="innerHTML"
|
||||
onclick="return confirm('Remove {artist.name}?')">
|
||||
hx-confirm="Remove {artist.name}?">
|
||||
<svg class="icon-sm" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="3 6 5 6 21 6"/><path d="M19 6v14a2 2 0 01-2 2H7a2 2 0 01-2-2V6m3 0V4a2 2 0 012-2h4a2 2 0 012 2v2"/></svg>
|
||||
</button>
|
||||
</td>
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
package com.discdrop;
|
||||
|
||||
import com.discdrop.db.DatabaseService;
|
||||
import com.discdrop.db.Record.ReleaseGroup;
|
||||
import com.discdrop.db.Record.TrackedArtist;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@QuarkusTest
|
||||
class AdditionalServiceTest {
|
||||
|
||||
@Inject
|
||||
DatabaseService db;
|
||||
|
||||
@BeforeEach
|
||||
void cleanUp() {
|
||||
for (var a : db.findAllArtists()) {
|
||||
db.deleteArtistCascading(a.mbid());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetFeedWithOnlyAlbumFilter() {
|
||||
db.insertArtist(new TrackedArtist("only-album", "Only Album", "", "",
|
||||
true, false, false, false, false, Instant.now(), Instant.now()));
|
||||
db.upsertReleaseGroups(List.of(
|
||||
new ReleaseGroup("rg-oa-1", "Album", "Album", "[]", "2020-01-01", "only-album", "Only Album", Instant.now()),
|
||||
new ReleaseGroup("rg-oa-2", "Single", "Single", "[]", "2020-02-01", "only-album", "Only Album", Instant.now())
|
||||
));
|
||||
|
||||
var result = db.getFeed(Map.of("only-album", List.of("Album")), 10, 0);
|
||||
assertEquals(1, result.entries().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetFeedWithNoMatchingTypes() {
|
||||
db.insertArtist(new TrackedArtist("no-match", "No Match", "", "",
|
||||
true, false, false, false, false, Instant.now(), Instant.now()));
|
||||
db.upsertReleaseGroups(List.of(
|
||||
new ReleaseGroup("rg-nm-1", "Album", "Album", "[]", "2020-01-01", "no-match", "No Match", Instant.now())
|
||||
));
|
||||
|
||||
var result = db.getFeed(Map.of("no-match", List.of("Single")), 10, 0);
|
||||
assertTrue(result.entries().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testArtistListSortOrder() {
|
||||
db.insertArtist(new TrackedArtist("z-artist", "ZZZ", "", "", true, false, false, false, false, Instant.now(), Instant.now()));
|
||||
db.insertArtist(new TrackedArtist("a-artist", "AAA", "", "", true, false, false, false, false, Instant.now(), Instant.now()));
|
||||
|
||||
var artists = db.findAllArtists();
|
||||
assertEquals("AAA", artists.get(0).name());
|
||||
assertEquals("ZZZ", artists.get(1).name());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteCascadingRollsBackOnFailure() {
|
||||
assertDoesNotThrow(() -> db.deleteArtistCascading("nonexistent"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpsertEmptyList() {
|
||||
assertDoesNotThrow(() -> db.upsertReleaseGroups(List.of()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetStalenessEmpty() {
|
||||
assertTrue(db.getStaleness(List.of(), 8).isEmpty());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package com.discdrop;
|
||||
|
||||
import com.discdrop.db.DatabaseService;
|
||||
import com.discdrop.db.Record.TrackedArtist;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@QuarkusTest
|
||||
class ArtistControllerExtendedTest {
|
||||
|
||||
@Inject
|
||||
DatabaseService db;
|
||||
|
||||
@BeforeEach
|
||||
void cleanUp() {
|
||||
for (var a : db.findAllArtists()) {
|
||||
db.deleteArtistCascading(a.mbid());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteArtist() {
|
||||
db.insertArtist(new TrackedArtist("del-1", "Delete", "", "",
|
||||
true, false, false, false, false, Instant.now(), Instant.now()));
|
||||
given().delete("/api/artists/del-1").then().statusCode(200);
|
||||
assertFalse(db.exists("del-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteArtistFromSearch() {
|
||||
db.insertArtist(new TrackedArtist("del-src", "SearchDel", "", "",
|
||||
true, false, false, false, false, Instant.now(), Instant.now()));
|
||||
given()
|
||||
.queryParam("q", "")
|
||||
.delete("/api/artists/del-src/from-search")
|
||||
.then().statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMonitoringToggleSingle() {
|
||||
db.insertArtist(new TrackedArtist("mon-ts", "ToggleS", "", "",
|
||||
true, false, false, false, false, Instant.now(), Instant.now()));
|
||||
|
||||
given().queryParam("type", "single").patch("/api/artists/mon-ts/monitor").then().statusCode(200);
|
||||
|
||||
var artist = db.findByMbid("mon-ts").orElseThrow();
|
||||
assertTrue(artist.singleMonitored());
|
||||
assertTrue(artist.albumMonitored());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMonitoringToggleAlbum() {
|
||||
db.insertArtist(new TrackedArtist("mon-ta", "ToggleA", "", "",
|
||||
true, false, false, false, false, Instant.now(), Instant.now()));
|
||||
|
||||
given().queryParam("type", "album").patch("/api/artists/mon-ta/monitor").then().statusCode(200);
|
||||
|
||||
var artist = db.findByMbid("mon-ta").orElseThrow();
|
||||
assertFalse(artist.albumMonitored());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMonitoringNonexistent() {
|
||||
given()
|
||||
.queryParam("type", "album")
|
||||
.patch("/api/artists/no-such/monitor")
|
||||
.then().statusCode(404);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMonitoringMissingType() {
|
||||
db.insertArtist(new TrackedArtist("mon-mt", "MissType", "", "",
|
||||
true, false, false, false, false, Instant.now(), Instant.now()));
|
||||
given()
|
||||
.patch("/api/artists/mon-mt/monitor")
|
||||
.then().statusCode(400);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
package com.discdrop;
|
||||
|
||||
import com.discdrop.db.DatabaseService;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.http.ContentType;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
@QuarkusTest
|
||||
class ArtistControllerTest {
|
||||
|
||||
@Inject
|
||||
DatabaseService db;
|
||||
|
||||
@BeforeEach
|
||||
void cleanUp() {
|
||||
for (var a : db.findAllArtists()) {
|
||||
db.deleteArtistCascading(a.mbid());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSearchEndpointEmpty() {
|
||||
given()
|
||||
.queryParam("q", "")
|
||||
.when().get("/api/artists/search")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("No artists found"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testArtistListEndpoint() {
|
||||
given()
|
||||
.when().get("/api/artists/list")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("artist-list"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSettingsForm() {
|
||||
given()
|
||||
.when().get("/api/settings/form")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("cache_ttl_hours"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSaveSettings() {
|
||||
given()
|
||||
.formParam("cache_ttl_hours", "12")
|
||||
.formParam("default_types", "Album", "Single")
|
||||
.when().patch("/api/settings")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("Settings saved"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSaveSettingsInvalidTtl() {
|
||||
given()
|
||||
.formParam("cache_ttl_hours", "invalid")
|
||||
.formParam("default_types", "Album")
|
||||
.when().patch("/api/settings")
|
||||
.then()
|
||||
.statusCode(400);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFeedTableEndpoint() {
|
||||
given()
|
||||
.when().get("/api/artists/feed-table")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFeedTableWithPage() {
|
||||
given()
|
||||
.queryParam("page", "1")
|
||||
.when().get("/api/artists/feed-table")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testReleasesPage() {
|
||||
given()
|
||||
.when().get("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("DiscDrop"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testArtistsPage() {
|
||||
given()
|
||||
.when().get("/artists")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRssFeed() {
|
||||
given()
|
||||
.when().get("/feed/rss.xml")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.contentType(ContentType.XML);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOpml() {
|
||||
given()
|
||||
.when().get("/feed/opml")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.contentType(ContentType.XML);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package com.discdrop;
|
||||
|
||||
import com.discdrop.db.DatabaseService;
|
||||
import com.discdrop.db.Record.MonitoringFlags;
|
||||
import com.discdrop.db.Record.TrackedArtist;
|
||||
import com.discdrop.service.ArtistService;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@QuarkusTest
|
||||
class ArtistServiceTest {
|
||||
|
||||
@Inject
|
||||
ArtistService artistService;
|
||||
|
||||
@Inject
|
||||
DatabaseService db;
|
||||
|
||||
@BeforeEach
|
||||
void cleanUp() {
|
||||
for (var a : db.findAllArtists()) {
|
||||
db.deleteArtistCascading(a.mbid());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAddArtistWithDefaultMonitoring() {
|
||||
var artist = new TrackedArtist("add-1", "Add Artist", "", "US",
|
||||
true, false, false, false, false, Instant.now(), Instant.now());
|
||||
db.insertArtist(artist);
|
||||
|
||||
assertTrue(db.exists("add-1"));
|
||||
var found = db.findByMbid("add-1");
|
||||
assertTrue(found.isPresent());
|
||||
assertEquals("Add Artist", found.get().name());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFindByMbidNotFound() {
|
||||
assertTrue(artistService.findByMbid("nonexistent").isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFindAll() {
|
||||
db.insertArtist(new TrackedArtist("all-1", "First", "", "", true, false, false, false, false, Instant.now(), Instant.now()));
|
||||
db.insertArtist(new TrackedArtist("all-2", "Second", "", "", true, false, false, false, false, Instant.now(), Instant.now()));
|
||||
|
||||
assertEquals(2, artistService.findAll().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRemoveArtist() {
|
||||
db.insertArtist(new TrackedArtist("rem-1", "Remove Me", "", "", true, false, false, false, false, Instant.now(), Instant.now()));
|
||||
assertTrue(db.exists("rem-1"));
|
||||
|
||||
artistService.removeArtist("rem-1");
|
||||
assertFalse(db.exists("rem-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdateMonitoring() {
|
||||
db.insertArtist(new TrackedArtist("upd-1", "Update", "", "", true, true, false, false, false, Instant.now(), Instant.now()));
|
||||
artistService.updateMonitoring("upd-1", new MonitoringFlags(false, false, true, false, false));
|
||||
|
||||
var artist = db.findByMbid("upd-1").orElseThrow();
|
||||
assertFalse(artist.albumMonitored());
|
||||
assertFalse(artist.singleMonitored());
|
||||
assertTrue(artist.epMonitored());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package com.discdrop;
|
||||
|
||||
import com.discdrop.db.DatabaseService;
|
||||
import com.discdrop.db.Record.ReleaseGroup;
|
||||
import com.discdrop.db.Record.TrackedArtist;
|
||||
import com.discdrop.service.CacheService;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@QuarkusTest
|
||||
class CacheServiceTest {
|
||||
|
||||
@Inject
|
||||
CacheService cacheService;
|
||||
|
||||
@Inject
|
||||
DatabaseService db;
|
||||
|
||||
@BeforeEach
|
||||
void cleanUp() {
|
||||
for (var a : db.findAllArtists()) {
|
||||
db.deleteArtistCascading(a.mbid());
|
||||
}
|
||||
db.setSetting("cache_ttl_hours", "8");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDefaultTtl() {
|
||||
assertEquals(8, cacheService.getTtlHours());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCustomTtl() {
|
||||
db.setSetting("cache_ttl_hours", "24");
|
||||
assertEquals(24, cacheService.getTtlHours());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testNeedsRefreshNoArtist() {
|
||||
assertTrue(cacheService.needsRefresh("nonexistent"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testNeedsRefreshWithCachedData() {
|
||||
db.insertArtist(new TrackedArtist("cached-artist", "Cached", "", "",
|
||||
true, false, false, false, false, Instant.now(), Instant.now()));
|
||||
db.upsertReleaseGroups(List.of(
|
||||
new ReleaseGroup("rg-cache-1", "Cached Release", "Album", "[]",
|
||||
"2020-01-01", "cached-artist", "Cached", Instant.now())
|
||||
));
|
||||
|
||||
assertFalse(cacheService.needsRefresh("cached-artist"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBatchStaleness() {
|
||||
db.insertArtist(new TrackedArtist("batch-a", "A", "", "", true, false, false, false, false, Instant.now(), Instant.now()));
|
||||
db.insertArtist(new TrackedArtist("batch-b", "B", "", "", true, false, false, false, false, Instant.now(), Instant.now()));
|
||||
|
||||
db.upsertReleaseGroups(List.of(
|
||||
new ReleaseGroup("rg-ba-1", "R1", "Album", "[]", "2020-01-01", "batch-b", "B", Instant.now())
|
||||
));
|
||||
|
||||
var staleness = cacheService.needsRefresh(List.of("batch-a", "batch-b"));
|
||||
assertTrue(staleness.get("batch-a"));
|
||||
assertFalse(staleness.get("batch-b"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testIsStaleWithNoData() {
|
||||
assertTrue(cacheService.isStale("nonexistent-artist"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testIsStaleWithFreshData() {
|
||||
db.insertArtist(new TrackedArtist("fresh-artist", "Fresh", "", "",
|
||||
true, false, false, false, false, Instant.now(), Instant.now()));
|
||||
db.upsertReleaseGroups(List.of(
|
||||
new ReleaseGroup("rg-fresh-1", "Fresh Release", "Album", "[]",
|
||||
"2020-01-01", "fresh-artist", "Fresh", Instant.now())
|
||||
));
|
||||
|
||||
assertFalse(cacheService.isStale("fresh-artist"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
package com.discdrop;
|
||||
|
||||
import com.discdrop.db.DatabaseService;
|
||||
import com.discdrop.db.Record.MonitoringFlags;
|
||||
import com.discdrop.db.Record.ReleaseGroup;
|
||||
import com.discdrop.db.Record.TrackedArtist;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.*;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@QuarkusTest
|
||||
class DatabaseServiceTest {
|
||||
|
||||
@Inject
|
||||
DatabaseService db;
|
||||
|
||||
@BeforeEach
|
||||
void cleanDb() {
|
||||
for (var a : db.findAllArtists()) {
|
||||
db.deleteArtistCascading(a.mbid());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testInsertAndFindArtist() {
|
||||
var artist = new TrackedArtist("mbid-1", "Test Artist", "", "UK",
|
||||
true, false, false, false, false,
|
||||
Instant.now(), Instant.now());
|
||||
db.insertArtist(artist);
|
||||
|
||||
assertTrue(db.exists("mbid-1"));
|
||||
var found = db.findByMbid("mbid-1");
|
||||
assertTrue(found.isPresent());
|
||||
assertEquals("Test Artist", found.get().name());
|
||||
assertTrue(found.get().albumMonitored());
|
||||
assertFalse(found.get().singleMonitored());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFindAllArtists() {
|
||||
db.insertArtist(new TrackedArtist("a", "Artist A", "", "", true, false, false, false, false, Instant.now(), Instant.now()));
|
||||
db.insertArtist(new TrackedArtist("b", "Artist B", "", "", true, false, false, false, false, Instant.now(), Instant.now()));
|
||||
assertEquals(2, db.findAllArtists().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdateMonitoring() {
|
||||
db.insertArtist(new TrackedArtist("m-1", "Test", "", "", true, false, false, false, false, Instant.now(), Instant.now()));
|
||||
db.updateMonitoring("m-1", new MonitoringFlags(false, true, false, false, false));
|
||||
|
||||
var artist = db.findByMbid("m-1").orElseThrow();
|
||||
assertFalse(artist.albumMonitored());
|
||||
assertTrue(artist.singleMonitored());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteArtist() {
|
||||
db.insertArtist(new TrackedArtist("del-1", "Delete Me", "", "", true, false, false, false, false, Instant.now(), Instant.now()));
|
||||
assertTrue(db.exists("del-1"));
|
||||
db.deleteArtist("del-1");
|
||||
assertFalse(db.exists("del-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpsertAndGetFeed() {
|
||||
db.insertArtist(new TrackedArtist("feed-artist", "Feed Artist", "", "", true, false, false, false, false, Instant.now(), Instant.now()));
|
||||
|
||||
var groups = List.of(
|
||||
new ReleaseGroup("rg-1", "Album One", "Album", "[]", "2020-01-01", "feed-artist", "Feed Artist", Instant.now()),
|
||||
new ReleaseGroup("rg-2", "Single One", "Single", "[]", "2021-01-01", "feed-artist", "Feed Artist", Instant.now())
|
||||
);
|
||||
db.upsertReleaseGroups(groups);
|
||||
|
||||
var types = Map.of("feed-artist", List.of("Album", "Single"));
|
||||
var result = db.getFeed(types, 10, 0);
|
||||
assertEquals(2, result.entries().size());
|
||||
assertFalse(result.hasMore());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetFeedWithPagination() {
|
||||
db.insertArtist(new TrackedArtist("page-artist", "Page Artist", "", "", true, true, false, false, false, Instant.now(), Instant.now()));
|
||||
|
||||
var groups = new ArrayList<ReleaseGroup>();
|
||||
for (int i = 1; i <= 15; i++) {
|
||||
groups.add(new ReleaseGroup("rg-" + i, "Release " + i, i % 2 == 0 ? "Album" : "Single", "[]",
|
||||
"2020-" + String.format("%02d", i % 12 + 1) + "-01", "page-artist", "Page Artist", Instant.now()));
|
||||
}
|
||||
db.upsertReleaseGroups(groups);
|
||||
|
||||
var types = Map.of("page-artist", List.of("Album", "Single"));
|
||||
var page1 = db.getFeed(types, 10, 0);
|
||||
assertEquals(10, page1.entries().size());
|
||||
assertTrue(page1.hasMore());
|
||||
|
||||
var page2 = db.getFeed(types, 10, 10);
|
||||
assertEquals(5, page2.entries().size());
|
||||
assertFalse(page2.hasMore());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSettings() {
|
||||
assertEquals("8", db.getSetting("cache_ttl_hours", "8"));
|
||||
db.setSetting("cache_ttl_hours", "12");
|
||||
assertEquals("12", db.getSetting("cache_ttl_hours", "8"));
|
||||
assertEquals("default", db.getSetting("nonexistent", "default"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetStaleness() {
|
||||
db.insertArtist(new TrackedArtist("stale-1", "Stale Artist", "", "", true, false, false, false, false, Instant.now(), Instant.now()));
|
||||
db.insertArtist(new TrackedArtist("stale-2", "Fresh Artist", "", "", true, false, false, false, false, Instant.now(), Instant.now()));
|
||||
|
||||
var groups = List.of(
|
||||
new ReleaseGroup("rg-s1", "Release", "Album", "[]", "2020-01-01", "stale-2", "Fresh Artist", Instant.now())
|
||||
);
|
||||
db.upsertReleaseGroups(groups);
|
||||
|
||||
var staleness = db.getStaleness(List.of("stale-1", "stale-2"), 1000);
|
||||
assertTrue(staleness.get("stale-1"));
|
||||
assertFalse(staleness.get("stale-2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteArtistCascading() {
|
||||
db.insertArtist(new TrackedArtist("cascade-1", "Cascade", "", "", true, false, false, false, false, Instant.now(), Instant.now()));
|
||||
db.upsertReleaseGroups(List.of(
|
||||
new ReleaseGroup("rg-c1", "R1", "Album", "[]", "2020-01-01", "cascade-1", "Cascade", Instant.now())
|
||||
));
|
||||
assertEquals(1, db.countReleaseGroupsForArtist("cascade-1"));
|
||||
|
||||
db.deleteArtistCascading("cascade-1");
|
||||
assertFalse(db.exists("cascade-1"));
|
||||
assertEquals(0, db.countReleaseGroupsForArtist("cascade-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFeedPerArtistFiltering() {
|
||||
db.insertArtist(new TrackedArtist("art-a", "Artist A", "", "", true, false, false, false, false, Instant.now(), Instant.now()));
|
||||
db.insertArtist(new TrackedArtist("art-b", "Artist B", "", "", false, true, false, false, false, Instant.now(), Instant.now()));
|
||||
|
||||
db.upsertReleaseGroups(List.of(
|
||||
new ReleaseGroup("rg-a1", "A Album", "Album", "[]", "2020-01-01", "art-a", "Artist A", Instant.now()),
|
||||
new ReleaseGroup("rg-b1", "B Single", "Single", "[]", "2020-02-01", "art-b", "Artist B", Instant.now()),
|
||||
new ReleaseGroup("rg-b2", "B Album", "Album", "[]", "2020-03-01", "art-b", "Artist B", Instant.now())
|
||||
));
|
||||
|
||||
var types = Map.of(
|
||||
"art-a", List.of("Album"),
|
||||
"art-b", List.of("Single")
|
||||
);
|
||||
var result = db.getFeed(types, 10, 0);
|
||||
assertEquals(2, result.entries().size()); // A-Album + B-Single. B-Album excluded.
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package com.discdrop;
|
||||
|
||||
import com.discdrop.client.dto.*;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class DtoTest {
|
||||
|
||||
final ObjectMapper json = new ObjectMapper();
|
||||
|
||||
@Test
|
||||
void testArtistSearchParse() throws Exception {
|
||||
var input = "{\"artists\":[{\"id\":\"mbid-1\",\"name\":\"Test Artist\",\"disambiguation\":\"rock band\",\"area\":{\"name\":\"UK\"},\"type\":\"Group\",\"country\":\"GB\"}],\"count\":1}";
|
||||
var result = json.readValue(input, ArtistSearchResponse.class);
|
||||
assertEquals(1, result.count());
|
||||
assertEquals("Test Artist", result.artists().get(0).name());
|
||||
assertEquals("rock band", result.artists().get(0).disambiguation());
|
||||
assertEquals("UK", result.artists().get(0).area().name());
|
||||
assertEquals("Group", result.artists().get(0).type());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testArtistSearchWithNoResults() throws Exception {
|
||||
var input = "{\"artists\":[],\"count\":0}";
|
||||
var result = json.readValue(input, ArtistSearchResponse.class);
|
||||
assertTrue(result.artists().isEmpty());
|
||||
assertEquals(0, result.count());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testArtistDetailsParse() throws Exception {
|
||||
var input = "{\"id\":\"mbid-d-1\",\"name\":\"Detail Artist\",\"disambiguation\":\"\",\"area\":{\"name\":\"US\"},\"type\":\"Person\",\"country\":\"US\",\"relations\":[]}";
|
||||
var result = json.readValue(input, ArtistDetailsResponse.class);
|
||||
assertEquals("Detail Artist", result.name());
|
||||
assertEquals("US", result.area().name());
|
||||
assertTrue(result.relations().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testArtistDetailsWithRelations() throws Exception {
|
||||
var input = "{\"id\":\"mbid-r-1\",\"name\":\"Related\",\"area\":null,\"type\":\"Group\",\"country\":\"DE\",\"relations\":[{\"type\":\"allmusic\",\"url\":{\"resource\":\"https://allmusic.com/artist/123\"}}]}";
|
||||
var result = json.readValue(input, ArtistDetailsResponse.class);
|
||||
assertEquals(1, result.relations().size());
|
||||
assertEquals("allmusic", result.relations().get(0).type());
|
||||
assertEquals("https://allmusic.com/artist/123", result.relations().get(0).url().resource());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testReleaseGroupBrowseParse() throws Exception {
|
||||
var input = "{\"release-groups\":[{\"id\":\"rg-1\",\"title\":\"Test Album\",\"first-release-date\":\"2020-01-15\",\"primary-type\":\"Album\",\"secondary-types\":[\"Live\"],\"artist-credit\":[{\"artist\":{\"id\":\"art-1\",\"name\":\"Test Artist\"}}]}],\"release-group-count\":1}";
|
||||
var result = json.readValue(input, ReleaseGroupBrowseResponse.class);
|
||||
assertEquals(1, result.releaseGroupCount());
|
||||
var rg = result.releaseGroups().get(0);
|
||||
assertEquals("Test Album", rg.title());
|
||||
assertEquals("Album", rg.primaryType());
|
||||
assertEquals("2020-01-15", rg.firstReleaseDate());
|
||||
assertEquals(1, rg.secondaryTypes().size());
|
||||
assertEquals("Live", rg.secondaryTypes().get(0));
|
||||
assertEquals("Test Artist", rg.artistCredit().get(0).artist().name());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testReleaseGroupBrowseEmpty() throws Exception {
|
||||
var input = "{\"release-groups\":[],\"release-group-count\":0}";
|
||||
var result = json.readValue(input, ReleaseGroupBrowseResponse.class);
|
||||
assertTrue(result.releaseGroups().isEmpty());
|
||||
assertEquals(0, result.releaseGroupCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUnknownPropertiesIgnored() throws Exception {
|
||||
var input = "{\"id\":\"x\",\"name\":\"Test\",\"unknown_field\":true,\"area\":{\"name\":\"US\"},\"type\":\"Group\",\"country\":\"US\",\"relations\":[]}";
|
||||
var result = json.readValue(input, ArtistDetailsResponse.class);
|
||||
assertEquals("Test", result.name());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testArtistSearchPartialData() throws Exception {
|
||||
var input = "{\"artists\":[{\"id\":\"m-1\",\"name\":\"Minimal\"}],\"count\":1}";
|
||||
var result = json.readValue(input, ArtistSearchResponse.class);
|
||||
assertEquals("Minimal", result.artists().get(0).name());
|
||||
assertNull(result.artists().get(0).area());
|
||||
assertNull(result.artists().get(0).type());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package com.discdrop;
|
||||
|
||||
import com.discdrop.service.FeedService;
|
||||
import com.discdrop.service.CacheService;
|
||||
import com.discdrop.db.DatabaseService;
|
||||
import com.discdrop.db.Record.TrackedArtist;
|
||||
import com.discdrop.db.Record.ReleaseGroup;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@QuarkusTest
|
||||
class FeedServiceTest {
|
||||
|
||||
@Inject
|
||||
FeedService feedService;
|
||||
|
||||
@Inject
|
||||
CacheService cacheService;
|
||||
|
||||
@Inject
|
||||
DatabaseService db;
|
||||
|
||||
@BeforeEach
|
||||
void cleanUp() {
|
||||
for (var a : db.findAllArtists()) {
|
||||
db.deleteArtistCascading(a.mbid());
|
||||
}
|
||||
db.setSetting("cache_ttl_hours", "100");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testEmptyFeed() {
|
||||
assertTrue(feedService.getFeedEntries(1).entries().isEmpty());
|
||||
assertTrue(feedService.getAllFeedEntries().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFeedWithData() {
|
||||
db.insertArtist(new TrackedArtist("feed-a", "Feed A", "", "",
|
||||
true, true, false, false, false, Instant.now(), Instant.now()));
|
||||
db.upsertReleaseGroups(List.of(
|
||||
new ReleaseGroup("feed-rg-1", "Album One", "Album", "[]", "2020-01-01", "feed-a", "Feed A", Instant.now()),
|
||||
new ReleaseGroup("feed-rg-2", "Single One", "Single", "[]", "2020-02-01", "feed-a", "Feed A", Instant.now())
|
||||
));
|
||||
|
||||
assertEquals(2, feedService.getAllFeedEntries().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testPagination() {
|
||||
db.insertArtist(new TrackedArtist("page-artist", "Page A", "", "",
|
||||
true, true, true, false, false, Instant.now(), Instant.now()));
|
||||
var groups = new java.util.ArrayList<ReleaseGroup>();
|
||||
for (int i = 1; i <= 25; i++) {
|
||||
groups.add(new ReleaseGroup("page-rg-" + i, "Rel " + i, i % 3 == 0 ? "EP" : (i % 2 == 0 ? "Single" : "Album"), "[]",
|
||||
"2020-" + (i % 12 + 1) + "-01", "page-artist", "Page A", Instant.now()));
|
||||
}
|
||||
db.upsertReleaseGroups(groups);
|
||||
|
||||
var page1 = feedService.getFeedEntries(1);
|
||||
assertEquals(10, page1.entries().size());
|
||||
assertTrue(page1.hasMore());
|
||||
|
||||
var page3 = feedService.getFeedEntries(3);
|
||||
assertEquals(5, page3.entries().size());
|
||||
assertFalse(page3.hasMore());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMonitoringFiltersFeed() {
|
||||
db.insertArtist(new TrackedArtist("mon-artist", "Monitored", "", "",
|
||||
true, false, false, false, false, Instant.now(), Instant.now()));
|
||||
db.upsertReleaseGroups(List.of(
|
||||
new ReleaseGroup("mon-rg-1", "Album", "Album", "[]", "2020-01-01", "mon-artist", "Monitored", Instant.now()),
|
||||
new ReleaseGroup("mon-rg-2", "Single", "Single", "[]", "2020-02-01", "mon-artist", "Monitored", Instant.now())
|
||||
));
|
||||
|
||||
var feed = feedService.getAllFeedEntries();
|
||||
assertEquals(1, feed.size());
|
||||
assertEquals("Album", feed.get(0).primaryType());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testNoMonitoredTypesShowsNothing() {
|
||||
db.insertArtist(new TrackedArtist("none-artist", "None", "", "",
|
||||
false, false, false, false, false, Instant.now(), Instant.now()));
|
||||
db.upsertReleaseGroups(List.of(
|
||||
new ReleaseGroup("none-rg-1", "Album", "Album", "[]", "2020-01-01", "none-artist", "None", Instant.now())
|
||||
));
|
||||
|
||||
assertTrue(feedService.getAllFeedEntries().isEmpty());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package com.discdrop;
|
||||
|
||||
import com.discdrop.db.Record.ReleaseGroup;
|
||||
import com.discdrop.util.RSSFeedBuilder;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@QuarkusTest
|
||||
class RSSFeedBuilderTest {
|
||||
|
||||
@Inject
|
||||
RSSFeedBuilder feedBuilder;
|
||||
|
||||
@Test
|
||||
void testEmptyRss() {
|
||||
var xml = feedBuilder.buildRss(List.of());
|
||||
assertTrue(xml.contains("<title>DiscDrop"));
|
||||
assertTrue(xml.contains("<rss"));
|
||||
assertTrue(xml.contains("</rss>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRssWithEntries() {
|
||||
var entries = List.of(
|
||||
new ReleaseGroup("mbid-1", "Album One", "Album", "[]", "2020-01-15", "artist-1", "Artist One", Instant.now()),
|
||||
new ReleaseGroup("mbid-2", "Single Two", "Single", "[\"Live\"]", "2021-06-01", "artist-2", "Artist Two", Instant.now())
|
||||
);
|
||||
var xml = feedBuilder.buildRss(entries);
|
||||
|
||||
assertTrue(xml.contains("Artist One"));
|
||||
assertTrue(xml.contains("Album One"));
|
||||
assertTrue(xml.contains("mbid-1"));
|
||||
assertTrue(xml.contains("Artist Two"));
|
||||
assertTrue(xml.contains("Single Two"));
|
||||
assertTrue(xml.contains("mbid-2"));
|
||||
assertTrue(xml.contains("Album"));
|
||||
assertTrue(xml.contains("Single"));
|
||||
assertTrue(xml.contains("2020-01-15"));
|
||||
assertTrue(xml.contains("2021-06-01"));
|
||||
assertTrue(xml.contains("coverartarchive"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOpml() {
|
||||
var opml = feedBuilder.buildOpml();
|
||||
assertTrue(opml.contains("<opml"));
|
||||
assertTrue(opml.contains("DiscDrop"));
|
||||
assertTrue(opml.contains("feed/rss.xml"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRssHasRequiredElements() {
|
||||
var entries = List.of(
|
||||
new ReleaseGroup("mbid-3", "Test Release", "Album", "[]", "2022-03-15", "artist-3", "Artist Three", Instant.now())
|
||||
);
|
||||
var xml = feedBuilder.buildRss(entries);
|
||||
|
||||
assertTrue(xml.contains("<guid"));
|
||||
assertTrue(xml.contains("mbz-rg-mbid-3"));
|
||||
assertTrue(xml.contains("<pubDate"));
|
||||
assertTrue(xml.contains("<category"));
|
||||
assertTrue(xml.contains("musicbrainz.org/release-group/mbid-3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRssHandlesMissingDate() {
|
||||
var entries = List.of(
|
||||
new ReleaseGroup("mbid-4", "No Date", "Album", "[]", null, "artist-4", "Artist Four", Instant.now())
|
||||
);
|
||||
var xml = feedBuilder.buildRss(entries);
|
||||
assertTrue(xml.contains("Artist Four"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRssEscapesHtml() {
|
||||
var entries = List.of(
|
||||
new ReleaseGroup("mbid-5", "Title & <More>", "Album", "[]", "2020-01-01", "artist-5", "Artist & Band", Instant.now())
|
||||
);
|
||||
var xml = feedBuilder.buildRss(entries);
|
||||
assertTrue(xml.contains("&") || xml.contains("&amp;"));
|
||||
assertTrue(xml.contains("<") || xml.contains("&lt;"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
quarkus.http.port=0
|
||||
|
||||
discdrop.db.path=target/test-discdrop.db
|
||||
discdrop.cache.ttl-hours=100
|
||||
discdrop.base-url=http://localhost:8080
|
||||
|
||||
discdrop.user-agent=DiscDrop/1.0 (test)
|
||||
Reference in New Issue
Block a user