From dc88032682549b76e91cee400bef073c8e8971bb Mon Sep 17 00:00:00 2001 From: Maria Brauer Date: Mon, 6 Jan 2025 09:17:26 +0100 Subject: [PATCH] resolved sonarQube issues --- .../test}/CliJsonSchemaGeneratorTest.java | 8 ++-- .../src/test/resources/validCliYaml.yaml | 0 .../configuration/JsonSchemaGenerator.java | 42 ++++++++++++------- .../JsonSchemaGeneratingTest.java | 8 ++-- .../test}/MavenJsonSchemaGeneratorTest.java | 8 ++-- 5 files changed, 41 insertions(+), 25 deletions(-) rename cli/{application/src/test/java => test/src/test/java/com/buschmais/jqassistant/commandline/test}/CliJsonSchemaGeneratorTest.java (92%) rename cli/{application => test}/src/test/resources/validCliYaml.yaml (100%) rename maven/src/test/java/{ => com/buschmais/jqassistant/maven/test}/MavenJsonSchemaGeneratorTest.java (90%) diff --git a/cli/application/src/test/java/CliJsonSchemaGeneratorTest.java b/cli/test/src/test/java/com/buschmais/jqassistant/commandline/test/CliJsonSchemaGeneratorTest.java similarity index 92% rename from cli/application/src/test/java/CliJsonSchemaGeneratorTest.java rename to cli/test/src/test/java/com/buschmais/jqassistant/commandline/test/CliJsonSchemaGeneratorTest.java index 8f1dc859f..596fec675 100644 --- a/cli/application/src/test/java/CliJsonSchemaGeneratorTest.java +++ b/cli/test/src/test/java/com/buschmais/jqassistant/commandline/test/CliJsonSchemaGeneratorTest.java @@ -1,3 +1,5 @@ +package com.buschmais.jqassistant.commandline.test; + import java.io.FileInputStream; import java.io.IOException; import java.util.Map; @@ -19,19 +21,19 @@ import static io.smallrye.config._private.ConfigLogging.log; import static org.assertj.core.api.Assertions.assertThat; -public class CliJsonSchemaGeneratorTest { +class CliJsonSchemaGeneratorTest { private final JsonSchemaGenerator generator = new JsonSchemaGenerator(); private JsonNode node; @BeforeEach - public void generateSchema() throws IOException { + void generateSchema() throws IOException { String path = "target/generated-resources/schema/jqassistant-configuration-cli.schema.json"; node = generator.generateSchema(CliConfiguration.class, path); } @Test - public void testValidYaml() throws Exception { + void testValidYaml() throws Exception { assertThat(validateYaml("src/test/resources/validCliYaml.yaml")).isEmpty(); } diff --git a/cli/application/src/test/resources/validCliYaml.yaml b/cli/test/src/test/resources/validCliYaml.yaml similarity index 100% rename from cli/application/src/test/resources/validCliYaml.yaml rename to cli/test/src/test/resources/validCliYaml.yaml diff --git a/core/runtime/src/main/java/com/buschmais/jqassistant/core/runtime/api/configuration/JsonSchemaGenerator.java b/core/runtime/src/main/java/com/buschmais/jqassistant/core/runtime/api/configuration/JsonSchemaGenerator.java index 09467c9db..5794d8e3a 100644 --- a/core/runtime/src/main/java/com/buschmais/jqassistant/core/runtime/api/configuration/JsonSchemaGenerator.java +++ b/core/runtime/src/main/java/com/buschmais/jqassistant/core/runtime/api/configuration/JsonSchemaGenerator.java @@ -16,6 +16,8 @@ import com.google.common.base.CaseFormat; import io.smallrye.config.WithDefault; +import static io.smallrye.config._private.ConfigLogging.log; + public class JsonSchemaGenerator { public ObjectNode generateSchema(Class clazz, String path) throws IOException { @@ -27,7 +29,7 @@ public ObjectNode generateSchema(Class clazz, String path) throws IOException configBuilder.forMethods() .withTargetTypeOverridesResolver(target -> getResolvedTypes(target, target.getType())); configBuilder.forMethods() - .withPropertyNameOverrideResolver((member) -> mapToKebabCase(member.getName())); + .withPropertyNameOverrideResolver(member -> mapToKebabCase(member.getName())); configBuilder.forTypesInGeneral() .withCustomDefinitionProvider(new MapDefinitionProvider()); configBuilder.forMethods() @@ -62,23 +64,28 @@ public ObjectNode generateSchema(Class clazz, String path) throws IOException private static ObjectNode wrapJqassistant(ObjectNode schema) { ObjectNode propertiesNode = JsonNodeFactory.instance.objectNode(); ObjectNode jqaWrapper = JsonNodeFactory.instance.objectNode(); - ObjectNode definitionWrapper = JsonNodeFactory.instance.objectNode(); + + String properties = "properties"; + String object = "object"; + String defs = "$defs"; + String type = "type"; + for (Map.Entry property : schema.properties()) { if (property.getKey() - .equals("properties")) { - propertiesNode.put("type", "object"); - propertiesNode.set("properties", property.getValue()); + .equals(properties)) { + propertiesNode.put(type, object); + propertiesNode.set(properties, property.getValue()); propertiesNode.put("additionalProperties", false); } if (property.getKey() - .equals("$defs")) { - definitionWrapper.set("$defs", property.getValue()); + .equals(defs)) { + definitionWrapper.set(defs, property.getValue()); } } jqaWrapper.set("jqassistant", propertiesNode); - definitionWrapper.put("type", "object"); - definitionWrapper.set("properties", jqaWrapper); + definitionWrapper.put(type, object); + definitionWrapper.set(properties, jqaWrapper); return definitionWrapper; } @@ -92,7 +99,7 @@ private static void saveSchemaToFile(ObjectNode schema, String path) throws IOEx ObjectMapper objectMapper = new ObjectMapper(); objectMapper.writerWithDefaultPrettyPrinter() .writeValue(file, schema); - System.out.println("Schema saved: " + file.getAbsolutePath()); + log.info("Schema saved: " + file.getAbsolutePath()); } private static List getResolvedTypes(MethodScope target, ResolvedType resolvedType) { @@ -124,17 +131,22 @@ public CustomDefinition provideCustomSchemaDefinition(ResolvedType targetType, S if (!targetType.isInstanceOf(Map.class)) { return null; } - ResolvedType keyType = context.getTypeContext().getTypeParameterFor(targetType, Map.class, 0); - ResolvedType valueType = context.getTypeContext().getTypeParameterFor(targetType, Map.class, 1); + ResolvedType keyType = context.getTypeContext() + .getTypeParameterFor(targetType, Map.class, 0); + ResolvedType valueType = context.getTypeContext() + .getTypeParameterFor(targetType, Map.class, 1); if (keyType == null || !keyType.isInstanceOf(String.class)) { return null; } if (valueType == null) { - valueType = context.getTypeContext().resolve(Object.class); + valueType = context.getTypeContext() + .resolve(Object.class); } - ObjectNode customSchema = context.getGeneratorConfig().createObjectNode(); + ObjectNode customSchema = context.getGeneratorConfig() + .createObjectNode(); customSchema.put(context.getKeyword(SchemaKeyword.TAG_TYPE), "object"); - ObjectNode unkownNameWrapper = context.getGeneratorConfig().createObjectNode(); + ObjectNode unkownNameWrapper = context.getGeneratorConfig() + .createObjectNode(); ObjectNode valueTypeDefinition = context.createDefinition(valueType); unkownNameWrapper.set("^.*$", valueTypeDefinition); customSchema.set(context.getKeyword(SchemaKeyword.TAG_PATTERN_PROPERTIES), unkownNameWrapper); diff --git a/core/runtime/src/test/java/com/buschmais/jqassistant/core/runtime/api/configuration/JsonSchemaGeneratingTest.java b/core/runtime/src/test/java/com/buschmais/jqassistant/core/runtime/api/configuration/JsonSchemaGeneratingTest.java index cefcedae1..46388e03b 100644 --- a/core/runtime/src/test/java/com/buschmais/jqassistant/core/runtime/api/configuration/JsonSchemaGeneratingTest.java +++ b/core/runtime/src/test/java/com/buschmais/jqassistant/core/runtime/api/configuration/JsonSchemaGeneratingTest.java @@ -18,25 +18,25 @@ import static io.smallrye.config._private.ConfigLogging.log; import static org.assertj.core.api.Assertions.assertThat; -public class JsonSchemaGeneratingTest { +class JsonSchemaGeneratingTest { private final JsonSchemaGenerator generator = new JsonSchemaGenerator(); private JsonNode node; @BeforeEach - public void generateSchema() throws IOException { + void generateSchema() throws IOException { String path = "target/generated-resources/schema/jqassistant-configuration.schema.json"; node = generator.generateSchema(Configuration.class, path); } @Test - public void testValidYaml() throws Exception { + void testValidYaml() throws Exception { assertThat(validateYaml("src/test/resources/testdata/generate-schema/validJQAYaml.yaml")).isEmpty(); } @Test - public void testInvalidYaml() throws Exception { + void testInvalidYaml() throws Exception { assertThat(validateYaml("src/test/resources/testdata/generate-schema/invalidJQAYaml.yaml")).hasSize(3); } diff --git a/maven/src/test/java/MavenJsonSchemaGeneratorTest.java b/maven/src/test/java/com/buschmais/jqassistant/maven/test/MavenJsonSchemaGeneratorTest.java similarity index 90% rename from maven/src/test/java/MavenJsonSchemaGeneratorTest.java rename to maven/src/test/java/com/buschmais/jqassistant/maven/test/MavenJsonSchemaGeneratorTest.java index 01ea1d5c9..668f53b68 100644 --- a/maven/src/test/java/MavenJsonSchemaGeneratorTest.java +++ b/maven/src/test/java/com/buschmais/jqassistant/maven/test/MavenJsonSchemaGeneratorTest.java @@ -1,3 +1,5 @@ +package com.buschmais.jqassistant.maven.test; + import java.io.File; import java.io.IOException; import java.util.Set; @@ -12,16 +14,16 @@ import com.networknt.schema.JsonSchemaFactory; import com.networknt.schema.SpecVersion; import com.networknt.schema.ValidationMessage; -import org.junit.Test; +import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; -public class MavenJsonSchemaGeneratorTest { +class MavenJsonSchemaGeneratorTest { private final JsonSchemaGenerator generator = new JsonSchemaGenerator(); @Test - public void generateSchema() throws IOException { + void generateSchema() throws IOException { JsonNode node = generator.generateSchema(MavenConfiguration.class, "target/generated-resources/schema/jqassistant-configuration-maven.schema.json"); File file = new File("target/generated-resources/schema/jqassistant-configuration-maven.schema.json"); assertThat(node).isNotNull();