Skip to content

Commit

Permalink
Merge branch 'main' into Add-more-unit-test-coverage-to-Search-package-
Browse files Browse the repository at this point in the history
  • Loading branch information
Pdzly authored Jun 24, 2024
2 parents 217c54c + ce3f550 commit ce9b0a6
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 11 deletions.
13 changes: 7 additions & 6 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ jobs:
with:
java-version: '17'
distribution: 'corretto'
- name: Build with Gradle
uses: gradle/gradle-build-action@v3
with:
arguments: build
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v3

- name: Run build in a subdirectory
run: ./gradlew build

# Set up BuildKit Docker container builder to be able to build
# multi-platform images and export cache
Expand All @@ -54,7 +55,7 @@ jobs:
# https://github.com/docker/login-action
- name: Log into registry ${{ env.REGISTRY }}
if: github.event_name != 'pull_request'
uses: docker/login-action@e92390c5fb421da1463c202d546fed0ec5c39f20 # v3.1.0
uses: docker/login-action@0d4c9c5ea7693da7b068278f7b52bda2a190a446 # v3.2.0
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
Expand All @@ -72,7 +73,7 @@ jobs:
# https://github.com/docker/build-push-action
- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action@2cdde995de11925a030ce8070c3d77a52ffcf1c0 # v5.3.0
uses: docker/build-push-action@ca052bb54ab0790a636c9b5f226502c73d547a25 # v5.4.0
with:
context: .
platforms: linux/amd64,linux/arm64
Expand Down
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.5'
id 'org.springframework.boot' version '3.3.0'
id 'io.spring.dependency-management' version '1.1.5'
id 'jacoco'
}
Expand Down Expand Up @@ -29,6 +29,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.flywaydb:flyway-core'
implementation 'org.flywaydb:flyway-mysql'
implementation 'org.flywaydb:flyway-database-postgresql'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-amqp'
Expand All @@ -52,7 +53,7 @@ dependencies {
implementation 'cn.apiclub.tool:simplecaptcha:1.2.2'
implementation 'com.github.jai-imageio:jai-imageio-core:1.4.0'

implementation 'org.springframework.boot:spring-boot-starter-mail:3.2.5'
implementation 'org.springframework.boot:spring-boot-starter-mail:3.3.0'
implementation 'org.apache.commons:commons-text:1.12.0'
implementation 'org.thymeleaf:thymeleaf:3.1.2.RELEASE'
implementation 'org.thymeleaf:thymeleaf-spring6:3.1.2.RELEASE'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public class SecurityConfig {
public SecurityFilterChain filterChain(final HttpSecurity http) throws Exception {

http
.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests((requests) -> requests
.anyRequest().permitAll()
)
Expand All @@ -44,4 +43,4 @@ public SecurityFilterChain filterChain(final HttpSecurity http) throws Exception
.addFilterBefore(jwtFilter, UsernamePasswordAuthenticationFilter.class);
return http.build();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.sublinks.sublinksapi.federation.enums;

public enum RoutingKey {
ACTOR_CREATE("actor.create");
ACTOR_CREATE("actor.create"),
POST_CREATE("post.create"),
COMMENT_CREATE("comment.create");

private final String value;

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

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

0 comments on commit ce9b0a6

Please sign in to comment.