From 4d873dd7f0356d6801e711adb7c44868949bbd7b Mon Sep 17 00:00:00 2001 From: Mathieu Cartoixa Date: Mon, 22 Feb 2021 11:06:03 +0100 Subject: [PATCH] Add logs (and tests) to the force-user-permset-assign task --- .../sfdx/force/user/permset/AssignTask.java | 38 +++++--- .../force/user/permset/AssignTaskTest.java | 89 +++++++++++++++++++ .../ant/sfdx/force/user/permset/assign.json | 14 +++ .../ant/sfdx/force/user/permset/assign.xml | 19 ++++ 4 files changed, 150 insertions(+), 10 deletions(-) create mode 100644 src/test/com/mcartoixa/ant/sfdx/force/user/permset/AssignTaskTest.java create mode 100644 src/test/com/mcartoixa/ant/sfdx/force/user/permset/assign.json create mode 100644 src/test/com/mcartoixa/ant/sfdx/force/user/permset/assign.xml diff --git a/src/main/com/mcartoixa/ant/sfdx/force/user/permset/AssignTask.java b/src/main/com/mcartoixa/ant/sfdx/force/user/permset/AssignTask.java index 6a3150e..d19c7eb 100644 --- a/src/main/com/mcartoixa/ant/sfdx/force/user/permset/AssignTask.java +++ b/src/main/com/mcartoixa/ant/sfdx/force/user/permset/AssignTask.java @@ -40,16 +40,34 @@ protected void doParse(final JSONObject json) { if (json != null) { final JSONObject result = json.getJSONObject("result"); - if (result != null && result.has("failures")) { - final JSONArray failures = result.getJSONArray("failures"); - if (failures != null) { - for (int i = 0; i < failures.length(); i++) { - final JSONObject f = failures.getJSONObject(i); - final String message = f.optString("message"); - if (message != null && !message.isEmpty()) { - this.log(message, Project.MSG_ERR); - if (AssignTask.this.getFailOnError() && !AssignTask.this.hasErrorMessage()) { - AssignTask.this.setErrorMessage("Permissions could not be assigned."); + if (result != null) { + if (result.has("successes")) { + final JSONArray successes = result.getJSONArray("successes"); + if (successes != null) { + for (int i = 0; i < successes.length(); i++) { + final JSONObject s = successes.getJSONObject(i); + this.log( + String.format( + "Permission set %s assigned to %s", + s.optString("value"), + s.optString("name") + ), + Project.MSG_INFO + ); + } + } + } + if (result.has("failures")) { + final JSONArray failures = result.getJSONArray("failures"); + if (failures != null) { + for (int i = 0; i < failures.length(); i++) { + final JSONObject f = failures.getJSONObject(i); + final String message = f.optString("message"); + if (message != null && !message.isEmpty()) { + this.log(message, Project.MSG_ERR); + if (AssignTask.this.getFailOnError() && !AssignTask.this.hasErrorMessage()) { + AssignTask.this.setErrorMessage("Permissions could not be assigned."); + } } } } diff --git a/src/test/com/mcartoixa/ant/sfdx/force/user/permset/AssignTaskTest.java b/src/test/com/mcartoixa/ant/sfdx/force/user/permset/AssignTaskTest.java new file mode 100644 index 0000000..7a4bc72 --- /dev/null +++ b/src/test/com/mcartoixa/ant/sfdx/force/user/permset/AssignTaskTest.java @@ -0,0 +1,89 @@ +/* + * Copyright 2021 Mathieu Cartoixa. + * + * 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 com.mcartoixa.ant.sfdx.force.user.permset; + +import java.io.File; +import org.apache.tools.ant.BuildFileRule; +import org.apache.tools.ant.Project; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +/** + * + * @author Mathieu Cartoixa + */ +public class AssignTaskTest { + + @Rule + public final BuildFileRule buildRule = new BuildFileRule(); + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + public AssignTaskTest() { + } + + @Before + public void setUp() { + buildRule.configureProject("src/test/com/mcartoixa/ant/sfdx/force/user/permset/assign.xml", Project.MSG_DEBUG); + } + + @Test + public void executeShouldSetStatusProperty() { + buildRule.executeTarget("execute"); + Assert.assertEquals("Status property should be set", "0", buildRule.getProject().getProperty("execute.status")); + } + + @Test + public void executeShouldAddJsonArgument() { + buildRule.executeTarget("execute"); + Assert.assertTrue("Full log should contain --json argument", buildRule.getFullLog().contains("'--json'")); + } + + @Test + public void executeShouldAddPermsetNameArgument() { + buildRule.executeTarget("execute"); + Assert.assertTrue("Full log should contain -n argument", buildRule.getFullLog().contains("'-n'" + System.lineSeparator() + "'testpermset'")); + } + + @Test + public void executeShouldAddOnBehalfOfArgument() { + buildRule.executeTarget("execute"); + Assert.assertTrue("Full log should contain -o argument", buildRule.getFullLog().contains("'-o'" + System.lineSeparator() + "'testtarget'")); + } + + @Test + public void executeShouldAddTargetUsernameArgument() { + buildRule.executeTarget("execute"); + Assert.assertTrue("Full log should contain -u argument", buildRule.getFullLog().contains("'-u'" + System.lineSeparator() + "'testuser'")); + } + + @Test + public void executeShouldLogAssignedPermissionSet() { + buildRule.executeTarget("execute"); + Assert.assertTrue("Assigned permission set should be logged " + buildRule.getLog(), buildRule.getLog().contains("Permission set testpermset assigned to testuser")); + } + + @Test + public void executeQuietShouldHaveNoOutput() { + buildRule.executeTarget("execute-quiet"); + Assert.assertTrue("Log should be empty", buildRule.getLog().isEmpty()); + } + +} diff --git a/src/test/com/mcartoixa/ant/sfdx/force/user/permset/assign.json b/src/test/com/mcartoixa/ant/sfdx/force/user/permset/assign.json new file mode 100644 index 0000000..5f6e45e --- /dev/null +++ b/src/test/com/mcartoixa/ant/sfdx/force/user/permset/assign.json @@ -0,0 +1,14 @@ +{ + "result": { + "failures": [ + + ], + "successes": [ + { + "name": "testuser", + "value": "testpermset" + } + ] + }, + "status": 0 +} diff --git a/src/test/com/mcartoixa/ant/sfdx/force/user/permset/assign.xml b/src/test/com/mcartoixa/ant/sfdx/force/user/permset/assign.xml new file mode 100644 index 0000000..4aa6921 --- /dev/null +++ b/src/test/com/mcartoixa/ant/sfdx/force/user/permset/assign.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + +