Skip to content

Commit

Permalink
Merge pull request #10578 from renanfranca/10553-define-preset-json-f…
Browse files Browse the repository at this point in the history
…ile-name-at-spring-boot-application

define preset json file name at spring boot application config
  • Loading branch information
pascalgrimaud authored Aug 17, 2024
2 parents 1e03441 + d6503b6 commit 9006a9d
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package tech.jhipster.lite.project.domain.resource;

public record JHipsterPresetFile(String name) {}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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.resource.JHipsterPresetFile;
import tech.jhipster.lite.shared.error.domain.Assert;
import tech.jhipster.lite.shared.error.domain.GeneratorException;

Expand All @@ -26,18 +27,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 JHipsterPresetFile presetFile;
private final ObjectWriter writer;
private final FileSystemProjectDownloader downloader;

public FileSystemProjectsRepository(ObjectMapper json, ProjectFormatter formatter, ProjectFiles projectFiles) {
public FileSystemProjectsRepository(
ObjectMapper json,
ProjectFormatter formatter,
ProjectFiles projectFiles,
JHipsterPresetFile presetFile
) {
this.json = json;
this.formatter = formatter;
this.projectFiles = projectFiles;
this.presetFile = presetFile;

writer = json.writerWithDefaultPrettyPrinter();
downloader = new FileSystemProjectDownloader();
Expand Down Expand Up @@ -102,6 +109,6 @@ public Collection<Preset> getPresets() {
}

private String presetFilePath() {
return PRESET_FOLDER + PRESET_FILE;
return PRESET_FOLDER + presetFile.name();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package tech.jhipster.lite.project.infrastructure.secondary;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties("jhlite-preset-file")
class JHipsterPresetFileProperties {

private String name;

public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package tech.jhipster.lite.project.infrastructure.secondary;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import tech.jhipster.lite.project.domain.resource.JHipsterPresetFile;

@Configuration
@EnableConfigurationProperties(JHipsterPresetFileProperties.class)
class JHipsterPresetFilePropertiesConfiguration {

@Bean
JHipsterPresetFile jhipsterPresetFile() {
return new JHipsterPresetFile(new JHipsterPresetFileProperties().getName());
}
}
3 changes: 3 additions & 0 deletions src/main/resources/config/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ jhlite-hidden-resources:
slugs:
- svelte-core

jhlite-preset-file:
name: preset.json

server:
port: 7471

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@
import tech.jhipster.lite.JHLiteApp;
import tech.jhipster.lite.cucumber.rest.CucumberRestTestContext;
import tech.jhipster.lite.project.infrastructure.secondary.FakedFileSystemProjectFilesConfiguration;
import tech.jhipster.lite.project.infrastructure.secondary.FakedJHipsterPresetFilePropertiesConfiguration;
import tech.jhipster.lite.project.infrastructure.secondary.MockedProjectFormatterConfiguration;

@ActiveProfiles("test")
@CucumberContextConfiguration
@SpringBootTest(
classes = { JHLiteApp.class, MockedProjectFormatterConfiguration.class, FakedFileSystemProjectFilesConfiguration.class },
classes = {
JHLiteApp.class,
MockedProjectFormatterConfiguration.class,
FakedFileSystemProjectFilesConfiguration.class,
FakedJHipsterPresetFilePropertiesConfiguration.class,
},
webEnvironment = WebEnvironment.RANDOM_PORT
)
public class CucumberConfiguration {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package tech.jhipster.lite.project.infrastructure.secondary;

import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import tech.jhipster.lite.project.domain.resource.JHipsterPresetFile;

@TestConfiguration
public class FakedJHipsterPresetFilePropertiesConfiguration {

@Bean
JHipsterPresetFile jhipsterPresetFile() {
return new JHipsterPresetFile("preset.json");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
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.project.domain.resource.JHipsterPresetFile;
import tech.jhipster.lite.shared.error.domain.GeneratorException;

@UnitTest
Expand All @@ -41,7 +42,8 @@ class FileSystemProjectsRepositoryTest {
private static final FileSystemProjectsRepository projects = new FileSystemProjectsRepository(
JsonHelper.jsonMapper(),
mock(ProjectFormatter.class),
mock(ProjectFiles.class)
mock(ProjectFiles.class),
mock(JHipsterPresetFile.class)
);

@Nested
Expand Down Expand Up @@ -148,7 +150,8 @@ void shouldHandleSerializationError() throws JsonProcessingException {
FileSystemProjectsRepository fileSystemProjectsRepository = new FileSystemProjectsRepository(
json,
mock(ProjectFormatter.class),
mock(ProjectFiles.class)
mock(ProjectFiles.class),
mock(JHipsterPresetFile.class)
);

assertThatThrownBy(() -> fileSystemProjectsRepository.save(projectHistory())).isExactlyInstanceOf(GeneratorException.class);
Expand Down Expand Up @@ -191,7 +194,8 @@ void shouldHandleDeserializationErrors() throws IOException {
FileSystemProjectsRepository fileSystemProjectsRepository = new FileSystemProjectsRepository(
json,
mock(ProjectFormatter.class),
mock(ProjectFiles.class)
mock(ProjectFiles.class),
mock(JHipsterPresetFile.class)
);

assertThatThrownBy(() -> fileSystemProjectsRepository.getHistory(path)).isExactlyInstanceOf(GeneratorException.class);
Expand Down Expand Up @@ -229,7 +233,8 @@ void shouldHandleDeserializationErrors() throws IOException {
FileSystemProjectsRepository fileSystemProjectsRepository = new FileSystemProjectsRepository(
json,
mock(ProjectFormatter.class),
mockProjectFilesWithValidPresetJson()
mockProjectFilesWithValidPresetJson(),
new JHipsterPresetFile("preset.json")
);

assertThatThrownBy(fileSystemProjectsRepository::getPresets).isExactlyInstanceOf(GeneratorException.class);
Expand All @@ -242,7 +247,8 @@ void shouldNotReturnPresetFromUnknownFile() {
FileSystemProjectsRepository fileSystemProjectsRepository = new FileSystemProjectsRepository(
JsonHelper.jsonMapper(),
mock(ProjectFormatter.class),
projectFiles
projectFiles,
new JHipsterPresetFile("preset.json")
);

assertThatThrownBy(fileSystemProjectsRepository::getPresets).isExactlyInstanceOf(GeneratorException.class);
Expand All @@ -253,7 +259,8 @@ void shouldGetExistingPreset() {
FileSystemProjectsRepository fileSystemProjectsRepository = new FileSystemProjectsRepository(
JsonHelper.jsonMapper(),
mock(ProjectFormatter.class),
mockProjectFilesWithValidPresetJson()
mockProjectFilesWithValidPresetJson(),
new JHipsterPresetFile("preset.json")
);

Collection<Preset> presets = fileSystemProjectsRepository.getPresets();
Expand Down

0 comments on commit 9006a9d

Please sign in to comment.