Skip to content

Commit

Permalink
Change approach to creating files, better to have basic code managing…
Browse files Browse the repository at this point in the history
… it than fighting throughout projects with resources.
  • Loading branch information
mstrankowski committed Nov 20, 2023
1 parent 53ec538 commit b365c8d
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 24 deletions.
14 changes: 14 additions & 0 deletions tests/tas-mtls/src/test/java/org/alfresco/rest/MtlsRestTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.alfresco.rest;

import javax.net.ssl.SSLHandshakeException;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Method;

import io.restassured.RestAssured;
Expand Down Expand Up @@ -114,4 +117,15 @@ protected FolderModel selectSharedFolder(UserModel user) {

return folderModel;
}

protected File createTestFile(String fileName, String fileContent) throws IOException
{
File testFile = new File(fileName);
try (FileWriter fileWriter = new FileWriter(testFile))
{
fileWriter.write(fileContent);
}

return testFile;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,54 @@
import org.alfresco.utility.model.UserModel;
import org.springframework.test.context.ContextConfiguration;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;

@ContextConfiguration("classpath:alfresco-mtls-context.xml")
public class SearchServiceTest extends MtlsRestTest
{
public static final String TEXT_FILE = "testing-search-mtls.txt";
private static final String TEST_FILE_NAME = "testing-search-mtls.txt";
private static final String TEST_FILE_KEYWORD = "incomprehensible";
private static final String TEST_FILE_CONTENT = "We need to verify indexing working in solr/elasticsearch to do that we need to upload this \n"
+ "text file and search with a word inside it,\n"
+ "like \"" + TEST_FILE_KEYWORD + "\" to verify it has been indexed properly.";

private UserModel adminUser;
private File testFile;

@BeforeClass(alwaysRun = true) public void dataPreparation()
@BeforeClass(alwaysRun = true)
public void dataPreparation() throws IOException
{
adminUser = dataUser.getAdminUser();
testFile = createTestFile(TEST_FILE_NAME, TEST_FILE_CONTENT);
}

@AfterClass(alwaysRun = true)
public void dataCleanup()
{
if (testFile != null && testFile.exists()) {
testFile.delete();
}
}

@Test public void testIndexingWithMTLSEnabledTest() throws InterruptedException
@Test
public void testIndexingWithMTLSEnabledTest() throws InterruptedException
{
FolderModel folderModel = selectSharedFolder(adminUser);
RestNodeModel fileNode = null;
try
{
int initialSearchResultsCount = countSearchResults();
int initialSearchResultsCount = countSearchResults(TEST_FILE_KEYWORD);

restClient.authenticateUser(adminUser).configureRequestSpec().addMultiPart("filedata", Utility.getTestResourceFile(TEXT_FILE));
restClient.authenticateUser(adminUser).configureRequestSpec().addMultiPart("filedata", testFile);
fileNode = restClient.authenticateUser(adminUser).withCoreAPI().usingNode(folderModel).createNode();

verifyResultsIncreaseWithRetry(initialSearchResultsCount);
verifyResultsIncreaseWithRetry(TEST_FILE_KEYWORD, initialSearchResultsCount);
}
finally
{
Expand All @@ -48,23 +69,23 @@ public class SearchServiceTest extends MtlsRestTest
}
}

private int countSearchResults() {
private int countSearchResults(String keyword) {
RestRequestQueryModel queryModel = new RestRequestQueryModel();
queryModel.setLanguage("afts");
queryModel.setQuery("incomprehensible");
queryModel.setQuery(keyword);

SearchRequest searchRequest = new SearchRequest(queryModel);
SearchResponse searchResponse = restClient.authenticateUser(adminUser).withSearchAPI().search(searchRequest);

return searchResponse.getEntries().size();
}

private void verifyResultsIncreaseWithRetry(int initialSearchWordCount) throws InterruptedException {
private void verifyResultsIncreaseWithRetry(String keyword, int initialSearchWordCount) throws InterruptedException {
int retryDelay = 5000;
int retryLimit = 10;
for (int i = 0; i < retryLimit; i++) {
LOGGER.info("Attempt: " + (i+1));
if (countSearchResults() == initialSearchWordCount + 1) {
if (countSearchResults(keyword) == initialSearchWordCount + 1) {
return;
}
Thread.sleep(retryDelay);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,66 @@
import org.springframework.http.HttpStatus;
import org.springframework.test.context.ContextConfiguration;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;

@ContextConfiguration ("classpath:alfresco-mtls-context.xml")
public class TransformServiceTest extends MtlsRestTest
{
public static final String TEXT_FILE = "testing-transform-mtls.txt";
private static final String TEST_FILE_NAME = "testing-transform-mtls.txt";
private static final String TEST_FILE_CONTENT = "Random text for transform tests";

private UserModel adminUser;
private File testFile;

@BeforeClass (alwaysRun = true)
public void dataPreparation()
public void dataPreparation() throws IOException
{
adminUser = dataUser.getAdminUser();
testFile = createTestFile(TEST_FILE_NAME, TEST_FILE_CONTENT);
}

@AfterClass(alwaysRun = true)
public void dataCleanup()
{
if (testFile != null && testFile.exists()) {
testFile.delete();
}
}

@Test
public void testRenditionWithMTLSEnabledTest()
{
FolderModel testFolder = selectSharedFolder(adminUser);
FileModel testFile = new FileModel(TEXT_FILE);
FileModel testFileModel = new FileModel(testFile.getName());

try {
restClient.authenticateUser(adminUser).configureRequestSpec().addMultiPart("filedata", Utility.getTestResourceFile(TEXT_FILE));
restClient.authenticateUser(adminUser).configureRequestSpec().addMultiPart("filedata", testFile);
RestNodeModel rnm = restClient.authenticateUser(adminUser).withCoreAPI().usingNode(testFolder).createNode();
testFile.setNodeRef(rnm.getId());
testFileModel.setNodeRef(rnm.getId());

restClient.authenticateUser(adminUser).withCoreAPI().usingNode(testFile).createNodeRendition("pdf");
restClient.authenticateUser(adminUser).withCoreAPI().usingNode(testFileModel).createNodeRendition("pdf");
restClient.assertStatusCodeIs(HttpStatus.ACCEPTED);

String status = restClient.withCoreAPI().usingNode(testFile).getNodeRenditionUntilIsCreated("pdf").getStatus();
String status = restClient.withCoreAPI().usingNode(testFileModel).getNodeRenditionUntilIsCreated("pdf").getStatus();
Assert.assertEquals(status, "CREATED");

restClient.authenticateUser(adminUser).withCoreAPI().usingNode(testFile).createNodeRendition("doclib");
restClient.authenticateUser(adminUser).withCoreAPI().usingNode(testFileModel).createNodeRendition("doclib");
restClient.assertStatusCodeIs(HttpStatus.ACCEPTED);

status = restClient.withCoreAPI().usingNode(testFile).getNodeRenditionUntilIsCreated("doclib").getStatus();
status = restClient.withCoreAPI().usingNode(testFileModel).getNodeRenditionUntilIsCreated("doclib").getStatus();
Assert.assertEquals(status, "CREATED");
}
finally
{
//Clean up file for easier local retries of test
if (testFile.getNodeRef() != null)
if (testFileModel.getNodeRef() != null)
{
restClient.authenticateUser(adminUser).withCoreAPI().usingNode(testFolder).deleteNode(testFile.getNodeRef());
restClient.authenticateUser(adminUser).withCoreAPI().usingNode(testFolder).deleteNode(testFileModel.getNodeRef());
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions tests/tas-mtls/src/test/resources/testing-search-mtls.txt

This file was deleted.

This file was deleted.

0 comments on commit b365c8d

Please sign in to comment.