Skip to content

Commit 62fdaf0

Browse files
committed
Add simple tests for EndpointHelper and EndpointSplitter
1 parent 59057b0 commit 62fdaf0

File tree

3 files changed

+66
-14
lines changed

3 files changed

+66
-14
lines changed

spotify-web-api-core/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@
3636
<version>${lombok.version}</version>
3737
<scope>provided</scope>
3838
</dependency>
39+
<dependency>
40+
<groupId>org.junit.jupiter</groupId>
41+
<artifactId>junit-jupiter</artifactId>
42+
<version>${junit.version}</version>
43+
<scope>test</scope>
44+
</dependency>
3945
</dependencies>
4046

4147
<build>

spotify-web-api-core/src/main/java/de/sonallux/spotify/core/EndpointSplitter.java

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,13 @@ public static void splitReorderOrReplacePlaylistsTracksEndpoint(SpotifyWebApi ap
8989
var reorderParameterNames = List.of("Authorization", "Content-Type", "playlist_id", "range_start", "insert_before", "range_length", "snapshot_id");
9090
var replaceParameterNames = List.of("Authorization", "Content-Type", "playlist_id", "uris");
9191

92-
var reorderResponseDescription = "On a successful **reorder** operation, the response" +
93-
" body contains a `snapshot_id` in JSON format and the HTTP status code" +
94-
" in the response header is `200` OK. The `snapshot_id` can be used to" +
95-
" identify your playlist version in future requests.";
96-
97-
var replaceResponseDescription = "On a successful **replace** operation, the HTTP status" +
98-
" code in the response header is `201` Created.";
99-
100-
var errorResponseDescription = "On error, the header status code is an [error code](https://developer.spotify.com/documentation/web-api/#response-status-codes)," +
101-
" the response body contains an [error object](https://developer.spotify.com/documentation/web-api/#response-schema)," +
102-
" and the existing playlist is unmodified. Trying to set an item when you" +
103-
" do not have the user's authorization returns error `403` Forbidden.";
92+
var responseDescriptionParts = endpoint.getResponseDescription().split("\n\n");
93+
if (responseDescriptionParts.length != 3
94+
|| !responseDescriptionParts[0].contains("reorder")
95+
|| !responseDescriptionParts[1].contains("replace")
96+
|| !responseDescriptionParts[2].contains("error")) {
97+
throw new IllegalArgumentException("Response description has changed, can not split endpoint-reorder-or-replace-playlists-tracks");
98+
}
10499

105100
var reorderEndpoint = new SpotifyWebApiEndpoint(
106101
"endpoint-reorder-playlists-tracks",
@@ -110,7 +105,7 @@ public static void splitReorderOrReplacePlaylistsTracksEndpoint(SpotifyWebApi ap
110105
endpoint.getHttpMethod(),
111106
endpoint.getPath(),
112107
endpoint.getParameters().stream().filter(p -> reorderParameterNames.contains(p.getName())).collect(Collectors.toList()),
113-
reorderResponseDescription + "\n\n" + errorResponseDescription,
108+
responseDescriptionParts[0] + "\n\n" + responseDescriptionParts[2],
114109
endpoint.getScopes(),
115110
endpoint.getNotes(),
116111
List.of(new SpotifyWebApiEndpoint.ResponseType("SnapshotIdObject", 200))
@@ -127,7 +122,7 @@ public static void splitReorderOrReplacePlaylistsTracksEndpoint(SpotifyWebApi ap
127122
endpoint.getHttpMethod(),
128123
endpoint.getPath(),
129124
endpoint.getParameters().stream().filter(p -> replaceParameterNames.contains(p.getName())).collect(Collectors.toList()),
130-
replaceResponseDescription + "\n\n" + errorResponseDescription,
125+
responseDescriptionParts[1] + "\n\n" + responseDescriptionParts[2],
131126
endpoint.getScopes(),
132127
endpoint.getNotes(),
133128
List.of(new SpotifyWebApiEndpoint.ResponseType("SnapshotIdObject", 201))
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package de.sonallux.spotify.core;
2+
3+
import de.sonallux.spotify.core.model.SpotifyWebApi;
4+
import de.sonallux.spotify.core.model.SpotifyWebApiEndpoint;
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
8+
import java.io.IOException;
9+
import java.util.stream.Collectors;
10+
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
import static org.junit.jupiter.api.Assertions.assertTrue;
13+
14+
class EndpointHelperTest {
15+
16+
SpotifyWebApi spotifyWebApi;
17+
18+
@BeforeEach
19+
void setup() throws IOException {
20+
spotifyWebApi = SpotifyWebApiUtils.load();
21+
}
22+
23+
@Test
24+
void testFixDuplicateEndpointParameters() {
25+
assertTrue(spotifyWebApi.getCategoryList().stream().flatMap(c -> c.getEndpointList().stream())
26+
.anyMatch(this::hasDuplicateEndpointParameters));
27+
28+
EndpointHelper.fixDuplicateEndpointParameters(spotifyWebApi);
29+
30+
assertTrue(spotifyWebApi.getCategoryList().stream().flatMap(c -> c.getEndpointList().stream())
31+
.noneMatch(this::hasDuplicateEndpointParameters));
32+
}
33+
34+
private boolean hasDuplicateEndpointParameters(SpotifyWebApiEndpoint endpoint) {
35+
return endpoint.getParameters().stream()
36+
.collect(Collectors.groupingBy(SpotifyWebApiEndpoint.Parameter::getName))
37+
.entrySet().stream()
38+
.anyMatch(entry -> entry.getValue().size() > 1);
39+
}
40+
41+
@Test
42+
void testSplitEndpoints() {
43+
var endpointCount = spotifyWebApi.getCategoryList().stream().mapToLong(c -> c.getEndpointList().size()).sum();
44+
45+
EndpointHelper.splitEndpoints(spotifyWebApi);
46+
47+
var endpointCountAfterSplit = spotifyWebApi.getCategoryList().stream().mapToLong(c -> c.getEndpointList().size()).sum();
48+
49+
assertEquals(endpointCount + 2, endpointCountAfterSplit);
50+
}
51+
}

0 commit comments

Comments
 (0)