Skip to content

Commit

Permalink
automation: correct output of array values
Browse files Browse the repository at this point in the history
Convert the array to string beforehand as the array to string just
shows the class name.
Fix cast of list, use interface instead of implementation.
Remove unnecessary stubbings.

Signed-off-by: thc202 <[email protected]>
  • Loading branch information
thc202 committed Sep 7, 2023
1 parent 8d995eb commit d3fc232
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 11 deletions.
3 changes: 2 additions & 1 deletion addOns/automation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ All notable changes to this add-on will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## Unreleased

### Fixed
- Correct output of array values set to the jobs.

## [0.31.0] - 2023-09-07
### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public static void applyParamsToObject(
"automation.info.setparam",
objectName, // TODO changed param
key,
value));
toStringValue(value)));
} catch (Exception e) {
progress.error(
Constant.messages.getString(
Expand Down Expand Up @@ -326,6 +326,13 @@ public static void applyParamsToObject(
}
}

private static Object toStringValue(Object value) {
if (value == null || !value.getClass().isArray()) {
return value;
}
return Arrays.toString((Object[]) value);
}

public static void applyObjectToObject(
Object srcObject,
Object destObject,
Expand Down Expand Up @@ -471,7 +478,7 @@ static <T> T objectToType(Object obj, Class<?> t) {
} else if (List.class.equals(t)) {
if (obj instanceof List) {
List<T> list = new ArrayList<>();
list.addAll((ArrayList) obj);
list.addAll((List) obj);
return (T) list;
} else {
LOGGER.error("Unable to map to an List from {}", obj.getClass().getCanonicalName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,16 @@
import org.zaproxy.addon.automation.AutomationJob;
import org.zaproxy.addon.automation.AutomationPlan;
import org.zaproxy.addon.automation.AutomationProgress;
import org.zaproxy.addon.automation.ExtensionAutomation;
import org.zaproxy.zap.extension.script.ExtensionScript;
import org.zaproxy.zap.extension.script.ScriptEngineWrapper;
import org.zaproxy.zap.extension.script.ScriptNode;
import org.zaproxy.zap.extension.script.ScriptWrapper;
import org.zaproxy.zap.testutils.TestUtils;
import org.zaproxy.zap.utils.I18N;

/** Unit test for {@link JobUtils}. */
class JobUtilsUnitTest {
class JobUtilsUnitTest extends TestUtils {

enum enumeration {
aaa,
Expand All @@ -69,6 +71,30 @@ static void setUp() {
Constant.messages = new I18N(Locale.getDefault());
}

@Test
void shouldApplyParamsToObject() {
// Given
mockMessages(new ExtensionAutomation());
AutomationProgress progress = mock(AutomationProgress.class);
HashMap<String, Object> src = new HashMap<>();
src.put("bool", "true");
src.put("valueString", "String");
src.put("array", List.of(1, "A"));
src.put("list", List.of(2, "B"));
Data dest = new Data();
// When
JobUtils.applyParamsToObject(src, dest, "name", null, progress);
// Then
verify(progress).info("Job name set bool = true");
assertThat(dest.isBool(), is(equalTo(Boolean.TRUE)));
verify(progress).info("Job name set valueString = String");
assertThat(dest.getValueString(), is(equalTo("String")));
verify(progress).info("Job name set array = [1, A]");
assertThat(dest.getArray(), is(equalTo(new Object[] {1, "A"})));
verify(progress).info("Job name set list = [2, B]");
assertThat(dest.getList(), is(equalTo(List.of(2, "B"))));
}

@Test
void shouldApplyObjectToObject() {
// Given
Expand All @@ -91,7 +117,6 @@ void shouldApplyObjectToObjectWhileIgnoringSpecifiedPropertyNames() {
Data dest = new Data();
AutomationProgress progress = mock(AutomationProgress.class);
AutomationEnvironment env = mock(AutomationEnvironment.class);
given(env.replaceVars(any())).willAnswer(invocation -> invocation.getArgument(0));
// When
JobUtils.applyObjectToObject(
source, dest, "name", new String[] {"valueString", "bool"}, progress, env);
Expand Down Expand Up @@ -260,7 +285,6 @@ void shouldGetExistingScriptWrapper() {
AutomationProgress progress = mock(AutomationProgress.class);
ScriptWrapper otherScriptWrapper = mock(ScriptWrapper.class);
given(otherScriptWrapper.getFile()).willReturn(new File("/other-script.ext"));
given(otherScriptWrapper.getEngineName()).willReturn(engineName);
ScriptWrapper otherScriptWrapper2 = mock(ScriptWrapper.class);
given(otherScriptWrapper2.getFile()).willReturn(file);
given(otherScriptWrapper2.getEngineName()).willReturn("other engine");
Expand Down Expand Up @@ -399,9 +423,11 @@ private static ExtensionScript mockExtensionLoader(ExtensionScript extensionScri
return extensionScript;
}

private static class Data {
static class Data {
private String valueString;
private Boolean bool;
private Object[] array;
private List<Object> list;

Data() {}

Expand All @@ -414,8 +440,6 @@ public String getValueString() {
return valueString;
}

@SuppressWarnings("unused")
// Used by reflection
public void setValueString(String valueString) {
this.valueString = valueString;
}
Expand All @@ -424,10 +448,24 @@ public Boolean isBool() {
return bool;
}

@SuppressWarnings("unused")
// Used by reflection
public void setBool(Boolean bool) {
this.bool = bool;
}

public Object[] getArray() {
return array;
}

public void setArray(Object[] array) {
this.array = array;
}

public List<Object> getList() {
return list;
}

public void setList(List<Object> list) {
this.list = list;
}
}
}

0 comments on commit d3fc232

Please sign in to comment.