Skip to content

Commit

Permalink
Added new api module to support custom rule and ported 3 existing rul…
Browse files Browse the repository at this point in the history
…es into new design. I
  • Loading branch information
thiyagugk committed Aug 27, 2022
1 parent 13db6fb commit 712e863
Show file tree
Hide file tree
Showing 42 changed files with 420 additions and 246 deletions.
59 changes: 59 additions & 0 deletions api/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
plugins {
id "com.diffplug.spotless" version "6.9.1"
}

dependencies {
implementation 'org.eclipse.microprofile.openapi:microprofile-openapi-api:3.0'
testImplementation 'org.slf4j:slf4j-jdk14:2.0.0'
testImplementation 'org.openapitools.empoa:empoa-simple-models-impl:2.0.0'
testImplementation 'nl.jqno.equalsverifier:equalsverifier:3.10.1'
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.12.0'
implementation group: 'org.reflections', name: 'reflections', version: '0.10.2'
}

java {
withJavadocJar()
withSourcesJar()
}

spotless {
java {
removeUnusedImports()
trimTrailingWhitespace()
palantirJavaFormat()
}
}

publishing {
publications {
mavenJava(MavenPublication) {
artifactId = 'openapi-style-validator-api'
pom {
name = 'OpenAPI Style Validator - api'
description = 'module to expose API for writing rules to validate the style and standards of an OpenAPI specification'
packaging = 'jar'
url = 'https://openapitools.github.io/openapi-style-validator/'
inceptionYear = "2019"
licenses {
license {
name = 'Apache 2.0 License'
url = 'http://www.apache.org/licenses/LICENSE-2.0'
}
}
developers {
developer {
id = 'JFCote'
name = 'Jean-Francois Cote'
email = '[email protected]'
}
}
scm {
connection = 'scm:git:https://github.com/openapitools/openapi-style-validator.git'
developerConnection = 'scm:git:https://github.com/openapitools/openapi-style-validator.git'
url = 'https://github.com/openapitools/openapi-style-validator/'
}
}
from components.java
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.openapitools.openapistylevalidator.api;

import java.util.Optional;
import org.eclipse.microprofile.openapi.models.OpenAPI;
import org.openapitools.openapistylevalidator.error.StyleError;

public interface Rule {

String ruleName();

String description();

Optional<StyleError> execute(OpenAPI api);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.openapitools.openapistylevalidator.styleerror;
package org.openapitools.openapistylevalidator.error;

import java.util.Objects;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.openapitools.openapistylevalidator.styleerror;
package org.openapitools.openapistylevalidator.error;

import java.util.Objects;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.openapitools.openapistylevalidator.styleerror;
package org.openapitools.openapistylevalidator.error;

import java.util.Objects;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.openapitools.openapistylevalidator.styleerror;
package org.openapitools.openapistylevalidator.error;

import java.util.Objects;
import org.eclipse.microprofile.openapi.models.PathItem;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.openapitools.openapistylevalidator.styleerror;
package org.openapitools.openapistylevalidator.error;

import java.util.Objects;
import org.eclipse.microprofile.openapi.models.PathItem;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.openapitools.openapistylevalidator.error;

public enum StyleCheckSection {
APIInfo,
Operations,
Models,
Naming,
OpenAPI,
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
package org.openapitools.openapistylevalidator.styleerror;
package org.openapitools.openapistylevalidator.error;

public class StyleError {

public enum StyleCheckSection {
APIInfo,
Operations,
Models,
Naming,
OpenAPI,
}

final StyleCheckSection styleCheckSection;
final String fieldNames;
final String description;
Expand Down
1 change: 1 addition & 0 deletions cli/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ apply plugin: 'application'

dependencies {
implementation project(':openapi-style-validator-lib')
implementation project(':openapi-style-validator-api')
implementation 'commons-cli:commons-cli:1.5.0'
implementation 'org.slf4j:slf4j-jdk14:2.0.0'
implementation 'io.swagger.parser.v3:swagger-parser:2.0.33'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.apache.commons.cli.Options;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.openapitools.openapistylevalidator.styleerror.StyleError;
import org.openapitools.openapistylevalidator.error.StyleError;

class OutputUtils {
private static final Logger logger = LogManager.getLogger(OutputUtils.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
import org.apache.commons.cli.CommandLine;
import org.eclipse.microprofile.openapi.models.OpenAPI;
import org.openapitools.empoa.swagger.core.internal.SwAdapter;
import org.openapitools.openapistylevalidator.OpenAPIStyleValidator;
import org.openapitools.openapistylevalidator.OpenApiSpecStyleValidator;
import org.openapitools.openapistylevalidator.ValidatorParameters;
import org.openapitools.openapistylevalidator.styleerror.StyleError;
import org.openapitools.openapistylevalidator.error.StyleError;

public class ValidationInitiator {
public List<StyleError> validate(OptionManager optionManager, CommandLine commandLine) {
Expand All @@ -31,4 +32,11 @@ private OpenAPI parseToOpenAPIModels(OptionManager optionManager, CommandLine co
io.swagger.v3.oas.models.OpenAPI swaggerOpenAPI = parserResult.getOpenAPI();
return SwAdapter.toOpenAPI(swaggerOpenAPI);
}

public List<StyleError> validateV2(OptionManager optionManager, CommandLine commandLine) {
OpenAPI openAPI = parseToOpenAPIModels(optionManager, commandLine);
ValidatorParameters parameters = optionManager.getOptionalValidatorParametersOrDefault(commandLine);
OpenAPIStyleValidator openAPIStyleValidator = new OpenAPIStyleValidator(openAPI, parameters);
return openAPIStyleValidator.validate();
}
}
23 changes: 0 additions & 23 deletions cli/src/main/java/rules/CheckTagsAreDefinedRule.java

This file was deleted.

31 changes: 0 additions & 31 deletions cli/src/main/java/rules/OperationHasTagRule.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.apache.commons.cli.ParseException;
import org.junit.jupiter.api.*;
import org.mockito.Mockito;
import org.openapitools.openapistylevalidator.styleerror.StyleError;
import org.openapitools.openapistylevalidator.error.StyleError;
import org.opentest4j.MultipleFailuresError;

class ValidationInitiatorTest {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.openapitools.openapistylevalidator.cli;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Options;
Expand All @@ -10,18 +13,13 @@
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.openapitools.openapistylevalidator.styleerror.StyleError;
import org.openapitools.openapistylevalidator.error.StyleError;
import org.opentest4j.MultipleFailuresError;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

class CustomRuleValidatorTest {
class ValidationInitiatorV2Test {
private static final DefaultParser parser = new DefaultParser();
public static final String SRC_TEST_RESOURCES_MISSING_TAG_YAML = "src/test/resources/missing_tags.yaml";

public static final String SRC_TEST_RESOURCES_PING_YAML = "src/test/resources/ping.yaml";

private final OutputUtils outputUtils = Mockito.mock(OutputUtils.class);
private OptionManager optionManager;
Expand All @@ -45,21 +43,20 @@ class Validate {
class GivenFileProvidedWithInvalidTagDefinedYaml {
@BeforeEach
void init() throws ParseException {
commandLine = parser.parse(options, new String[] {"-s", SRC_TEST_RESOURCES_MISSING_TAG_YAML});
commandLine = parser.parse(options, new String[] {"-s", SRC_TEST_RESOURCES_PING_YAML});
}

@Test
void returnSevenErrors() {
List<StyleError> errorList = validationInitiator.validate(optionManager, commandLine);

sevenErrorsAssertions(errorList);
List<StyleError> errorList = validationInitiator.validateV2(optionManager, commandLine);
fiveErrorsAssertions(errorList);
}
}
}

private void sevenErrorsAssertions(List<StyleError> errorList) throws MultipleFailuresError {
private void fiveErrorsAssertions(List<StyleError> errorList) throws MultipleFailuresError {
Assertions.assertAll(
() -> assertEquals(7, errorList.size()),
() -> assertEquals(3, errorList.size()),
() -> assertEquals(
"*ERROR* Section: APIInfo: 'license' -> Should be present and not empty",
errorList.get(0).toString()),
Expand All @@ -68,22 +65,6 @@ private void sevenErrorsAssertions(List<StyleError> errorList) throws MultipleFa
errorList.get(1).toString()),
() -> assertEquals(
"*ERROR* Section: APIInfo: 'contact' -> Should be present and not empty",
errorList.get(2).toString()),
() -> assertEquals(
"*ERROR* in Operation POST /ping 'description' -> This field should be present and not empty",
errorList.get(3).toString()),
() -> assertEquals(
"*ERROR* in Operation POST /ping 'summary' -> This field should be present and not empty",
errorList.get(4).toString()),
() -> assertEquals(
"*ERROR* Section: Operations: 'tags' -> FIX ME is not defined under tag section",
errorList.get(5).toString()),

() -> assertEquals(
"*ERROR* Section: Operations: 'tags' -> FIX ME is not defined under tag section",
errorList.get(5).toString()),
() -> assertEquals(
"*ERROR* Section: OpenAPI: 'tags' -> Should be present and not empty",
errorList.get(6).toString()));
errorList.get(2).toString()));
}
}
14 changes: 0 additions & 14 deletions cli/src/test/resources/missing_tags.yaml

This file was deleted.

1 change: 1 addition & 0 deletions gradle-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {

dependencies {
implementation project(':openapi-style-validator-lib')
implementation project(':openapi-style-validator-api')
implementation 'io.swagger.parser.v3:swagger-parser:2.0.33'
implementation 'org.openapitools.empoa:empoa-swagger-core:2.0.0'
testImplementation 'junit:junit:4.13.2'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.openapitools.openapistylevalidator.OpenApiSpecStyleValidator;
import org.openapitools.openapistylevalidator.ValidatorParameters;
import org.openapitools.openapistylevalidator.ValidatorParameters.NamingConvention;
import org.openapitools.openapistylevalidator.styleerror.StyleError;
import org.openapitools.openapistylevalidator.error.StyleError;

import java.util.List;

Expand Down
1 change: 1 addition & 0 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ plugins {
}

dependencies {
implementation project(':openapi-style-validator-api')
implementation 'org.eclipse.microprofile.openapi:microprofile-openapi-api:3.0'
testImplementation 'org.slf4j:slf4j-jdk14:2.0.0'
testImplementation 'org.openapitools.empoa:empoa-simple-models-impl:2.0.0'
Expand Down
Loading

0 comments on commit 712e863

Please sign in to comment.