Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #2867: Add support for GET /feed/{id} #2892

Merged
merged 2 commits into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 {
mithmatt marked this conversation as resolved.
Show resolved Hide resolved
return new PostList(dao.listPosts(id));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I presume we do not have href for posts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is correct

}

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();
}
}