This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository coselmar. See http://git.codelutin.com/coselmar.git commit 0eaf8f5e894f766dc6e4cafd35efe526321ad447 Author: Yannick Martel <martel@©odelutin.com> Date: Tue Nov 18 18:13:26 2014 +0100 prepare user account creation and login operrations --- .../errors/InvalidCredentialException.java | 11 + .../services/AbstractCoselmarWebServiceTest.java | 92 +++++++++ .../services/FakeCoselmarApplicationContext.java | 224 +++++++++++++++++++++ .../services/FakeCoselmarServicesContext.java | 51 +++++ .../coselmar/services/UsersWebServiceTest.java | 46 +++++ 5 files changed, 424 insertions(+) diff --git a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/errors/InvalidCredentialException.java b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/errors/InvalidCredentialException.java new file mode 100644 index 0000000..a0c89eb --- /dev/null +++ b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/errors/InvalidCredentialException.java @@ -0,0 +1,11 @@ +package fr.ifremer.coselmar.services.errors; + +/** + * @author ymartel (martel@codelutin.com) + */ +public class InvalidCredentialException extends Exception { + + public InvalidCredentialException(String message) { + super(message); + } +} diff --git a/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/AbstractCoselmarWebServiceTest.java b/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/AbstractCoselmarWebServiceTest.java new file mode 100644 index 0000000..b15688a --- /dev/null +++ b/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/AbstractCoselmarWebServiceTest.java @@ -0,0 +1,92 @@ +package fr.ifremer.coselmar.services; + +import java.io.File; +import java.io.IOException; +import java.util.Locale; + +import fr.ifremer.coselmar.persistence.CoselmarPersistenceContext; +import org.apache.commons.logging.Log; +import org.debux.webmotion.unittest.WebMotionTest; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.nuiton.util.DateUtil; + +import static org.apache.commons.logging.LogFactory.getLog; + +/** + * @author ymartel <martel@codelutin.com> + */ +public class AbstractCoselmarWebServiceTest extends WebMotionTest { + + private static final Log log = getLog(AbstractCoselmarWebServiceTest.class); + + @Rule + public final FakeCoselmarApplicationContext application = new FakeCoselmarApplicationContext("coselmar-services.properties"); + + @Override + protected int getPort() { + return application.getPort(); + } + + @Before + public void startServer() throws Exception { + + CoselmarServicesApplicationContext applicationContext = + new CoselmarServicesApplicationContext(application.getApplicationConfig(), + application.getTopiaApplicationContext()) { + + @Override + public CoselmarServicesContext newServiceContext(CoselmarPersistenceContext persistenceContext, Locale locale) { + + FakeCoselmarServicesContext servicesContext = FakeCoselmarServicesContext.newServiceContext( + DateUtil.createDate(1, 1, 2014), + Locale.FRANCE, + application.getApplicationConfig(), + application.getTopiaApplicationContext(), + application.newPersistenceContext()); + return servicesContext; + + } + + }; + + applicationContext.init(); + + CoselmarServicesApplicationContext.setApplicationContext(applicationContext); + + super.startServer(); + + } + + @Override + protected String getServerBaseDirectory() { + + return new File(application.getTestBasedir(), + "tomcat_" + application.getPort()).getAbsolutePath(); + + } + + @After + public void stopServer() throws Exception { + + if (log.isTraceEnabled()) { + log.trace("closing application context " + application); + } + + application.close(); + server.stop(); + server.destroy(); + + } + + protected void showTestResult(String content) throws IOException { + + String testName = application.getMethodName(); + + if (log.isInfoEnabled()) { + log.info("test *" + testName + "* result\n" + content); + } + + } +} diff --git a/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/FakeCoselmarApplicationContext.java b/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/FakeCoselmarApplicationContext.java new file mode 100644 index 0000000..50509f9 --- /dev/null +++ b/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/FakeCoselmarApplicationContext.java @@ -0,0 +1,224 @@ +package fr.ifremer.coselmar.services; + +import java.io.File; +import java.io.IOException; +import java.net.ServerSocket; +import java.util.LinkedList; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Properties; +import java.util.concurrent.atomic.AtomicInteger; + +import fr.ifremer.coselmar.persistence.CoselmarPersistenceContext; +import fr.ifremer.coselmar.persistence.CoselmarTopiaApplicationContext; +import fr.ifremer.coselmar.persistence.CoselmarTopiaPersistenceContext; +import fr.ifremer.coselmar.services.config.CoselmarServicesConfig; +import fr.ifremer.coselmar.services.config.CoselmarServicesConfigOption; +import org.apache.commons.logging.Log; +import org.junit.rules.TestWatcher; +import org.junit.runner.Description; +import org.nuiton.topia.junit.ConfigurationHelper; +import org.nuiton.util.DateUtil; + +import static org.apache.commons.logging.LogFactory.getLog; + + +/** + * @author ymartel <martel@codelutin.com> + */ +public class FakeCoselmarApplicationContext extends TestWatcher implements CoselmarApplicationContext { + + private static Log log = getLog(FakeCoselmarApplicationContext.class); + + protected static AtomicInteger portNumberCounter = new AtomicInteger(9999); + + protected File testBasedir; + + protected List<CoselmarTopiaPersistenceContext> openedTransactions = new LinkedList<>(); + + protected CoselmarTopiaApplicationContext applicationContext; + + protected CoselmarServicesConfig configuration; + + protected String methodName; + + protected int currentPortNumber; + + protected final String configurationPath; + + public FakeCoselmarApplicationContext(String configurationPath) { + this.configurationPath = configurationPath; + } + + @Override + protected void starting(Description description) { + + // get an available port + currentPortNumber = getAvailablePort(); + if (log.isDebugEnabled()) { + log.debug("Using port: " + currentPortNumber); + } + + methodName = description.getMethodName(); + + // get test directory + testBasedir = ConfigurationHelper.getTestSpecificDirectory( + description.getTestClass(), + description.getMethodName()); + + if (log.isDebugEnabled()) { + log.debug("testBasedir: " + testBasedir); + } + + init(); + + } + + @Override + public void finished(Description description) { + + close(); + } + + @Override + public void init() { + + // --- create configuration --- // + + Properties defaultvalues = new Properties(); + defaultvalues.put(CoselmarServicesConfigOption.DATA_DIRECTORY.getKey(), testBasedir.getAbsolutePath()); + + configuration = new CoselmarServicesConfig(configurationPath, defaultvalues); + + // --- create topia application context --- // + + Map<String, String> topiaProperties = configuration.getTopiaProperties(); + applicationContext = new CoselmarTopiaApplicationContext(topiaProperties); + + } + + @Override + public void close() { + + if (applicationContext != null && !applicationContext.isClosed()) { + + for (CoselmarTopiaPersistenceContext openedTransaction : openedTransactions) { + + if (log.isTraceEnabled()) { + log.trace("closing transaction " + openedTransaction); + } + + if (!openedTransaction.isClosed()) { + + openedTransaction.close(); + + } + + } + + if (log.isTraceEnabled()) { + log.trace("closing transaction " + applicationContext); + } + + applicationContext.close(); + + } + } + + @Override + public CoselmarTopiaApplicationContext getTopiaApplicationContext() { + return applicationContext; + } + + @Override + public CoselmarServicesConfig getApplicationConfig() { + return configuration; + } + + @Override + public CoselmarTopiaPersistenceContext newPersistenceContext() { + + CoselmarTopiaPersistenceContext persistenceContext; + + persistenceContext = applicationContext.newPersistenceContext(); + + if (log.isTraceEnabled()) { + log.trace("opened transaction " + persistenceContext); + } + + openedTransactions.add(persistenceContext); + + return persistenceContext; + + } + + @Override + public FakeCoselmarServicesContext newServiceContext(CoselmarPersistenceContext persistenceContext, Locale locale) { + + FakeCoselmarServicesContext serviceContext = FakeCoselmarServicesContext.newServiceContext( + DateUtil.createDate(1, 1, 2014), + Locale.FRANCE, + getApplicationConfig(), + getTopiaApplicationContext(), + newPersistenceContext()); + return serviceContext; + + } + + public File getTestBasedir() { + return testBasedir; + } + + public int getPort() { + return currentPortNumber; + } + + public String getMethodName() { + return methodName; + } + + protected int getAvailablePort() { + + int port = portNumberCounter.getAndIncrement(); + + boolean portTaken = false; + ServerSocket socket = null; + + try { + + socket = new ServerSocket(port); + + } catch (IOException e) { + + portTaken = true; + + } finally { + + if (socket != null) + + try { + + socket.close(); + + } catch (IOException e) { + + if (log.isDebugEnabled()) { + log.debug("Already used port: " + port); + } + + } + + } + + if (portTaken) { + + port = getAvailablePort(); + + } + + return port; + + } + +} diff --git a/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/FakeCoselmarServicesContext.java b/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/FakeCoselmarServicesContext.java new file mode 100644 index 0000000..d7af8a8 --- /dev/null +++ b/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/FakeCoselmarServicesContext.java @@ -0,0 +1,51 @@ +package fr.ifremer.coselmar.services; + +import java.util.Date; +import java.util.Locale; + +import com.google.common.base.Preconditions; +import fr.ifremer.coselmar.persistence.CoselmarTopiaApplicationContext; +import fr.ifremer.coselmar.persistence.CoselmarTopiaPersistenceContext; +import fr.ifremer.coselmar.services.config.CoselmarServicesConfig; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * @author ymartel <martel@codelutin.com> + */ +public class FakeCoselmarServicesContext extends DefaultCoselmarServicesContext { + + private static final Log log = + LogFactory.getLog(FakeCoselmarServicesContext.class); + + protected Date date; + + + public static FakeCoselmarServicesContext newServiceContext(Date now, + Locale locale, + CoselmarServicesConfig serviceConfig, + CoselmarTopiaApplicationContext applicationcontext, + CoselmarTopiaPersistenceContext persistenceContext) { + + FakeCoselmarServicesContext serviceContext = new FakeCoselmarServicesContext(); + serviceContext.setPersistenceContext(persistenceContext); + serviceContext.setCoselmarServicesConfig(serviceConfig); + serviceContext.setTopiaApplicationContext(applicationcontext); + serviceContext.setLocale(locale); + serviceContext.setDate(now); + return serviceContext; + } + + @Override + public Date getNow() { + Preconditions.checkState(date != null, "you must provide a date before running service test"); + if (log.isTraceEnabled()) { + log.trace("injecting fake date in service: " + date); + } + return date; + } + + public void setDate(Date date) { + this.date = date; + } +} diff --git a/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/UsersWebServiceTest.java b/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/UsersWebServiceTest.java new file mode 100644 index 0000000..cb50372 --- /dev/null +++ b/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/UsersWebServiceTest.java @@ -0,0 +1,46 @@ +package fr.ifremer.coselmar.services; + +import java.util.Locale; + +import org.apache.http.client.fluent.Request; +import org.apache.http.client.fluent.Response; +import org.junit.Assert; +import org.junit.Test; + +/** + * @author ymartel <martel@codelutin.com> + */ +public class UsersWebServiceTest extends AbstractCoselmarWebServiceTest { + + protected FakeCoselmarServicesContext serviceContext; + + protected FakeCoselmarServicesContext getServiceContext() { + + if (serviceContext == null) { + serviceContext = application.newServiceContext(application.newPersistenceContext(), Locale.FRANCE); + } + + return serviceContext; + } + + @Test + public void testAuthentication() throws Exception { + + Request addUserRequest = createRequest("/v1/users") + .addParameter("user", "{firstName: 'test', name: 'her', mail: 'test@test.org', role: 'supervisor', qualification: 'unit tester', password : 'iamatester'}") + .Post(); + + String content = addUserRequest.execute().returnContent().asString(); + showTestResult(content); + Assert.assertNotNull(content); + + Request loginRequest = createRequest("/v1/login") + .addParameter("mail", "test@test.org") + .addParameter("password", "iamatester") + .Post(); + + Response loginResponse = loginRequest.execute(); + Assert.assertEquals(200, loginResponse.returnResponse().getStatusLine().getStatusCode()); + + } +} -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.