Skip to content

Commit

Permalink
Fix #2867: Add support for GET /feed/{id}/posts (#2892)
Browse files Browse the repository at this point in the history
  • Loading branch information
vivekratnavel committed Feb 21, 2022
1 parent 1656ca4 commit d0e6a3f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ public ThreadCount getThreadsCount(String link, boolean isResolved) throws IOExc
return threadCount;
}

public List<Post> listPosts(String threadId) throws IOException {
Thread thread = get(threadId);
return thread.getPosts();
}

@Transaction
public List<Thread> listThreads(String link) throws IOException {
if (link == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.text.ParseException;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -83,6 +85,20 @@ static class ThreadList extends ResultList<Thread> {
}
}

public static class PostList extends ResultList<Post> {
@SuppressWarnings("unused") /* Required for tests */
public PostList() {}

public PostList(List<Post> data, String beforeCursor, String afterCursor, int total)
throws GeneralSecurityException, UnsupportedEncodingException {
super(data, beforeCursor, afterCursor, total);
}

public PostList(List<Post> listPosts) {
super(listPosts);
}
}

@GET
@Operation(
summary = "List threads",
Expand Down Expand Up @@ -187,6 +203,22 @@ public Response addPost(@Context UriInfo uriInfo, @PathParam("id") String id, @V
return Response.created(thread.getHref()).entity(thread).build();
}

@GET
@Path("/{id}/posts")
@Operation(
summary = "Get all the posts of a thread",
tags = "feeds",
description = "Get all the posts of an existing thread.",
responses = {
@ApiResponse(
responseCode = "200",
description = "The posts of the given thread.",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = PostList.class))),
})
public PostList getPosts(@Context UriInfo uriInfo, @PathParam("id") String id) throws IOException {
return new PostList(dao.listPosts(id));
}

private Thread getThread(SecurityContext securityContext, CreateThread create) {
return new Thread()
.withId(UUID.randomUUID())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.openmetadata.catalog.entity.teams.Team;
import org.openmetadata.catalog.entity.teams.User;
import org.openmetadata.catalog.resources.databases.TableResourceTest;
import org.openmetadata.catalog.resources.feeds.FeedResource.PostList;
import org.openmetadata.catalog.resources.feeds.FeedResource.ThreadList;
import org.openmetadata.catalog.type.Column;
import org.openmetadata.catalog.type.ColumnDataType;
Expand Down Expand Up @@ -234,10 +235,25 @@ void post_addPostWithNonExistentFrom_404() {
void post_validAddPost_200() throws HttpResponseException {
Thread thread = createAndCheck(create(), AUTH_HEADERS);
// Add 10 posts and validate
for (int i = 0; i < 10; i++) {
int POST_COUNT = 10;
for (int i = 0; i < POST_COUNT; i++) {
Post post = createPost();
thread = addPostAndCheck(thread, post, AUTH_HEADERS);
}

// Check if get posts API returns all the posts
PostList postList = listPosts(thread.getId().toString(), AUTH_HEADERS);
// Thread also has the first message as a post.
// So, the total count should be POST_COUNT+1
assertEquals(POST_COUNT + 1, postList.getData().size());
}

@Test
void get_listPosts_404() {
assertResponse(
() -> listPosts(NON_EXISTENT_ENTITY.toString(), AUTH_HEADERS),
NOT_FOUND,
entityNotFound("Thread", NON_EXISTENT_ENTITY));
}

public static Thread createAndCheck(CreateThread create, Map<String, String> authHeaders)
Expand Down Expand Up @@ -311,6 +327,11 @@ public static ThreadList listThreads(String entityLink, Map<String, String> auth
return TestUtils.get(target, ThreadList.class, authHeaders);
}

public static PostList listPosts(String threadId, Map<String, String> authHeaders) throws HttpResponseException {
WebTarget target = getResource(String.format("feed/%s/posts", threadId));
return TestUtils.get(target, PostList.class, authHeaders);
}

public static ThreadCount listThreadsCount(String entityLink, Map<String, String> authHeaders)
throws HttpResponseException {
WebTarget target = getResource("feed/count");
Expand All @@ -322,6 +343,6 @@ private int getThreadCount(String entityLink, Map<String, String> authHeaders) t
List<EntityLinkThreadCount> linkThreadCount = listThreadsCount(entityLink, authHeaders).getCounts();
EntityLinkThreadCount threadCount =
linkThreadCount.stream().filter(l -> l.getEntityLink().equals(entityLink)).findFirst().orElseThrow();
return (int) threadCount.getCount();
return threadCount.getCount();
}
}

0 comments on commit d0e6a3f

Please sign in to comment.