Skip to content

Commit

Permalink
Code coverage (#6)
Browse files Browse the repository at this point in the history
* Update gradle wrapper
* Increase code coverage
* remove code duplications
  • Loading branch information
kdebisschop authored Feb 29, 2020
1 parent 54e0aa1 commit c403a96
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 150 deletions.
8 changes: 7 additions & 1 deletion src/main/java/com/bioraft/rundeck/conditional/IfElse.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ public IfElse(PluginStepContext ctx) {
this.ctx = ctx;
}

/**
* Setter to control elevation to global context.
*
* @param elevate Flag indicating if variable should be make global.
* @return self
*/
public IfElse setElevate(boolean elevate) {
this.elevate = elevate;
return this;
Expand Down Expand Up @@ -110,7 +116,7 @@ public void ifElse(String group, String name, String testValue, String operator,
}

if (matched.equals("")) {
if (ifFalse == null || ifFalse.length() == 0) {
if (ifFalse.length() == 0) {
ctx.getLogger().log(Constants.DEBUG_LEVEL, "No match, default is empty.");
return;
}
Expand Down
36 changes: 30 additions & 6 deletions src/main/java/com/bioraft/rundeck/conditional/Switch.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,30 @@ public class Switch {

private PluginStepContext ctx;

private Map<String, Object> cfg;

private String defaultValue;

/**
* Constructor sets PluginStepContext.
* Constructor sets PluginStepContext, configuration map, and default value.
*
* @param ctx Plugin step context
* @param cfg Configuration map.
* @param defaultValue The default value, if defined.
*/
public Switch(PluginStepContext ctx) {
public Switch(PluginStepContext ctx, Map<String, Object> cfg, String defaultValue) {
this.ctx = ctx;
this.cfg = cfg;

boolean cfgHasDefault = cfg.containsKey(CFG_DEFAULT_VALUE);
if (cfgHasDefault) {
if (cfg.get(CFG_DEFAULT_VALUE) == null) {
this.defaultValue = null;
} else {
this.defaultValue = cfg.get(CFG_DEFAULT_VALUE).toString();
}
}

}

/**
Expand All @@ -56,14 +73,21 @@ public Switch(PluginStepContext ctx) {
* @param name The name of the variable.
* @param cases The switch cases as test1:value1;test2:value2
* @param test The string to test the cases against.
* @param defaultValue The value to return if no cases match
* @param elevate If specified, also create a variable in global export
* context.
*/
public void switchCase(String group, String name, String cases, String test, String defaultValue, boolean elevate) throws JsonProcessingException {
public void switchCase(String group, String name, String cases, String test, boolean elevate) throws JsonProcessingException {
group = cfg.getOrDefault("group", group).toString();
name = cfg.getOrDefault("name", name).toString();
cases = cfg.getOrDefault("cases", cases).toString();
test = cfg.getOrDefault("testValue", test).toString();

String message = "Setting " + group + "." + name + " based on " + test + " " + cases;
ctx.getLogger().log(Constants.DEBUG_LEVEL, message);

// If no case was matched, assign defaultValue if it is not null.
try {
if (!switchCase(group, name, cases, test, elevate)) {
if (!switchCase2(group, name, cases, test, elevate)) {
if (defaultValue != null && defaultValue.length() > 0) {
addOutput(elevate, group, name, defaultValue);
ctx.getLogger().log(Constants.DEBUG_LEVEL, "No match, using default.");
Expand All @@ -89,7 +113,7 @@ public void switchCase(String group, String name, String cases, String test, Str
*
* @return True if matched, false otherwise.
*/
public boolean switchCase(String group, String name, String cases, String test, boolean elevate) throws JsonProcessingException {
public boolean switchCase2(String group, String name, String cases, String test, boolean elevate) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode map = objectMapper.readTree(ensureStringIsJsonObject(cases));
Iterator<Map.Entry<String, JsonNode>> iterator = map.fields();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import java.util.Map;

import com.dtolabs.rundeck.core.Constants;
import com.dtolabs.rundeck.core.common.INodeEntry;
import com.dtolabs.rundeck.core.execution.workflow.steps.node.NodeStepException;
import com.dtolabs.rundeck.core.plugins.Plugin;
Expand All @@ -32,7 +31,6 @@

import static com.dtolabs.rundeck.core.plugins.configuration.StringRenderingConstants.CODE_SYNTAX_MODE;
import static com.dtolabs.rundeck.core.plugins.configuration.StringRenderingConstants.DISPLAY_TYPE_KEY;
import static com.bioraft.rundeck.conditional.Switch.CFG_DEFAULT_VALUE;

/**
* Workflow Node Step Plug-in to choose one of several values to uplift into a
Expand Down Expand Up @@ -73,27 +71,10 @@ public class SwitchCaseNodeStepPlugin implements NodeStepPlugin {
public void executeNodeStep(PluginStepContext ctx, Map<String, Object> cfg, INodeEntry node)
throws NodeStepException {

group = cfg.getOrDefault("group", this.group).toString();
name = cfg.getOrDefault("name", this.name).toString();
cases = cfg.getOrDefault("cases", this.cases).toString();
testValue = cfg.getOrDefault("testValue", this.testValue).toString();
elevateToGlobal = cfg.getOrDefault("elevateToGlobal", String.valueOf(elevateToGlobal)).equals("true");

boolean globalHasDefault = defaultValue != null && defaultValue.length() > 0;
boolean cfgHasDefault = cfg.containsKey(CFG_DEFAULT_VALUE) && cfg.get(CFG_DEFAULT_VALUE) != null;
if (cfgHasDefault) {
this.defaultValue = cfg.get(CFG_DEFAULT_VALUE).toString();
}

String message = "Setting " + group + "." + name + " based on " + testValue + " " + cases;
ctx.getLogger().log(Constants.DEBUG_LEVEL, message);

try {
if (cfgHasDefault || globalHasDefault) {
(new Switch(ctx)).switchCase(group, name, cases, testValue, defaultValue, elevateToGlobal);
} else {
(new Switch(ctx)).switchCase(group, name, cases, testValue, elevateToGlobal);
}
(new Switch(ctx, cfg, defaultValue)).switchCase(group, name, cases, testValue, elevateToGlobal);
} catch (JsonProcessingException e) {
throw new NodeStepException(e.getMessage(), Switch.Causes.INVALID_JSON, node.getNodename());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import java.util.Map;

import com.dtolabs.rundeck.core.Constants;
import com.dtolabs.rundeck.core.execution.workflow.steps.StepException;
import com.dtolabs.rundeck.core.plugins.Plugin;
import com.dtolabs.rundeck.plugins.ServiceNameConstants;
Expand All @@ -31,7 +30,6 @@

import static com.dtolabs.rundeck.core.plugins.configuration.StringRenderingConstants.CODE_SYNTAX_MODE;
import static com.dtolabs.rundeck.core.plugins.configuration.StringRenderingConstants.DISPLAY_TYPE_KEY;
import static com.bioraft.rundeck.conditional.Switch.CFG_DEFAULT_VALUE;

/**
* Workflow Node Step Plug-in to choose one of several values to uplift into a
Expand Down Expand Up @@ -70,27 +68,10 @@ public class SwitchCaseStepPlugin implements StepPlugin {
@Override
public void executeStep(final PluginStepContext ctx, final Map<String, Object> cfg) throws StepException {

group = cfg.getOrDefault("group", this.group).toString();
name = cfg.getOrDefault("name", this.name).toString();
cases = cfg.getOrDefault("cases", this.cases).toString();
testValue = cfg.getOrDefault("testValue", this.testValue).toString();
elevateToGlobal = cfg.getOrDefault("elevateToGlobal", String.valueOf(elevateToGlobal)).equals("true");

boolean globalHasDefault = defaultValue != null && defaultValue.length() > 0;
boolean cfgHasDefault = cfg.containsKey(CFG_DEFAULT_VALUE) && cfg.get(CFG_DEFAULT_VALUE) != null;
if (cfgHasDefault) {
this.defaultValue = cfg.get(CFG_DEFAULT_VALUE).toString();
}

ctx.getLogger().log(Constants.DEBUG_LEVEL,
"Setting " + group + "." + name + " based on " + testValue + " " + cases);

try {
if (cfgHasDefault || globalHasDefault) {
(new Switch(ctx)).switchCase(group, name, cases, testValue, defaultValue, elevateToGlobal);
} else {
(new Switch(ctx)).switchCase(group, name, cases, testValue, elevateToGlobal);
}
(new Switch(ctx, cfg, defaultValue)).switchCase(group, name, cases, testValue, elevateToGlobal);
} catch (JsonProcessingException e) {
throw new StepException(e.getMessage(), Switch.Causes.INVALID_JSON);
}
Expand Down
37 changes: 31 additions & 6 deletions src/test/java/com/bioraft/rundeck/conditional/IfElseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
package com.bioraft.rundeck.conditional;

import com.dtolabs.rundeck.core.dispatcher.ContextView;
import com.dtolabs.rundeck.core.execution.workflow.SharedOutputContext;
import com.dtolabs.rundeck.core.execution.workflow.steps.StepException;
import com.dtolabs.rundeck.plugins.PluginLogger;
import com.dtolabs.rundeck.plugins.step.PluginStepContext;
import org.junit.Before;
Expand Down Expand Up @@ -60,7 +60,7 @@ public void setUp() {
}

@Test
public void runTrueTests() throws StepException {
public void runTrueTests() {
int i = 0;
this.runTestTrue("apple", "eq", "apple", ++i);
this.runTestTrue("apple", "ne", "pear", ++i);
Expand All @@ -79,7 +79,7 @@ public void runTrueTests() throws StepException {
}

@Test
public void runFalseTests() throws StepException {
public void runFalseTests() {
int i = 0;
this.runTestFalse("apple", "eq", "apples", ++i);
this.runTestFalse("apple", "ne", "apple", ++i);
Expand All @@ -97,7 +97,33 @@ public void runFalseTests() throws StepException {
this.runTestFalse("2.0", "!=", "2.00", ++i);
}

private void runTestTrue(String testValue, String operator, String comparison, int calls) throws StepException {
@Test
public void runNoDefaultTests() {
when(context.getOutputContext()).thenReturn(sharedOutputContext);
when(context.getLogger()).thenReturn(logger);

this.plugin.setElevate(false).setCfg(configuration).
ifElse("raft", "test", "apple", "EQ", "apples", "1", "");
verify(context, never()).getOutputContext();
verify(sharedOutputContext, never()).addOutput(anyString(), anyString(), anyString());
}

@Test
public void testElevation() {
String group = "raft";
String name = "test";
String ifTrue = "1";

when(context.getOutputContext()).thenReturn(sharedOutputContext);
when(context.getLogger()).thenReturn(logger);

this.plugin.setElevate(true).setCfg(configuration).ifElse(group, name, name, "EQ", name, ifTrue, ifTrue);
verify(context, times(2)).getOutputContext();
verify(sharedOutputContext, times(1)).addOutput(eq(group), eq(name), eq(ifTrue));
verify(sharedOutputContext, times(1)).addOutput(any(ContextView.class), eq("export"), anyString(), eq(ifTrue));
}

private void runTestTrue(String testValue, String operator, String comparison, int calls) {
String group = "raft";
String name = "test";
String ifTrue = "1";
Expand All @@ -111,8 +137,7 @@ private void runTestTrue(String testValue, String operator, String comparison, i
verify(sharedOutputContext, atLeast(calls)).addOutput(eq(group), eq(name), eq(ifTrue));
}

private void runTestFalse(String testValue, String operator, String comparison, int calls)
throws StepException {
private void runTestFalse(String testValue, String operator, String comparison, int calls) {
String group = "boat";
String name = "real";
String ifTrue = "yes";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@
*/
package com.bioraft.rundeck.conditional;

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.HashMap;
import java.util.Map;

import org.junit.Before;
Expand All @@ -46,7 +44,7 @@
* @since 2019-12-11
*/
@RunWith(MockitoJUnitRunner.class)
public class SwitchCaseNodeStepPluginTest {
public class SwitchCaseNodeStepPluginTest extends SwitchTestBase {

SwitchCaseNodeStepPlugin plugin;

Expand All @@ -61,30 +59,12 @@ public class SwitchCaseNodeStepPluginTest {

@Mock
INodeEntry node;
private final String group = "raft";
private final String name = "test";
private final String testValue = "any";
private final String defaultValue = "any";

@Before
public void setUp() {
this.plugin = new SwitchCaseNodeStepPlugin();
}

@Test
public void testEnsureStringIsJsonObject() {
assertEquals("", Switch.ensureStringIsJsonObject(null));
String given = "\"a\": \"1\"";
String expected = "{" + given + "}";
assertEquals(expected, Switch.ensureStringIsJsonObject(given));
assertEquals(expected, Switch.ensureStringIsJsonObject(given + ","));
assertEquals(expected, Switch.ensureStringIsJsonObject(given + "}"));
assertEquals(expected, Switch.ensureStringIsJsonObject("{" + given));
assertEquals(expected, Switch.ensureStringIsJsonObject("{" + given + "}"));
assertEquals(expected, Switch.ensureStringIsJsonObject("{" + given + ",}"));
assertEquals(expected, Switch.ensureStringIsJsonObject("{" + given + ", } "));
}

@Test
public void runTestOne() throws NodeStepException {
Map<String, String> cases = ImmutableMap.<String, String>builder().put("k1", "v1").put("k2", "v2").build();
Expand All @@ -103,62 +83,18 @@ public void returnsDefaultOnNoMatch() throws NodeStepException {
this.runTest(testValue, "k3", cases, defaultValue);
}

@Test
public void runTestDefaultIsNull() throws NodeStepException {
Map<String, Object> configuration = new HashMap<>();
configuration.put("defaultValue", null);
this.runTestNoDefault(configuration);
}

@Test
public void runTestNoDefaultValue() throws NodeStepException {
Map<String, Object> configuration = new HashMap<>();
this.runTestNoDefault(configuration);
}

@Test
public void testStrippingTrailingComma() throws NodeStepException {
StringBuffer caseString = new StringBuffer();
Map<String, String> cases = ImmutableMap.<String, String>builder().put("k1", "v1").put("k2", "v2").build();
cases.forEach((k, v) -> caseString.append('"').append(k).append('"').append(":").append('"').append(v).append('"').append(","));
validInput(caseString.toString());
}

@Test(expected = NodeStepException.class)
public void testInvalidCases() throws NodeStepException {
StringBuffer caseString = new StringBuffer();
Map<String, String> cases = ImmutableMap.<String, String>builder().put("k1", "v1").put("k2", "v2").build();
cases.forEach((k, v) -> caseString.append('"').append(k).append('"').append(":").append('"').append(v).append('"').append("."));
invalidInput(caseString.toString());
}

private void validInput(String caseString)
throws NodeStepException {

Map<String, Object> configuration = new HashMap<>();
configuration.put("group", group);
configuration.put("name", name);
configuration.put("cases", caseString);
configuration.put("testValue", testValue);
configuration.put("defaultValue", defaultValue);

when(context.getOutputContext()).thenReturn(sharedOutputContext);
when(context.getLogger()).thenReturn(logger);

this.plugin.executeNodeStep(context, configuration, node);
verify(context, times(1)).getOutputContext();
verify(sharedOutputContext, times(1)).addOutput(eq(group), eq(name), eq(defaultValue));
invalidInput(caseString);
}

private void invalidInput(String caseString)
private void invalidInput(StringBuffer caseString)
throws NodeStepException {

Map<String, Object> configuration = new HashMap<>();
configuration.put("group", group);
configuration.put("name", name);
configuration.put("cases", caseString);
configuration.put("testValue", testValue);
configuration.put("defaultValue", defaultValue);
Map<String, Object> configuration = getConfiguration(testValue, caseString, defaultValue);

when(context.getOutputContext()).thenReturn(sharedOutputContext);
when(context.getLogger()).thenReturn(logger);
Expand All @@ -172,12 +108,7 @@ private void runTest(String expected, String testValue, Map<String, String> case
cases.forEach((k, v) -> caseString.append('"').append(k).append('"').append(":").append('"').append(v).append('"').append(","));
caseString.setLength(caseString.length() - 1);

Map<String, Object> configuration = new HashMap<>();
configuration.put("group", group);
configuration.put("name", name);
configuration.put("cases", caseString);
configuration.put("testValue", testValue);
configuration.put("defaultValue", defaultValue);
Map<String, Object> configuration = getConfiguration(testValue, caseString, defaultValue);

when(context.getOutputContext()).thenReturn(sharedOutputContext);
when(context.getLogger()).thenReturn(logger);
Expand Down
Loading

0 comments on commit c403a96

Please sign in to comment.