MNT-22686 Load template from the classpath (#1576)

This commit is contained in:
Piotr Żurek
2022-11-24 14:40:13 +01:00
committed by GitHub
parent 74e44acb1c
commit 80c6a0127d

View File

@@ -1,8 +1,11 @@
package org.alfresco.rest.actions.email; package org.alfresco.rest.actions.email;
import static org.hamcrest.Matchers.notNullValue; import static java.util.Objects.requireNonNull;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.notNullValue;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable; import java.io.Serializable;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@@ -10,13 +13,11 @@ import javax.json.JsonObject;
import org.alfresco.rest.RestTest; import org.alfresco.rest.RestTest;
import org.alfresco.rest.core.JsonBodyGenerator; import org.alfresco.rest.core.JsonBodyGenerator;
import org.alfresco.rest.core.RestWrapper; import org.alfresco.utility.model.FileModel;
import org.alfresco.rest.model.RestNodeModel; import org.alfresco.utility.model.FileType;
import org.alfresco.utility.model.FolderModel; import org.alfresco.utility.model.FolderModel;
import org.alfresco.utility.model.UserModel; 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.springframework.http.HttpStatus;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@@ -29,9 +30,6 @@ public class EmailTemplateTest extends RestTest {
private UserModel testUser; private UserModel testUser;
private FolderModel testFolder; private FolderModel testFolder;
@Autowired
protected RestWrapper restClient;
@BeforeClass(alwaysRun = true) @BeforeClass(alwaysRun = true)
public void dataPreparation() throws Exception { public void dataPreparation() throws Exception {
adminUser = dataUser.getAdminUser(); adminUser = dataUser.getAdminUser();
@@ -68,16 +66,26 @@ public class EmailTemplateTest extends RestTest {
.assertThat().body("entry.id", notNullValue()); .assertThat().body("entry.id", notNullValue());
} }
private String uploadEmailTemplate(String templateName) private String uploadEmailTemplate(String templateName) throws IOException
{ {
restClient.authenticateUser(adminUser) final String templateContent = getTemplateContent(templateName);
.configureRequestSpec() final FileModel templateToCreate = new FileModel(templateName, FileType.TEXT_PLAIN, templateContent);
.addMultiPart("filedata", Utility.getResourceTestDataFile(templateName));
RestNodeModel template = restClient.authenticateUser(adminUser).withCoreAPI().usingResource(testFolder).createNode(); final FileModel createdTemplate = dataContent.usingAdmin()
restClient.assertStatusCodeIs(HttpStatus.CREATED); .usingResource(testFolder)
.createContent(templateToCreate);
return template.getId(); return createdTemplate.getNodeRef();
}
private String getTemplateContent(String templateName) throws IOException
{
final String templateClasspathLocation = "/shared-resources/testdata/" + templateName;
try (InputStream templateStream = getClass().getResourceAsStream(templateClasspathLocation))
{
requireNonNull(templateStream, "Couldn't locate `" + templateClasspathLocation + "`");
return new String(templateStream.readAllBytes());
}
} }
private static Map<String, Serializable> createMailWithTemplateParameters(UserModel sender, UserModel recipient, String templateId, Serializable model) private static Map<String, Serializable> createMailWithTemplateParameters(UserModel sender, UserModel recipient, String templateId, Serializable model)