### Question:
ServerFactoryModule extends AbstractModule { @Provides @Singleton public OpushServer createServer(Injector injector) { return new LateInjectionServer(injector, serverConfiguration); } ServerFactoryModule(ServerConfiguration serverConfiguration); @Provides @Singleton OpushServer createServer(Injector injector);  }### Answer:
@Test public void printNoLogWhenUpToDate() throws SQLException { StatusSummary status = StatusSummary.status(Status.UP_TO_DATE).build(); expect(databaseConnectionProvider.getConnection()).andReturn(databaseConnection); expect(cassandraSchemaService.getStatus()).andReturn(status); expect(injector.getInstance(OpushJettyServerFactory.class)).andReturn(jettyFactory); expect(jettyFactory.buildServer( serverConfiguration.port(), serverConfiguration.threadPoolSize(), serverConfiguration.selectorCount())) .andReturn(opushServer); mocks.replay(); new ServerFactoryModule.LateInjectionServer(injector, serverConfiguration).createServer(); mocks.verify(); }
@Test public void printLogWhenUpdateAvailable() throws SQLException { StatusSummary status = StatusSummary.status(Status.UPGRADE_AVAILABLE).build(); expect(databaseConnectionProvider.getConnection()).andReturn(databaseConnection); expect(cassandraSchemaService.getStatus()).andReturn(status); expect(injector.getInstance(OpushJettyServerFactory.class)).andReturn(jettyFactory); expect(jettyFactory.buildServer( serverConfiguration.port(), serverConfiguration.threadPoolSize(), serverConfiguration.selectorCount())) .andReturn(opushServer); logger.warn("Cassandra schema not up-to-date, update is advised"); expectLastCall(); mocks.replay(); new ServerFactoryModule.LateInjectionServer(injector, serverConfiguration).createServer(); mocks.verify(); }
@Test public void printLogWhenUpdateRequired() throws SQLException { StatusSummary status = StatusSummary.status(Status.UPGRADE_REQUIRED).build(); expect(databaseConnectionProvider.getConnection()).andReturn(databaseConnection); expect(cassandraSchemaService.getStatus()).andReturn(status); expect(injector.getInstance(NoopServer.class)).andReturn(noopServer); logger.error("Cassandra schema too old, starting administration services only"); expectLastCall(); mocks.replay(); new ServerFactoryModule.LateInjectionServer(injector, serverConfiguration).createServer(); mocks.verify(); }
@Test public void printLogWhenNoSchema() throws SQLException { StatusSummary status = StatusSummary.status(Status.NOT_INITIALIZED).build(); expect(databaseConnectionProvider.getConnection()).andReturn(databaseConnection); expect(cassandraSchemaService.getStatus()).andReturn(status); expect(injector.getInstance(NoopServer.class)).andReturn(noopServer); logger.error("Cassandra schema not installed, starting administration services only"); expectLastCall(); mocks.replay(); new ServerFactoryModule.LateInjectionServer(injector, serverConfiguration).createServer(); mocks.verify(); }
@Test public void printLogWhenExecutionError() throws SQLException { StatusSummary status = StatusSummary.status(Status.EXECUTION_ERROR).message("expected message").build(); expect(databaseConnectionProvider.getConnection()).andReturn(databaseConnection); expect(cassandraSchemaService.getStatus()).andReturn(status); expect(injector.getInstance(NoopServer.class)).andReturn(noopServer); logger.error("{}, starting administration services only", "expected message"); expectLastCall(); mocks.replay(); new ServerFactoryModule.LateInjectionServer(injector, serverConfiguration).createServer(); mocks.verify(); }
@Test(expected=RuntimeException.class) public void printLogAndTriggerExceptionWhenNoDBConnection() throws SQLException { expect(databaseConnectionProvider.getConnection()).andThrow(new SQLException("reason")); logger.error("Cannot get database connection, verify your configuration then restart opush"); expectLastCall(); mocks.replay(); try { new ServerFactoryModule.LateInjectionServer(injector, serverConfiguration).createServer(); } finally { mocks.verify(); } }