@Provides @Singleton JMAPConfiguration provideConfiguration(PropertiesProvider propertiesProvider, FileSystem fileSystem) throws ConfigurationException, IOException { try { Configuration configuration = propertiesProvider.getConfiguration("jmap"); return JMAPConfiguration.builder() .enabled(configuration.getBoolean("enabled", true)) .keystore(configuration.getString("tls.keystoreURL")) .secret(configuration.getString("tls.secret")) .jwtPublicKeyPem(loadPublicKey(fileSystem, Optional.ofNullable(configuration.getString("jwt.publickeypem.url")))) .port(configuration.getInt("jmap.port", DEFAULT_JMAP_PORT)) .build(); } catch (FileNotFoundException e) { LOGGER.warn("Could not find JMAP configuration file. JMAP server will not be enabled."); return JMAPConfiguration.builder() .disable() .build(); } }
@Test public void buildShouldThrowWhenKeystoreIsEmpty() { assertThatThrownBy(() -> JMAPConfiguration.builder() .enable() .keystore("") .build()) .isInstanceOf(IllegalStateException.class) .hasMessage("'keystore' is mandatory"); }
@Test public void buildShouldThrowWhenKeystoreIsNull() { assertThatThrownBy(() -> JMAPConfiguration.builder() .enable() .keystore(null) .build()) .isInstanceOf(IllegalStateException.class) .hasMessage("'keystore' is mandatory"); }
@Test public void buildShouldWorkWhenRandomPort() { JMAPConfiguration expectedJMAPConfiguration = new JMAPConfiguration(ENABLED, "keystore", "secret", Optional.of("file://conf/jwt_publickey"), Optional.empty()); JMAPConfiguration jmapConfiguration = JMAPConfiguration.builder() .enable() .keystore("keystore") .secret("secret") .jwtPublicKeyPem(Optional.of("file://conf/jwt_publickey")) .randomPort() .build(); assertThat(jmapConfiguration).isEqualToComparingFieldByField(expectedJMAPConfiguration); }
@Test public void buildShouldThrowWhenJwtPublicKeyPemIsEmpty() { assertThatThrownBy(() -> JMAPConfiguration.builder() .enable() .keystore("keystore") .secret("secret") .jwtPublicKeyPem(Optional.empty()) .build()) .isInstanceOf(IllegalStateException.class); }
@Test public void buildShouldThrowWhenSecretIsEmpty() { assertThatThrownBy(() -> JMAPConfiguration.builder() .enable() .keystore("keystore") .secret("") .build()) .isInstanceOf(IllegalStateException.class) .hasMessage("'secret' is mandatory"); }
@Test public void buildShouldWorkWhenFixedPort() { JMAPConfiguration expectedJMAPConfiguration = new JMAPConfiguration(ENABLED, "keystore", "secret", Optional.of("file://conf/jwt_publickey"), Optional.of(80)); JMAPConfiguration jmapConfiguration = JMAPConfiguration.builder() .enable() .keystore("keystore") .secret("secret") .jwtPublicKeyPem(Optional.of("file://conf/jwt_publickey")) .port(80) .build(); assertThat(jmapConfiguration).isEqualToComparingFieldByField(expectedJMAPConfiguration); }
@Test public void buildShouldThrowWhenSecretIsNull() { assertThatThrownBy(() -> JMAPConfiguration.builder() .enable() .keystore("keystore") .secret(null) .build()) .isInstanceOf(IllegalStateException.class) .hasMessage("'secret' is mandatory"); }
public static Builder newConfigurationBuilder() { return JMAPConfiguration.builder() .enable() .keystore("keystore") .secret("james72laBalle") .jwtPublicKeyPem(Optional.of(JWT_PUBLIC_KEY)); }
@Test public void buildShouldThrowWhenJwtPublicKeyPemIsNull() { assertThatThrownBy(() -> JMAPConfiguration.builder() .enable() .keystore("keystore") .secret("secret") .jwtPublicKeyPem(null) .build()) .isInstanceOf(NullPointerException.class); }
@Provides @Singleton JMAPConfiguration provideConfiguration() throws FileNotFoundException, ConfigurationException { return JMAPConfiguration.builder() .enable() .keystore("keystore") .secret("james72laBalle") .jwtPublicKeyPem(Optional.of(PUBLIC_PEM_KEY)) .randomPort() .build(); } }
@Test(expected = KeyStoreException.class) public void initShouldThrowOnUnknownKeystore() throws Exception { JMAPConfiguration jmapConfiguration = JamesSignatureHandlerProvider.newConfigurationBuilder() .keystore("badAliasKeystore") .secret("password") .build(); JamesSignatureHandler signatureHandler = new JamesSignatureHandler(JamesSignatureHandlerProvider.newFileSystem(), jmapConfiguration); signatureHandler.init(); }