Skip to content
Permalink
Browse files

Merge remote-tracking branch 'origin/integrationtestsRefactoring' int…

…o dev
  • Loading branch information
helkv committed Nov 22, 2019
2 parents b25b422 + 9ffb3f9 commit b99c204fcd768b976f33646bb12abb4b570dd0fe
@@ -418,6 +418,12 @@
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>4.0.1</version>
<scope>test</scope>
</dependency>
<!-- START JSF 2 -->
<dependency>
<groupId>com.sun.faces</groupId>
@@ -574,7 +580,7 @@
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>elasticsearch</artifactId>
<version>1.10.7</version>
<version>1.12.3</version>
</dependency>
<!-- Add slf4j-Dependency to get the TestContainers Log (Solves: 'SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".') -->
<dependency>
@@ -2,8 +2,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.jena.query.Dataset;
import org.apache.jena.query.ReadWrite;
@@ -25,7 +23,6 @@

private final CRUDTransactionType type;
private boolean lazy = false;
private ExecutorService executor = Executors.newFixedThreadPool(4);


public enum CRUDTransactionType {
@@ -7,15 +7,13 @@
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.apache.logging.log4j.Logger;

import de.mpg.imeji.exceptions.ImejiException;

import org.apache.logging.log4j.LogManager;

import org.apache.jena.Jena;
import org.apache.jena.query.Dataset;
import org.apache.jena.tdb.TDBFactory;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import de.mpg.imeji.exceptions.ImejiException;


/**
@@ -8,8 +8,8 @@
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.io.filefilter.WildcardFileFilter;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import de.mpg.imeji.logic.util.TempFileUtil;

@@ -23,21 +23,28 @@
@Override
public Integer call() throws Exception {
final IOFileFilter filter = new WildcardFileFilter(IMEJI_TEMP_FILE_REGEX);
LOGGER.info("Deleting all imeji temp file from: " + TempFileUtil.TEMP_DIR + " ...");
final Iterator<File> iterator = FileUtils.iterateFiles(TempFileUtil.TEMP_DIR, filter, null);
int success = 0;
int count = 0;
while (iterator.hasNext()) {
final File file = iterator.next();
try {
count++;
FileUtils.forceDelete(file);
success++;
} catch (final IOException e) {
LOGGER.error("File " + file.getAbsolutePath() + " can not be deleted");
File tempDir = TempFileUtil.getTempDirectory();

if (tempDir != null && tempDir.exists()) {
LOGGER.info("Deleting all imeji temp file from: " + tempDir + " ...");
final Iterator<File> iterator = FileUtils.iterateFiles(tempDir, filter, null);
int success = 0;
int count = 0;
while (iterator.hasNext()) {
final File file = iterator.next();
try {
count++;
FileUtils.forceDelete(file);
success++;
} catch (final IOException e) {
LOGGER.error("File " + file.getAbsolutePath() + " can not be deleted");
}
}
LOGGER.info("Deleting all imeji temp file done! " + success + " from " + count + " deleted tmp files.");
} else {
LOGGER.info("Temp directory does not exist. No temp files to delete.");
}
LOGGER.info("Deleting all imeji temp file done! " + success + " from " + count + " deleted tmp files.");

return 1;
}

@@ -49,6 +49,14 @@ public UserGroupFactory setGrants(Collection<String> grants) {
return this;
}

/**
* Add multiple grants to the UserGroup.
*
* Grants must be of the form "ShareRoles_type,collection_URI".
*
* @param grants The grants to add
* @return the UserGroupFactory
*/
public UserGroupFactory addGrants(String... grants) {
for (String g : grants) {
userGroup.getGrants().add(g);
@@ -4,6 +4,9 @@
import java.io.IOException;
import java.net.URISyntaxException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import de.mpg.imeji.logic.config.util.PropertyReader;

/**
@@ -14,8 +17,9 @@
*/
public class TempFileUtil {

private static final Logger LOGGER = LogManager.getLogger(TempFileUtil.class);
public static final String IMEJI_TEMP_FILE_PREFIX = "imeji";
public static final File TEMP_DIR = initTempDirectory();
private static File tempDir = initTempDirectory();

/**
* Private Constructor
@@ -36,10 +40,28 @@ private static File initTempDirectory() {
}
return f;
} catch (IOException e) {
LOGGER.error("Error creating the temp directory.", e);
return null;
}
}

public static File getTempDirectory() {
return tempDir;
}

/**
* Gets the temp directory. Creates a new temp directory if none exists.
*
* @return The temp directory
*/
public static File getOrCreateTempDirectory() {
if (tempDir == null || !tempDir.exists()) {
tempDir = initTempDirectory();
}

return tempDir;
}

/**
* Create a temp {@link File}. This method should be used to ensure that all temp files are
* correctly removed
@@ -50,7 +72,7 @@ private static File initTempDirectory() {
* @throws IOException
*/
public static File createTempFile(String name, String extension) throws IOException {
return File.createTempFile(IMEJI_TEMP_FILE_PREFIX + name, extension, TEMP_DIR);
return File.createTempFile(IMEJI_TEMP_FILE_PREFIX + name, extension, tempDir);
}

}
@@ -2,8 +2,14 @@

import java.io.File;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.rules.TestRule;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;

import de.mpg.imeji.exceptions.ImejiException;
import de.mpg.imeji.logic.core.collection.CollectionService;
@@ -14,6 +20,7 @@
import de.mpg.imeji.logic.model.factory.CollectionFactory;
import de.mpg.imeji.logic.model.factory.ImejiFactory;
import de.mpg.imeji.logic.security.user.UserService;
import de.mpg.imeji.util.ConcurrencyUtil;
import de.mpg.imeji.util.ElasticsearchTestUtil;
import de.mpg.imeji.util.ImejiTestResources;
import de.mpg.imeji.util.JenaUtil;
@@ -23,9 +30,19 @@
*/
public class SuperServiceTest {

private static final Logger LOGGER = LogManager.getLogger(SuperServiceTest.class);

protected static CollectionImeji collectionBasic = null;
protected static Item item = null;

@Rule
public TestRule watcher = new TestWatcher() {
@Override
protected void starting(Description description) {
LOGGER.info("Starting test: " + description.getClassName() + "." + description.getMethodName());
}
};

@BeforeClass
public static void setup() {
ElasticsearchTestUtil.startElasticsearch();
@@ -34,6 +51,7 @@ public static void setup() {

@AfterClass
public static void tearDown() throws Exception {
ConcurrencyUtil.waitForImejiThreadsToComplete();
ElasticsearchTestUtil.stopElasticsearch();
JenaUtil.closeJena();
}
@@ -30,6 +30,7 @@
import de.mpg.imeji.rest.to.LicenseTO;
import de.mpg.imeji.rest.to.defaultItemTO.DefaultItemTO;
import de.mpg.imeji.rest.to.defaultItemTO.DefaultItemWithFileTO;
import de.mpg.imeji.util.ConcurrencyUtil;
import de.mpg.imeji.util.ElasticsearchTestUtil;
import de.mpg.imeji.util.ImejiTestResources;
import de.mpg.imeji.util.JenaUtil;
@@ -76,6 +77,7 @@ public static void setup() throws IOException, URISyntaxException {

@AfterClass
public static void shutdown() throws IOException, URISyntaxException, InterruptedException {
ConcurrencyUtil.waitForImejiThreadsToComplete();
ElasticsearchTestUtil.stopElasticsearch();
JenaUtil.closeJena();
app = null;
@@ -14,6 +14,7 @@
import de.mpg.imeji.logic.security.authorization.util.PasswordGenerator;
import de.mpg.imeji.logic.security.user.UserService;
import de.mpg.imeji.logic.util.StringHelper;
import de.mpg.imeji.util.ConcurrencyUtil;
import de.mpg.imeji.util.ElasticsearchTestUtil;
import de.mpg.imeji.util.JenaUtil;

@@ -33,6 +34,7 @@ public static void setup() {

@AfterClass
public static void tearDown() throws Exception {
ConcurrencyUtil.waitForImejiThreadsToComplete();
ElasticsearchTestUtil.stopElasticsearch();
JenaUtil.closeJena();
}
@@ -19,6 +19,7 @@
import de.mpg.imeji.logic.security.authentication.impl.APIKeyAuthentication;
import de.mpg.imeji.logic.security.user.UserService;
import de.mpg.imeji.rest.process.AdminProcess;
import de.mpg.imeji.util.ConcurrencyUtil;
import de.mpg.imeji.util.ElasticsearchTestUtil;
import de.mpg.imeji.util.JenaUtil;

@@ -43,6 +44,7 @@ public static void setup() throws ImejiException, JoseException, NoSuchAlgorithm

@AfterClass
public static void tearDown() throws Exception {
ConcurrencyUtil.waitForImejiThreadsToComplete();
ElasticsearchTestUtil.stopElasticsearch();
JenaUtil.closeJena();
}
@@ -101,6 +103,7 @@ public void loginWithAPIKey() throws ImejiException, JoseException {
public void loginWithWrongAPIKey() throws ImejiException, JoseException {
User usertest = new UserService().retrieve(JenaUtil.TEST_USER_EMAIL, Imeji.adminUser);
try {
//Throws an AuthenticationError and logs an InvalidJwtSignatureException message
AuthenticationFactory.factory(generateAPIKEYAuthenticationHeader(usertest.getApiKey() + "abc")).doLogin();
Assert.fail();
} catch (AuthenticationError e) {
@@ -280,7 +280,7 @@ public void delete() {
collectionService.create(collectionToDelete, defaultUser);
defaultUser = userService.retrieve(defaultUser.getId(), sysadmin);
Item itemToDelete = ImejiFactory.newItem(collectionToDelete);
itemService.create(itemToDelete, collectionToDelete, defaultUser);
itemService.createWithFile(itemToDelete, ImejiTestResources.getTestJpg(), "Test.jpg", collectionToDelete, defaultUser);
delete_Test("private Collection, edit grant user", collectionToDelete, userEditGrant, NotAllowedError.class);
Lock lock = new Lock(itemToDelete.getId().toString(), null);
Locks.lock(lock);
@@ -13,6 +13,7 @@

import de.mpg.imeji.exceptions.ImejiException;
import de.mpg.imeji.exceptions.NotFoundException;
import de.mpg.imeji.logic.config.Imeji;
import de.mpg.imeji.logic.core.collection.CollectionService;
import de.mpg.imeji.logic.core.content.ContentService;
import de.mpg.imeji.logic.core.item.ItemService;
@@ -26,6 +27,7 @@
import de.mpg.imeji.logic.storage.StorageController;
import de.mpg.imeji.logic.util.StorageUtils;
import de.mpg.imeji.test.logic.service.SuperServiceTest;
import de.mpg.imeji.util.ConcurrencyUtil;
import de.mpg.imeji.util.ImejiTestResources;

/**
@@ -60,6 +62,9 @@ public static void specificSetup() {
item = ImejiFactory.newItem(collection);
new ItemService().create(item, collection, defaultUser);
content = new ContentService().create(item, ImejiTestResources.getTest1Jpg(), defaultUser);

//Wait for content being completely created -> ContentService().delete() leads to errors otherwise
ConcurrencyUtil.waitForThreadsToComplete(Imeji.getCONTENT_EXTRACTION_EXECUTOR(), Imeji.getINTERNAL_STORAGE_EXECUTOR());
} catch (ImejiException e) {
LOGGER.error("Exception in setup of ContentServiceTest", e);
}
@@ -145,9 +150,12 @@ public void delete() {
}
// Restore content
content = (new ContentService()).create(item, ImejiTestResources.getTest1Jpg(), defaultUser);
Thread.sleep(50); // Wait for the content to be processed

//Wait for content being completely created -> ContentService().delete() leads to errors otherwise
ConcurrencyUtil.waitForThreadsToComplete(Imeji.getCONTENT_EXTRACTION_EXECUTOR(), Imeji.getINTERNAL_STORAGE_EXECUTOR());

content = service.retrieve(content.getId().toString());
} catch (ImejiException | InterruptedException e) {
} catch (ImejiException e) {
Assert.fail(e.getMessage());
}

0 comments on commit b99c204

Please sign in to comment.
You can’t perform that action at this time.