Skip to content

Commit

Permalink
fix(json): Fix unnecessary Spring dependency in Json schema filter
Browse files Browse the repository at this point in the history
- No need to catch NoSuchBeanException from Spring here
- Add static DSL entry method for object mapping payload builder
  • Loading branch information
christophd committed Sep 20, 2023
1 parent 45b6935 commit 1d93306
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package org.citrusframework.dsl;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.citrusframework.message.builder.ObjectMappingPayloadBuilder;
import org.citrusframework.validation.json.JsonMappingValidationProcessor;
import org.citrusframework.validation.json.JsonMessageValidationContext;

Expand All @@ -44,4 +46,24 @@ public static JsonMessageValidationContext.Builder json() {
public static <T> JsonMappingValidationProcessor.Builder<T> validate(Class<T> type) {
return JsonMappingValidationProcessor.Builder.validate(type);
}

/**
* Static builder method constructing a mapping payload builder.
* @param payload
* @return
*/
public static ObjectMappingPayloadBuilder marshal(Object payload) {
return new ObjectMappingPayloadBuilder(payload);
}


/**
* Static builder method constructing a mapping payload builder.
* @param payload
* @param mapper
* @return
*/
public static ObjectMappingPayloadBuilder marshal(Object payload, ObjectMapper mapper) {
return new ObjectMappingPayloadBuilder(payload, mapper);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,18 @@

package org.citrusframework.validation.json.schema;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.citrusframework.exceptions.CitrusRuntimeException;
import org.citrusframework.json.JsonSchemaRepository;
import org.citrusframework.json.schema.SimpleJsonSchema;
import org.citrusframework.spi.ReferenceResolver;
import org.citrusframework.validation.json.JsonMessageValidationContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* This class is responsible for filtering {@link SimpleJsonSchema}s based on a {@link JsonMessageValidationContext}.
Expand Down Expand Up @@ -61,20 +59,14 @@ public List<SimpleJsonSchema> filter(List<JsonSchemaRepository> schemaRepositori

private List<SimpleJsonSchema> getSchemaFromContext(JsonMessageValidationContext jsonMessageValidationContext,
ReferenceResolver referenceResolver) {
try {
SimpleJsonSchema simpleJsonSchema =
referenceResolver.resolve(jsonMessageValidationContext.getSchema(), SimpleJsonSchema.class);
SimpleJsonSchema simpleJsonSchema =
referenceResolver.resolve(jsonMessageValidationContext.getSchema(), SimpleJsonSchema.class);

if (logger.isDebugEnabled()) {
logger.debug("Found specified schema: \"" + jsonMessageValidationContext.getSchema() + "\".");
}

return Collections.singletonList(simpleJsonSchema);
} catch (NoSuchBeanDefinitionException e) {
throw new CitrusRuntimeException(
"Could not find the specified schema: \"" + jsonMessageValidationContext.getSchema() + "\".",
e);
if (logger.isDebugEnabled()) {
logger.debug("Found specified schema: \"" + jsonMessageValidationContext.getSchema() + "\".");
}

return Collections.singletonList(simpleJsonSchema);
}

private List<SimpleJsonSchema> filterByRepositoryName(List<JsonSchemaRepository> schemaRepositories,
Expand All @@ -101,10 +93,10 @@ private List<SimpleJsonSchema> mergeRepositories(List<JsonSchemaRepository> sche
}

private boolean isSchemaSpecified(JsonMessageValidationContext context) {
return StringUtils.hasText(context.getSchema());
return context.getSchema() != null && !context.getSchema().isEmpty() && !context.getSchema().isBlank();
}

private boolean isSchemaRepositorySpecified(JsonMessageValidationContext context) {
return StringUtils.hasText(context.getSchemaRepository());
return context.getSchemaRepository() != null && !context.getSchemaRepository().isEmpty() && !context.getSchemaRepository().isBlank();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@
import java.util.Collections;
import java.util.List;

import org.citrusframework.spi.ReferenceResolver;
import org.citrusframework.exceptions.CitrusRuntimeException;
import org.citrusframework.json.JsonSchemaRepository;
import org.citrusframework.json.schema.SimpleJsonSchema;
import org.citrusframework.spi.ReferenceResolver;
import org.citrusframework.validation.json.JsonMessageValidationContext;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -193,7 +192,7 @@ public void testNoSchemaFoundThrowsException() {

//Setup application validationContext
when(referenceResolverMock.resolve(validationContext.getSchema(), SimpleJsonSchema.class))
.thenThrow(NoSuchBeanDefinitionException.class);
.thenThrow(CitrusRuntimeException.class);

//WHEN
jsonSchemaFilter.filter(schemaRepositories, validationContext, referenceResolverMock);
Expand Down

0 comments on commit 1d93306

Please sign in to comment.