diff --git a/src/main/java/io/candydoc/plugin/model/ManifestDto.java b/src/main/java/io/candydoc/plugin/model/ManifestDto.java new file mode 100644 index 00000000..aa8a69c7 --- /dev/null +++ b/src/main/java/io/candydoc/plugin/model/ManifestDto.java @@ -0,0 +1,18 @@ +package io.candydoc.plugin.model; + +import java.util.List; +import lombok.Builder; +import lombok.Getter; +import lombok.extern.jackson.Jacksonized; + +@Jacksonized +@Builder +@Getter +public final class ManifestDto { + + private List files; + + public static ManifestDto from(List files) { + return ManifestDto.builder().files(files).build(); + } +} diff --git a/src/main/java/io/candydoc/plugin/save_ubiquitous_language/file/UbiquitousLanguageAsFile.java b/src/main/java/io/candydoc/plugin/save_ubiquitous_language/file/UbiquitousLanguageAsFile.java index a3b6e46c..6fc8e972 100644 --- a/src/main/java/io/candydoc/plugin/save_ubiquitous_language/file/UbiquitousLanguageAsFile.java +++ b/src/main/java/io/candydoc/plugin/save_ubiquitous_language/file/UbiquitousLanguageAsFile.java @@ -1,42 +1,36 @@ package io.candydoc.plugin.save_ubiquitous_language.file; -import com.fasterxml.jackson.databind.ObjectMapper; import io.candydoc.ddd.extract_domain_vocabulary.UbiquitousLanguageRepository; -import io.candydoc.ddd.extract_domain_vocabulary.UbiquitousLanguageSavingFailed; import io.candydoc.ddd.model.UbiquitousLanguage; +import io.candydoc.plugin.model.ManifestDto; import io.candydoc.plugin.model.UbiquitousLanguageDto; import io.candydoc.plugin.save_ubiquitous_language.file.json.UbiquitousLanguageAsJson; +import io.candydoc.plugin.save_ubiquitous_language.file.strategy.SerializationStrategy; import io.candydoc.plugin.save_ubiquitous_language.file.yml.UbiquitousLanguageAsYml; -import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.util.List; import java.util.Set; -import lombok.Getter; -import lombok.RequiredArgsConstructor; +import java.util.stream.Collectors; import lombok.experimental.Accessors; -@RequiredArgsConstructor @Accessors(fluent = true) public abstract class UbiquitousLanguageAsFile implements UbiquitousLanguageRepository { - private final ObjectMapper serializer; - @Getter private final Path destinationFolder; - @Getter private final String extension; + private final SerializationStrategy serializationStrategy; + + public UbiquitousLanguageAsFile(SerializationStrategy serializationStrategy) { + this.serializationStrategy = serializationStrategy.initDirectory(); + } @Override public void saveAll(Set ubiquitousLanguages) { - try { - Files.createDirectories(destinationFolder); + List filePaths = + ubiquitousLanguages.stream() + .map(ubiquitousLanguage -> saveToFile(ubiquitousLanguage).toString()) + .collect(Collectors.toList()); - for (UbiquitousLanguage ubiquitousLanguage : ubiquitousLanguages) { - UbiquitousLanguageDto dto = UbiquitousLanguageDto.from(ubiquitousLanguage); - String fileName = dto.getName().toLowerCase().replace(" ", "_"); - serializer.writeValue(destinationFolder.resolve(fileName + "." + extension).toFile(), dto); - } - - } catch (IOException e) { - throw new UbiquitousLanguageSavingFailed(e); - } + Manifest.generateFrom(this.serializationStrategy, filePaths); } public static UbiquitousLanguageAsFile forFormat(String outputFormat, String outputDir) { @@ -57,4 +51,25 @@ public static UbiquitousLanguageAsFile forFormat(String outputFormat, String out } throw new IllegalArgumentException("Supported file formats are 'json', 'yml'."); } + + public String getExtension() { + return this.serializationStrategy.getExtension(); + } + + public Path getDestinationFolder() { + return this.serializationStrategy.getDestinationFolder(); + } + + private Path saveToFile(UbiquitousLanguage ubiquitousLanguage) { + UbiquitousLanguageDto dto = UbiquitousLanguageDto.from(ubiquitousLanguage); + return serializationStrategy.writeToFile(dto.getName().toLowerCase(), dto); + } + + private static class Manifest { + public static void generateFrom( + SerializationStrategy serializationStrategy, List filePaths) { + ManifestDto manifestDto = ManifestDto.from(filePaths); + serializationStrategy.writeToFile("MANIFEST", manifestDto); + } + } } diff --git a/src/main/java/io/candydoc/plugin/save_ubiquitous_language/file/json/UbiquitousLanguageAsJson.java b/src/main/java/io/candydoc/plugin/save_ubiquitous_language/file/json/UbiquitousLanguageAsJson.java index e00351b6..3786f45d 100644 --- a/src/main/java/io/candydoc/plugin/save_ubiquitous_language/file/json/UbiquitousLanguageAsJson.java +++ b/src/main/java/io/candydoc/plugin/save_ubiquitous_language/file/json/UbiquitousLanguageAsJson.java @@ -1,12 +1,11 @@ package io.candydoc.plugin.save_ubiquitous_language.file.json; -import com.fasterxml.jackson.core.JsonFactory; -import com.fasterxml.jackson.databind.ObjectMapper; import io.candydoc.plugin.save_ubiquitous_language.file.UbiquitousLanguageAsFile; +import io.candydoc.plugin.save_ubiquitous_language.file.strategy.SerializationStrategyFactory; import java.nio.file.Path; public class UbiquitousLanguageAsJson extends UbiquitousLanguageAsFile { public UbiquitousLanguageAsJson(Path destinationFolder) { - super(new ObjectMapper(new JsonFactory()), destinationFolder, "json"); + super(SerializationStrategyFactory.forJson(destinationFolder)); } } diff --git a/src/main/java/io/candydoc/plugin/save_ubiquitous_language/file/strategy/DefaultSerializationStrategy.java b/src/main/java/io/candydoc/plugin/save_ubiquitous_language/file/strategy/DefaultSerializationStrategy.java new file mode 100644 index 00000000..e89a4fbb --- /dev/null +++ b/src/main/java/io/candydoc/plugin/save_ubiquitous_language/file/strategy/DefaultSerializationStrategy.java @@ -0,0 +1,31 @@ +package io.candydoc.plugin.save_ubiquitous_language.file.strategy; + +import com.fasterxml.jackson.databind.ObjectMapper; +import io.candydoc.ddd.extract_domain_vocabulary.UbiquitousLanguageSavingFailed; +import java.io.IOException; +import java.nio.file.Path; +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +public class DefaultSerializationStrategy implements SerializationStrategy { + + private final ObjectMapper serializer; + @Getter private final Path destinationFolder; + @Getter private final String extension; + + @Override + public Path writeToFile(String filename, Object dto) { + String fileName = getFileName(filename); + try { + serializer.writeValue(destinationFolder.resolve(fileName).toFile(), dto); + } catch (IOException e) { + throw new UbiquitousLanguageSavingFailed(e); + } + return Path.of("./", fileName); + } + + private String getFileName(String name) { + return name.replace(" ", "_") + "." + extension; + } +} diff --git a/src/main/java/io/candydoc/plugin/save_ubiquitous_language/file/strategy/SerializationStrategy.java b/src/main/java/io/candydoc/plugin/save_ubiquitous_language/file/strategy/SerializationStrategy.java new file mode 100644 index 00000000..57e14773 --- /dev/null +++ b/src/main/java/io/candydoc/plugin/save_ubiquitous_language/file/strategy/SerializationStrategy.java @@ -0,0 +1,23 @@ +package io.candydoc.plugin.save_ubiquitous_language.file.strategy; + +import io.candydoc.ddd.extract_domain_vocabulary.UbiquitousLanguageSavingFailed; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +public interface SerializationStrategy { + Path writeToFile(String filename, Object dto); + + Path getDestinationFolder(); + + String getExtension(); + + default SerializationStrategy initDirectory() { + try { + Files.createDirectories(getDestinationFolder()); + } catch (IOException e) { + throw new UbiquitousLanguageSavingFailed(e); + } + return this; + } +} diff --git a/src/main/java/io/candydoc/plugin/save_ubiquitous_language/file/strategy/SerializationStrategyFactory.java b/src/main/java/io/candydoc/plugin/save_ubiquitous_language/file/strategy/SerializationStrategyFactory.java new file mode 100644 index 00000000..6b107b1a --- /dev/null +++ b/src/main/java/io/candydoc/plugin/save_ubiquitous_language/file/strategy/SerializationStrategyFactory.java @@ -0,0 +1,18 @@ +package io.candydoc.plugin.save_ubiquitous_language.file.strategy; + +import com.fasterxml.jackson.core.JsonFactory; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; +import java.nio.file.Path; + +public class SerializationStrategyFactory { + public static SerializationStrategy forJson(Path destinationFolder) { + return new DefaultSerializationStrategy( + new ObjectMapper(new JsonFactory()), destinationFolder, "json"); + } + + public static SerializationStrategy forYml(Path destinationFolder) { + return new DefaultSerializationStrategy( + new ObjectMapper(new YAMLFactory()), destinationFolder, "yml"); + } +} diff --git a/src/main/java/io/candydoc/plugin/save_ubiquitous_language/file/yml/UbiquitousLanguageAsYml.java b/src/main/java/io/candydoc/plugin/save_ubiquitous_language/file/yml/UbiquitousLanguageAsYml.java index 169004f0..0a88fe12 100644 --- a/src/main/java/io/candydoc/plugin/save_ubiquitous_language/file/yml/UbiquitousLanguageAsYml.java +++ b/src/main/java/io/candydoc/plugin/save_ubiquitous_language/file/yml/UbiquitousLanguageAsYml.java @@ -1,12 +1,11 @@ package io.candydoc.plugin.save_ubiquitous_language.file.yml; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import io.candydoc.plugin.save_ubiquitous_language.file.UbiquitousLanguageAsFile; +import io.candydoc.plugin.save_ubiquitous_language.file.strategy.SerializationStrategyFactory; import java.nio.file.Path; public class UbiquitousLanguageAsYml extends UbiquitousLanguageAsFile { public UbiquitousLanguageAsYml(Path destinationFolder) { - super(new ObjectMapper(new YAMLFactory()), destinationFolder, "yml"); + super(SerializationStrategyFactory.forYml(destinationFolder)); } } diff --git a/src/test/java/io/candydoc/plugin/save_ubiquitous_language/file/UbiquitousLanguageAsFileTest.java b/src/test/java/io/candydoc/plugin/save_ubiquitous_language/file/UbiquitousLanguageAsFileTest.java index 73fe13cb..be827978 100644 --- a/src/test/java/io/candydoc/plugin/save_ubiquitous_language/file/UbiquitousLanguageAsFileTest.java +++ b/src/test/java/io/candydoc/plugin/save_ubiquitous_language/file/UbiquitousLanguageAsFileTest.java @@ -23,8 +23,8 @@ void valid_output_format(String validFormat, Class expectedInstance) { UbiquitousLanguageAsFile.forFormat(validFormat, "target"); assertThat(ubiquitousLanguageAsFile).isInstanceOf(expectedInstance); - assertThat(ubiquitousLanguageAsFile.extension()).isEqualTo(validFormat); - assertThat(ubiquitousLanguageAsFile.destinationFolder()) + assertThat(ubiquitousLanguageAsFile.getExtension()).isEqualTo(validFormat); + assertThat(ubiquitousLanguageAsFile.getDestinationFolder()) .isEqualTo(Path.of("target", "candy-doc", "ubiquitous_language")); } diff --git a/src/test/java/io/candydoc/plugin/save_ubiquitous_language/file/json/UbiquitousLanguageAsJsonTest.java b/src/test/java/io/candydoc/plugin/save_ubiquitous_language/file/json/UbiquitousLanguageAsJsonTest.java index 53da20ff..e4af9520 100644 --- a/src/test/java/io/candydoc/plugin/save_ubiquitous_language/file/json/UbiquitousLanguageAsJsonTest.java +++ b/src/test/java/io/candydoc/plugin/save_ubiquitous_language/file/json/UbiquitousLanguageAsJsonTest.java @@ -10,6 +10,7 @@ import java.util.Set; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; class UbiquitousLanguageAsJsonTest { @@ -85,4 +86,66 @@ public void save_orphan_vocabulary_as_json() { assertThat(actualFile).exists().json().isSameAs(referenceFile); } + + @Nested + class ManifestTest { + + @Test + void generate_manifest_from_bounded_context_file_paths() { + // given + FoundDomainObjects foundDomainObjects = + new FoundDomainObjects() + .addAll(DomainVocabularyFixtures.DOMAIN_OBJECTS_IN_BOUNDED_CONTEXT); + + Set ubiquitousLanguages = + UbiquitousLanguage.fromDomainObjects(foundDomainObjects); + + // when + ubiquitousLanguageAsJson.saveAll(ubiquitousLanguages); + + // then + Path manifestReferenceFile = REFERENCE_FOLDER.resolve("MANIFEST_bounded_context_one.json"); + Path manifestActualFile = DESTINATION_FOLDER.resolve("MANIFEST.json"); + + assertThat(manifestActualFile).exists().json().isSameAs(manifestReferenceFile); + } + + @Test + void generate_manifest_from_shared_kernel_file_paths() { + // given + FoundDomainObjects foundDomainObjects = + new FoundDomainObjects().addAll(DomainVocabularyFixtures.DOMAIN_OBJECTS_IN_SHARED_KERNEL); + + Set ubiquitousLanguages = + UbiquitousLanguage.fromDomainObjects(foundDomainObjects); + + // when + ubiquitousLanguageAsJson.saveAll(ubiquitousLanguages); + + // then + Path manifestReferenceFile = REFERENCE_FOLDER.resolve("MANIFEST_shared_kernel_one.json"); + Path manifestActualFile = DESTINATION_FOLDER.resolve("MANIFEST.json"); + + assertThat(manifestActualFile).exists().json().isSameAs(manifestReferenceFile); + } + + @Test + void generate_manifest_from_orphan_vocabulary_file_paths() { + // given + FoundDomainObjects foundDomainObjects = + new FoundDomainObjects().addAll(DomainVocabularyFixtures.ORPHAN_DOMAIN_OBJECTS); + + Set ubiquitousLanguages = + UbiquitousLanguage.fromDomainObjects(foundDomainObjects); + + // when + ubiquitousLanguageAsJson.saveAll(ubiquitousLanguages); + + // then + Path manifestReferenceFile = REFERENCE_FOLDER.resolve("MANIFEST_orphan_vocabulary.json"); + Path manifestActualFile = DESTINATION_FOLDER.resolve("MANIFEST.json"); + + assertThat(manifestActualFile).exists().json().isSameAs(manifestReferenceFile); + } + } } diff --git a/src/test/java/io/candydoc/plugin/save_ubiquitous_language/file/yml/UbiquitousLanguageAsYmlTest.java b/src/test/java/io/candydoc/plugin/save_ubiquitous_language/file/yml/UbiquitousLanguageAsYmlTest.java index ba8107b7..438d4786 100644 --- a/src/test/java/io/candydoc/plugin/save_ubiquitous_language/file/yml/UbiquitousLanguageAsYmlTest.java +++ b/src/test/java/io/candydoc/plugin/save_ubiquitous_language/file/yml/UbiquitousLanguageAsYmlTest.java @@ -10,6 +10,7 @@ import java.util.Set; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; class UbiquitousLanguageAsYmlTest { @@ -30,7 +31,7 @@ public void tearDown() { } @Test - public void save_bounded_context_as_json() { + public void save_bounded_context_as_yml() { // given FoundDomainObjects foundDomainObjects = new FoundDomainObjects().addAll(DomainVocabularyFixtures.DOMAIN_OBJECTS_IN_BOUNDED_CONTEXT); @@ -49,7 +50,7 @@ public void save_bounded_context_as_json() { } @Test - public void save_shared_kernel_as_json() { + public void save_shared_kernel_as_yml() { // given FoundDomainObjects foundDomainObjects = new FoundDomainObjects().addAll(DomainVocabularyFixtures.DOMAIN_OBJECTS_IN_SHARED_KERNEL); @@ -68,7 +69,7 @@ public void save_shared_kernel_as_json() { } @Test - public void save_orphan_vocabulary_as_json() { + public void save_orphan_vocabulary_as_yml() { // given FoundDomainObjects foundDomainObjects = new FoundDomainObjects().addAll(DomainVocabularyFixtures.ORPHAN_DOMAIN_OBJECTS); @@ -85,4 +86,66 @@ public void save_orphan_vocabulary_as_json() { assertThat(actualFile).exists().yml().isSameAs(referenceFile); } + + @Nested + class ManifestTest { + + @Test + void generate_manifest_from_bounded_context_file_paths() { + // given + FoundDomainObjects foundDomainObjects = + new FoundDomainObjects() + .addAll(DomainVocabularyFixtures.DOMAIN_OBJECTS_IN_BOUNDED_CONTEXT); + + Set ubiquitousLanguages = + UbiquitousLanguage.fromDomainObjects(foundDomainObjects); + + // when + ubiquitousLanguageAsYml.saveAll(ubiquitousLanguages); + + // then + Path manifestReferenceFile = REFERENCE_FOLDER.resolve("MANIFEST_bounded_context_one.yml"); + Path manifestActualFile = DESTINATION_FOLDER.resolve("MANIFEST.yml"); + + assertThat(manifestActualFile).exists().yml().isSameAs(manifestReferenceFile); + } + + @Test + void generate_manifest_from_shared_kernel_file_paths() { + // given + FoundDomainObjects foundDomainObjects = + new FoundDomainObjects().addAll(DomainVocabularyFixtures.DOMAIN_OBJECTS_IN_SHARED_KERNEL); + + Set ubiquitousLanguages = + UbiquitousLanguage.fromDomainObjects(foundDomainObjects); + + // when + ubiquitousLanguageAsYml.saveAll(ubiquitousLanguages); + + // then + Path manifestReferenceFile = REFERENCE_FOLDER.resolve("MANIFEST_shared_kernel_one.yml"); + Path manifestActualFile = DESTINATION_FOLDER.resolve("MANIFEST.yml"); + + assertThat(manifestActualFile).exists().yml().isSameAs(manifestReferenceFile); + } + + @Test + void generate_manifest_from_orphan_vocabulary_file_paths() { + // given + FoundDomainObjects foundDomainObjects = + new FoundDomainObjects().addAll(DomainVocabularyFixtures.ORPHAN_DOMAIN_OBJECTS); + + Set ubiquitousLanguages = + UbiquitousLanguage.fromDomainObjects(foundDomainObjects); + + // when + ubiquitousLanguageAsYml.saveAll(ubiquitousLanguages); + + // then + Path manifestReferenceFile = REFERENCE_FOLDER.resolve("MANIFEST_orphan_vocabulary.yml"); + Path manifestActualFile = DESTINATION_FOLDER.resolve("MANIFEST.yml"); + + assertThat(manifestActualFile).exists().yml().isSameAs(manifestReferenceFile); + } + } } diff --git a/src/test/resources/json/MANIFEST_bounded_context_one.json b/src/test/resources/json/MANIFEST_bounded_context_one.json new file mode 100644 index 00000000..2b77dc1c --- /dev/null +++ b/src/test/resources/json/MANIFEST_bounded_context_one.json @@ -0,0 +1,5 @@ +{ + "files": [ + "./bounded_context_one.json" + ] +} \ No newline at end of file diff --git a/src/test/resources/json/MANIFEST_orphan_vocabulary.json b/src/test/resources/json/MANIFEST_orphan_vocabulary.json new file mode 100644 index 00000000..6a6519aa --- /dev/null +++ b/src/test/resources/json/MANIFEST_orphan_vocabulary.json @@ -0,0 +1,5 @@ +{ + "files": [ + "./orphan_vocabulary.json" + ] +} \ No newline at end of file diff --git a/src/test/resources/json/MANIFEST_shared_kernel_one.json b/src/test/resources/json/MANIFEST_shared_kernel_one.json new file mode 100644 index 00000000..704a046d --- /dev/null +++ b/src/test/resources/json/MANIFEST_shared_kernel_one.json @@ -0,0 +1,5 @@ +{ + "files": [ + "./shared_kernel_one.json" + ] +} \ No newline at end of file diff --git a/src/test/resources/yml/MANIFEST_bounded_context_one.yml b/src/test/resources/yml/MANIFEST_bounded_context_one.yml new file mode 100644 index 00000000..2705b77f --- /dev/null +++ b/src/test/resources/yml/MANIFEST_bounded_context_one.yml @@ -0,0 +1,2 @@ +files: +- ./bounded_context_one.yml diff --git a/src/test/resources/yml/MANIFEST_orphan_vocabulary.yml b/src/test/resources/yml/MANIFEST_orphan_vocabulary.yml new file mode 100644 index 00000000..5c1a9a6f --- /dev/null +++ b/src/test/resources/yml/MANIFEST_orphan_vocabulary.yml @@ -0,0 +1,2 @@ +files: +- ./orphan_vocabulary.yml diff --git a/src/test/resources/yml/MANIFEST_shared_kernel_one.yml b/src/test/resources/yml/MANIFEST_shared_kernel_one.yml new file mode 100644 index 00000000..1b06c3cd --- /dev/null +++ b/src/test/resources/yml/MANIFEST_shared_kernel_one.yml @@ -0,0 +1,2 @@ +files: +- ./shared_kernel_one.yml