Skip to content

Commit

Permalink
resolved sonarQube issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Bramaten committed Jan 6, 2025
1 parent 98c25f7 commit dc88032
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package com.buschmais.jqassistant.commandline.test;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;
Expand All @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
Expand Down Expand Up @@ -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<String, JsonNode> 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;
}

Expand All @@ -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<ResolvedType> getResolvedTypes(MethodScope target, ResolvedType resolvedType) {
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package com.buschmais.jqassistant.maven.test;

import java.io.File;
import java.io.IOException;
import java.util.Set;
Expand All @@ -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();
Expand Down

0 comments on commit dc88032

Please sign in to comment.