Skip to content

Commit

Permalink
Merge pull request #10297 from renanfranca/10239-manage-landscape-pre…
Browse files Browse the repository at this point in the history
…set-configuration

manage landscape preset configuration
  • Loading branch information
renanfranca authored Jul 18, 2024
2 parents 69de6e3 + 9303ced commit 467be59
Show file tree
Hide file tree
Showing 11 changed files with 196 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tech.jhipster.lite.project.domain.history;
package tech.jhipster.lite.project.domain;

import tech.jhipster.lite.shared.error.domain.Assert;

Expand Down
14 changes: 14 additions & 0 deletions src/main/java/tech/jhipster/lite/project/domain/ModulesSlugs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package tech.jhipster.lite.project.domain;

import java.util.Collection;
import tech.jhipster.lite.shared.error.domain.Assert;

public record ModulesSlugs(Collection<ModuleSlug> modules) {
public ModulesSlugs {
Assert.notEmpty("modules", modules);
}

public static ModulesSlugs from(Collection<String> modules) {
return new ModulesSlugs(modules.stream().map(ModuleSlug::new).toList());
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package tech.jhipster.lite.project.domain;

import java.util.Collection;
import java.util.Optional;
import tech.jhipster.lite.project.domain.download.Project;
import tech.jhipster.lite.project.domain.history.ProjectHistory;
import tech.jhipster.lite.project.domain.preset.Preset;

public interface ProjectsRepository {
void format(ProjectPath path);
Expand All @@ -12,4 +14,6 @@ public interface ProjectsRepository {
void save(ProjectHistory history);

ProjectHistory getHistory(ProjectPath path);

Collection<Preset> getPresets();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.time.Instant;
import java.util.Map;
import tech.jhipster.lite.project.domain.ModuleSlug;
import tech.jhipster.lite.shared.error.domain.Assert;

public final class ProjectAction {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/tech/jhipster/lite/project/domain/preset/Preset.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package tech.jhipster.lite.project.domain.preset;

import tech.jhipster.lite.project.domain.ModulesSlugs;
import tech.jhipster.lite.shared.error.domain.Assert;

public record Preset(PresetName name, ModulesSlugs modules) {
public Preset {
Assert.notNull("name", name);
Assert.notNull("modules", modules);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package tech.jhipster.lite.project.domain.preset;

import tech.jhipster.lite.shared.error.domain.Assert;

public record PresetName(String name) {
public PresetName {
Assert.notBlank("name", name);
}

public static PresetName from(String name) {
return new PresetName(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Optional;
import org.springframework.stereotype.Repository;
import tech.jhipster.lite.module.domain.ProjectFiles;
import tech.jhipster.lite.project.domain.ProjectPath;
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.shared.error.domain.Assert;
import tech.jhipster.lite.shared.error.domain.GeneratorException;

Expand All @@ -22,15 +25,18 @@ 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_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) {
public FileSystemProjectsRepository(ObjectMapper json, ProjectFormatter formatter, ProjectFiles projectFiles) {
this.json = json;
this.formatter = formatter;
this.projectFiles = projectFiles;

writer = json.writerWithDefaultPrettyPrinter();
downloader = new FileSystemProjectDownloader();
Expand Down Expand Up @@ -84,4 +90,13 @@ public ProjectHistory getHistory(ProjectPath path) {
private Path historyFilePath(ProjectPath path) {
return Paths.get(path.get(), HISTORY_FOLDER, HISTORY_FILE);
}

@Override
public Collection<Preset> getPresets() {
try {
return json.readValue(projectFiles.readBytes(PRESET_FILE), PersistedPresets.class).toDomain();
} catch (IOException e) {
throw GeneratorException.technicalError("Can't read presets: " + e.getMessage(), e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package tech.jhipster.lite.project.infrastructure.secondary;

import java.util.Collection;
import tech.jhipster.lite.project.domain.ModulesSlugs;
import tech.jhipster.lite.project.domain.preset.Preset;
import tech.jhipster.lite.project.domain.preset.PresetName;

record PersistedPreset(String name, Collection<String> modules) {
public Preset toDomain() {
return new Preset(PresetName.from(name), ModulesSlugs.from(modules));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package tech.jhipster.lite.project.infrastructure.secondary;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Collection;
import tech.jhipster.lite.project.domain.preset.Preset;

record PersistedPresets(@JsonProperty("presets") Collection<PersistedPreset> presets) {
public Collection<Preset> toDomain() {
return presets.stream().map(PersistedPreset::toDomain).toList();
}
}
2 changes: 1 addition & 1 deletion src/test/java/tech/jhipster/lite/TestFileUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package tech.jhipster.lite;

import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static java.nio.file.StandardCopyOption.*;

import java.io.IOException;
import java.nio.file.FileSystems;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,25 @@
import tech.jhipster.lite.JsonHelper;
import tech.jhipster.lite.TestFileUtils;
import tech.jhipster.lite.UnitTest;
import tech.jhipster.lite.module.domain.ProjectFiles;
import tech.jhipster.lite.module.infrastructure.secondary.FileSystemProjectFiles;
import tech.jhipster.lite.project.domain.ModuleSlug;
import tech.jhipster.lite.project.domain.ModulesSlugs;
import tech.jhipster.lite.project.domain.ProjectPath;
import tech.jhipster.lite.project.domain.download.Project;
import tech.jhipster.lite.project.domain.download.ProjectName;
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.GeneratorException;

@UnitTest
class FileSystemProjectsRepositoryTest {

private static final FileSystemProjectsRepository projects = new FileSystemProjectsRepository(
JsonHelper.jsonMapper(),
mock(ProjectFormatter.class)
mock(ProjectFormatter.class),
mock(ProjectFiles.class)
);

@Nested
Expand Down Expand Up @@ -139,7 +146,11 @@ void shouldHandleSerializationError() throws JsonProcessingException {
ObjectMapper json = mock(ObjectMapper.class);
when(json.writerWithDefaultPrettyPrinter()).thenReturn(writer);

FileSystemProjectsRepository fileSystemProjectsRepository = new FileSystemProjectsRepository(json, mock(ProjectFormatter.class));
FileSystemProjectsRepository fileSystemProjectsRepository = new FileSystemProjectsRepository(
json,
mock(ProjectFormatter.class),
mock(ProjectFiles.class)
);

assertThatThrownBy(() -> fileSystemProjectsRepository.save(projectHistory())).isExactlyInstanceOf(GeneratorException.class);
}
Expand Down Expand Up @@ -178,7 +189,11 @@ void shouldHandleDeserializationErrors() throws IOException {
ObjectMapper json = mock(ObjectMapper.class);
when(json.readValue(any(byte[].class), eq(PersistedProjectHistory.class))).thenThrow(IOException.class);

FileSystemProjectsRepository fileSystemProjectsRepository = new FileSystemProjectsRepository(json, mock(ProjectFormatter.class));
FileSystemProjectsRepository fileSystemProjectsRepository = new FileSystemProjectsRepository(
json,
mock(ProjectFormatter.class),
mock(ProjectFiles.class)
);

assertThatThrownBy(() -> fileSystemProjectsRepository.getHistory(path)).isExactlyInstanceOf(GeneratorException.class);
}
Expand All @@ -204,6 +219,48 @@ void shouldGetExistingHistory() {
}
}

@Nested
@DisplayName("Get preset")
class FileSystemProjectsRepositoryGetPresetTest {

@Test
void shouldHandleDeserializationErrors() throws IOException {
ObjectMapper json = mock(ObjectMapper.class);
when(json.readValue(any(byte[].class), eq(PersistedPresets.class))).thenThrow(IOException.class);
FileSystemProjectsRepository fileSystemProjectsRepository = new FileSystemProjectsRepository(
json,
mock(ProjectFormatter.class),
mockProjectFilesWithValidPresetJson()
);

assertThatThrownBy(fileSystemProjectsRepository::getPresets).isExactlyInstanceOf(GeneratorException.class);
}

@Test
void shouldNotReturnPresetFromUnknownFile() {
FileSystemProjectsRepository fileSystemProjectsRepository = new FileSystemProjectsRepository(
JsonHelper.jsonMapper(),
mock(ProjectFormatter.class),
new FileSystemProjectFiles()
);

assertThatThrownBy(fileSystemProjectsRepository::getPresets).isExactlyInstanceOf(GeneratorException.class);
}

@Test
void shouldGetExistingPreset() {
FileSystemProjectsRepository fileSystemProjectsRepository = new FileSystemProjectsRepository(
JsonHelper.jsonMapper(),
mock(ProjectFormatter.class),
mockProjectFilesWithValidPresetJson()
);

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

assertThat(presets).containsExactly(expectedPreset());
}
}

private static FolderBuilder folder() {
return new FolderBuilder();
}
Expand Down Expand Up @@ -248,4 +305,56 @@ public ProjectPath build() {
return new ProjectPath(folder.toString());
}
}

private static ProjectFiles mockProjectFilesWithValidPresetJson() {
ProjectFiles projectFiles = mock(ProjectFiles.class);

String validPresetJson =
"""
{
"presets": [
{
"name": "angular + spring boot",
"modules": [
"init",
"application-service-hexagonal-architecture-documentation",
"maven-java",
"prettier",
"angular-core",
"java-base",
"maven-wrapper",
"spring-boot",
"spring-boot-mvc-empty",
"logs-spy",
"spring-boot-tomcat"
]
}
]
}
""";
lenient().when(projectFiles.readBytes("preset.json")).thenReturn(validPresetJson.getBytes());

return projectFiles;
}

private static Preset expectedPreset() {
return new Preset(
new PresetName("angular + spring boot"),
new ModulesSlugs(
List.of(
new ModuleSlug("init"),
new ModuleSlug("application-service-hexagonal-architecture-documentation"),
new ModuleSlug("maven-java"),
new ModuleSlug("prettier"),
new ModuleSlug("angular-core"),
new ModuleSlug("java-base"),
new ModuleSlug("maven-wrapper"),
new ModuleSlug("spring-boot"),
new ModuleSlug("spring-boot-mvc-empty"),
new ModuleSlug("logs-spy"),
new ModuleSlug("spring-boot-tomcat")
)
)
);
}
}

0 comments on commit 467be59

Please sign in to comment.