FirstRunContainerLifecycleListener implements ContainerLifecycleListener { @Override public void onStartup(final Container container) { Boolean firstRun = servletConfig.getBoolean(Config.FIRST_RUN, false); if (!firstRun) { bootstrapApplication(); servletConfig.addProperty(Config.FIRST_RUN, true); } } @Inject  FirstRunContainerLifecycleListener(
            final SessionFactory sessionFactory,
            @Named(ServletConfigFactory.GROUP_NAME)
            final Configuration servletConfig,
            final DatabaseMigrationState migrationState); @Override void onStartup(final Container container); @Override void onReload(final Container container); @Override void onShutdown(final Container container);  }
@Test public void assertOnlyRunOnce() { Properties p = new Properties(); p.put(Config.FIRST_RUN, true); Configuration test = new MapConfiguration(p); SessionFactory mockFactory = Mockito.mock(SessionFactory.class); Container mockContainer = Mockito.mock(Container.class); ContainerLifecycleListener l = new FirstRunContainerLifecycleListener( mockFactory, test, new DatabaseMigrationState()); l.onStartup(mockContainer); Mockito.verifyNoMoreInteractions(mockFactory); Mockito.verifyNoMoreInteractions(mockContainer); }
@Test public void assertBootstrapSuccessful() { Configuration testConfig = new HibernateConfiguration( getSessionFactory(), ServletConfigFactory.GROUP_NAME); Container mockContainer = Mockito.mock(Container.class); ContainerLifecycleListener l = new FirstRunContainerLifecycleListener( getSessionFactory(), testConfig, new DatabaseMigrationState()); l.onStartup(mockContainer); Session s = getSession(); String appId = testConfig.getString(Config.APPLICATION_ID); BigInteger appByte = IdUtil.fromString(appId); assertNotNull(appByte); Application application = s.get(Application.class, appByte); assertNotNull(application); String clientId = testConfig.getString(Config.APPLICATION_CLIENT_ID); BigInteger clientByte = IdUtil.fromString(clientId); assertNotNull(clientByte); Client client = s.get(Client.class, clientByte); assertNotNull(client); String adminId = testConfig.getString(Config.APPLICATION_ADMIN_ID); BigInteger adminByte = IdUtil.fromString(adminId); assertNotNull(adminByte); User user = s.get(User.class, adminByte); assertNotNull(user); testConfig.clear(); }