Skip to content

Commit

Permalink
Added test property for FAIL_ON_UNKNOWN_PROPERTIES configuration (#4377)
Browse files Browse the repository at this point in the history
* Added test property for FAIL_ON_UNKNOWN_PROPERTIES configuration

* Customize ObjectMapper in YamlResourceLoader from RecipeSpec only

* Use star import for Assertions

---------

Co-authored-by: Tim te Beek <tim@moderne.io>
  • Loading branch information
ajturnerora and timtebeek authored Aug 4, 2024
1 parent af4f5e6 commit c3bddfe
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,34 @@ public YamlResourceLoader(InputStream yamlInput,
* @param source Declarative recipe source
* @param properties Placeholder properties
* @param classLoader Optional classloader to use with jackson. If not specified, the runtime classloader will be used.
* @param dependencyResourceLoaders Optional resource loaders for recipes from dependencies
* @throws UncheckedIOException On unexpected IOException
*/
public YamlResourceLoader(InputStream yamlInput,
URI source,
Properties properties,
@Nullable ClassLoader classLoader,
Collection<? extends ResourceLoader> dependencyResourceLoaders) throws UncheckedIOException {
this(yamlInput, source, properties, classLoader, dependencyResourceLoaders, jsonMapper -> {
});
}

/**
* Load a declarative recipe, optionally using the specified classloader and optionally including resource loaders
* for recipes from dependencies.
*
* @param yamlInput Declarative recipe yaml input stream
* @param source Declarative recipe source
* @param properties Placeholder properties
* @param classLoader Optional classloader to use with jackson. If not specified, the runtime classloader will be used.
* @param dependencyResourceLoaders Optional resource loaders for recipes from dependencies
* @param mapperCustomizer Customizer for the ObjectMapper
* @throws UncheckedIOException On unexpected IOException
*/
public YamlResourceLoader(InputStream yamlInput, URI source, Properties properties,
@Nullable ClassLoader classLoader,
Collection<? extends ResourceLoader> dependencyResourceLoaders,
Consumer<ObjectMapper> mapperCustomizer) {
this.source = source;
this.dependencyResourceLoaders = dependencyResourceLoaders;

Expand All @@ -148,6 +169,8 @@ public YamlResourceLoader(InputStream yamlInput,
.build()
.registerModule(new ParameterNamesModule())
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

mapperCustomizer.accept(mapper);
maybeAddKotlinModule(mapper);

this.classLoader = classLoader;
Expand Down Expand Up @@ -482,7 +505,7 @@ public Collection<CategoryDescriptor> listCategoryDescriptors() {
@Language("markdown")
String packageName = (String) c.get("packageName");
if (packageName.endsWith("." + CategoryTree.CORE) ||
packageName.contains("." + CategoryTree.CORE + ".")) {
packageName.contains("." + CategoryTree.CORE + ".")) {
throw new IllegalArgumentException("The package name 'core' is reserved.");
}

Expand Down
158 changes: 79 additions & 79 deletions rewrite-core/src/test/java/org/openrewrite/RecipeLifecycleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@
import java.util.UUID;

import static java.util.Objects.requireNonNull;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.assertj.core.api.Assertions.*;
import static org.openrewrite.Recipe.noop;
import static org.openrewrite.test.RewriteTest.toRecipe;
import static org.openrewrite.test.SourceSpecs.text;
Expand Down Expand Up @@ -276,47 +275,48 @@ public PlainText visitText(PlainText text, ExecutionContext ctx) {
@Test
void canCallImperativeRecipeWithoutArgsFromDeclarative() {
rewriteRun(spec -> spec.recipeFromYaml("""
---
type: specs.openrewrite.org/v1beta/recipe
name: test.recipe
displayName: Test Recipe
description: Test Recipe.
recipeList:
- org.openrewrite.NoArgRecipe
""",
"test.recipe"
---
type: specs.openrewrite.org/v1beta/recipe
name: test.recipe
displayName: Test Recipe
description: Test Recipe.
recipeList:
- org.openrewrite.NoArgRecipe
""",
"test.recipe"
),
text("Hi", "NoArgRecipeHi"));
}

@Test
void canCallImperativeRecipeWithUnnecessaryArgsFromDeclarative() {
rewriteRun(spec -> spec.recipeFromYaml("""
---
type: specs.openrewrite.org/v1beta/recipe
name: test.recipe
displayName: Test Recipe
description: Test Recipe.
recipeList:
- org.openrewrite.NoArgRecipe:
foo: bar
""",
"test.recipe"
),
text("Hi", "NoArgRecipeHi"));
void canNotCallImperativeRecipeWithUnnecessaryArgsFromDeclarativeInTests() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
rewriteRun(spec -> spec.recipeFromYaml("""
---
type: specs.openrewrite.org/v1beta/recipe
name: test.recipe
displayName: Test Recipe
description: Test Recipe.
recipeList:
- org.openrewrite.NoArgRecipe:
foo: bar
""",
"test.recipe"
),
text("Hi", "NoArgRecipeHi")));
}

@Test
void canCallRecipeWithNoExplicitConstructor() {
rewriteRun(spec -> spec.recipeFromYaml("""
---
type: specs.openrewrite.org/v1beta/recipe
name: test.recipe
displayName: Test Recipe
description: Test Recipe.
recipeList:
- org.openrewrite.DefaultConstructorRecipe
""",
---
type: specs.openrewrite.org/v1beta/recipe
name: test.recipe
displayName: Test Recipe
description: Test Recipe.
recipeList:
- org.openrewrite.DefaultConstructorRecipe
""",
"test.recipe"
),
text("Hi", "DefaultConstructorRecipeHi"));
Expand All @@ -325,28 +325,28 @@ void canCallRecipeWithNoExplicitConstructor() {
@Test
void declarativeRecipeChain() {
rewriteRun(spec -> spec.recipeFromYaml("""
---
type: specs.openrewrite.org/v1beta/recipe
name: test.recipe.a
displayName: Test Recipe
description: Test Recipe.
recipeList:
- test.recipe.b
---
type: specs.openrewrite.org/v1beta/recipe
name: test.recipe.b
displayName: Test Recipe
description: Test Recipe.
recipeList:
- test.recipe.c
---
type: specs.openrewrite.org/v1beta/recipe
name: test.recipe.c
displayName: Test Recipe
description: Test Recipe.
recipeList:
- org.openrewrite.NoArgRecipe
""",
---
type: specs.openrewrite.org/v1beta/recipe
name: test.recipe.a
displayName: Test Recipe
description: Test Recipe.
recipeList:
- test.recipe.b
---
type: specs.openrewrite.org/v1beta/recipe
name: test.recipe.b
displayName: Test Recipe
description: Test Recipe.
recipeList:
- test.recipe.c
---
type: specs.openrewrite.org/v1beta/recipe
name: test.recipe.c
displayName: Test Recipe
description: Test Recipe.
recipeList:
- org.openrewrite.NoArgRecipe
""",
"test.recipe.a"
),
text("Hi", "NoArgRecipeHi"));
Expand All @@ -356,34 +356,34 @@ void declarativeRecipeChain() {
void declarativeRecipeChainAcrossFiles() {
rewriteRun(spec -> spec.recipe(Environment.builder()
.load(new YamlResourceLoader(new ByteArrayInputStream("""
---
type: specs.openrewrite.org/v1beta/recipe
name: test.recipe.c
displayName: Test Recipe
description: Test Recipe.
recipeList:
- org.openrewrite.NoArgRecipe
""".getBytes()),
---
type: specs.openrewrite.org/v1beta/recipe
name: test.recipe.c
displayName: Test Recipe
description: Test Recipe.
recipeList:
- org.openrewrite.NoArgRecipe
""".getBytes()),
URI.create("rewrite.yml"), new Properties()))
.load(new YamlResourceLoader(new ByteArrayInputStream("""
---
type: specs.openrewrite.org/v1beta/recipe
name: test.recipe.b
displayName: Test Recipe
description: Test Recipe.
recipeList:
- test.recipe.c
""".getBytes()),
---
type: specs.openrewrite.org/v1beta/recipe
name: test.recipe.b
displayName: Test Recipe
description: Test Recipe.
recipeList:
- test.recipe.c
""".getBytes()),
URI.create("rewrite.yml"), new Properties()))
.load(new YamlResourceLoader(new ByteArrayInputStream("""
---
type: specs.openrewrite.org/v1beta/recipe
name: test.recipe.a
displayName: Test Recipe
description: Test Recipe.
recipeList:
- test.recipe.b
""".getBytes()),
---
type: specs.openrewrite.org/v1beta/recipe
name: test.recipe.a
displayName: Test Recipe
description: Test Recipe.
recipeList:
- test.recipe.b
""".getBytes()),
URI.create("rewrite.yml"), new Properties()))
.build()
.activateRecipes("test.recipe.a")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.openrewrite.test;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
Expand Down Expand Up @@ -145,7 +146,8 @@ public RecipeSpec recipeFromResources(String... activeRecipes) {

private static Recipe recipeFromInputStream(InputStream yaml, String... activeRecipes) {
return Environment.builder()
.load(new YamlResourceLoader(yaml, URI.create("rewrite.yml"), new Properties()))
.load(new YamlResourceLoader(yaml, URI.create("rewrite.yml"), new Properties(), null, Collections.emptyList(),
mapper -> mapper.enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)))
.build()
.activateRecipes(activeRecipes);
}
Expand Down

0 comments on commit c3bddfe

Please sign in to comment.