Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[incubator-kie-issues#1373] Fixing rest generation config. #3584

Merged
merged 3 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public void setApplicationProperty(String key, String value) {
public <T> Optional<T> getApplicationProperty(String property, Class<T> clazz) {
return Optional.ofNullable(convert(properties.getProperty(property), clazz));
}

@Override
public void removeApplicationProperty(String key) {
properties.remove(key);
}
};
}

Expand All @@ -57,4 +62,6 @@ public <T> Optional<T> getApplicationProperty(String property, Class<T> clazz) {
Collection<String> getApplicationProperties();

void setApplicationProperty(String key, String value);

void removeApplicationProperty(String key);
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ public void setApplicationProperty(String key, String value) {
applicationProperties.setApplicationProperty(key, value);
}

@Override
public void removeApplicationProperty(String key) {
applicationProperties.removeApplicationProperty(key);
}

@Override
public String getPackageName() {
return packageName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public class ApplicationGenerator {
private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationGenerator.class);

public static final String APPLICATION_CLASS_NAME = "Application";
private static final GeneratedFileType APPLICATION_SECTION_TYPE = GeneratedFileType.of("APPLICATION_SECTION", GeneratedFileType.Category.SOURCE);
private static final GeneratedFileType APPLICATION_SECTION_TYPE = GeneratedFileType.of("APPLICATION_SECTION",
GeneratedFileType.Category.SOURCE);

private final ApplicationContainerGenerator applicationMainGenerator;
private ApplicationConfigGenerator applicationConfigGenerator;
Expand Down Expand Up @@ -95,8 +96,11 @@ public Collection<GeneratedFile> generate() {

public List<GeneratedFile> generateComponents() {
return generators.stream()
.flatMap(gen -> gen.generate().stream())
.filter(this::filterGeneratedFile)
.flatMap(gen -> {
boolean keepRestFile = keepRestFile(gen);
return gen.generate().stream()
.filter(generatedFile -> filterGeneratedFile(generatedFile, keepRestFile));
})
.collect(Collectors.toList());
}

Expand All @@ -112,8 +116,12 @@ public GeneratedFile generateApplicationDescriptor() {
return applicationMainGenerator.generate();
}

private boolean filterGeneratedFile(GeneratedFile generatedFile) {
boolean keepFile = context.hasRESTGloballyAvailable() || !REST_TYPE.equals(generatedFile.type());
boolean keepRestFile(Generator generator) {
return context.hasRESTForGenerator(generator);
}

private boolean filterGeneratedFile(GeneratedFile generatedFile, boolean keepRestFile) {
boolean keepFile = keepRestFile || !REST_TYPE.equals(generatedFile.type());
if (!keepFile) {
LOGGER.warn("Skipping file because REST is disabled: " + generatedFile.relativePath());
}
Expand All @@ -134,7 +142,7 @@ private Collection<GeneratedFile> generateApplicationSections() {

/**
* Method to wire Generator with ApplicationGenerator if enabled
*
*
* @param generator
* @param <G>
* @return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.kie.kogito.codegen.api.Generator.REST_TYPE;
import static org.kie.kogito.codegen.api.context.KogitoBuildContext.generateRESTConfigurationKeyForResource;

public class ApplicationGeneratorTest {

Expand Down Expand Up @@ -168,6 +169,66 @@ public void disableGlobalRestGeneration(KogitoBuildContext.Builder contextBuilde
}
}

@ParameterizedTest
@MethodSource("org.kie.kogito.codegen.api.utils.KogitoContextTestUtils#contextBuilders")
public void keepRestFile(KogitoBuildContext.Builder contextBuilder) {
final KogitoBuildContext context = contextBuilder.build();
final ApplicationGenerator appGenerator = new ApplicationGenerator(context);
final MockGenerator restGenerator = new MockGenerator(context, true);
final String generateRESTConfigurationKeyForResource = generateRESTConfigurationKeyForResource(restGenerator.name());

assertThat(appGenerator.registerGeneratorIfEnabled(restGenerator))
.isNotEmpty();
assertThat(appGenerator.getGenerators()).hasSize(1);

if (context.hasRESTForGenerator(restGenerator)) {
// globally and engine-specific disable REST
context.setApplicationProperty(KogitoBuildContext.KOGITO_GENERATE_REST, "false");
context.setApplicationProperty(generateRESTConfigurationKeyForResource, "false");
assertThat(appGenerator.keepRestFile(restGenerator)).isFalse();

// globally disable REST, engine-specific enable REST
context.setApplicationProperty(KogitoBuildContext.KOGITO_GENERATE_REST, "false");
context.setApplicationProperty(generateRESTConfigurationKeyForResource, "true");
assertThat(appGenerator.keepRestFile(restGenerator)).isFalse();

// globally enable REST, engine-specific disable REST
context.setApplicationProperty(KogitoBuildContext.KOGITO_GENERATE_REST, "true");
context.setApplicationProperty(generateRESTConfigurationKeyForResource, "false");
assertThat(appGenerator.keepRestFile(restGenerator)).isFalse();

// globally and engine-specific enable REST
context.setApplicationProperty(KogitoBuildContext.KOGITO_GENERATE_REST, "true");
context.setApplicationProperty(generateRESTConfigurationKeyForResource, "true");
assertThat(appGenerator.keepRestFile(restGenerator)).isTrue();

// engine-specific enable REST
context.removeApplicationProperty(KogitoBuildContext.KOGITO_GENERATE_REST);
context.setApplicationProperty(generateRESTConfigurationKeyForResource, "true");
assertThat(appGenerator.keepRestFile(restGenerator)).isTrue();

// engine-specific disable REST
context.removeApplicationProperty(KogitoBuildContext.KOGITO_GENERATE_REST);
context.setApplicationProperty(generateRESTConfigurationKeyForResource, "false");
assertThat(appGenerator.keepRestFile(restGenerator)).isFalse();

// globally enable REST
context.setApplicationProperty(KogitoBuildContext.KOGITO_GENERATE_REST, "true");
context.removeApplicationProperty(generateRESTConfigurationKeyForResource);
assertThat(appGenerator.keepRestFile(restGenerator)).isTrue();

// globally disable REST
context.setApplicationProperty(KogitoBuildContext.KOGITO_GENERATE_REST, "false");
context.removeApplicationProperty(generateRESTConfigurationKeyForResource);
assertThat(appGenerator.keepRestFile(restGenerator)).isFalse();

// defaults
context.removeApplicationProperty(KogitoBuildContext.KOGITO_GENERATE_REST);
context.removeApplicationProperty(generateRESTConfigurationKeyForResource);
assertThat(appGenerator.keepRestFile(restGenerator)).isTrue();
}
}

private void assertCompilationUnit(final CompilationUnit compilationUnit, final boolean checkCDI) {
assertThat(compilationUnit).isNotNull();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,9 @@ public <T> Optional<T> getApplicationProperty(String property, Class<T> clazz) {
public void setApplicationProperty(String key, String value) {
System.setProperty(key, value);
}

@Override
public void removeApplicationProperty(String key) {
System.clearProperty(key);
}
}
Loading