Skip to content

Commit

Permalink
Trying for 100% coverage of Paths and Echo.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamcin committed Jan 6, 2019
1 parent c583507 commit 5a61580
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 11 deletions.
7 changes: 5 additions & 2 deletions core/src/main/java/net/adamcin/oakpal/core/checks/Echo.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
Expand Down Expand Up @@ -58,7 +59,8 @@ public String getCheckName() {

@Override
public void identifyPackage(final PackageId packageId, final File file) {
echo("identifyPackage(packageId: %s, file: %s)", packageId, file.getAbsolutePath());
echo("identifyPackage(packageId: %s, file: %s)", packageId,
Optional.ofNullable(file).map(File::getAbsolutePath).orElse(null));
}

@Override
Expand Down Expand Up @@ -86,7 +88,8 @@ public void deletedPath(final PackageId packageId, final String path, final Sess

@Override
public void afterExtract(final PackageId packageId, final Session inspectSession) throws RepositoryException {
echo("afterExtract(packageId: %s, inspectSession: %s)", packageId, inspectSession.isLive());
echo("afterExtract(packageId: %s, inspectSession: %s)", packageId,
Optional.ofNullable(inspectSession).map(Session::getUserID).orElse(null));
}

@Override
Expand Down
12 changes: 3 additions & 9 deletions core/src/main/java/net/adamcin/oakpal/core/checks/Paths.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import net.adamcin.oakpal.core.ProgressCheck;
import net.adamcin.oakpal.core.ProgressCheckFactory;
import net.adamcin.oakpal.core.SimpleProgressCheck;
import net.adamcin.oakpal.core.SimpleViolation;
import net.adamcin.oakpal.core.Violation;
import org.apache.jackrabbit.vault.packaging.PackageId;
import org.json.JSONObject;
Expand Down Expand Up @@ -66,6 +65,7 @@ public final class Paths implements ProgressCheckFactory {
public static final String CONFIG_RULES = "rules";
public static final String CONFIG_DENY_ALL_DELETES = "denyAllDeletes";
public static final String CONFIG_SEVERITY = "severity";
public static final Violation.Severity DEFAULT_SEVERITY = Violation.Severity.MAJOR;

@Override
public ProgressCheck newInstance(final JSONObject config) throws Exception {
Expand All @@ -75,7 +75,7 @@ public ProgressCheck newInstance(final JSONObject config) throws Exception {
&& config.optBoolean(CONFIG_DENY_ALL_DELETES);

final Violation.Severity severity = Violation.Severity.valueOf(
config.optString(CONFIG_SEVERITY, Violation.Severity.MAJOR.name()).toUpperCase());
config.optString(CONFIG_SEVERITY, DEFAULT_SEVERITY.name()).toUpperCase());

return new Check(rules, denyAllDeletes, severity);
}
Expand All @@ -100,13 +100,7 @@ public String getCheckName() {
public void importedPath(final PackageId packageId, final String path, final Node node)
throws RepositoryException {

Rule lastMatch = Rule.fuzzyDefaultAllow(rules);
for (Rule rule : rules) {
if (rule.matches(path)) {
lastMatch = rule;
}
}

Rule lastMatch = Rule.lastMatch(rules, path);
if (lastMatch.isDeny()) {
reportViolation(severity,
String.format("imported path %s matches deny pattern %s", path,
Expand Down
81 changes: 81 additions & 0 deletions core/src/test/java/net/adamcin/oakpal/core/checks/EchoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright 2019 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.core.checks;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import javax.jcr.RepositoryException;

import org.junit.Test;

public class EchoTest {


@Test
public void testGetReportedViolations() {
Echo echo = new Echo();
assertNotNull("reported violations should not be null", echo.getReportedViolations());
assertTrue("reported violations should be empty", echo.getReportedViolations().isEmpty());
}

@Test
public void testGetCheckName() {
assertEquals("echo's check name should be 'echo'", "echo", new Echo().getCheckName());
}

@Test
public void testStartedScan() {
new Echo().startedScan();
}

@Test
public void testIdentifyPackage() {
new Echo().identifyPackage(null, null);
}

@Test
public void testIdentifySubpackage() {
new Echo().identifySubpackage(null, null);
}

@Test
public void testBeforeExtract() throws RepositoryException {
new Echo().beforeExtract(null, null, null, null, null);
}

@Test
public void testImportedPath() throws RepositoryException {
new Echo().importedPath(null, null, null);
}

@Test
public void testDeletedPath() throws RepositoryException {
new Echo().deletedPath(null, null, null);
}

@Test
public void testAfterExtract() throws RepositoryException {
new Echo().afterExtract(null, null);
}

@Test
public void testFinishedScan() {
new Echo().finishedScan();
}
}
34 changes: 34 additions & 0 deletions core/src/test/java/net/adamcin/oakpal/core/checks/PathsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@

package net.adamcin.oakpal.core.checks;

import static java.util.Collections.singletonList;
import static net.adamcin.oakpal.core.OrgJson.arr;
import static net.adamcin.oakpal.core.OrgJson.key;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.Collections;
import java.util.regex.Pattern;

import net.adamcin.oakpal.core.CheckReport;
Expand Down Expand Up @@ -81,4 +84,35 @@ public void testBadSeverity() throws Exception {
assertTrue("bad severity should cause exception on construction.", threw);
});
}

@Test
public void testDeletedPath() throws Exception {
Paths.Check allDeletesCheck = new Paths.Check(Collections.emptyList(), true, Paths.DEFAULT_SEVERITY);
assertTrue("reported violations should be empty before deletedPath",
allDeletesCheck.getReportedViolations().isEmpty());
allDeletesCheck.deletedPath(null, "/foo", null);
assertFalse("reported violations should not be empty after deletedPath",
allDeletesCheck.getReportedViolations().isEmpty());

Paths.Check deletesByRuleCheck = new Paths.Check(
singletonList(new Rule(Rule.RuleType.ALLOW, Pattern.compile("/foo"))),
false, Paths.DEFAULT_SEVERITY);

assertTrue("reported violations should be empty before deletedPath",
deletesByRuleCheck.getReportedViolations().isEmpty());


deletesByRuleCheck.deletedPath(null, "/foo", null);

assertTrue("reported violations should be empty after deletedPath for /foo: " +
deletesByRuleCheck.getReportedViolations(),
deletesByRuleCheck.getReportedViolations().isEmpty());

deletesByRuleCheck.deletedPath(null, "/bar", null);

assertFalse("reported violations should not be empty after deletedPath for /bar",
deletesByRuleCheck.getReportedViolations().isEmpty());


}
}

0 comments on commit 5a61580

Please sign in to comment.