Browse Source

implemented SqliteMailConfig

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
sqlite
Stephan Richter 2 months ago
parent
commit
79de646bf7
  1. 40
      de.srsoftware.oidc.api/src/test/java/de/srsoftware/oidc/api/MailConfigTest.java
  2. 2
      de.srsoftware.oidc.app/src/main/java/de/srsoftware/oidc/app/Application.java
  3. 12
      de.srsoftware.oidc.datastore.file/src/test/java/de/srsoftware/oidc/datastore/file/FileStoreMailConfigTest.java
  4. 200
      de.srsoftware.oidc.datastore.sqlite/src/main/java/de/srsoftware/oidc/datastore/sqlite/SqliteMailConfig.java
  5. 37
      de.srsoftware.oidc.datastore.sqlite/src/test/java/de/srsoftware/oidc/datastore/sqlite/SqliteMailConfigTest.java

40
de.srsoftware.oidc.api/src/test/java/de/srsoftware/oidc/api/MailConfigTest.java

@ -8,12 +8,14 @@ import static org.junit.jupiter.api.Assertions.*;
import jakarta.mail.Authenticator; import jakarta.mail.Authenticator;
import jakarta.mail.PasswordAuthentication; import jakarta.mail.PasswordAuthentication;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.Map; import java.util.Map;
import java.util.Random; import java.util.Random;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
public abstract class MailConfigTest { public abstract class MailConfigTest {
protected abstract MailConfig mailConfig(); protected abstract MailConfig mailConfig();
protected abstract void reOpen();
@Test @Test
public void testSmtpHost() { public void testSmtpHost() {
@ -151,4 +153,42 @@ public abstract class MailConfigTest {
assertEquals(password, pwa.getPassword()); assertEquals(password, pwa.getPassword());
assertEquals(address, pwa.getUserName()); assertEquals(address, pwa.getUserName());
} }
@Test
public void testSave() throws SQLException {
var host = uuid();
var port = new Random().nextInt();
var address = uuid();
var password = uuid();
mailConfig().senderPassword(password);
mailConfig().senderAddress(address);
mailConfig().smtpHost(host);
mailConfig().smtpPort(port);
mailConfig().startTls(true);
mailConfig().smtpAuth(false);
mailConfig().save();
reOpen();
var map = mailConfig().map();
assertEquals(map, Map.of( //
SMTP_HOST, host, //
SMTP_PORT, port, //
SMTP_AUTH, false, //
SMTP_USER, address, //
START_TLS, true));
mailConfig().startTls(false);
mailConfig().smtpAuth(true);
mailConfig().save();
reOpen();
map = mailConfig().map();
assertEquals(map, Map.of( //
SMTP_HOST, host, //
SMTP_PORT, port, //
SMTP_AUTH, true, //
SMTP_USER, address, //
START_TLS, false));
}
} }

2
de.srsoftware.oidc.app/src/main/java/de/srsoftware/oidc/app/Application.java

@ -108,7 +108,7 @@ public class Application {
}; };
} }
private static MailConfig setupMailConfig(Configuration config, Path defaultFile, FileStoreProvider fileStoreProvider) { private static MailConfig setupMailConfig(Configuration config, Path defaultFile, FileStoreProvider fileStoreProvider) throws SQLException {
var mailConfigLocation = new File(config.getOrDefault("mail_config_storage",defaultFile)); var mailConfigLocation = new File(config.getOrDefault("mail_config_storage",defaultFile));
return switch (extension(mailConfigLocation)){ return switch (extension(mailConfigLocation)){
case "db", "sqlite", "sqlite3" -> new SqliteMailConfig(connectionProvider.get(mailConfigLocation)); case "db", "sqlite", "sqlite3" -> new SqliteMailConfig(connectionProvider.get(mailConfigLocation));

12
de.srsoftware.oidc.datastore.file/src/test/java/de/srsoftware/oidc/datastore/file/FileStoreMailConfigTest.java

@ -13,6 +13,7 @@ import org.junit.jupiter.api.BeforeEach;
public class FileStoreMailConfigTest extends MailConfigTest { public class FileStoreMailConfigTest extends MailConfigTest {
private MailConfig mailConfig; private MailConfig mailConfig;
private File storage;
@Override @Override
@ -20,9 +21,18 @@ public class FileStoreMailConfigTest extends MailConfigTest {
return mailConfig; return mailConfig;
} }
@Override
protected void reOpen() {
try {
mailConfig = new FileStore(storage, null);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@BeforeEach @BeforeEach
public void setup() throws IOException { public void setup() throws IOException {
var storage = new File("/tmp/" + UUID.randomUUID()); storage = new File("/tmp/" + UUID.randomUUID());
if (storage.exists()) storage.delete(); if (storage.exists()) storage.delete();
mailConfig = new FileStore(storage, null); mailConfig = new FileStore(storage, null);
} }

200
de.srsoftware.oidc.datastore.sqlite/src/main/java/de/srsoftware/oidc/datastore/sqlite/SqliteMailConfig.java

@ -1,71 +1,183 @@
/* © SRSoftware 2024 */ /* © SRSoftware 2024 */
package de.srsoftware.oidc.datastore.sqlite; package de.srsoftware.oidc.datastore.sqlite;
import static de.srsoftware.oidc.api.Constants.*;
import de.srsoftware.oidc.api.MailConfig; import de.srsoftware.oidc.api.MailConfig;
import jakarta.mail.Authenticator; import jakarta.mail.Authenticator;
import jakarta.mail.PasswordAuthentication;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException;
public class SqliteMailConfig implements MailConfig { public class SqliteMailConfig extends SqliteStore implements MailConfig {
public SqliteMailConfig(Connection connection) { private static final String STORE_VERSION = "mail_config_store_version";
} private static final String CREATE_STORE_VERSION = "INSERT INTO metainfo (key,value) VALUES ('" + STORE_VERSION + "','0')";
private static final String SELECT_STORE_VERSION = "SELECT * FROM metainfo WHERE key = '" + STORE_VERSION + "'";
private static final String SET_STORE_VERSION = "UPDATE metainfo SET value = ? WHERE key = '" + STORE_VERSION + "'";
private static final String CREATE_MAIL_CONFIG_TABLE = "CREATE TABLE mail_config (key VARCHAR(64) PRIMARY KEY, value VARCHAR(255));";
private static final String SAVE_MAILCONFIG = "INSERT INTO mail_config (key, value) VALUES (?, ?) ON CONFLICT DO UPDATE SET value = ?";
private static final String SELECT_MAILCONFIG = "SELECT * FROM mail_config";
private String smtpHost, senderAddress, password;
@Override private int smtpPort;
public String smtpHost() { private boolean smtpAuth, startTls;
return ""; private Authenticator auth;
}
@Override public SqliteMailConfig(Connection connection) throws SQLException {
public MailConfig smtpHost(String newValue) { super(connection);
return null; smtpHost = "";
smtpPort = 0;
senderAddress = "";
password = "";
smtpAuth = true;
startTls = true;
var rs = conn.prepareStatement(SELECT_MAILCONFIG).executeQuery();
while (rs.next()) {
var key = rs.getString(1);
switch (key) {
case SMTP_PORT -> smtpPort = rs.getInt(2);
case SMTP_HOST -> smtpHost = rs.getString(2);
case START_TLS -> startTls = rs.getBoolean(2);
case SMTP_AUTH -> smtpAuth = rs.getBoolean(2);
case SMTP_PASSWORD -> password = rs.getString(2);
case SMTP_USER -> senderAddress = rs.getString(2);
} }
@Override
public int smtpPort() {
return 0;
} }
@Override
public MailConfig smtpPort(int newValue) {
return null;
} }
@Override private void createStoreTables() throws SQLException {
public String senderAddress() { conn.prepareStatement(CREATE_MAIL_CONFIG_TABLE).execute();
return "";
} }
@Override
public MailConfig senderAddress(String newValue) {
return null;
}
@Override @Override
public String senderPassword() { protected void initTables() throws SQLException {
return ""; var rs = conn.prepareStatement(SELECT_STORE_VERSION).executeQuery();
int availableVersion = 1;
int currentVersion;
if (rs.next()) {
currentVersion = rs.getInt("value");
rs.close();
} else {
rs.close();
conn.prepareStatement(CREATE_STORE_VERSION).execute();
currentVersion = 0;
} }
@Override conn.setAutoCommit(false);
public MailConfig senderPassword(String newValue) { var stmt = conn.prepareStatement(SET_STORE_VERSION);
return null; while (currentVersion < availableVersion) {
try {
switch (currentVersion) {
case 0:
createStoreTables();
break;
} }
stmt.setInt(1, ++currentVersion);
@Override stmt.execute();
public MailConfig startTls(boolean newValue) { conn.commit();
return null; }
catch (Exception e) {
conn.rollback();
LOG.log(System.Logger.Level.ERROR, "Failed to update at {} = {}", STORE_VERSION, currentVersion);
break;
} }
@Override
public MailConfig smtpAuth(boolean newValue) {
return null;
} }
conn.setAutoCommit(true);
}
@Override @Override
public Authenticator authenticator() { public String smtpHost() {
return null; return smtpHost;
}
@Override
public MailConfig smtpHost(String newValue) {
smtpHost = newValue;
return this;
}
@Override
public int smtpPort() {
return smtpPort;
}
@Override
public MailConfig smtpPort(int newValue) {
smtpPort = newValue;
return this;
}
@Override
public String senderAddress() {
return senderAddress;
}
@Override
public MailConfig senderAddress(String newValue) {
senderAddress = newValue;
return this;
}
@Override
public String senderPassword() {
return password;
}
@Override
public MailConfig senderPassword(String newValue) {
password = newValue;
return this;
}
@Override
public boolean startTls() {
return startTls;
}
@Override
public boolean smtpAuth() {
return smtpAuth;
}
@Override
public MailConfig startTls(boolean newValue) {
startTls = newValue;
return this;
}
@Override
public MailConfig smtpAuth(boolean newValue) {
smtpAuth = newValue;
return this;
}
@Override
public Authenticator authenticator() {
if (auth == null) {
auth = new Authenticator() {
// override the getPasswordAuthentication method
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(senderAddress(), senderPassword());
}
};
} }
return auth;
}
@Override @Override
public MailConfig save() { public MailConfig save() {
return null; try {
var stmt = conn.prepareStatement(SAVE_MAILCONFIG);
for (var entry : map().entrySet()) {
stmt.setString(1, entry.getKey());
stmt.setObject(2, entry.getValue());
stmt.setObject(3, entry.getValue());
stmt.execute();
}
return this;
} catch (SQLException e) {
throw new RuntimeException(e);
} }
} }
}

37
de.srsoftware.oidc.datastore.sqlite/src/test/java/de/srsoftware/oidc/datastore/sqlite/SqliteMailConfigTest.java

@ -0,0 +1,37 @@
/* © SRSoftware 2024 */
package de.srsoftware.oidc.datastore.sqlite;
import static de.srsoftware.utils.Strings.uuid;
import de.srsoftware.oidc.api.MailConfig;
import de.srsoftware.oidc.api.MailConfigTest;
import java.io.File;
import java.sql.SQLException;
import org.junit.jupiter.api.BeforeEach;
public class SqliteMailConfigTest extends MailConfigTest {
private SqliteMailConfig mailConfig;
private File dbFile;
@Override
protected MailConfig mailConfig() {
return mailConfig;
}
@Override
protected void reOpen() {
try {
var conn = new ConnectionProvider().get(dbFile);
mailConfig = new SqliteMailConfig(conn);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@BeforeEach
public void setup() throws SQLException {
dbFile = new File("/tmp/" + uuid() + ".sqlite");
var conn = new ConnectionProvider().get(dbFile);
mailConfig = new SqliteMailConfig(conn);
}
}
Loading…
Cancel
Save