Skip to content

Commit

Permalink
support for json output with severity
Browse files Browse the repository at this point in the history
  • Loading branch information
Przemek Bednarek committed Jun 29, 2018
1 parent 2896517 commit b866dc8
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 53 deletions.
5 changes: 4 additions & 1 deletion src/main/java/com/cognifide/secureaem/AbstractTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,13 @@ public void test() throws IOException {
try {
result = doTest();
} catch (Exception e) {
LOG.error("Error during test", e);
if (!TestRunParameters.SILENT_MODE) {
LOG.error("Error during test", e);
}
if (!(e instanceof InvalidConfigurationException)) {
addErrorMessage("Exception occured: " + e.toString());
}

result = TestResult.EXCEPTION;
}
} else {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/cognifide/secureaem/TestRunParameters.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.cognifide.secureaem;

public class TestRunParameters {
public static boolean SILENT_MODE;
}
60 changes: 47 additions & 13 deletions src/main/java/com/cognifide/secureaem/cli/Main.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
package com.cognifide.secureaem.cli;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
Expand All @@ -17,28 +10,46 @@
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import org.apache.commons.lang3.StringUtils;

import com.cognifide.secureaem.AbstractTest;
import com.cognifide.secureaem.Configuration;
import com.cognifide.secureaem.TestResult;
import com.cognifide.secureaem.TestRunParameters;
import com.cognifide.secureaem.json.Severity;
import com.cognifide.secureaem.json.SingleTestResult;
import com.cognifide.secureaem.json.TestSuiteResult;
import com.google.gson.Gson;

public class Main {

private static final String DEFAULT_TEST_SUITE_PATH = "/test_suite.properties";

private static final String CMD_SUITE_OPTION = "suite";

private static boolean jsonMode;

private static TestSuiteResult testSuiteResult = new TestSuiteResult();

public static void main(String[] args) throws Exception {
CommandLine cmdLine = createOptions(args);
if (!cmdLine.hasOption('a') && !cmdLine.hasOption('p') && !cmdLine.hasOption('d')) {
printf("Usage: ");
printf("java -jar secure-aem.jar [-a AUTHOR_URL] [-p PUBLISH_URL] [-d DISPATCHER_URL] ");
printf("java -jar secure-aem.jar [-a AUTHOR_URL] [-p PUBLISH_URL] [-d DISPATCHER_URL] [-m MODE]");
System.exit(1);
}
selectMode(cmdLine);
List<TestLoader> testLoaders = createTestLoaders(cmdLine);
boolean result = true;
for (TestLoader testLoader : testLoaders) {
result = doTest(testLoader, cmdLine) && result;
}
printOutput();
System.exit(result ? 0 : -1);
}

Expand All @@ -49,9 +60,13 @@ private static List<TestLoader> createTestLoaders(CommandLine cmdLine)
String line;
while ((line = reader.readLine()) != null) {
String[] parameters = line.split(", ");
if (parameters.length == 2) {
if (parameters.length >= 2) {
Class clazz = Class.forName(parameters[0]);
testLoaders.add(new TestLoader(clazz, parameters[1]));
Severity severity = Severity.MAJOR;
if (parameters.length == 3) {
severity = Severity.of(parameters[2]);
}
testLoaders.add(new TestLoader(clazz, parameters[1], severity));
}
}
return testLoaders;
Expand Down Expand Up @@ -99,11 +114,28 @@ private static boolean doTest(TestLoader testLoader, CommandLine cmdLine) throws
}
}
printf("");
testSuiteResult.addTestResult(
new SingleTestResult(
xmlConfigReader.getMetadataValue("jcr:title"), test, testLoader.getSeverity()));
return test.getResult() == TestResult.OK;
}

private static void printf(String format, Object... args) {
System.out.println(String.format(format, args));
if (!TestRunParameters.SILENT_MODE) {
System.out.println(String.format(format, args));
}
}

private static void selectMode(CommandLine cmdLine) {
jsonMode = cmdLine.hasOption('m') &&
StringUtils.equals(cmdLine.getOptionValue('m'), "json");
TestRunParameters.SILENT_MODE = jsonMode;
}

private static void printOutput() {
if (jsonMode) {
System.out.println(new Gson().toJson(testSuiteResult));
}
}

private static CommandLine createOptions(String[] args) throws ParseException {
Expand All @@ -113,9 +145,11 @@ private static CommandLine createOptions(String[] args) throws ParseException {
options.addOption("d", true, "dispatcher URL");
options.addOption(CMD_SUITE_OPTION, true, "test suite");
options.addOption("aCredentials", true, "author credentials");
options.addOption("pCredentials", true, "publish credentials");

options.addOption("pCredentials", true, "publish credentials");
options.addOption("m", true, "mode");

CommandLineParser parser = new PosixParser();
return parser.parse(options, args);
}

}
10 changes: 9 additions & 1 deletion src/main/java/com/cognifide/secureaem/cli/TestLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@

