-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from The-Huginn/fetch-config-file
Fetch&Deserialize Config File from a public GitHub repo
- Loading branch information
Showing
9 changed files
with
175 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.jboss.config; | ||
|
||
import org.eclipse.microprofile.config.spi.Converter; | ||
|
||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
|
||
/** | ||
* Class containing only String but due to some preprocessing of this | ||
* property we need to have it encapsulated. We expect the repository | ||
* to be publicly available. | ||
*/ | ||
public class GitHubRawUrl { | ||
|
||
private final String rawContentsUrl; | ||
|
||
private GitHubRawUrl(String rawContentsUrl) { | ||
this.rawContentsUrl = rawContentsUrl; | ||
} | ||
|
||
public String getRawContentsUrl() { | ||
return rawContentsUrl; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return rawContentsUrl; | ||
} | ||
|
||
public static class GitHubRepoToRawUrlConverter implements Converter<GitHubRawUrl> { | ||
|
||
private static final String RAW_CONTENTS = "https://raw.githubusercontent.com"; | ||
private static final String RELATIVE_PATH = "main/.github/" + LotteryConfig.FILE_NAME; | ||
|
||
public GitHubRepoToRawUrlConverter() { | ||
} | ||
|
||
@Override | ||
public GitHubRawUrl convert(String value) { | ||
try { | ||
String repo = new URI(value).getPath(); | ||
String rawContentsURI = RAW_CONTENTS + repo + (repo.endsWith("/") ? "" : "/") + RELATIVE_PATH; | ||
return new GitHubRawUrl(rawContentsURI); | ||
|
||
} catch (URISyntaxException e) { | ||
throw new RuntimeException("Provided GitHub repo url is not valid"); | ||
} | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package org.jboss.config; | ||
|
||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; | ||
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; | ||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | ||
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule; | ||
import io.quarkus.arc.All; | ||
import io.quarkus.jackson.ObjectMapperCustomizer; | ||
import jakarta.inject.Singleton; | ||
import jakarta.ws.rs.Produces; | ||
|
||
import java.util.List; | ||
|
||
public class YamlObjectMapper { | ||
@Singleton | ||
@Produces | ||
ObjectMapper objectMapper(@All List<ObjectMapperCustomizer> customizers) { | ||
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory()); | ||
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); | ||
objectMapper.registerModule(new JavaTimeModule()); | ||
objectMapper.registerModule(new Jdk8Module()); | ||
objectMapper.registerModule(new ParameterNamesModule()); | ||
|
||
// Apply all ObjectMapperCustomizer beans (incl. Quarkus) | ||
for (ObjectMapperCustomizer customizer : customizers) { | ||
customizer.customize(objectMapper); | ||
} | ||
|
||
return objectMapper; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
src/main/resources/META-INF/services/org.eclipse.microprofile.config.spi.Converter
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 @@ | ||
org.jboss.config.GitHubRawUrl$GitHubRepoToRawUrlConverter |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
camel.component.jira.jira-url=https://issues.redhat.com/ | ||
%test.jira-issue-lottery.access-token=ignored | ||
%test.jira-issue-lottery.access-token=ignored | ||
jira-issue-lottery.config-file-repo=https://github.com/The-Huginn/jira-issue-lottery |
57 changes: 57 additions & 0 deletions
57
src/test/java/org/jboss/set/state/ConfigDeserializationTest.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,57 @@ | ||
package org.jboss.set.state; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
import jakarta.inject.Inject; | ||
import org.jboss.config.GitHubRawUrl; | ||
import org.jboss.config.LotteryConfig; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
|
||
@QuarkusTest | ||
public class ConfigDeserializationTest { | ||
|
||
@Inject | ||
ObjectMapper objectMapper; | ||
|
||
@Test | ||
void testDeserializingSimpleConfigFile() { | ||
String configFile = """ | ||
delay: P14D | ||
participants: | ||
- username: The-Huginn | ||
days: [MONDAY] | ||
projects: | ||
- project: WFLY | ||
components: [Logging] | ||
maxIssues: 5"""; | ||
|
||
Assertions.assertTrue(objectMapper.getFactory() instanceof YAMLFactory); | ||
try { | ||
objectMapper.readValue(configFile, LotteryConfig.class); | ||
} catch (IOException e) { | ||
Assertions.fail("Expecting no problem deserializing the config file %s"); | ||
} | ||
} | ||
|
||
@Test | ||
void testConvertingGitHubRepoUrlWithoutTrailingSlash() { | ||
String repoUrl = "https://github.com/The-Huginn/jira-issue-lottery"; | ||
GitHubRawUrl gitHubRawUrl = new GitHubRawUrl.GitHubRepoToRawUrlConverter().convert(repoUrl); | ||
Assertions.assertEquals( | ||
"https://raw.githubusercontent.com/The-Huginn/jira-issue-lottery/main/.github/jira-issue-lottery.yml", | ||
gitHubRawUrl.getRawContentsUrl()); | ||
} | ||
|
||
@Test | ||
void testConvertingGitHubRepoUrlWithTrailingSlash() { | ||
String repoUrl = "https://github.com/The-Huginn/jira-issue-lottery/"; | ||
GitHubRawUrl gitHubRawUrl = new GitHubRawUrl.GitHubRepoToRawUrlConverter().convert(repoUrl); | ||
Assertions.assertEquals( | ||
"https://raw.githubusercontent.com/The-Huginn/jira-issue-lottery/main/.github/jira-issue-lottery.yml", | ||
gitHubRawUrl.getRawContentsUrl()); | ||
} | ||
} |