forked from sublinks/sublinks-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into Add-more-unit-test-coverage-to-Search-package-…
- Loading branch information
Showing
7 changed files
with
160 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
src/main/java/com/sublinks/sublinksapi/federation/enums/RoutingKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/main/java/com/sublinks/sublinksapi/federation/listeners/CommentCreateListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.sublinks.sublinksapi.federation.listeners; | ||
|
||
import com.sublinks.sublinksapi.comment.entities.Comment; | ||
import com.sublinks.sublinksapi.comment.events.CommentCreatedEvent; | ||
import com.sublinks.sublinksapi.federation.enums.RoutingKey; | ||
import com.sublinks.sublinksapi.queue.services.Producer; | ||
import lombok.Getter; | ||
import lombok.NonNull; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.Setter; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.ApplicationListener; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
@Getter | ||
@Setter | ||
public class CommentCreateListener implements ApplicationListener<CommentCreatedEvent> { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(CommentCreateListener.class); | ||
|
||
final private Producer federationProducer; | ||
@Value("${sublinks.federation.exchange}") | ||
private String federationExchange; | ||
|
||
@Override | ||
public void onApplicationEvent(@NonNull CommentCreatedEvent event) { | ||
|
||
if (getFederationProducer() == null) { | ||
logger.error("federation producer is not instantiated properly"); | ||
return; | ||
} | ||
|
||
Comment comment = event.getComment(); | ||
|
||
final com.sublinks.sublinksapi.federation.models.Comment commentMsg = | ||
com.sublinks.sublinksapi.federation.models.Comment.builder() | ||
.id(comment.getActivityPubId()) | ||
.author_id(comment.getPerson() | ||
.getActivityPubId()) | ||
.content(comment.getCommentBody()) | ||
.post_id(comment.getPost() | ||
.getActivityPubId()) | ||
.url_stub(comment.getPath()) | ||
.published(comment.getCreatedAt()) | ||
.build(); | ||
|
||
getFederationProducer().sendMessage(getFederationExchange(), | ||
RoutingKey.COMMENT_CREATE.getValue(), | ||
commentMsg); | ||
logger.info(String.format("comment created %s", comment.getActivityPubId())); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/sublinks/sublinksapi/federation/models/Comment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.sublinks.sublinksapi.federation.models; | ||
|
||
import lombok.Builder; | ||
import java.util.Date; | ||
|
||
/** | ||
* Represents a comment in federation queue. | ||
*/ | ||
@SuppressWarnings("checkstyle:RecordComponentName") | ||
@Builder | ||
public record Comment( | ||
String id, | ||
String url_stub, | ||
String post_id, | ||
String author_id, | ||
Boolean nsfw, | ||
String content, | ||
Date published | ||
) { | ||
|
||
} |
69 changes: 69 additions & 0 deletions
69
src/test/java/com/sublinks/sublinksapi/federation/CommentCreateUnitTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.sublinks.sublinksapi.federation; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertAll; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.eq; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import com.sublinks.sublinksapi.comment.entities.Comment; | ||
import com.sublinks.sublinksapi.comment.events.CommentCreatedEvent; | ||
import com.sublinks.sublinksapi.federation.enums.ActorType; | ||
import com.sublinks.sublinksapi.federation.enums.RoutingKey; | ||
import com.sublinks.sublinksapi.federation.listeners.CommentCreateListener; | ||
import com.sublinks.sublinksapi.federation.models.Actor; | ||
import com.sublinks.sublinksapi.person.entities.Person; | ||
import com.sublinks.sublinksapi.post.entities.Post; | ||
import com.sublinks.sublinksapi.queue.services.Producer; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.ArgumentCaptor; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
|
||
@ExtendWith(SpringExtension.class) | ||
public class CommentCreateUnitTests { | ||
|
||
@MockBean | ||
private Producer producer; | ||
private CommentCreateListener listener; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
|
||
listener = new CommentCreateListener(producer); | ||
listener.setFederationExchange("testExchange"); | ||
} | ||
|
||
@Test | ||
void testOnApplicationEvent() { | ||
|
||
Comment comment = new Comment(); | ||
Person person = new Person(); | ||
Post post = new Post(); | ||
|
||
person.setActivityPubId("testPersonActivityPubId"); | ||
post.setActivityPubId("testPostActivityPubId"); | ||
comment.setActivityPubId("testId"); | ||
comment.setCommentBody("Test Body"); | ||
comment.setPerson(person); | ||
comment.setPost(post); | ||
|
||
CommentCreatedEvent event = new CommentCreatedEvent(this, comment); | ||
|
||
ArgumentCaptor<com.sublinks.sublinksapi.federation.models.Comment> commentCaptor = ArgumentCaptor.forClass( | ||
com.sublinks.sublinksapi.federation.models.Comment.class); | ||
|
||
listener.onApplicationEvent(event); | ||
|
||
verify(listener.getFederationProducer(), times(1)).sendMessage(eq("testExchange"), | ||
eq(RoutingKey.COMMENT_CREATE.getValue()), commentCaptor.capture()); | ||
|
||
com.sublinks.sublinksapi.federation.models.Comment capturedComment = commentCaptor.getValue(); | ||
assertAll( | ||
() -> assertEquals("testId", capturedComment.id()), | ||
() -> assertEquals("Test Body", capturedComment.content()) | ||
); | ||
} | ||
} |