import com.cognifide.secureaem.AbstractTest;
import com.cognifide.secureaem.Configuration;
import com.cognifide.secureaem.json.Severity;

public class TestLoader {

private final Class<? extends AbstractTest> clazz;

private final String componentName;

public TestLoader(Class<? extends AbstractTest> clazz, String componentName) {
private Severity severity;

public TestLoader(Class<? extends AbstractTest> clazz, String componentName, Severity severity) {
this.clazz = clazz;
this.componentName = componentName;
this.severity = severity;
}

public AbstractTest getTest(Configuration config) throws Exception {
Expand All @@ -24,5 +28,9 @@ public AbstractTest getTest(Configuration config) throws Exception {
String getComponentName() {
return componentName;
}

Severity getSeverity() {
return severity;
}

}
21 changes: 21 additions & 0 deletions src/main/java/com/cognifide/secureaem/json/Severity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.cognifide.secureaem.json;

import java.util.Arrays;

import org.apache.commons.lang3.StringUtils;

public enum Severity {
BLOCKER("BLOCKER"), CRITICAL("CRITICAL"), MAJOR("MAJOR"), MINOR("MINOR"), INFO("INFO");

private String value;

private Severity(String value) {
this.value = value;
}

public static Severity of(String value) {
return Arrays.stream(Severity.values())
.filter(severity -> StringUtils.equals(severity.value, value))
.findFirst().orElse(null);
}
}
41 changes: 41 additions & 0 deletions src/main/java/com/cognifide/secureaem/json/SingleTestResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.cognifide.secureaem.json;

import java.util.List;
import java.util.Set;

import com.cognifide.secureaem.AbstractTest;
import com.cognifide.secureaem.TestResult;

@SuppressWarnings("unused") //serialized
public class SingleTestResult {

private String name;

private TestResult testResult;

private List<String> errorMessages;

private List<String> infoMessages;

private Set<String> environments;

private Severity severity;

public SingleTestResult(String name, AbstractTest abstractTest, Severity severity) {
this.name = name;
testResult = abstractTest.getResult();
errorMessages = abstractTest.getErrorMessages();
infoMessages = abstractTest.getInfoMessages();
environments = abstractTest.getEnvironments();
this.severity = severity;
}

public TestResult getTestResult() {
return testResult;
}

public Severity getSeverity() {
return severity;
}

}
51 changes: 51 additions & 0 deletions src/main/java/com/cognifide/secureaem/json/TestSuiteResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.cognifide.secureaem.json;

import java.util.ArrayList;
import java.util.List;

import com.cognifide.secureaem.TestResult;

public class TestSuiteResult {

List<SingleTestResult> testResults = new ArrayList<>();

int passed = 0;
int failed = 0;

int blocker = 0;
int critical = 0;
int major = 0;
int minor = 0;
int info = 0;

public void addTestResult(SingleTestResult testResult) {
testResults.add(testResult);
appendResults(testResult);
}

private void appendResults(SingleTestResult testResult) {
if (testResult.getTestResult() != TestResult.OK) {
failed++;
switch (testResult.getSeverity()) {
case BLOCKER:
blocker++;
break;
case CRITICAL:
critical++;
break;
case MAJOR:
major++;
break;
case MINOR:
minor++;
break;
case INFO:
default:
info++;
break;
}
} else {
passed++;
}
}
}
27 changes: 14 additions & 13 deletions src/main/java/com/cognifide/secureaem/tests/BundlesTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.cognifide.secureaem.tests;

import com.google.gson.Gson;

import com.cognifide.secureaem.AbstractTest;
import com.cognifide.secureaem.Configuration;
import com.cognifide.secureaem.markers.AuthorTest;
import com.cognifide.secureaem.markers.PublishTest;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthenticationException;
Expand All @@ -16,10 +15,11 @@
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.cognifide.secureaem.AbstractTest;
import com.cognifide.secureaem.Configuration;
import com.cognifide.secureaem.markers.AuthorTest;
import com.cognifide.secureaem.markers.PublishTest;
import com.google.gson.Gson;

/**
* Check if the bundle is present
Expand All @@ -44,9 +44,9 @@ public BundlesTest(Configuration config) {
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(request);
String body = EntityUtils.toString(response.getEntity());
if (response.getStatusLine().getStatusCode() == 401) {
if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_UNAUTHORIZED) {
throw new AuthenticationException("Cannot authenticate user " + credentials.getUserName());
} else if (response.getStatusLine().getStatusCode() == 200) {
} else if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {
@SuppressWarnings("unchecked") Map<String, List<Map<String, String>>> map = GSON
.fromJson(body, Map.class);
String[] bundlesConfig = config.getStringList("bundles");
Expand All @@ -63,4 +63,5 @@ public BundlesTest(Configuration config) {
}
return true;
}
}

}
49 changes: 24 additions & 25 deletions src/main/resources/test_suite.properties
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
com.cognifide.secureaem.tests.ConfigValidation, config-validation
com.cognifide.secureaem.tests.DefaultPasswordsTest, default-passwords
com.cognifide.secureaem.tests.CrxdeLogsTest, crxde-logs
com.cognifide.secureaem.tests.PageContentTest, dispatcher-access
com.cognifide.secureaem.tests.PageContentTest, shindig-proxy
com.cognifide.secureaem.tests.PublishPathsTest, third-party
com.cognifide.secureaem.tests.GroovyConsoleTest, groovy-console
com.cognifide.secureaem.tests.PageContentTest, etc-tools
com.cognifide.secureaem.tests.ExtensionsTest, content-grabbing
com.cognifide.secureaem.tests.ExtensionsTest, feed-selector
com.cognifide.secureaem.tests.WcmDebugTest, wcm-debug
com.cognifide.secureaem.tests.WebDavTest, webdav
com.cognifide.secureaem.tests.PathsTest, felix-console
com.cognifide.secureaem.tests.PageContentTest, geometrixx
com.cognifide.secureaem.tests.ExtensionsTest, redundant-selectors
com.cognifide.secureaem.tests.ReplicationTransportUsersTest, replication-transport-users
com.cognifide.secureaem.tests.BundlesTest, crx-development-bundles
com.cognifide.secureaem.tests.BundlesTest, sling-development-bundle
com.cognifide.secureaem.tests.ErrorHandlerTest, error-handler
com.cognifide.secureaem.tests.SlingReferrerFilterTest, sling-referrer-filter
com.cognifide.secureaem.tests.WcmFilterTest, wcm-filter
com.cognifide.secureaem.tests.HtmlLibraryManagerTest, html-library-manager
com.cognifide.secureaem.tests.SlingJsHandlerTest, sling-js-handler
com.cognifide.secureaem.tests.SlingJspScriptHandlerTest, sling-jsp-script-handler

com.cognifide.secureaem.tests.ConfigValidation, config-validation, INFO
com.cognifide.secureaem.tests.DefaultPasswordsTest, default-passwords, BLOCKER
com.cognifide.secureaem.tests.CrxdeLogsTest, crxde-logs, CRITICAL
com.cognifide.secureaem.tests.PageContentTest, dispatcher-access, BLOCKER
com.cognifide.secureaem.tests.PageContentTest, shindig-proxy, CRITICAL
com.cognifide.secureaem.tests.PublishPathsTest, third-party, CRITICAL
com.cognifide.secureaem.tests.GroovyConsoleTest, groovy-console, CRITICAL
com.cognifide.secureaem.tests.PageContentTest, etc-tools, CRITICAL
com.cognifide.secureaem.tests.ExtensionsTest, content-grabbing, CRITICAL
com.cognifide.secureaem.tests.ExtensionsTest, feed-selector, MAJOR
com.cognifide.secureaem.tests.WcmDebugTest, wcm-debug, MAJOR
com.cognifide.secureaem.tests.WebDavTest, webdav, MAJOR
com.cognifide.secureaem.tests.PathsTest, felix-console, MAJOR
com.cognifide.secureaem.tests.PageContentTest, geometrixx, MAJOR
com.cognifide.secureaem.tests.ExtensionsTest, redundant-selectors, MAJOR
com.cognifide.secureaem.tests.ReplicationTransportUsersTest, replication-transport-users, MAJOR
com.cognifide.secureaem.tests.BundlesTest, crx-development-bundles, CRITICAL
com.cognifide.secureaem.tests.BundlesTest, sling-development-bundle, CRITICAL
com.cognifide.secureaem.tests.ErrorHandlerTest, error-handler, MAJOR
com.cognifide.secureaem.tests.SlingReferrerFilterTest, sling-referrer-filter, MAJOR
com.cognifide.secureaem.tests.WcmFilterTest, wcm-filter, MAJOR
com.cognifide.secureaem.tests.HtmlLibraryManagerTest, html-library-manager, MAJOR
com.cognifide.secureaem.tests.SlingJsHandlerTest, sling-js-handler, MAJOR
com.cognifide.secureaem.tests.SlingJspScriptHandlerTest, sling-jsp-script-handler, MAJOR

0 comments on commit b866dc8

Please sign in to comment.