Skip to content

Commit 0825939

Browse files
committed
feat: Add endpoint to group contentTypes by their contentTypeCategory
BREAKING CHANGE: Added GET /api/contentType/categories
1 parent 1b905bb commit 0825939

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed

datashare-app/src/main/java/org/icij/datashare/mode/LocalMode.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ protected Routes addModeConfiguration(Routes routes) {
5252
add(FtmResource.class).
5353
add(NoteResource.class).
5454
add(NerResource.class).
55+
add(ContentTypeResource.class).
5556
filter(IndexWaiterFilter.class).
5657
filter(CsrfFilter.class).
5758
filter(LocalUserFilter.class);

datashare-app/src/main/java/org/icij/datashare/mode/ServerMode.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ protected Routes addModeConfiguration(Routes routes) {
9595
add(NerResource.class).
9696
add(ApiKeyResource.class).
9797
add(ProjectResource.class).
98+
add(ContentTypeResource.class).
9899
filter(CsrfFilter.class).
99100
filter(ApiKeyFilter.class).
100101
filter(Filter.class);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.icij.datashare.web;
2+
3+
import com.google.inject.Singleton;
4+
import io.swagger.v3.oas.annotations.Operation;
5+
import io.swagger.v3.oas.annotations.Parameter;
6+
import io.swagger.v3.oas.annotations.enums.ParameterIn;
7+
import io.swagger.v3.oas.annotations.responses.ApiResponse;
8+
import net.codestory.http.annotations.Post;
9+
import net.codestory.http.annotations.Prefix;
10+
import org.icij.datashare.text.ContentTypeCategory;
11+
12+
import java.util.List;
13+
import java.util.Map;
14+
15+
import static java.util.stream.Collectors.groupingBy;
16+
17+
@Singleton
18+
@Prefix("/api/contentType")
19+
public class ContentTypeResource {
20+
21+
@Operation(description = "Returns the list of contentTypes in parameter grouped by contentTypeCategories",
22+
parameters = {
23+
@Parameter(name = "contentTypes", description = "The list of contentTypes to group", in = ParameterIn.QUERY),
24+
}
25+
)
26+
@ApiResponse(responseCode = "200", description = "The list of contentTypes was successfully grouped", useReturnTypeSchema = true)
27+
@Post("/categories")
28+
public Map<ContentTypeCategory, List<String>> groupByCategories(List<String> contentTypes) {
29+
return contentTypes.stream().collect(groupingBy(ContentTypeCategory::fromContentType));
30+
}
31+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.icij.datashare.web;
2+
3+
import com.fasterxml.jackson.core.type.TypeReference;
4+
import org.icij.datashare.json.JsonObjectMapper;
5+
import org.icij.datashare.text.ContentTypeCategory;
6+
import org.icij.datashare.web.testhelpers.AbstractProdWebServerTest;
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
10+
import java.util.List;
11+
import java.util.Map;
12+
13+
import static org.fest.assertions.Assertions.assertThat;
14+
15+
public class ContentTypeResourceTest extends AbstractProdWebServerTest {
16+
17+
@Before
18+
public void setUp() {
19+
configure(routes -> routes.add(new ContentTypeResource()));
20+
}
21+
22+
@Test
23+
public void test_group_by_categories_with_audio_and_video() throws Exception {
24+
String content = post("/api/contentType/categories", "[\"audio/mp3\", \"video/mp4\"]").response().content();
25+
Map<ContentTypeCategory, List<String>> result = JsonObjectMapper.readValue(content, new TypeReference<>() {});
26+
27+
assertThat(result).hasSize(2);
28+
assertThat(result.get(ContentTypeCategory.AUDIO)).containsExactly("audio/mp3");
29+
assertThat(result.get(ContentTypeCategory.VIDEO)).containsExactly("video/mp4");
30+
}
31+
32+
@Test
33+
public void test_group_by_categories_with_unknown_content_type() throws Exception {
34+
String content = post("/api/contentType/categories", "[\"application/unknown\"]").response().content();
35+
Map<ContentTypeCategory, List<String>> result = JsonObjectMapper.readValue(content, new TypeReference<>() {});
36+
37+
assertThat(result).hasSize(1);
38+
assertThat(result.get(ContentTypeCategory.OTHER)).containsExactly("application/unknown");
39+
}
40+
41+
@Test
42+
public void test_group_by_categories_with_empty_list() throws Exception {
43+
String content = post("/api/contentType/categories", "[]").response().content();
44+
Map<ContentTypeCategory, List<String>> result = JsonObjectMapper.readValue(content, new TypeReference<>() {});
45+
46+
assertThat(result).isEmpty();
47+
}
48+
49+
@Test
50+
public void test_group_by_categories_with_image() throws Exception {
51+
String content = post("/api/contentType/categories", "[\"image/png\"]").response().content();
52+
Map<ContentTypeCategory, List<String>> result = JsonObjectMapper.readValue(content, new TypeReference<>() {});
53+
54+
assertThat(result).hasSize(1);
55+
assertThat(result.get(ContentTypeCategory.IMAGE)).containsExactly("image/png");
56+
}
57+
58+
@Test
59+
public void test_group_by_categories_mixed() throws Exception {
60+
String content = post("/api/contentType/categories", "[\"audio/mp3\", \"application/unknown\", \"image/jpeg\"]").response().content();
61+
Map<ContentTypeCategory, List<String>> result = JsonObjectMapper.readValue(content, new TypeReference<>() {});
62+
63+
assertThat(result).hasSize(3);
64+
assertThat(result.get(ContentTypeCategory.AUDIO)).containsExactly("audio/mp3");
65+
assertThat(result.get(ContentTypeCategory.OTHER)).containsExactly("application/unknown");
66+
assertThat(result.get(ContentTypeCategory.IMAGE)).containsExactly("image/jpeg");
67+
}
68+
}

0 commit comments

Comments
 (0)