-
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.
- Loading branch information
Showing
12 changed files
with
365 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
name: CodeCov for Spring Boot | ||
|
||
on: | ||
push: | ||
paths: | ||
- '**/*.java' # Triggers the workflow only when .java files are changed | ||
workflow_dispatch: # Allows manual triggering of the workflow | ||
|
||
permissions: | ||
contents: read # Grants read access to repository contents | ||
|
||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.ref }} | ||
cancel-in-progress: true # Cancels any in-progress runs of the same workflow and branch | ||
|
||
jobs: | ||
jacoco: | ||
runs-on: ubuntu-latest | ||
timeout-minutes: 20 | ||
steps: | ||
# Checkout the repository | ||
- uses: actions/checkout@v4 | ||
|
||
# Set up JDK 22 | ||
- name: Set up JDK 22 | ||
uses: actions/setup-java@v3 | ||
with: | ||
java-version: "22" | ||
distribution: "corretto" | ||
|
||
# Set permissions for /tmp directory (if necessary) | ||
- name: Set permissions for /tmp directory | ||
run: sudo chmod -R 777 /tmp | ||
|
||
# Decode and set up secrets | ||
- name: Setup secrets | ||
run: | | ||
echo "${{ secrets.APPLICATION_YML }}" | base64 --decode > src/main/resources/application-secret.yml | ||
echo "${{ secrets.V1_SQL }}" | base64 --decode > src/main/resources/db/migration/V1__Create_festivals_table.sql | ||
echo "${{ secrets.BAD_WORD_LIST }}" | base64 --decode > src/main/resources/badwords.txt | ||
echo "${{ secrets.BAD_WORD_LIST }}" | base64 --decode > src/test/resources/badwords.txt | ||
# Ensure Maven Wrapper is executable (if using Maven Wrapper) | ||
- name: Ensure Maven Wrapper is executable | ||
run: chmod +x mvnw | ||
|
||
# Build and Run Tests with Maven (including JaCoCo) | ||
- name: Build with Maven | ||
run: ./mvnw clean verify -DskipTests=false | ||
|
||
# Upload coverage to Codecov | ||
- name: Upload coverage to Codecov | ||
uses: codecov/codecov-action@v4 | ||
with: | ||
token: ${{ secrets.CODECOV_TOKEN }} | ||
files: target/jacoco/jacoco.xml | ||
fail_ci_if_error: true |
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
Binary file not shown.
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
24 changes: 24 additions & 0 deletions
24
src/test/java/csw/korea/festival/main/LuceneConfigurationTest.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,24 @@ | ||
package csw.korea.festival.main; | ||
|
||
import csw.korea.festival.main.festival.model.Festival; | ||
import jakarta.persistence.EntityManager; | ||
import jakarta.transaction.Transactional; | ||
import org.hibernate.search.mapper.orm.Search; | ||
import org.hibernate.search.mapper.orm.massindexing.MassIndexer; | ||
import org.springframework.boot.test.context.TestConfiguration; | ||
import org.springframework.context.annotation.Bean; | ||
|
||
@TestConfiguration | ||
public class LuceneConfigurationTest { | ||
@Bean | ||
@Transactional | ||
public MassIndexer indexLuceneData(EntityManager entityManager) { | ||
MassIndexer massIndexer = Search.session(entityManager).massIndexer(Festival.class); | ||
try { | ||
massIndexer.startAndWait(); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
return massIndexer; | ||
} | ||
} |
13 changes: 0 additions & 13 deletions
13
src/test/java/csw/korea/festival/main/MainApplicationTests.java
This file was deleted.
Oops, something went wrong.
51 changes: 51 additions & 0 deletions
51
src/test/java/csw/korea/festival/main/config/web/RateLimiterServiceTest.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,51 @@ | ||
package csw.korea.festival.main.config.web; | ||
|
||
import io.github.bucket4j.Bucket; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.time.Duration; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class RateLimiterServiceTest { | ||
|
||
private final RateLimiterService rateLimiterService = new RateLimiterService(); | ||
|
||
@Test | ||
public void testResolveBucket_createsBucket() { | ||
String key = "testKey"; | ||
long capacity = 10; | ||
long refillTokens = 5; | ||
Duration refillDuration = Duration.ofSeconds(10); | ||
|
||
Bucket bucket = rateLimiterService.resolveBucket(key, capacity, refillTokens, refillDuration); | ||
assertNotNull(bucket); | ||
} | ||
|
||
@Test | ||
public void testResolveBucket_reuseBucket() { | ||
String key = "testKey"; | ||
long capacity = 10; | ||
long refillTokens = 5; | ||
Duration refillDuration = Duration.ofSeconds(10); | ||
|
||
Bucket bucket1 = rateLimiterService.resolveBucket(key, capacity, refillTokens, refillDuration); | ||
Bucket bucket2 = rateLimiterService.resolveBucket(key, capacity, refillTokens, refillDuration); | ||
|
||
assertSame(bucket1, bucket2); // same bucket is reused | ||
} | ||
|
||
@Test | ||
public void testBucketCapacityLimit() { | ||
String key = "testKeyCapacity"; | ||
long capacity = 3; | ||
long refillTokens = 1; | ||
Duration refillDuration = Duration.ofMinutes(1); | ||
|
||
Bucket bucket = rateLimiterService.resolveBucket(key, capacity, refillTokens, refillDuration); | ||
assertTrue(bucket.tryConsume(1)); | ||
assertTrue(bucket.tryConsume(1)); | ||
assertTrue(bucket.tryConsume(1)); | ||
assertFalse(bucket.tryConsume(1)); // Capacity exceeded | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
src/test/java/csw/korea/festival/main/config/web/RateLimitingAspectTest.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,78 @@ | ||
package csw.korea.festival.main.config.web; | ||
|
||
import csw.korea.festival.main.common.annotation.RateLimited; | ||
import io.github.bucket4j.Bandwidth; | ||
import io.github.bucket4j.Bucket; | ||
import io.github.bucket4j.Refill; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.reflect.MethodSignature; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import org.springframework.web.context.request.RequestContextHolder; | ||
import org.springframework.web.context.request.ServletRequestAttributes; | ||
|
||
import java.time.Duration; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.Mockito.*; | ||
|
||
public class RateLimitingAspectTest { | ||
|
||
private RateLimitingAspect rateLimitingAspect; | ||
|
||
@Mock | ||
private RateLimiterService rateLimiterService; | ||
|
||
@Mock | ||
private ProceedingJoinPoint joinPoint; | ||
|
||
@Mock | ||
private MethodSignature methodSignature; | ||
|
||
@Mock | ||
private HttpServletRequest request; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
rateLimitingAspect = new RateLimitingAspect(rateLimiterService); | ||
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); | ||
|
||
when(joinPoint.getSignature()).thenReturn(methodSignature); | ||
when(methodSignature.getMethod()).thenAnswer(invocation -> this.getClass().getDeclaredMethod("dummyMethod")); | ||
} | ||
|
||
@RateLimited(key = "testKey", capacity = 3, refillTokens = 1, refillDurationMillis = 60000) | ||
public void dummyMethod() { | ||
} | ||
|
||
@Test | ||
public void testRateLimitingPass() throws Throwable { | ||
when(rateLimiterService.resolveBucket(anyString(), anyLong(), anyLong(), any(Duration.class))) | ||
.thenReturn(Bucket.builder().addLimit(Bandwidth.classic(3, Refill.greedy(1, Duration.ofMinutes(1)))).build()); | ||
|
||
rateLimitingAspect.rateLimit(joinPoint, methodSignature.getMethod().getAnnotation(RateLimited.class)); | ||
verify(joinPoint, times(1)).proceed(); | ||
} | ||
|
||
@Test | ||
public void testRateLimitingExceeded() { | ||
Bandwidth limit = Bandwidth | ||
.builder() | ||
.capacity(1) | ||
.refillGreedy(1, Duration.ofMinutes(1)) | ||
.build(); | ||
Bucket bucket = Bucket.builder().addLimit(limit).build(); | ||
when(rateLimiterService.resolveBucket(anyString(), anyLong(), anyLong(), any(Duration.class))) | ||
.thenReturn(bucket); | ||
|
||
bucket.tryConsume(1); // Consume the only token available | ||
|
||
assertThrows(RateLimitExceededException.class, () -> | ||
rateLimitingAspect.rateLimit(joinPoint, methodSignature.getMethod().getAnnotation(RateLimited.class)) | ||
); | ||
} | ||
} |
Oops, something went wrong.