Skip to content

Commit

Permalink
Introduce unmarshal methods that take java.nio.Path
Browse files Browse the repository at this point in the history
Signed-off-by: Jurrie Overgoor <[email protected]>
  • Loading branch information
Jurrie committed Feb 16, 2024
1 parent 97e10a3 commit fcd7c95
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

public class Serialization {

Expand Down Expand Up @@ -65,19 +66,28 @@ public static <T> T unmarshal(File file) throws IOException {
}

public static <T> T unmarshal(File file, Class<T> clazz) throws IOException {
try (InputStream fis = Files.newInputStream(file.toPath())) {
return unmarshal(file.toPath(), clazz);
}

public static <T> T unmarshal(File file, TypeReference<T> type) throws IOException {
return unmarshal(file.toPath(), type);
}

public static <T> T unmarshal(Path file, Class<T> clazz) throws IOException {
try (InputStream fis = Files.newInputStream(file)) {
return unmarshal(fis, clazz);
}
}

public static <T> T unmarshal(File file, TypeReference<T> type) throws IOException {
try (InputStream fis = Files.newInputStream(file.toPath())) {
public static <T> T unmarshal(Path file, TypeReference<T> type) throws IOException {
try (InputStream fis = Files.newInputStream(file)) {
return unmarshal(fis, type);
}
}
public static <T> T unmarshal(URL url, Class<T> type) throws IOException {

public static <T> T unmarshal(URL url, Class<T> clazz) throws IOException {
try (InputStream is = url.openStream()){
return unmarshal(is, type);
return unmarshal(is, clazz);
}
}

Expand All @@ -95,8 +105,8 @@ public static <T> T unmarshal(InputStream is, TypeReference<T> type) {
return KUBERNETES_SERIALIZATION.unmarshal(is, type);
}

public static <T> T unmarshal(String string, Class<T> type) {
return KUBERNETES_SERIALIZATION.unmarshal(string, type);
public static <T> T unmarshal(String string, Class<T> clazz) {
return KUBERNETES_SERIALIZATION.unmarshal(string, clazz);
}

public static <T> T unmarshal(String string, TypeReference<T> type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void generateHelmCharts() throws IOException {
helmService.generateHelmCharts(helmConfig.build());
// Then
final Map<String, Object> chartYaml = Serialization.unmarshal(
helmOutputDirectory.resolve("kubernetes").resolve("Chart.yaml").toFile(),
helmOutputDirectory.resolve("kubernetes").resolve("Chart.yaml"),
new TypeReference<Map<String, Object>>() {});
assertThat(chartYaml)
.contains(
Expand Down Expand Up @@ -164,7 +164,7 @@ void generateHelmCharts_withValidChartYamlFragment_usesMergedChart() throws Exce
new HelmService(jKubeConfiguration, resourceServiceConfig, new KitLogger.SilentLogger())
.generateHelmCharts(helmConfig.build());
// Then
final Map<?, ?> savedChart = Serialization.unmarshal(helmOutputDirectory.resolve("kubernetes").resolve("Chart.yaml").toFile(), Map.class);
final Map<?, ?> savedChart = Serialization.unmarshal(helmOutputDirectory.resolve("kubernetes").resolve("Chart.yaml"), Map.class);
assertThat(savedChart)
.hasFieldOrPropertyWithValue("apiVersion", "v1")
.hasFieldOrPropertyWithValue("name", "name-from-fragment")
Expand Down Expand Up @@ -199,7 +199,7 @@ void generateHelmCharts_withValidValuesYamlFragment_usesMergedValues() throws Ex
new HelmService(jKubeConfiguration, resourceServiceConfig, new KitLogger.SilentLogger())
.generateHelmCharts(helmConfig.build());
// Then
final Map<?, ?> savedValues = Serialization.unmarshal(helmOutputDirectory.resolve("kubernetes").resolve("values.yaml").toFile(), Map.class);
final Map<?, ?> savedValues = Serialization.unmarshal(helmOutputDirectory.resolve("kubernetes").resolve("values.yaml"), Map.class);
assertThat(savedValues)
.hasFieldOrPropertyWithValue("replaceableProperty", "the-value")
.hasFieldOrPropertyWithValue("replicaCount", 1)
Expand Down Expand Up @@ -270,7 +270,7 @@ void createChartYamlWithDependencies() throws Exception {
helmService.generateHelmCharts(helmConfig.build());
// Then
final Map<String, Object> chartYaml = Serialization.unmarshal(
helmOutputDirectory.resolve("kubernetes").resolve("Chart.yaml").toFile(),
helmOutputDirectory.resolve("kubernetes").resolve("Chart.yaml"),
new TypeReference<Map<String, Object>>() {});
assertThat(chartYaml)
.contains(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ void executeInternal_findTemplatesFromProvidedFile() throws Exception {
.hasFieldOrPropertyWithValue("metadata.name", "the-template-for-params");
final Map<String, Object> savedChart = Serialization.unmarshal(
projectDir.resolve("target").resolve("jkube").resolve("helm").resolve("empty-project")
.resolve("kubernetes").resolve("values.yaml").toFile(), new TypeReference<Map<String, Object>>() {});
.resolve("kubernetes").resolve("values.yaml"), new TypeReference<Map<String, Object>>() {});
assertThat(savedChart).containsEntry("key", "value");
}

Expand Down

0 comments on commit fcd7c95

Please sign in to comment.