Skip to content

Commit

Permalink
Use @before and @after to setup and clear session in integration tests
Browse files Browse the repository at this point in the history
Some tests in TestRS were not properly initialising/clearing data and the
order of tests being run caused tests to fail. Rather than initialising
and clearing data in individual tests, refactor the code to use @before and
@after annotations to automatically perform these tasks before and after
every test ran - ensuring better test seperation.
  • Loading branch information
louise-davies committed Jan 16, 2019
1 parent f44fbe5 commit f1b1f1c
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 227 deletions.
32 changes: 13 additions & 19 deletions src/test/java/org/icatproject/integration/TestRS.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.Before;
import org.junit.After;

/**
* These tests are for those aspects that cannot be tested by the core tests. In
Expand All @@ -70,10 +72,20 @@ public static void beforeClass() throws Exception {
}
}

@Before
public void initializeSession() throws Exception {
wSession.setAuthz();
}

@After
public void clearSession() throws Exception {
wSession.clear();
wSession.clearAuthz();
}

@Ignore("Test fails because of bug in eclipselink")
@Test
public void testDistinctBehaviour() throws Exception {
wSession.clear();
ICAT icat = new ICAT(System.getProperty("serverUrl"));
Map<String, String> credentials = new HashMap<>();
credentials.put("username", "root");
Expand Down Expand Up @@ -329,7 +341,6 @@ private void checkResultFromLuceneSearch(Session session, String val, JsonArray
}

private Session setupLuceneTest() throws Exception {
wSession.setAuthz();
ICAT icat = new ICAT(System.getProperty("serverUrl"));
Map<String, String> credentials = new HashMap<>();
credentials.put("username", "notroot");
Expand All @@ -351,7 +362,6 @@ private Session setupLuceneTest() throws Exception {
System.out.println(props);

// Get known configuration
wSession.clear();
Path path = Paths.get(ClassLoader.class.getResource("/icat.port").toURI());
session.importMetaData(path, DuplicateAction.CHECK, Attributes.USER);

Expand Down Expand Up @@ -394,10 +404,8 @@ public void testGet() throws Exception {
Session session = icat.login("db", credentials);

// Get known configuration
wSession.clear();
Path path = Paths.get(ClassLoader.class.getResource("/icat.port").toURI());
session.importMetaData(path, DuplicateAction.CHECK, Attributes.USER);
wSession.setAuthz();

long fid = search(session, "Facility.id", 1).getJsonNumber(0).longValueExact();

Expand Down Expand Up @@ -428,10 +436,8 @@ public void testSearchWithNew() throws Exception {
Session session = icat.login("db", credentials);

// Get known configuration
wSession.clear();
Path path = Paths.get(ClassLoader.class.getResource("/icat.port").toURI());
session.importMetaData(path, DuplicateAction.CHECK, Attributes.USER);
wSession.setAuthz();

JsonArray array;

Expand Down Expand Up @@ -488,10 +494,8 @@ public void testSearch() throws Exception {
Session session = icat.login("db", credentials);

// Get known configuration
wSession.clear();
Path path = Paths.get(ClassLoader.class.getResource("/icat.port").toURI());
session.importMetaData(path, DuplicateAction.CHECK, Attributes.USER);
wSession.setAuthz();

JsonArray array;

Expand Down Expand Up @@ -596,7 +600,6 @@ public void testSearch() throws Exception {

@Test
public void authzForUpdateAttribute() throws Exception {
wSession.setAuthz();
Session session = createAndPopulate();

// Just start with Facility, two InvestigationTypes and Investigation
Expand Down Expand Up @@ -738,7 +741,6 @@ public void authzForUpdateAttribute() throws Exception {

@Test
public void authzForUpdate() throws Exception {
wSession.setAuthz();
Session session = createAndPopulate();

// Make sure that fetching a non-id Double gives no problems
Expand Down Expand Up @@ -1249,18 +1251,15 @@ public void testWriteBad() throws Exception {
}

private Session createAndPopulate() throws Exception {
wSession.setAuthz();
ICAT icat = new ICAT(System.getProperty("serverUrl"));
Map<String, String> credentials = new HashMap<>();
credentials.put("username", "notroot");
credentials.put("password", "password");
Session session = icat.login("db", credentials);

// Get known configuration
wSession.clear();
Path path = Paths.get(ClassLoader.class.getResource("/icat.port").toURI());
session.importMetaData(path, DuplicateAction.CHECK, Attributes.USER);
wSession.setAuthz();

return session;
}
Expand Down Expand Up @@ -1381,7 +1380,6 @@ public void exportMetaDataDumpRoot() throws Exception {
}

private void exportMetaDataDump(Map<String, String> credentials) throws Exception {
wSession.clear();
ICAT icat = new ICAT(System.getProperty("serverUrl"));
Session session = icat.login("db", credentials);
Path path = Paths.get(ClassLoader.class.getResource("/icat.port").toURI());
Expand All @@ -1393,7 +1391,6 @@ private void exportMetaDataDump(Map<String, String> credentials) throws Exceptio

// Get known configuration
rootSession.importMetaData(path, DuplicateAction.CHECK, Attributes.ALL);
wSession.setAuthz();

Path dump1 = Files.createTempFile("dump1", ".tmp");
Path dump2 = Files.createTempFile("dump2", ".tmp");
Expand All @@ -1419,7 +1416,6 @@ private void exportMetaDataDump(Map<String, String> credentials) throws Exceptio
@Ignore("Test fails - appears brittle to differences in timezone")
@Test
public void exportMetaDataQuery() throws Exception {
wSession.clear();
ICAT icat = new ICAT(System.getProperty("serverUrl"));
Map<String, String> credentials = new HashMap<>();
credentials.put("username", "root");
Expand All @@ -1429,7 +1425,6 @@ public void exportMetaDataQuery() throws Exception {

// Get known configuration
session.importMetaData(path, DuplicateAction.CHECK, Attributes.ALL);
wSession.setAuthz();

Path dump = Files.createTempFile("dump1", ".tmp");
start = System.currentTimeMillis();
Expand Down Expand Up @@ -1471,7 +1466,6 @@ public void importMetaDataAllNotRoot() throws Exception {
}

private void importMetaData(Attributes attributes, String userName) throws Exception {
wSession.clear();
ICAT icat = new ICAT(System.getProperty("serverUrl"));
Map<String, String> credentials = new HashMap<>();
credentials.put("username", "root");
Expand Down
Loading

0 comments on commit f1b1f1c

Please sign in to comment.