diff --git a/datamover_console/sourceTest/java/ch/systemsx/cisd/datamover/console/server/ConfigParametersTest.java b/datamover_console/sourceTest/java/ch/systemsx/cisd/datamover/console/server/ConfigParametersTest.java index 93dd5557cad4ec3e07595d13afc13b5d5c630c1e..a72df43aed7ac96fba55a83208b7c5a9eb976182 100644 --- a/datamover_console/sourceTest/java/ch/systemsx/cisd/datamover/console/server/ConfigParametersTest.java +++ b/datamover_console/sourceTest/java/ch/systemsx/cisd/datamover/console/server/ConfigParametersTest.java @@ -24,19 +24,21 @@ import static ch.systemsx.cisd.datamover.console.server.ConfigParameters.WORKING import static org.testng.AssertJUnit.assertEquals; import static org.testng.AssertJUnit.fail; +import java.io.File; import java.util.Properties; import org.testng.annotations.Test; import ch.rinn.restrictions.Friend; import ch.systemsx.cisd.common.exceptions.ConfigurationFailureException; +import ch.systemsx.cisd.common.utilities.FileUtilities; /** + * Test cases for corresponding {@link ConfigParameters} class. * - * * @author Franz-Josef Elmer */ -@Friend(toClasses=ConfigParameters.class) +@Friend(toClasses = ConfigParameters.class) public class ConfigParametersTest { @Test @@ -51,7 +53,7 @@ public class ConfigParametersTest assertEquals("Given key '" + TARGETS + "' not found in properties '[]'", e.getMessage()); } } - + @Test public void testMissingDatamovers() { @@ -68,7 +70,7 @@ public class ConfigParametersTest + ", 1." + LOCATION + "]'", e.getMessage()); } } - + @Test public void testEmptyDatamovers() { @@ -82,11 +84,10 @@ public class ConfigParametersTest fail("ConfigurationFailureException expected"); } catch (ConfigurationFailureException e) { - assertEquals("Property '" + DATAMOVERS + "' is an empty string.", - e.getMessage()); + assertEquals("Property '" + DATAMOVERS + "' is an empty string.", e.getMessage()); } } - + @Test public void testEmptyTargets() { @@ -99,11 +100,10 @@ public class ConfigParametersTest fail("ConfigurationFailureException expected"); } catch (ConfigurationFailureException e) { - assertEquals("Property '" + TARGETS + "' is an empty string.", - e.getMessage()); + assertEquals("Property '" + TARGETS + "' is an empty string.", e.getMessage()); } } - + @Test public void testProperConfigParameters() { @@ -115,12 +115,14 @@ public class ConfigParametersTest properties.setProperty("dm1." + WORKING_DIRECTORY, "wd1"); properties.setProperty("dm2." + WORKING_DIRECTORY, "wd2"); ConfigParameters configParameters = new ConfigParameters(properties); - + assertEquals(60000, configParameters.getRefreshTimeInterval()); assertEquals("{t1=/target1, t2=/target2}", configParameters.getTargets().toString()); - assertEquals("{dm1=wd1, dm2=wd2}", configParameters.getDatamoversWorkingDirectories().toString()); + assertEquals("{dm1=" + FileUtilities.getCanonicalPath(new File("wd1")) + ", dm2=" + + FileUtilities.getCanonicalPath(new File("wd2")) + "}", configParameters + .getDatamoversWorkingDirectories().toString()); } - + @Test public void testProperConfigParametersWithRefreshTimeInterval() { @@ -131,7 +133,7 @@ public class ConfigParametersTest properties.setProperty(DATAMOVERS, "dm"); properties.setProperty("dm." + WORKING_DIRECTORY, "wd"); ConfigParameters configParameters = new ConfigParameters(properties); - + assertEquals(10000, configParameters.getRefreshTimeInterval()); } } diff --git a/datamover_console/sourceTest/java/ch/systemsx/cisd/datamover/console/server/DatamoverConsolerServiceTest.java b/datamover_console/sourceTest/java/ch/systemsx/cisd/datamover/console/server/DatamoverConsolerServiceTest.java index 85604c9aaa634243d1810aa1ffc33295e5b1b063..4018a7094ac8e3595709c9a4efe494688910dcb5 100644 --- a/datamover_console/sourceTest/java/ch/systemsx/cisd/datamover/console/server/DatamoverConsolerServiceTest.java +++ b/datamover_console/sourceTest/java/ch/systemsx/cisd/datamover/console/server/DatamoverConsolerServiceTest.java @@ -20,6 +20,7 @@ import static org.testng.AssertJUnit.assertEquals; import static org.testng.AssertJUnit.assertSame; import static org.testng.AssertJUnit.fail; +import java.io.File; import java.util.List; import java.util.Map; import java.util.Properties; @@ -37,6 +38,7 @@ import ch.rinn.restrictions.Friend; import ch.systemsx.cisd.authentication.IAuthenticationService; import ch.systemsx.cisd.authentication.Principal; import ch.systemsx.cisd.common.servlet.IRequestContextProvider; +import ch.systemsx.cisd.common.utilities.FileUtilities; import ch.systemsx.cisd.datamover.console.client.EnvironmentFailureException; import ch.systemsx.cisd.datamover.console.client.IDatamoverConsoleService; import ch.systemsx.cisd.datamover.console.client.InvalidSessionException; @@ -46,26 +48,32 @@ import ch.systemsx.cisd.datamover.console.client.dto.DatamoverStatus; import ch.systemsx.cisd.datamover.console.client.dto.User; /** - * - * * @author Franz-Josef Elmer */ -@Friend(toClasses=ConfigParameters.class) +@Friend(toClasses = ConfigParameters.class) public class DatamoverConsolerServiceTest { private static final int EXPIRATION_TIME = 42; private static final String LOGGED_OUT_MESSAGE = "You are not logged in or your session has expired. Please log in."; - + private Mockery context; + private IAuthenticationService authenticationService; + private IRequestContextProvider requestContextProvider; + private IDatamoverConsoleFactory consoleFactory; + private IConsoleActionLog actionLog; + private HttpServletRequest request; + private HttpSession session; + private IDatamoverConsole dm1; + private IDatamoverConsole dm2; @BeforeMethod @@ -81,7 +89,7 @@ public class DatamoverConsolerServiceTest dm1 = context.mock(IDatamoverConsole.class, "datamover 1"); dm2 = context.mock(IDatamoverConsole.class, "datamover 2"); } - + @AfterMethod public final void afterMethod() { @@ -89,33 +97,33 @@ public class DatamoverConsolerServiceTest // Otherwise one do not known which test failed. context.assertIsSatisfied(); } - + @Test public void testGetApplicationInfo() { context.checking(new Expectations() - { { - one(dm1).check(); - one(dm2).check(); - } - }); + { + one(dm1).check(); + one(dm2).check(); + } + }); ApplicationInfo applicationInfo = createService().getApplicationInfo(); assertEquals(42000, applicationInfo.getRefreshTimeInterval()); - + context.assertIsSatisfied(); } - + @Test public void testTryToGetCurrentUserUndefined() { prepareForNotAuthenticated(); - + assertEquals(null, createService().tryToGetCurrentUser()); - + context.assertIsSatisfied(); } - + @Test public void testTryToGetCurrentUser() { @@ -128,12 +136,12 @@ public class DatamoverConsolerServiceTest will(returnValue(user)); } }); - + assertSame(user, createService().tryToGetCurrentUser()); - + context.assertIsSatisfied(); } - + @Test public void testTryToLoginForInvalidApplication() { @@ -145,50 +153,50 @@ public class DatamoverConsolerServiceTest one(authenticationService).authenticateApplication(); will(returnValue(null)); - + one(actionLog).logFailedLoginAttempt("u"); } }); - + try { createService().tryToLogin("u", "p"); } catch (EnvironmentFailureException e) { - assertEquals("User 'u' failed to authenticate: application not authenticated.", - e.getMessage()); + assertEquals("User 'u' failed to authenticate: application not authenticated.", e + .getMessage()); } - + context.assertIsSatisfied(); } - + @Test public void testTryToLoginWithInvalidCredentials() { final String userName = "user"; final String password = "passwd"; context.checking(new Expectations() - { { - one(dm1).check(); - one(dm2).check(); - - one(authenticationService).authenticateApplication(); - String aToken = "atoken"; - will(returnValue(aToken)); - - one(authenticationService).authenticateUser(aToken, userName, password); - will(returnValue(false)); - - one(actionLog).logFailedLoginAttempt(userName); - } - }); - + { + one(dm1).check(); + one(dm2).check(); + + one(authenticationService).authenticateApplication(); + String aToken = "atoken"; + will(returnValue(aToken)); + + one(authenticationService).authenticateUser(aToken, userName, password); + will(returnValue(false)); + + one(actionLog).logFailedLoginAttempt(userName); + } + }); + assertEquals(null, createService().tryToLogin(userName, password)); - + context.assertIsSatisfied(); } - + @Test public void testTryToLoginWithValidCredentials() { @@ -201,38 +209,38 @@ public class DatamoverConsolerServiceTest { one(dm1).check(); one(dm2).check(); - + one(authenticationService).authenticateApplication(); String aToken = "atoken"; will(returnValue(aToken)); - + one(authenticationService).authenticateUser(aToken, userName, password); will(returnValue(true)); - + one(authenticationService).getPrincipal(aToken, userName); will(returnValue(new Principal("u", "John", "Doe", "jd@u.a"))); - + one(requestContextProvider).getHttpServletRequest(); will(returnValue(request)); - + one(request).getSession(true); will(returnValue(session)); - + one(session).setMaxInactiveInterval(EXPIRATION_TIME * 60); one(session).setAttribute(DatamoverConsoleService.SESSION_USER, user); - + one(actionLog).logSuccessfulLogin(); } }); - + final User actualUser = createService().tryToLogin(userName, password); assertEquals("u", actualUser.getUserCode()); assertEquals("John Doe", actualUser.getUserFullName()); assertEquals("jd@u.a", actualUser.getEmail()); - + context.assertIsSatisfied(); } - + @Test public void testLogout() { @@ -243,22 +251,22 @@ public class DatamoverConsolerServiceTest one(session).invalidate(); } }); - + createService().logout(); - + context.assertIsSatisfied(); } - + @Test public void testLogoutWithoutSession() { prepareForNotAuthenticated(); - + createService().logout(); - + context.assertIsSatisfied(); } - + @Test public void testListDatamoverInfos() { @@ -278,9 +286,9 @@ public class DatamoverConsolerServiceTest will(returnValue(DatamoverStatus.SHUTDOWN)); } }); - + List<DatamoverInfo> infos = service.listDatamoverInfos(); - + assertEquals(2, infos.size()); DatamoverInfo info1 = infos.get(0); assertEquals("dm1", info1.getName()); @@ -290,10 +298,10 @@ public class DatamoverConsolerServiceTest assertEquals("dm2", info2.getName()); assertEquals("target1", info2.getTargetLocation()); assertEquals(DatamoverStatus.SHUTDOWN, info2.getStatus()); - + context.assertIsSatisfied(); } - + @Test public void testListDatamoverInfoWithError() { @@ -316,37 +324,38 @@ public class DatamoverConsolerServiceTest will(returnValue(DatamoverStatus.IDLE)); } }); - + final List<DatamoverInfo> infos = service.listDatamoverInfos(); - + assertEquals(2, infos.size()); DatamoverInfo info1 = infos.get(0); assertEquals("dm1", info1.getName()); assertEquals("first target", info1.getTargetLocation()); assertEquals(DatamoverStatus.ERROR, info1.getStatus()); assertEquals(msg, info1.getErrorMessage()); - + context.assertIsSatisfied(); } - + @Test public void testGetTargets() { prepareForAuthenticated(); IDatamoverConsoleService service = createService(); Map<String, String> targets = service.getTargets(); - - assertEquals("{t1=target1, t2=target2}", targets.toString()); - + + assertEquals("{t1=" + FileUtilities.getCanonicalPath(new File("target1")) + ", t2=" + + FileUtilities.getCanonicalPath(new File("target2")) + "}", targets.toString()); + context.assertIsSatisfied(); } - + @Test public void testListDatamoverInfosNotAuthenticated() { prepareForNotAuthenticated(); IDatamoverConsoleService service = createService(); - + try { service.listDatamoverInfos(); @@ -355,16 +364,16 @@ public class DatamoverConsolerServiceTest { assertEquals(LOGGED_OUT_MESSAGE, e.getMessage()); } - + context.assertIsSatisfied(); } - + @Test public void testGetTargetsNotAuthenticated() { prepareForNotAuthenticated(); IDatamoverConsoleService service = createService(); - + try { service.getTargets(); @@ -373,10 +382,10 @@ public class DatamoverConsolerServiceTest { assertEquals(LOGGED_OUT_MESSAGE, e.getMessage()); } - + context.assertIsSatisfied(); } - + @Test public void testStartDatamover() { @@ -390,30 +399,30 @@ public class DatamoverConsolerServiceTest one(actionLog).logStartDatamover(datamoverName, location); } }); - + IDatamoverConsoleService service = createService(); service.startDatamover(datamoverName, location); - + context.assertIsSatisfied(); } - + @Test public void testStartUnknownDatamover() { prepareForAuthenticated(); - + IDatamoverConsoleService service = createService(); service.startDatamover("unknown", "new location"); - + context.assertIsSatisfied(); } - + @Test public void testStartDatamoverNotAuthenticated() { prepareForNotAuthenticated(); IDatamoverConsoleService service = createService(); - + try { service.startDatamover("dm1", "l2"); @@ -422,10 +431,10 @@ public class DatamoverConsolerServiceTest { assertEquals(LOGGED_OUT_MESSAGE, e.getMessage()); } - + context.assertIsSatisfied(); } - + @Test public void testShutdownDatamover() { @@ -438,30 +447,30 @@ public class DatamoverConsolerServiceTest one(actionLog).logShutdownDatamover(datamoverName); } }); - + IDatamoverConsoleService service = createService(); service.shutdownDatamover(datamoverName); - + context.assertIsSatisfied(); } - + @Test public void testShutdownUnknownDatamover() { prepareForAuthenticated(); - + IDatamoverConsoleService service = createService(); service.shutdownDatamover("unknown"); - + context.assertIsSatisfied(); } - + @Test public void testShutdownDatamoverNotAuthenticated() { prepareForNotAuthenticated(); IDatamoverConsoleService service = createService(); - + try { service.shutdownDatamover("dm2"); @@ -470,10 +479,10 @@ public class DatamoverConsolerServiceTest { assertEquals(LOGGED_OUT_MESSAGE, e.getMessage()); } - + context.assertIsSatisfied(); } - + private void prepareForAuthenticated() { context.checking(new Expectations() @@ -481,41 +490,43 @@ public class DatamoverConsolerServiceTest { one(dm1).check(); one(dm2).check(); - + one(requestContextProvider).getHttpServletRequest(); will(returnValue(request)); - + one(request).getSession(false); will(returnValue(session)); } }); } - + private void prepareForNotAuthenticated() { context.checking(new Expectations() - { { - one(dm1).check(); - one(dm2).check(); - - one(requestContextProvider).getHttpServletRequest(); - will(returnValue(request)); - - one(request).getSession(false); - will(returnValue(null)); - } - }); + { + one(dm1).check(); + one(dm2).check(); + + one(requestContextProvider).getHttpServletRequest(); + will(returnValue(request)); + + one(request).getSession(false); + will(returnValue(null)); + } + }); } - + private IDatamoverConsoleService createService() { context.checking(new Expectations() { { - one(consoleFactory).create("dm1", "wd1"); + one(consoleFactory).create("dm1", + FileUtilities.getCanonicalPath(new File("wd1"))); will(returnValue(dm1)); - one(consoleFactory).create("dm2", "wd2"); + one(consoleFactory).create("dm2", + FileUtilities.getCanonicalPath(new File("wd2"))); will(returnValue(dm2)); } }); @@ -534,5 +545,4 @@ public class DatamoverConsolerServiceTest service.setSessionExpirationPeriodInMinutes(EXPIRATION_TIME); return service; } - }