Skip to content

Commit

Permalink
feat(fabric8io#1382): Allow to provide additional fragment filename m…
Browse files Browse the repository at this point in the history
…appings
  • Loading branch information
lordofthejars committed Sep 18, 2018
1 parent bf5332c commit e76aaf0
Show file tree
Hide file tree
Showing 14 changed files with 331 additions and 7 deletions.
24 changes: 24 additions & 0 deletions core/src/main/java/io/fabric8/maven/core/config/MappingConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.fabric8.maven.core.config;

import org.apache.maven.plugins.annotations.Parameter;

public class MappingConfig {

@Parameter(required = true)
private String kind;

@Parameter(required = true)
private String filenames;

public String getKind() {
return kind;
}

public String getFilenames() {
return filenames;
}

public String[] getFilenamesAsArray() {
return filenames.split(",\\s*");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public class ResourceConfig {

@Parameter
private MetaDataConfig annotations = new MetaDataConfig();
;

@Parameter
private List<VolumeConfig> volumes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class AsciiDocParser {
* This method returns an @code{IllegalArgumentException} if does not contain two columns.
*
* @param table definition in AsciiDoc format.
* @return A serialization of all columns, being pair elements the first column and the odd elements the second column. In previous example @code{"cm","ConfigMap","cronjob","CronJob"}
* @return A serialization of all columns.
*/
public Map<String, List<String>> serializeKindFilenameTable(final InputStream table) {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.fabric8.maven.core.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class PropertiesMappingParser {

/**
* This method reads properties file to load custom mapping between kinds and filenames.
*
* <pre>
* ConfigMap=cm, configmap
* Service=service
* </pre>
*
* @param mapping
* stream of a properties file setting mappings between kinds and filenames.
*
* @return Serialization of all elements as a map
*/
public Map<String, List<String>> parse(final InputStream mapping) {

final Properties mappingProperties = new Properties();
try {
mappingProperties.load(mapping);

final Map<String, List<String>> serializedContent = new HashMap<>();

final Set<String> kinds = mappingProperties.stringPropertyNames();

for (String kind : kinds) {
final String filenames = mappingProperties.getProperty(kind);
final String[] filenameTypes = filenames.split(",");
final List<String> scannedFiletypes = new ArrayList<>();
for (final String filenameType : filenameTypes) {
scannedFiletypes.add(filenameType.trim());
}
serializedContent.put(kind, scannedFiletypes);
}

return serializedContent;
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
package io.fabric8.maven.core.util.kubernetes;

import io.fabric8.maven.core.util.AsciiDocParser;
import io.fabric8.maven.core.util.EnvUtil;
import io.fabric8.maven.core.util.PropertiesMappingParser;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
Expand All @@ -27,10 +32,20 @@ public class KindFilenameMapperUtil {
public static Map<String, List<String>> loadMappings() {

final String location = "/META-INF/fabric8/kind-filename-type-mapping-default.adoc";
final String locationMappingProperties =
EnvUtil.getEnvVarOrSystemProperty("fabric8.mapping", "/META-INF/fabric8/kind-filename-type-mapping-default.properties");

try (final InputStream mappingFile = loadContent(location)) {
try (final InputStream mappingFile = loadContent(location); final InputStream mappingPropertiesFile = loadContent(locationMappingProperties)) {
final AsciiDocParser asciiDocParser = new AsciiDocParser();
return asciiDocParser.serializeKindFilenameTable(mappingFile);
final Map<String, List<String>> defaultMapping = asciiDocParser.serializeKindFilenameTable(mappingFile);

if (mappingPropertiesFile != null) {
PropertiesMappingParser propertiesMappingParser = new PropertiesMappingParser();
defaultMapping.putAll(propertiesMappingParser.parse(mappingPropertiesFile));
}

return defaultMapping;

} catch (IOException e) {
throw new IllegalStateException(e);
}
Expand All @@ -40,7 +55,13 @@ private static InputStream loadContent(String location) {
InputStream resourceAsStream = KindFilenameMapperUtil.class.getResourceAsStream(location);

if (resourceAsStream == null) {
throw new IllegalArgumentException(String.format("%s cannot be found in classpath", location));
final File locationFile = new File(location);

try {
return new FileInputStream(locationFile);
} catch (FileNotFoundException e) {
return null;
}
}

return resourceAsStream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,15 @@ public boolean accept(File dir, String name) {

protected final static void initializeKindFilenameMapper() {
final Map<String, List<String>> mappings = KindFilenameMapperUtil.loadMappings();
updateKindFilenameMapper(mappings);
}

protected final static void remove(String kind, String filename) {
FILENAME_TO_KIND_MAPPER.remove(filename);
KIND_TO_FILENAME_MAPPER.remove(kind);
}

public final static void updateKindFilenameMapper(final Map<String, List<String>> mappings) {

final Set<Map.Entry<String, List<String>>> entries = mappings.entrySet();

Expand All @@ -206,6 +215,7 @@ protected final static void initializeKindFilenameMapper() {
KIND_TO_FILENAME_MAPPER.put(kind, filenameTypes.get(filenameTypes.size() - 1));

}

}

private static final String FILENAME_PATTERN = "^(?<name>.*?)(-(?<type>[^-]+))?\\.(?<ext>yaml|yml|json)$";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.fabric8.maven.core.util;

import java.io.ByteArrayInputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class PropertiesMappingParserTest {

private static final String MAPPING_PROPERTIES = "ConfigMap=cm, configmap";

@Test
public void should_read_mappings_from_properties_file() {

// Given

final PropertiesMappingParser propertiesMappingParser = new PropertiesMappingParser();

// When

final Map<String, List<String>> serializedContent =
propertiesMappingParser.parse(new ByteArrayInputStream(MAPPING_PROPERTIES.getBytes()));

// Then

final Map<String, List<String>> expectedSerlializedContent = new HashMap<>();
expectedSerlializedContent.put("ConfigMap", Arrays.asList("cm", "configmap"));

assertThat(serializedContent)
.containsAllEntriesOf(expectedSerlializedContent);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,31 @@

package io.fabric8.maven.core.util.kubernetes;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.RestoreSystemProperties;
import org.junit.rules.TemporaryFolder;

import static org.assertj.core.api.Assertions.assertThat;

public class KindFilenameMapperUtilTest {

private static final String MAPPING_PROPERTIES = "Var=foo, bar";
private static final String OVERRIDE_MAPPING_PROPERTIES = "ConfigMap=foo, bar";

@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

@Rule
public RestoreSystemProperties restoreSystemProperties = new RestoreSystemProperties();

@Test
public void should_load_default_mapping_file() {

Expand All @@ -43,6 +58,47 @@ public void should_load_default_mapping_file() {

}

@Test
public void should_load_mappings_from_custom_properties_file() throws IOException {

// Given

final File customMappings = temporaryFolder.newFile("custom.properties");
Files.write(customMappings.toPath(), MAPPING_PROPERTIES.getBytes());
System.setProperty("fabric8.mapping", customMappings.getAbsolutePath());

// When

final Map<String, List<String>> mappings = KindFilenameMapperUtil.loadMappings();

// Then
final Map<String, List<String>> expectedSerlializedContent = new HashMap<>();
expectedSerlializedContent.put("ConfigMap", Arrays.asList("cm", "configmap"));
expectedSerlializedContent.put("CronJob", Arrays.asList("cj", "cronjob"));
expectedSerlializedContent.put("Var", Arrays.asList("foo", "bar"));
assertThat(mappings).containsAllEntriesOf(expectedSerlializedContent);

}

@Test
public void should_load_mappings_and_override_from_custom_properties_file() throws IOException {

// Given

final File customMappings = temporaryFolder.newFile("custom.properties");
Files.write(customMappings.toPath(), OVERRIDE_MAPPING_PROPERTIES.getBytes());
System.setProperty("fabric8.mapping", customMappings.getAbsolutePath());

// When

final Map<String, List<String>> mappings = KindFilenameMapperUtil.loadMappings();

// Then
final Map<String, List<String>> expectedSerlializedContent = new HashMap<>();
expectedSerlializedContent.put("ConfigMap", Arrays.asList("foo", "bar"));
expectedSerlializedContent.put("CronJob", Arrays.asList("cj", "cronjob"));
assertThat(mappings).containsAllEntriesOf(expectedSerlializedContent);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@

package io.fabric8.maven.core.util.kubernetes;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.After;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class KubernetesResourceUtilTest {

@Test
public void should_load_kid_filename_mapper_in_bimap_fashion() {
public void should_load_kind_filename_mapper_in_bimap_fashion() {

// Given

Expand All @@ -45,4 +49,38 @@ public void should_load_kid_filename_mapper_in_bimap_fashion() {

}

@Test
public void should_update_kind_filename_mapper_in_bimap_fashion() {

// Given

final Map<String, List<String>> updated = new HashMap<>();
updated.put("Var", Arrays.asList("foo"));

// When

KubernetesResourceUtil.updateKindFilenameMapper(updated);

final Map<String, String> filenameToKindMapper = KubernetesResourceUtil.FILENAME_TO_KIND_MAPPER;
final Map<String, String> kindToFilenameMapper = KubernetesResourceUtil.KIND_TO_FILENAME_MAPPER;

// Then
assertThat(filenameToKindMapper)
.containsEntry("cm", "ConfigMap")
.containsEntry("configmap", "ConfigMap")
.containsEntry("secret", "Secret")
.containsEntry("foo", "Var");

assertThat(kindToFilenameMapper)
.containsEntry("ConfigMap", "configmap")
.containsEntry("Secret", "secret")
.containsEntry("Var", "foo");

}

@After
public void cleanCustomAdditions() {
KubernetesResourceUtil.remove("Var", "foo");
}

}
7 changes: 7 additions & 0 deletions parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,13 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.maven.plugin-testing</groupId>
<artifactId>maven-plugin-testing-harness</artifactId>
<version>3.3.0</version>
<scope>test</scope>
</dependency>

<!-- == doc ====================================== -->

<dependency>
Expand Down
6 changes: 6 additions & 0 deletions plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@
<artifactId>json-path-assert</artifactId>
</dependency>

<dependency>
<groupId>org.apache.maven.plugin-testing</groupId>
<artifactId>maven-plugin-testing-harness</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
Loading

0 comments on commit e76aaf0

Please sign in to comment.