Fix/mnt 22686 allow complex rest action params (#1569)

* MNT-22686 Allow more complex action params

* MNT-22686 Add email template test

Co-authored-by: pzurek <Piotr.Zurek@hyland.com>
Co-authored-by: Sara Aspery <sara.aspery@alfresco.com>
This commit is contained in:
Kacper Magdziarz
2022-11-23 16:11:48 +01:00
committed by GitHub
parent 38a9f6d3e1
commit a5f16f1b11
5 changed files with 107 additions and 6 deletions

View File

@@ -0,0 +1,95 @@
package org.alfresco.rest.actions.email;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import javax.json.JsonObject;
import org.alfresco.rest.RestTest;
import org.alfresco.rest.core.JsonBodyGenerator;
import org.alfresco.rest.core.RestWrapper;
import org.alfresco.rest.model.RestNodeModel;
import org.alfresco.utility.model.FolderModel;
import org.alfresco.utility.model.UserModel;
import org.alfresco.utility.Utility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class EmailTemplateTest extends RestTest {
public static final String MAIL_ACTION = "mail";
private UserModel adminUser;
private UserModel testUser;
private FolderModel testFolder;
@Autowired
protected RestWrapper restClient;
@BeforeClass(alwaysRun = true)
public void dataPreparation() throws Exception {
adminUser = dataUser.getAdminUser();
testUser = dataUser.createRandomTestUser();
testSite = dataSite.usingUser(testUser)
.createPublicRandomSite();
testFolder = dataContent.usingUser(testUser)
.usingSite(testSite)
.createFolder();
}
@Test
public void adminCanSendEmailUsingTemplateWithModelAsString() throws Exception
{
String templateId = uploadEmailTemplate("simpleEmailTemplate.ftl");
// Create the model for use with email template
JsonObject args = JsonBodyGenerator.defineJSON()
.add("args", JsonBodyGenerator.defineJSON()
.add("name", "testname")
.build())
.build();
String emailModel = args.toString();
// Send an email using the template
restClient.authenticateUser(adminUser)
.withCoreAPI()
.usingActions()
.executeAction(MAIL_ACTION, testFolder, createMailWithTemplateParameters(adminUser, testUser, templateId, emailModel));
restClient.onResponse()
.assertThat().statusCode(HttpStatus.ACCEPTED.value())
.assertThat().body("entry.id", notNullValue());
}
private String uploadEmailTemplate(String templateName)
{
restClient.authenticateUser(adminUser)
.configureRequestSpec()
.addMultiPart("filedata", Utility.getResourceTestDataFile(templateName));
RestNodeModel template = restClient.authenticateUser(adminUser).withCoreAPI().usingResource(testFolder).createNode();
restClient.assertStatusCodeIs(HttpStatus.CREATED);
return template.getId();
}
private static Map<String, Serializable> createMailWithTemplateParameters(UserModel sender, UserModel recipient, String templateId, Serializable model)
{
Map<String, Serializable> parameterValues = new HashMap<>();
parameterValues.put("from", sender.getEmailAddress());
parameterValues.put("to", recipient.getEmailAddress());
parameterValues.put("subject", "Test");
parameterValues.put("template", "workspace://SpacesStore/" + templateId);
parameterValues.put("template_model", model);
return parameterValues;
}
}

View File

@@ -0,0 +1,6 @@
<html>
<head></head>
<body>
Hello ${args.name}!
</body>
</html>

View File

@@ -363,13 +363,13 @@ public class ActionsImpl implements Actions
Map.Entry::getValue));
}
private Map<String, Serializable> extractActionParams(org.alfresco.service.cmr.action.ActionDefinition actionDefinition, Map<String, String> params)
private Map<String, Serializable> extractActionParams(org.alfresco.service.cmr.action.ActionDefinition actionDefinition, Map<String, ?> params)
{
Map<String, Serializable> parameterValues = new HashMap<>();
try
{
for (Map.Entry<String, String> entry : params.entrySet())
for (Map.Entry<String, ?> entry : params.entrySet())
{
String propertyName = entry.getKey();
Object propertyValue = entry.getValue();

View File

@@ -32,7 +32,7 @@ public class Action
private String id;
private String actionDefinitionId;
private String targetId;
Map<String, String> params;
private Map<String, ?> params;
public String getId()
{
@@ -64,12 +64,12 @@ public class Action
this.targetId = targetId;
}
public Map<String, String> getParams()
public Map<String, ?> getParams()
{
return params;
}
public void setParams(Map<String, String> params)
public void setParams(Map<String, ? extends Object> params)
{
this.params = params;
}

View File

@@ -78,7 +78,7 @@ public class Action extends org.alfresco.rest.api.model.Action implements Serial
String id = (String) jsonObject.get("id");
String actionDefinitionId = (String) jsonObject.get("actionDefinitionId");
String targetId = (String) jsonObject.get("targetId");
Map<String, String> params = (Map<String, String>) jsonObject.get("params");
Map<String, Object> params = (Map<String, Object>) jsonObject.get("params");
Action action = new Action();
action.setId(id);