Skip to content

Commit

Permalink
Constants refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
adamcin committed Apr 20, 2020
1 parent d1f39d8 commit 32ac25e
Show file tree
Hide file tree
Showing 55 changed files with 1,453 additions and 625 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ matrix:
dist: trusty
language: java
jdk: oraclejdk8
script: ./mvnw -B -e verify site site:stage
script: ./mvnw -B -e -fae verify site site:stage
after_success:
- ./mvnw -B -Pcoverage clean verify coveralls:report
- ./mvnw -B -fae -Pcoverage clean verify coveralls:report
- os: linux
dist: trusty
language: java
Expand Down Expand Up @@ -39,7 +39,7 @@ matrix:
# for jdk 11.0.1+13
osx_image: xcode10.1
script:
- ./mvnw -B -e verify
- ./mvnw -B -e -fae verify
cache:
directories:
- $HOME/.m2
62 changes: 62 additions & 0 deletions api/src/main/java/net/adamcin/oakpal/api/ApiConstants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2020 Mark Adamcin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.adamcin.oakpal.api;

import org.jetbrains.annotations.NotNull;

/**
* Hosts constants as static singleton getter methods defined by interfaces. This reduces the impact on semantic
* versioning rules of adding or modifying interface constants.
*/
public final class ApiConstants {
private ApiConstants() {
/* no construction */
}

private static final ViolationKeys VIOLATION_KEYS = new ViolationKeys() {
@Override
public String description() {
return "description";
}

@Override
public String severity() {
return "severity";
}

@Override
public String packages() {
return "packages";
}
};

/**
* Json key constant accessors for violations.
*/
public interface ViolationKeys {
String description();

String severity();

String packages();
}

@NotNull
public static ViolationKeys violationKeys() {
return VIOLATION_KEYS;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019 Mark Adamcin
* Copyright 2020 Mark Adamcin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019 Mark Adamcin
* Copyright 2020 Mark Adamcin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
38 changes: 33 additions & 5 deletions api/src/main/java/net/adamcin/oakpal/api/Rule.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package net.adamcin.oakpal.api;

import org.jetbrains.annotations.NotNull;

import javax.json.JsonArray;
import javax.json.JsonObject;
import java.util.List;
Expand All @@ -38,6 +40,29 @@
* </dl>
*/
public final class Rule implements JsonObjectConvertible {
public interface JsonKeys {
String type();

String pattern();
}

private static final JsonKeys KEYS = new JsonKeys() {
@Override
public String type() {
return "type";
}

@Override
public String pattern() {
return "pattern";
}
};

@NotNull
public static JsonKeys keys() {
return KEYS;
}

static final Pattern PATTERN_MATCH_ALL = Pattern.compile(".*");

/**
Expand All @@ -60,8 +85,11 @@ public final class Rule implements JsonObjectConvertible {
*/
public static final Rule DEFAULT_DENY = new Rule(RuleType.DENY, PATTERN_MATCH_ALL);

public static final String CONFIG_TYPE = "type";
public static final String CONFIG_PATTERN = "pattern";
@Deprecated
public static final String CONFIG_TYPE = keys().type();
@Deprecated
public static final String CONFIG_PATTERN = keys().pattern();

private final RuleType type;
private final Pattern pattern;

Expand Down Expand Up @@ -149,7 +177,7 @@ public boolean matches(String value) {
*/
@Override
public JsonObject toJson() {
return JavaxJson.key(CONFIG_TYPE, getType().name()).key(CONFIG_PATTERN, getPattern().pattern()).get();
return JavaxJson.key(keys().type(), getType().name()).key(keys().pattern(), getPattern().pattern()).get();
}

@Override
Expand Down Expand Up @@ -203,8 +231,8 @@ public static List<Rule> fromJsonArray(final JsonArray rulesArray) {
* @return a new rule
*/
public static Rule fromJson(final JsonObject ruleJson) {
return new Rule(Rule.RuleType.fromName(ruleJson.getString(CONFIG_TYPE)),
Pattern.compile(ruleJson.getString(CONFIG_PATTERN)));
return new Rule(Rule.RuleType.fromName(ruleJson.getString(keys().type())),
Pattern.compile(ruleJson.getString(keys().pattern())));
}

/**
Expand Down
78 changes: 78 additions & 0 deletions api/src/main/java/net/adamcin/oakpal/api/Severity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2020 Mark Adamcin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.adamcin.oakpal.api;

import org.jetbrains.annotations.NotNull;

import java.util.function.Predicate;

/**
* Levels of severity for violations detected during package scans.
*/
public enum Severity {
/**
* Unlikely to disrupt application functionality. Appropriate for reporting violations of
* code or style conventions, or inconsistency between modes of installation.
*/
MINOR(2),

/**
* Likely to be the source of component instability. Appropriate for importer errors, mistaken
* assumptions about root path dependencies or namespaces, or failures related to unit testing of
* application packages.
*/
MAJOR(1),

/**
* Likely to be the source of platform instability. Appropriate for reporting cross-package filter
* overlap, destructive ACL handling modes, destruction of authorable content, or security violations.
*/
SEVERE(0);

private final int ordinal;

Severity(int ordinal) {
this.ordinal = ordinal;
}

/**
* Runtime throwing function to lookup severity codes by name.
*
* @param name the severity level name
* @return the associated severity level
*/
public static Severity byName(final @NotNull String name) {
for (Severity value : values()) {
if (value.name().equalsIgnoreCase(name)) {
return value;
}
}
throw new IllegalArgumentException("Unknown severity level: " + name);
}

public boolean isLessSevereThan(Severity other) {
return this.ordinal > other.ordinal;
}

public Predicate<Severity> meetsMinimumSeverity() {
return other -> !other.isLessSevereThan(this);
}

public Severity maxSeverity(final @NotNull Severity other) {
return this.isLessSevereThan(other) ? other : this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,22 @@ protected void reportViolation(final Violation violation) {
collector.reportViolation(violation);
}

protected final void reportViolation(final Violation.Severity severity,
protected final void reportViolation(final Severity severity,
final String description,
final PackageId... packages) {
this.reportViolation(new SimpleViolation(severity, description, packages));
}

protected final void minorViolation(final String description, final PackageId... packages) {
this.reportViolation(new SimpleViolation(Violation.Severity.MINOR, description, packages));
this.reportViolation(new SimpleViolation(Severity.MINOR, description, packages));
}

protected final void majorViolation(final String description, final PackageId... packages) {
this.reportViolation(new SimpleViolation(Violation.Severity.MAJOR, description, packages));
this.reportViolation(new SimpleViolation(Severity.MAJOR, description, packages));
}

protected final void severeViolation(final String description, final PackageId... packages) {
this.reportViolation(new SimpleViolation(Violation.Severity.SEVERE, description, packages));
this.reportViolation(new SimpleViolation(Severity.SEVERE, description, packages));
}

@Override
Expand Down
8 changes: 4 additions & 4 deletions api/src/main/java/net/adamcin/oakpal/api/SimpleViolation.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ public static SimpleViolation fromReported(final Violation violation) {
}

public static SimpleViolation fromJson(final JsonObject jsonViolation) {
String vSeverity = jsonViolation.getString(KEY_SEVERITY, Violation.Severity.MINOR.name());
Violation.Severity severity = Violation.Severity.valueOf(vSeverity);
String description = jsonViolation.getString(KEY_DESCRIPTION, "");
List<PackageId> packages = optArray(jsonViolation, KEY_PACKAGES)
String vSeverity = jsonViolation.getString(ApiConstants.violationKeys().severity(), Severity.MINOR.name());
Severity severity = Severity.valueOf(vSeverity);
String description = jsonViolation.getString(ApiConstants.violationKeys().description(), "");
List<PackageId> packages = optArray(jsonViolation, ApiConstants.violationKeys().packages())
.map(array -> mapArrayOfStrings(array, PackageId::fromString, true))
.orElseGet(Collections::emptyList);

Expand Down
68 changes: 3 additions & 65 deletions api/src/main/java/net/adamcin/oakpal/api/Violation.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,79 +17,17 @@
package net.adamcin.oakpal.api;

import org.apache.jackrabbit.vault.packaging.PackageId;
import org.jetbrains.annotations.NotNull;

import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import java.util.Collection;
import java.util.function.Predicate;

/**
* Report type for validations.
*/
public interface Violation extends JsonObjectConvertible {
String KEY_DESCRIPTION = "description";
String KEY_SEVERITY = "severity";
String KEY_PACKAGES = "packages";

/**
* Levels of severity for violations detected during package scans.
*/
enum Severity {
/**
* Unlikely to disrupt application functionality. Appropriate for reporting violations of
* code or style conventions, or inconsistency between modes of installation.
*/
MINOR(2),

/**
* Likely to be the source of component instability. Appropriate for importer errors, mistaken
* assumptions about root path dependencies or namespaces, or failures related to unit testing of
* application packages.
*/
MAJOR(1),

/**
* Likely to be the source of platform instability. Appropriate for reporting cross-package filter
* overlap, destructive ACL handling modes, destruction of authorable content, or security violations.
*/
SEVERE(0);

private final int ordinal;

Severity(int ordinal) {
this.ordinal = ordinal;
}

/**
* Runtime throwing function to lookup severity codes by name.
*
* @param name the severity level name
* @return the associated severity level
*/
public static Severity byName(final @NotNull String name) {
for (Severity value : values()) {
if (value.name().equalsIgnoreCase(name)) {
return value;
}
}
throw new IllegalArgumentException("Unknown severity level: " + name);
}

public boolean isLessSevereThan(Severity other) {
return this.ordinal > other.ordinal;
}

public Predicate<Severity> meetsMinimumSeverity() {
return other -> !other.isLessSevereThan(this);
}

public Severity maxSeverity(final @NotNull Severity other) {
return this.isLessSevereThan(other) ? other : this;
}
}

/**
* Describe the severity of the violation.
Expand Down Expand Up @@ -121,17 +59,17 @@ public Severity maxSeverity(final @NotNull Severity other) {
default JsonObject toJson() {
JsonObjectBuilder json = Json.createObjectBuilder();
if (this.getSeverity() != null) {
json.add(KEY_SEVERITY, this.getSeverity().toString());
json.add(ApiConstants.violationKeys().severity(), this.getSeverity().toString());
}
if (this.getDescription() != null) {
json.add(KEY_DESCRIPTION, this.getDescription());
json.add(ApiConstants.violationKeys().description(), this.getDescription());
}
if (this.getPackages() != null && !this.getPackages().isEmpty()) {
JsonArrayBuilder array = Json.createArrayBuilder();
for (PackageId packageId : this.getPackages()) {
array.add(packageId.toString());
}
json.add(KEY_PACKAGES, array.build());
json.add(ApiConstants.violationKeys().packages(), array.build());
}
return json.build();
}
Expand Down
Loading

0 comments on commit 32ac25e

Please sign in to comment.