Repository: jhipster/jhipster-lite
Pull request #10610 (2024-08-19 15:04:52)

Set preset in Spring Config
- fix https://github.com/jhipster/jhipster-lite/issues/10553


renanfranca: @pascalgrimaud : A error was detected with `custom-jhlite` module, what do you think about this fix?
https://github.com/jhipster/jhipster-lite/pull/10609#issuecomment-2296549821

This must be done in this same PR.
pascalgrimaud: @renanfranca : I tested locally jhlite custom with the following code here and it works, as I used default override mechanism by Spring, so the property will be set with the default value preset.json

Patch:
diff --git a/src/main/java/tech/jhipster/lite/project/infrastructure/secondary/FileSystemProjectsRepository.java b/src/main/java/tech/jhipster/lite/project/infrastructure/secondary/FileSystemProjectsRepository.java
index be5c1dc38..1451aaeb3 100644
--- a/src/main/java/tech/jhipster/lite/project/infrastructure/secondary/FileSystemProjectsRepository.java
+++ b/src/main/java/tech/jhipster/lite/project/infrastructure/secondary/FileSystemProjectsRepository.java
@@ -8,6 +8,7 @@ import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.util.Collection;
 import java.util.Optional;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Repository;
 import tech.jhipster.lite.module.domain.ProjectFiles;
 import tech.jhipster.lite.project.domain.ProjectPath;
@@ -15,6 +16,7 @@ import tech.jhipster.lite.project.domain.ProjectsRepository;
 import tech.jhipster.lite.project.domain.download.Project;
 import tech.jhipster.lite.project.domain.history.ProjectHistory;
 import tech.jhipster.lite.project.domain.preset.Preset;
+import tech.jhipster.lite.project.domain.preset.PresetName;
 import tech.jhipster.lite.shared.error.domain.Assert;
 import tech.jhipster.lite.shared.error.domain.GeneratorException;
 
