Skip to content
This repository has been archived by the owner on Jun 9, 2024. It is now read-only.

Commit

Permalink
Add logs (and tests) to the force-user-permset-assign task
Browse files Browse the repository at this point in the history
  • Loading branch information
mcartoixa committed Feb 22, 2021
1 parent 844f573 commit 4d873dd
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 10 deletions.
38 changes: 28 additions & 10 deletions src/main/com/mcartoixa/ant/sfdx/force/user/permset/AssignTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}

}
14 changes: 14 additions & 0 deletions src/test/com/mcartoixa/ant/sfdx/force/user/permset/assign.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"result": {
"failures": [

],
"successes": [
{
"name": "testuser",
"value": "testpermset"
}
]
},
"status": 0
}
19 changes: 19 additions & 0 deletions src/test/com/mcartoixa/ant/sfdx/force/user/permset/assign.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>

<project name="AssignTaskTest" basedir="." xmlns:sfdx="com.mcartoixa.ant.sfdx">
<condition property="sfdx-path" value="..\..\..\sfdx.cmd" else="../../../sfdx">
<os family="dos" />
</condition>

<target name="init">
<taskdef uri="com.mcartoixa.ant.sfdx" resource="com/mcartoixa/ant/sfdx/antlib.xml" classpath="tmp/obj/classes" />
</target>

<target name="execute" depends="init">
<sfdx:force-user-permset-assign permsetname="testpermset" onbehalfof="testtarget" targetusername="testuser" statusproperty="execute.status" resultproperty="execute.result" executable="${sfdx-path}" />
</target>

<target name="execute-quiet" depends="init">
<sfdx:force-user-permset-assign quiet="true" executable="${sfdx-path}" />
</target>
</project>

0 comments on commit 4d873dd

Please sign in to comment.