From cbb347c01dd9170625d36e8f753e4c0f860c3167 Mon Sep 17 00:00:00 2001 From: Prashanth Ramakrishnan Date: Wed, 13 Dec 2023 23:17:48 +0530 Subject: [PATCH] GH-00 : Minor refactoring and fix typo in test package name --- .../operator/ConfigMapController.java | 7 +++- .../configmap/validators/SpecValidator.java | 37 +++++++++++-------- .../ConfigValidatorTest.java | 2 +- 3 files changed, 28 insertions(+), 18 deletions(-) rename src/test/java/com/rcube/configmap/{conifgvalidator => configvalidator}/ConfigValidatorTest.java (99%) diff --git a/src/main/java/com/rcube/configmap/operator/ConfigMapController.java b/src/main/java/com/rcube/configmap/operator/ConfigMapController.java index 3b61c4e..6f3df89 100644 --- a/src/main/java/com/rcube/configmap/operator/ConfigMapController.java +++ b/src/main/java/com/rcube/configmap/operator/ConfigMapController.java @@ -32,7 +32,10 @@ public class ConfigMapController implements Reconciler, @Override public UpdateControl reconcile(final ConfigMapCustomResource resource, final Context context) { try { - log.info("Context being reloaded for [{}]",context.getSecondaryResource(ConfigMap.class).map(map -> map.getMetadata().getName()).orElse("None")); + val reloadedResource = context.getSecondaryResource(ConfigMap.class) + .map(map -> map.getMetadata().getName()) + .orElse("None"); + log.info("Context being reloaded for [{}]", reloadedResource); specValidator.validateResourceContent(resource); createConfigMap(resource); resource.setStatus(ResourceStatus.builder().build()); @@ -73,7 +76,7 @@ private ConfigMap buildConfigMap(final ConfigMapCustomResource resource) { @Override public Map prepareEventSources(final EventSourceContext context) { - InformerConfiguration configuration = + final InformerConfiguration configuration = InformerConfiguration.from(ConfigMap.class, context) .build(); return EventSourceInitializer diff --git a/src/main/java/com/rcube/configmap/validators/SpecValidator.java b/src/main/java/com/rcube/configmap/validators/SpecValidator.java index 0ba6ab9..313634e 100644 --- a/src/main/java/com/rcube/configmap/validators/SpecValidator.java +++ b/src/main/java/com/rcube/configmap/validators/SpecValidator.java @@ -3,16 +3,16 @@ import com.networknt.schema.ValidationMessage; import com.rcube.configmap.operator.ConfigMapCustomResource; import com.rcube.configmap.operator.ResourceStatus; - -import java.util.HashMap; -import java.util.Map; -import java.util.Set; - import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.jetbrains.annotations.NotNull; import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + @Component @Slf4j @AllArgsConstructor @@ -24,7 +24,7 @@ public void validateResourceContent(final ConfigMapCustomResource resource) { final Map schemaResource = resource.getSpec().getConfig().getSchema(); if (schemaResource == null || CollectionUtils.isEmpty(schemaResource)) { - log.warn("Resource [{}] doesnt have a schema, it is recommended to have one", resource.getMetadata().getName()); + log.warn("Resource [{}] does not have a schema, it is recommended to have one", resource.getMetadata().getName()); return; } @@ -33,15 +33,7 @@ public void validateResourceContent(final ConfigMapCustomResource resource) { if (CollectionUtils.isEmpty(dataResource)) { throw new RuntimeException("Missing required fields in the data configuration."); } - final Map> schemaValidationResult = new HashMap<>(); - dataResource.forEach((key, data) -> { - if (schemaResource.containsKey(key)) { - final Set validation = jsonContentValidators.validate(schemaResource.get(key), data); - if (!CollectionUtils.isEmpty(validation)) { - schemaValidationResult.put(key, validation); - } - } - }); + final Map> schemaValidationResult = getValidationFailures(dataResource, schemaResource); if (!CollectionUtils.isEmpty(schemaValidationResult)) { throw new RuntimeException(String.format("Invalid data format %s", schemaValidationResult)); } @@ -51,4 +43,19 @@ public void validateResourceContent(final ConfigMapCustomResource resource) { throw ex; } } + + @NotNull + private Map> getValidationFailures(final Map dataResource, + final Map schemaResource) { + final Map> schemaValidationResult = new HashMap<>(); + dataResource.forEach((key, data) -> { + if (schemaResource.containsKey(key)) { + final Set validation = jsonContentValidators.validate(schemaResource.get(key), data); + if (!CollectionUtils.isEmpty(validation)) { + schemaValidationResult.put(key, validation); + } + } + }); + return schemaValidationResult; + } } diff --git a/src/test/java/com/rcube/configmap/conifgvalidator/ConfigValidatorTest.java b/src/test/java/com/rcube/configmap/configvalidator/ConfigValidatorTest.java similarity index 99% rename from src/test/java/com/rcube/configmap/conifgvalidator/ConfigValidatorTest.java rename to src/test/java/com/rcube/configmap/configvalidator/ConfigValidatorTest.java index e0e559a..5637743 100644 --- a/src/test/java/com/rcube/configmap/conifgvalidator/ConfigValidatorTest.java +++ b/src/test/java/com/rcube/configmap/configvalidator/ConfigValidatorTest.java @@ -1,4 +1,4 @@ -package com.rcube.configmap.conifgvalidator; +package com.rcube.configmap.configvalidator; import com.rcube.configmap.TestDataUtil; import com.rcube.configmap.operator.ConfigMapCustomResource;