@@ -26,18 +28,24 @@ class FileSystemProjectsRepository implements ProjectsRepository {
   private static final String HISTORY_FOLDER = ".jhipster/modules";
   private static final String HISTORY_FILE = "history.json";
   private static final String PRESET_FOLDER = "/";
-  private static final String PRESET_FILE = "preset.json";
 
   private final ObjectMapper json;
   private final ProjectFormatter formatter;
   private final ProjectFiles projectFiles;
   private final ObjectWriter writer;
   private final FileSystemProjectDownloader downloader;
-
-  public FileSystemProjectsRepository(ObjectMapper json, ProjectFormatter formatter, ProjectFiles projectFiles) {
+  private final PresetName presetName;
+
+  public FileSystemProjectsRepository(
+    ObjectMapper json,
+    ProjectFormatter formatter,
+    ProjectFiles projectFiles,
+    @Value("${jhlite-preset-file.name:preset.json}") String presetFileName
+  ) {
     this.json = json;
     this.formatter = formatter;
     this.projectFiles = projectFiles;
+    this.presetName = PresetName.from(presetFileName);
 
     writer = json.writerWithDefaultPrettyPrinter();
     downloader = new FileSystemProjectDownloader();
@@ -102,6 +110,6 @@ class FileSystemProjectsRepository implements ProjectsRepository {
   }
 
   private String presetFilePath() {
-    return PRESET_FOLDER + PRESET_FILE;
+    return PRESET_FOLDER + presetName.name();
   }
 }
diff --git a/src/main/resources/config/application.yml b/src/main/resources/config/application.yml
index b8d18f725..364a4bbc1 100644
--- a/src/main/resources/config/application.yml
+++ b/src/main/resources/config/application.yml
@@ -69,6 +69,9 @@ jhlite-hidden-resources:
   slugs:
     - svelte-core
 
+jhlite-preset-file:
+  name: preset.json
+
 server:
   port: 7471
 


Test patch:
diff --git a/src/test/java/tech/jhipster/lite/project/infrastructure/secondary/FileSystemProjectsRepositoryTest.java b/src/test/java/tech/jhipster/lite/project/infrastructure/secondary/FileSystemProjectsRepositoryTest.java
index cb5e06cad..7363bf32b 100644
--- a/src/test/java/tech/jhipster/lite/project/infrastructure/secondary/FileSystemProjectsRepositoryTest.java
+++ b/src/test/java/tech/jhipster/lite/project/infrastructure/secondary/FileSystemProjectsRepositoryTest.java
@@ -38,10 +38,12 @@ import tech.jhipster.lite.shared.error.domain.GeneratorException;
 @UnitTest
 class FileSystemProjectsRepositoryTest {
 
+  private static final String DEFAULT_PRESET_FILE = "preset.json";
   private static final FileSystemProjectsRepository projects = new FileSystemProjectsRepository(
     JsonHelper.jsonMapper(),
     mock(ProjectFormatter.class),
-    mock(ProjectFiles.class)
+    mock(ProjectFiles.class),
+    DEFAULT_PRESET_FILE
   );
 
   @Nested
@@ -148,7 +150,8 @@ class FileSystemProjectsRepositoryTest {
       FileSystemProjectsRepository fileSystemProjectsRepository = new FileSystemProjectsRepository(
         json,
         mock(ProjectFormatter.class),
-        mock(ProjectFiles.class)
+        mock(ProjectFiles.class),
+        DEFAULT_PRESET_FILE
       );
 
       assertThatThrownBy(() -> fileSystemProjectsRepository.save(projectHistory())).isExactlyInstanceOf(GeneratorException.class);
@@ -191,7 +194,8 @@ class FileSystemProjectsRepositoryTest {
       FileSystemProjectsRepository fileSystemProjectsRepository = new FileSystemProjectsRepository(
         json,
         mock(ProjectFormatter.class),
-        mock(ProjectFiles.class)
+        mock(ProjectFiles.class),
+        DEFAULT_PRESET_FILE
       );
 
       assertThatThrownBy(() -> fileSystemProjectsRepository.getHistory(path)).isExactlyInstanceOf(GeneratorException.class);
@@ -229,7 +233,8 @@ class FileSystemProjectsRepositoryTest {
       FileSystemProjectsRepository fileSystemProjectsRepository = new FileSystemProjectsRepository(
         json,
         mock(ProjectFormatter.class),
-        mockProjectFilesWithValidPresetJson()
+        mockProjectFilesWithValidPresetJson(),
+        DEFAULT_PRESET_FILE
       );
 
       assertThatThrownBy(fileSystemProjectsRepository::getPresets).isExactlyInstanceOf(GeneratorException.class);
@@ -242,7 +247,8 @@ class FileSystemProjectsRepositoryTest {
       FileSystemProjectsRepository fileSystemProjectsRepository = new FileSystemProjectsRepository(
         JsonHelper.jsonMapper(),
         mock(ProjectFormatter.class),
-        projectFiles
+        projectFiles,
+        DEFAULT_PRESET_FILE
       );
 
       assertThatThrownBy(fileSystemProjectsRepository::getPresets).isExactlyInstanceOf(GeneratorException.class);
@@ -253,7 +259,8 @@ class FileSystemProjectsRepositoryTest {
       FileSystemProjectsRepository fileSystemProjectsRepository = new FileSystemProjectsRepository(
         JsonHelper.jsonMapper(),
         mock(ProjectFormatter.class),
-        mockProjectFilesWithValidPresetJson()
+        mockProjectFilesWithValidPresetJson(),
+        DEFAULT_PRESET_FILE
       );
 
       Collection<Preset> presets = fileSystemProjectsRepository.getPresets();
diff --git a/src/test/resources/config/application-test.yml b/src/test/resources/config/application-test.yml
index 4389bb540..6968fb62e 100644
--- a/src/test/resources/config/application-test.yml
+++ b/src/test/resources/config/application-test.yml
@@ -14,6 +14,9 @@ jhlite-hidden-resources:
   tags:
   slugs:
 
+jhlite-preset-file:
+  name: preset.json
+
 server:
   port: 0