Skip to content

Commit

Permalink
fix (jkube-kit/common) : Add NonValidationKeywords to JsonMetaSchema …
Browse files Browse the repository at this point in the history
…in order to avoid schema validation warnings (eclipse-jkube#1934)

Add additional NonValidationKeywords in ResourceValidator's
JsonMetaSchema in order to avoid schema validation warnings about
unknown keywords.

Signed-off-by: Rohan Kumar <rohaan@redhat.com>
  • Loading branch information
rohanKanojia committed Jan 19, 2023
1 parent 174d189 commit 31c9134
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Usage:
* Fix #1459: Route Generation should support `8443` as default web port
* Fix #1546: Migrate to JUnit5 testing framework
* Fix #1858: Properties in image name not replaced
* Fix #1934: schema validation warnings during `mvn oc:resource` and `gradle k8sResource`
* Fix #1935: `oc:remote-dev` goal / `ocRemoteDev` task have wrong log prefixes
* Fix #1966: Old reference to fmp in documentation
* Fix #1974: Remove unused methods in KubernetesResourceUtil
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Copyright (c) 2019 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at:
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.jkube.gradle.plugin.tests;

import org.gradle.testkit.runner.BuildResult;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

class ResourceValidationIT {
@RegisterExtension
private final ITGradleRunnerExtension gradleRunner = new ITGradleRunnerExtension();

@Test
void k8sResource_whenRun_shouldValidateGeneratedResources() {
// When
final BuildResult result = gradleRunner.withITProject("spring-boot")
.withArguments("build", "k8sResource", "--stacktrace")
.build();
// Then
assertThat(result).extracting(BuildResult::getOutput)
.asString()
.containsPattern("validating .*deployment.yml resource")
.containsPattern("validating .*service.yml resource")
.doesNotContain("Unknown keyword");
}

@Test
void ocResource_whenRun_shouldValidateGeneratedResources() {
// When
final BuildResult result = gradleRunner.withITProject("spring-boot")
.withArguments("build", "ocResource", "--stacktrace")
.build();
// Then
assertThat(result).extracting(BuildResult::getOutput)
.asString()
.containsPattern("validating .*deploymentconfig.yml resource")
.containsPattern("validating .*service.yml resource")
.doesNotContain("Unknown keyword");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,7 @@ private JsonSchema getJsonSchema(URL schemaUrl, String kind) throws IOException
checkIfKindPropertyExists(kind);
getResourceProperties(kind, jsonSchema);
final JsonMetaSchema metaSchema = JsonMetaSchema.builder(v201909.getUri(), v201909)
.addKeyword(new NonValidationKeyword("javaType"))
.addKeyword(new NonValidationKeyword("javaInterfaces"))
.addKeyword(new NonValidationKeyword("resources"))
.addKeywords(createNonValidationKeywordList())
.build();
return new JsonSchemaFactory.Builder()
.defaultMetaSchemaURI(defaultUri).addMetaSchema(metaSchema).build()
Expand Down Expand Up @@ -199,7 +197,18 @@ public JsonObject getSchemaJson(URL schemaUrl) throws IOException {
return jsonObject;
}

private class ConstraintViolationImpl implements ConstraintViolation<ValidationMessage> {
static List<NonValidationKeyword> createNonValidationKeywordList() {
List<NonValidationKeyword> nonValidationKeywords = new ArrayList<>();
nonValidationKeywords.add(new NonValidationKeyword("javaType"));
nonValidationKeywords.add(new NonValidationKeyword("javaInterfaces"));
nonValidationKeywords.add(new NonValidationKeyword("resources"));
nonValidationKeywords.add(new NonValidationKeyword("javaOmitEmpty"));
nonValidationKeywords.add(new NonValidationKeyword("existingJavaType"));
nonValidationKeywords.add(new NonValidationKeyword("$module"));
return nonValidationKeywords;
}

private static class ConstraintViolationImpl implements ConstraintViolation<ValidationMessage> {

private ValidationMessage errorMsg;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
package org.eclipse.jkube.kit.common.util.validator;


import com.networknt.schema.AbstractKeyword;
import com.networknt.schema.NonValidationKeyword;
import org.assertj.core.api.Condition;
import org.eclipse.jkube.kit.common.KitLogger;
import org.eclipse.jkube.kit.common.util.ResourceClassifier;
Expand All @@ -23,6 +25,7 @@
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.nio.file.Paths;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down Expand Up @@ -66,6 +69,17 @@ void validateWithInvalidResource() throws Exception {
.has(new HasErrMessage("$.spec.replicas: string found, integer expected"));
}

@Test
void createNonValidationKeywordList_whenInvoked_shouldReturnNonValidationKeywordList() {
// Given + When
List<NonValidationKeyword> nonValidationKeywordList = ResourceValidator.createNonValidationKeywordList();

// Then
assertThat(nonValidationKeywordList)
.extracting(AbstractKeyword::getValue)
.containsExactlyInAnyOrder("javaType", "javaInterfaces", "resources", "javaOmitEmpty", "existingJavaType", "$module");
}

private static final class HasErrMessage extends Condition<Throwable> {

private final String message;
Expand Down

0 comments on commit 31c9134

Please sign in to comment.