Simplify LogEvent
Add static method to enum to avoid nested ifs over the enum's values.
Change-Id: I3d7858819b556ddeff8ec7df5c685914dffb497b
diff --git a/src/main/java/com/googlesource/gerrit/plugins/its/base/workflow/action/LogEvent.java b/src/main/java/com/googlesource/gerrit/plugins/its/base/workflow/action/LogEvent.java
index 3a46202..4abff71 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/its/base/workflow/action/LogEvent.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/its/base/workflow/action/LogEvent.java
@@ -34,7 +34,18 @@
ERROR,
WARN,
INFO,
- DEBUG
+ DEBUG;
+
+ static Level fromString(String s) {
+ if (s != null) {
+ for (Level level : Level.values()) {
+ if (s.toUpperCase().equals(level.toString())) {
+ return level;
+ }
+ }
+ }
+ return INFO;
+ }
}
public interface Factory {
@@ -67,21 +78,7 @@
@Override
public void execute(String issue, ActionRequest actionRequest, Set<Property> properties)
throws IOException {
- String levelParameter = actionRequest.getParameter(1);
- if (levelParameter != null) {
- levelParameter = levelParameter.toLowerCase();
- }
- Level level = Level.INFO;
- if ("error".equals(levelParameter)) {
- level = Level.ERROR;
- } else if ("warn".equals(levelParameter)) {
- level = Level.WARN;
- } else if ("info".equals(levelParameter)) {
- level = Level.INFO;
- } else if ("debug".equals(levelParameter)) {
- level = Level.DEBUG;
- }
-
+ Level level = Level.fromString(actionRequest.getParameter(1));
for (Property property : properties) {
logProperty(level, property);
}
diff --git a/src/test/java/com/googlesource/gerrit/plugins/its/base/workflow/action/LogEventTest.java b/src/test/java/com/googlesource/gerrit/plugins/its/base/workflow/action/LogEventTest.java
index 99fa2c3..54b9942 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/its/base/workflow/action/LogEventTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/its/base/workflow/action/LogEventTest.java
@@ -30,6 +30,16 @@
public class LogEventTest extends LoggingMockingTestCase {
private Injector injector;
+ public void testNull() throws IOException {
+ ActionRequest actionRequest = createMock(ActionRequest.class);
+ expect(actionRequest.getParameter(1)).andReturn(null);
+
+ replayMocks();
+
+ LogEvent logEvent = createLogEvent();
+ logEvent.execute("4711", actionRequest, new HashSet<>());
+ }
+
public void testEmpty() throws IOException {
ActionRequest actionRequest = createMock(ActionRequest.class);
expect(actionRequest.getParameter(1)).andReturn("");