NinjaPropertiesImplTool { public static void checkThatApplicationSecretIsSet( boolean isProd, String baseDirWithoutTrailingSlash, PropertiesConfiguration defaultConfiguration, Configuration compositeConfiguration) { String applicationSecret = compositeConfiguration.getString(NinjaConstant.applicationSecret); if (applicationSecret == null || applicationSecret.isEmpty()) { if (isProd) { String errorMessage = "Fatal error. Key application.secret not set. Please fix that."; logger.error(errorMessage); throw new RuntimeException(errorMessage); } logger.info("Key application.secret not set. Generating new one and setting in conf/application.conf."); String secret = SecretGenerator.generateSecret(); compositeConfiguration.setProperty(NinjaConstant.applicationSecret, secret); defaultConfiguration.setProperty(NinjaConstant.applicationSecret, secret); try { String pathToApplicationConfInSrcDir = baseDirWithoutTrailingSlash + File.separator + "src" + File.separator + "main" + File.separator + "java" + File.separator + NinjaProperties.CONF_FILE_LOCATION_BY_CONVENTION; Files.createParentDirs(new File(pathToApplicationConfInSrcDir)); defaultConfiguration.save(pathToApplicationConfInSrcDir); defaultConfiguration.save(); } catch (ConfigurationException e) { logger.error("Error while saving new secret to application.conf.", e); } catch (IOException e) { logger.error("Error while saving new secret to application.conf.", e); } } }  static void checkThatApplicationSecretIsSet(
                                                boolean isProd,
                                                String baseDirWithoutTrailingSlash,                                                
                                                PropertiesConfiguration defaultConfiguration,
                                                Configuration compositeConfiguration);  }
@Test(expected = RuntimeException.class) public void testProdThrowsError() throws Exception { PropertiesConfiguration defaultConfiguration = new PropertiesConfiguration(); Configuration compositeConfiguration = new PropertiesConfiguration(); String uuid = UUID.randomUUID().toString(); String baseDirWithoutTrailingSlash = "/tmp/ninja-test-" + uuid; boolean isProd = true; NinjaPropertiesImplTool.checkThatApplicationSecretIsSet(isProd, baseDirWithoutTrailingSlash, defaultConfiguration, compositeConfiguration); FileUtils.deleteDirectory(new File(baseDirWithoutTrailingSlash)); }
@Test public void testMissingSecretCreatesNewOneInDevMode() throws Exception { String uuid = UUID.randomUUID().toString(); String baseDirWithoutTrailingSlash = "/tmp/ninja-test-" + uuid; String devConf = baseDirWithoutTrailingSlash + File.separator + "src/main/java/conf/application.conf"; PropertiesConfiguration defaultConfiguration = new PropertiesConfiguration(); defaultConfiguration.setFileName(devConf); Configuration compositeConfiguration = new PropertiesConfiguration(); boolean isProd = false; NinjaPropertiesImplTool.checkThatApplicationSecretIsSet(isProd, baseDirWithoutTrailingSlash, defaultConfiguration, compositeConfiguration); assertTrue(compositeConfiguration.getString( NinjaConstant.applicationSecret).length() == 64); assertTrue(defaultConfiguration.getString( NinjaConstant.applicationSecret).length() == 64); assertTrue(Files.toString(new File(devConf), Charsets.UTF_8).contains( NinjaConstant.applicationSecret)); FileUtils.deleteDirectory(new File(baseDirWithoutTrailingSlash)); }
@Test public void testNothingHappensWhenApplicationSecretIsThere() throws Exception { PropertiesConfiguration defaultConfiguration = new PropertiesConfiguration(); Configuration compositeConfiguration = new PropertiesConfiguration(); compositeConfiguration.setProperty(NinjaConstant.applicationSecret, "secret"); String uuid = UUID.randomUUID().toString(); String baseDirWithoutTrailingSlash = "/tmp/ninja-test-" + uuid; boolean isProd = true; NinjaPropertiesImplTool.checkThatApplicationSecretIsSet(isProd, baseDirWithoutTrailingSlash, defaultConfiguration, compositeConfiguration); assertFalse(new File(baseDirWithoutTrailingSlash + "src/main/java/conf/application.conf").exists()); isProd = false; NinjaPropertiesImplTool.checkThatApplicationSecretIsSet(isProd, baseDirWithoutTrailingSlash, defaultConfiguration, compositeConfiguration); assertFalse(new File(baseDirWithoutTrailingSlash + "src/main/java/conf/application.conf").exists()); FileUtils.deleteDirectory(new File(baseDirWithoutTrailingSlash)); }