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

#194 Enable/Disable Cache Job per yaml configuration (#197) #202

Merged
merged 1 commit into from
Dec 29, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3,275 changes: 1 addition & 3,274 deletions frontend/package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>conorheffron</groupId>
<artifactId>ironoc</artifactId>
<version>5.5.1</version>
<version>5.5.2</version>
<packaging>war</packaging>

<distributionManagement>
Expand All @@ -22,15 +22,15 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.0</version>
<version>3.4.1</version>
<relativePath />
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>21</java.version>
<spring.version>3.4.0</spring.version>
<spring.version>3.4.1</spring.version>
<frontend-maven-plugin.version>1.15.1</frontend-maven-plugin.version>
<node.version>v20.16.0</node.version>
<npm.version>10.8.1</npm.version>
Expand Down Expand Up @@ -147,7 +147,7 @@
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>aws-sdk-java</artifactId>
<version>2.29.34</version>
<version>2.29.39</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/ironoc/portfolio/config/Properties.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public enum Properties {
STATIC_CONF_HANDLE_EXT("net.ironoc.portfolio.config.handle-extensions"),
STATIC_CONF_RESOURCE_HANDLER("net.ironoc.portfolio.config.resource-handler"),
STATIC_CONF_RESOURCE_LOC("net.ironoc.portfolio.config.resource-loc"),
IS_GITHUB_JOB_ENABLED("net.ironoc.portfolio.github.job-enable"),
GIT_API_ENDPOINT_REPOS("net.ironoc.portfolio.github.api.endpoint.repos"),
GIT_API_ENDPOINT_REPOS_PARAM_CACHE("net.ironoc.portfolio.github.api.endpoint.user-ids-cache"),
GIT_API_ENDPOINT_ISSUES("net.ironoc.portfolio.github.api.endpoint.issues"),
Expand All @@ -18,7 +19,7 @@ public enum Properties {
GIT_INSTANCE_FOLLOW_REDIRECTS("net.ironoc.portfolio.github.instance-follow-redirects"),
GIT_FOLLOW_REDIRECTS("net.ironoc.portfolio.github.follow-redirects");

private String key;
private final String key;

Properties(String key) {
this.key = key;
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/net/ironoc/portfolio/config/PropertyConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ public List<String> getGitApiEndpointProjectsCache() {
return extractValues(projects);
}

@Override
public boolean isCacheJobEnabled() {
return Boolean.parseBoolean(environment.getRequiredProperty(propertyKey.isCacheJobEnabled()));
}

private List<String> extractValues(String valuesStr) {
return Arrays.stream(StringUtils.split(valuesStr, ",")).toList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ public interface PropertyConfigI {
List<String> getGitApiEndpointUserIdsCache();

List<String> getGitApiEndpointProjectsCache();

boolean isCacheJobEnabled();
}
5 changes: 5 additions & 0 deletions src/main/java/net/ironoc/portfolio/config/PropertyKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,9 @@ public String getGitApiEndpointUserIdsCache() {
public String getGitApiEndpointProjectsCache() {
return Properties.GIT_API_ENDPOINT_ISSUES_PARAM_CACHE.getKey();
}

@Override
public String isCacheJobEnabled() {
return Properties.IS_GITHUB_JOB_ENABLED.getKey();
}
}
2 changes: 2 additions & 0 deletions src/main/java/net/ironoc/portfolio/config/PropertyKeyI.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ public interface PropertyKeyI {
String getGitApiEndpointUserIdsCache();

String getGitApiEndpointProjectsCache();

String isCacheJobEnabled();
}
15 changes: 12 additions & 3 deletions src/main/java/net/ironoc/portfolio/job/GitDetailsJob.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.ironoc.portfolio.job;

import net.ironoc.portfolio.config.PropertyConfigI;
import net.ironoc.portfolio.logger.AbstractLogger;
import net.ironoc.portfolio.service.GitProjectCache;
import net.ironoc.portfolio.service.GitRepoCache;
import net.ironoc.portfolio.service.GitDetails;
Expand All @@ -10,7 +11,7 @@
import org.springframework.stereotype.Component;

@Component
public class GitDetailsJob {
public class GitDetailsJob extends AbstractLogger {

private final GitRepoCache gitRepoCache;

Expand All @@ -33,12 +34,20 @@ public GitDetailsJob(GitDetails gitDetails,

@PostConstruct
public void populateCache() {
triggerJob();
if (propertyConfig.isCacheJobEnabled()) {
triggerJob();
} else {
warn("The job to pre-populate the cache of GitHub information is disabled.");
}
}

@Scheduled(cron = "${net.ironoc.portfolio.github.cron-job}")
public void triggerGitDetailsJob() {
triggerJob();
if (propertyConfig.isCacheJobEnabled()) {
triggerJob();
} else {
warn("The job to update the cache of GitHub information is disabled.");
}
}

private void triggerJob() {
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ net:
ironoc:
portfolio:
github:
job-enable: true
cron-job: "0 1 1 ? * *"
timeout:
connect: 100000
Expand All @@ -16,7 +17,7 @@ net:
projects-cache: "bio-cell-red-edge,booking-sys,cbio-skin-canc,fun-with-r,gene-expr,global-max-sim-matrix,graphql-box,ironoc,ironoc-db,ironoc-msg,ironoc-pytest,ironoc-rustt,ironoc-spark,mern-essential-training,mern-sandbox,nba-stats,netflix-movie-duration,normalise-fetalh,normalise-spotify,nyc-school-scores,python-sandbox,rabbitmq-tester,react-graphql-course,redis-tester,ronoc-packages"
config:
ignore-paths: api
handle-extensions: "html,js,json,csv,css,map,svg,eot,ttf,woff,appcache,png,jpg,jpeg,gif,ico,txt"
handle-extensions: "html,js,json,css,map,svg,eot,ttf,woff,png,jpg,jpeg,gif,ico"
resource-handler: "/**"
resource-loc: "classpath:/static/"

Expand Down
16 changes: 16 additions & 0 deletions src/test/java/net/ironoc/portfolio/config/PropertyConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,20 @@ public void test_getGitApiEndpointProjectsCache_success() {

assertThat(result, is(Arrays.stream(StringUtils.split(TEST_PROPS_VAL, ",")).toList()));
}

@Test
public void test_isCacheJobEnabled_success() {
// given
when(propertyKeyMock.isCacheJobEnabled()).thenReturn(Properties.IS_GITHUB_JOB_ENABLED.getKey());
when(environmentMock.getRequiredProperty(Properties.IS_GITHUB_JOB_ENABLED.getKey())).thenReturn("true");

// when
boolean result = propertyConfig.isCacheJobEnabled();

// then
verify(propertyKeyMock).isCacheJobEnabled();
verify(environmentMock).getRequiredProperty(Properties.IS_GITHUB_JOB_ENABLED.getKey());

assertThat(result, is(Boolean.TRUE.booleanValue()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,13 @@ public void test_getGitApiEndpointUserIdsCache_success() {
// then
assertThat(result, is("net.ironoc.portfolio.github.api.endpoint.user-ids-cache"));
}

@Test
public void test_isCacheJobEnabled_success() {
// when
String result = propertyKey.isCacheJobEnabled();

// then
assertThat(result, is("net.ironoc.portfolio.github.job-enable"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"net.ironoc.portfolio.config.resource-loc=\"classpath:/static/\"",
"net.ironoc.portfolio.github.api.endpoint.user-ids-cache=conorheffron",
"net.ironoc.portfolio.github.api.endpoint.projects-cache=\"ironoc,ironoc-db,booking-sys\"",
"net.ironoc.portfolio.github.cron-job=0 1 1 ? * *"
"net.ironoc.portfolio.github.cron-job=0 1 1 ? * *",
"net.ironoc.portfolio.github.job-enable=true"
})
public class GitProjectsControllerIntegrationTest {

Expand Down
43 changes: 42 additions & 1 deletion src/test/java/net/ironoc/portfolio/job/GitDetailsJobTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@

import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.never;

@ExtendWith(MockitoExtension.class)
public class GitDetailsJobTest {
Expand Down Expand Up @@ -48,6 +49,7 @@ public class GitDetailsJobTest {
@Test
public void test_triggerGitDetailsJob_success() {
// given
when(propertyConfigMock.isCacheJobEnabled()).thenReturn(true);
when(propertyConfigMock.getGitApiEndpointUserIdsCache()).thenReturn(List.of("conorheffron"));
when(propertyConfigMock.getGitApiEndpointProjectsCache())
.thenReturn(List.of("ironoc", "booking-sys"));
Expand All @@ -60,6 +62,7 @@ public void test_triggerGitDetailsJob_success() {
gitDetailsJob.triggerGitDetailsJob();

// then
verify(propertyConfigMock).isCacheJobEnabled();
verify(propertyConfigMock).getGitApiEndpointProjectsCache();
verify(propertyConfigMock).getGitApiEndpointUserIdsCache();
verify(gitDetailsMock).getRepoDetails(anyString());
Expand All @@ -71,6 +74,7 @@ public void test_triggerGitDetailsJob_success() {
@Test
public void test_populateCache_success() {
// given
when(propertyConfigMock.isCacheJobEnabled()).thenReturn(true);
when(propertyConfigMock.getGitApiEndpointUserIdsCache()).thenReturn(List.of("ironoc-test-id"));
when(propertyConfigMock.getGitApiEndpointProjectsCache())
.thenReturn(List.of("ironoc", "booking-sys", "ironoc-db", "nba-stats"));
Expand All @@ -83,11 +87,48 @@ public void test_populateCache_success() {
gitDetailsJob.populateCache();

// then
verify(propertyConfigMock).isCacheJobEnabled();
verify(propertyConfigMock).getGitApiEndpointProjectsCache();
verify(propertyConfigMock).getGitApiEndpointUserIdsCache();
verify(gitDetailsMock).getRepoDetails(anyString());
verify(gitRepoCache).put(anyString(), anyList());
verify(gitDetailsMock, times(4)).getIssues(anyString(), anyString());
verify(gitProjectCache, times(4)).put(anyString(), anyString(), anyList());
}

@Test
public void test_triggerGitDetailsJob_fail() {
// given
when(propertyConfigMock.isCacheJobEnabled()).thenReturn(false);

// when
gitDetailsJob.triggerGitDetailsJob();

// then
verify(propertyConfigMock).isCacheJobEnabled();
verify(propertyConfigMock, never()).getGitApiEndpointProjectsCache();
verify(propertyConfigMock, never()).getGitApiEndpointUserIdsCache();
verify(gitDetailsMock, never()).getRepoDetails(anyString());
verify(gitRepoCache, never()).put(anyString(), anyList());
verify(gitDetailsMock, never()).getIssues(anyString(), anyString());
verify(gitProjectCache, never()).put(anyString(), anyString(), anyList());
}

@Test
public void test_populateCache_fail() {
// given
when(propertyConfigMock.isCacheJobEnabled()).thenReturn(false);

// when
gitDetailsJob.populateCache();

// then
verify(propertyConfigMock).isCacheJobEnabled();
verify(propertyConfigMock, never()).getGitApiEndpointProjectsCache();
verify(propertyConfigMock, never()).getGitApiEndpointUserIdsCache();
verify(gitDetailsMock, never()).getRepoDetails(anyString());
verify(gitRepoCache, never()).put(anyString(), anyList());
verify(gitDetailsMock, never()).getIssues(anyString(), anyString());
verify(gitProjectCache, never()).put(anyString(), anyString(), anyList());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.ironoc.portfolio.config;
package net.ironoc.portfolio.resolver;

import jakarta.servlet.http.HttpServletRequest;
import net.ironoc.portfolio.resolver.PushStateResourceResolver;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down
Loading