Commit 532b4ca0 authored by Daniel Gultsch's avatar Daniel Gultsch
Browse files

change database configuration to make it easier testable

parent cd5ae61a
......@@ -13,11 +13,13 @@
"port": 4567
},
"db": {
"username": "***",
"password": "***",
"databases": {
"ejabberd": "ejabberd",
"quicksy": "quicksy"
"ejabberd" : {
"url": "jdbc:mariadb://localhost:3306/ejabberd?characterEncoding=utf8"
"username": "***",
"password": "***"
},
"quicksy" : {
"url": "jdbc:mariadb://localhost:3306/quicksy?characterEncoding=utf8"
}
},
"pay_pal": {
......
......@@ -16,6 +16,7 @@
package im.quicksy.server;
import im.quicksy.server.configuration.Configuration;
import im.quicksy.server.controller.*;
import im.quicksy.server.xmpp.synchronization.Entry;
import im.quicksy.server.xmpp.synchronization.PhoneBook;
......
......@@ -18,6 +18,7 @@ package im.quicksy.server;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;
import im.quicksy.server.configuration.Configuration;
import rocks.xmpp.addr.Jid;
public class Utils {
......
......@@ -14,7 +14,7 @@
* limitations under the License.
*/
package im.quicksy.server;
package im.quicksy.server.configuration;
import com.github.zafarkhaja.semver.Version;
......@@ -41,7 +41,7 @@ public class Configuration {
private XMPP xmpp = new XMPP();
private Web web = new Web();
private DB db = new DB();
private HashMap<String, DatabaseConfiguration> db;
private PayPal payPal = new PayPal();
private String twilioAuthToken;
private String cimAuthToken;
......@@ -50,6 +50,7 @@ public class Configuration {
private String domain;
private boolean validatePhoneNumbers = true;
private boolean preventRegistration = true;
private Configuration() {
}
......@@ -102,7 +103,7 @@ public class Configuration {
}
public boolean check() {
return domain != null && minVersion != null && web != null && xmpp != null && xmpp.check() && db != null && db.check() && payPal != null && payPal.check();
return domain != null && minVersion != null && web != null && xmpp != null && xmpp.check() && db != null && db.size() == 2 && payPal != null && payPal.check();
}
public Duration getAccountInactivity() {
......@@ -125,8 +126,8 @@ public class Configuration {
return web;
}
public DB getDb() {
return db;
public DatabaseConfigurationBundle getDatabaseConfigurationBundle() {
return new DatabaseConfigurationBundle.Builder().setEjabberdConfiguration(db.get("ejabberd")).setQuicksyConfiguration(db.get("quicksy")).build();
}
public String getTwilioAuthToken() {
......@@ -193,44 +194,6 @@ public class Configuration {
}
}
public static class DB {
private String host = "127.0.0.1";
private int port = 3306;
private String username;
private String password;
private HashMap<String, String> databases;
private int poolSize = 1;
public String getHost() {
return host;
}
public int getPort() {
return port;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public String getJdbcUri(String database) {
return String.format("jdbc:mariadb://%s:%d/%s?characterEncoding=utf8", host, port, databases.get(database));
}
public boolean check() {
return username != null && password != null && databases != null && databases.containsKey("ejabberd") && databases.containsKey("quicksy");
}
public int getPoolSize() {
return poolSize;
}
}
public static class PayPal {
private String username;
private String password;
......
/*
* Copyright 2018 Daniel Gultsch
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.quicksy.server.configuration;
public class DatabaseConfiguration {
private String url;
private String username;
private String password;
private int poolSize = 1;
private DatabaseConfiguration() {
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public String getJdbcUri() {
return url;
}
public int getPoolSize() {
return poolSize;
}
public static class Builder {
private final DatabaseConfiguration configuration = new DatabaseConfiguration();
public DatabaseConfiguration.Builder setUsername(String username) {
configuration.username = username;
return this;
}
public DatabaseConfiguration.Builder setPassword(String password) {
configuration.password = password;
return this;
}
public DatabaseConfiguration.Builder setUrl(String url) {
configuration.url = url;
return this;
}
public DatabaseConfiguration build() {
return configuration;
}
}
}
\ No newline at end of file
/*
* Copyright 2018 Daniel Gultsch
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.quicksy.server.configuration;
public class DatabaseConfigurationBundle {
private DatabaseConfiguration ejabberd;
private DatabaseConfiguration quicksy;
private DatabaseConfigurationBundle() {
}
public DatabaseConfiguration getEjabberdConfiguration() {
return ejabberd;
}
public DatabaseConfiguration getQuicksyConfiguration() {
return quicksy;
}
public static class Builder {
private final DatabaseConfigurationBundle bundle = new DatabaseConfigurationBundle();
public Builder setEjabberdConfiguration(DatabaseConfiguration configuration) {
bundle.ejabberd = configuration;
return this;
}
public Builder setQuicksyConfiguration(DatabaseConfiguration configuration) {
bundle.quicksy = configuration;
return this;
}
public DatabaseConfigurationBundle build() {
return bundle;
}
}
}
......@@ -19,7 +19,7 @@ package im.quicksy.server.controller;
import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;
import im.quicksy.server.Configuration;
import im.quicksy.server.configuration.Configuration;
import im.quicksy.server.Utils;
import im.quicksy.server.ejabberd.MyEjabberdApi;
import im.quicksy.server.pojo.Device;
......
......@@ -20,7 +20,7 @@ import com.github.zafarkhaja.semver.ParseException;
import com.github.zafarkhaja.semver.Version;
import com.google.common.base.Splitter;
import com.google.common.net.InetAddresses;
import im.quicksy.server.Configuration;
import im.quicksy.server.configuration.Configuration;
import im.quicksy.server.verification.TwilioVerificationProvider;
import im.quicksy.server.verification.VerificationProvider;
import org.slf4j.Logger;
......
......@@ -21,7 +21,7 @@ import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;
import de.gultsch.ejabberd.api.RequestFailedException;
import im.quicksy.server.Configuration;
import im.quicksy.server.configuration.Configuration;
import im.quicksy.server.database.Database;
import im.quicksy.server.ejabberd.MyEjabberdApi;
import im.quicksy.server.pojo.Entry;
......@@ -36,7 +36,6 @@ import im.quicksy.server.verification.VerificationProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rocks.xmpp.addr.Jid;
import rocks.xmpp.extensions.hashes.model.Hash;
import spark.*;
import java.util.HashMap;
......
......@@ -22,7 +22,7 @@ import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;
import de.gultsch.ejabberd.api.results.Last;
import im.quicksy.server.Configuration;
import im.quicksy.server.configuration.Configuration;
import im.quicksy.server.Utils;
import im.quicksy.server.ejabberd.MyEjabberdApi;
import im.quicksy.server.throttle.RateLimiter;
......
......@@ -16,7 +16,7 @@
package im.quicksy.server.controller;
import im.quicksy.server.Configuration;
import im.quicksy.server.configuration.Configuration;
import im.quicksy.server.database.Database;
import im.quicksy.server.throttle.Strategy;
import im.quicksy.server.throttle.VolumeLimiter;
......
......@@ -19,7 +19,9 @@ package im.quicksy.server.database;
import com.google.i18n.phonenumbers.Phonenumber;
import com.zaxxer.hikari.HikariDataSource;
import de.gultsch.xmpp.addr.adapter.Adapter;
import im.quicksy.server.Configuration;
import im.quicksy.server.configuration.Configuration;
import im.quicksy.server.configuration.DatabaseConfigurationBundle;
import im.quicksy.server.configuration.DatabaseConfiguration;
import im.quicksy.server.pojo.Entry;
import im.quicksy.server.pojo.Payment;
import im.quicksy.server.pojo.PaymentStatus;
......@@ -59,13 +61,9 @@ public class Database {
QUIRKS = new NoQuirks(converters);
}
private Database() {
this(createDatabase("ejabberd"),createDatabase("quicksy"));
}
public Database(Sql2o ejabberdDatabase, Sql2o quicksyDatabase) {
this.ejabberdDatabase = ejabberdDatabase;
this.quicksyDatabase = quicksyDatabase;
public Database(DatabaseConfigurationBundle configurationBundle) {
this.ejabberdDatabase = createDatabase(configurationBundle.getEjabberdConfiguration());
this.quicksyDatabase = createDatabase(configurationBundle.getQuicksyConfiguration());
setup(this.quicksyDatabase);
}
......@@ -76,19 +74,18 @@ public class Database {
}
}
private static Sql2o createDatabase(String database) {
private static Sql2o createDatabase(DatabaseConfiguration configuration) {
HikariDataSource dataSource = new HikariDataSource();
Configuration.DB dbConfig = Configuration.getInstance().getDb();
dataSource.setMaximumPoolSize(dbConfig.getPoolSize());
dataSource.setJdbcUrl(dbConfig.getJdbcUri(database));
dataSource.setUsername(dbConfig.getUsername());
dataSource.setPassword(dbConfig.getPassword());
dataSource.setMaximumPoolSize(configuration.getPoolSize());
dataSource.setJdbcUrl(configuration.getJdbcUri());
dataSource.setUsername(configuration.getUsername());
dataSource.setPassword(configuration.getPassword());
return new Sql2o(dataSource, QUIRKS);
}
public static synchronized Database getInstance() {
if (INSTANCE == null) {
INSTANCE = new Database();
INSTANCE = new Database(Configuration.getInstance().getDatabaseConfigurationBundle());
}
return INSTANCE;
}
......@@ -154,7 +151,6 @@ public class Database {
}
connection.commit();
} catch (Exception e) {
e.printStackTrace();
LOGGER.error(e.getMessage());
return false;
}
......
......@@ -19,15 +19,13 @@ package im.quicksy.server.pojo;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import im.quicksy.server.Configuration;
import im.quicksy.server.controller.BaseController;
import im.quicksy.server.configuration.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.FileReader;
import java.time.Instant;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
public class Voucher {
......
......@@ -17,7 +17,7 @@
package im.quicksy.server.utils;
import com.google.common.net.UrlEscapers;
import im.quicksy.server.Configuration;
import im.quicksy.server.configuration.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rocks.xmpp.addr.Jid;
......
......@@ -16,7 +16,7 @@
package im.quicksy.server.utils;
import im.quicksy.server.Configuration;
import im.quicksy.server.configuration.Configuration;
import im.quicksy.server.pojo.Payment;
import im.quicksy.server.pojo.ShoppingCartItem;
......
......@@ -20,7 +20,7 @@ import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import com.google.i18n.phonenumbers.Phonenumber;
import im.quicksy.server.Configuration;
import im.quicksy.server.configuration.Configuration;
import im.quicksy.server.verification.twilio.ErrorResponse;
import im.quicksy.server.verification.twilio.GenericResponse;
import im.quicksy.server.verification.twilio.StartResponse;
......
......@@ -16,13 +16,13 @@
package im.quicksy.server;
import com.zaxxer.hikari.HikariDataSource;
import im.quicksy.server.configuration.DatabaseConfiguration;
import im.quicksy.server.configuration.DatabaseConfigurationBundle;
import im.quicksy.server.database.Database;
import im.quicksy.server.pojo.Entry;
import im.quicksy.server.pojo.Payment;
import im.quicksy.server.pojo.PaymentMethod;
import org.junit.Test;
import org.sql2o.Sql2o;
import rocks.xmpp.addr.Jid;
import static junit.framework.TestCase.assertNotNull;
......@@ -34,14 +34,23 @@ public class DatabaseTest {
private static final Jid TEST_USER = Jid.of("test@example.com");
private static final DatabaseConfigurationBundle IN_MEMORY_DATABASE_CONFIGURATION;
static {
IN_MEMORY_DATABASE_CONFIGURATION = new DatabaseConfigurationBundle.Builder()
.setEjabberdConfiguration(new DatabaseConfiguration.Builder()
.setUrl(JDBC_URL)
.build())
.setQuicksyConfiguration(new DatabaseConfiguration.Builder()
.setUrl(JDBC_URL)
.build())
.build();
}
@Test
public void makePaymentCreateEntityAndReadBack() {
final Sql2o ejabberd = new Sql2o(JDBC_URL, null, null);
HikariDataSource dataSource = new HikariDataSource();
dataSource.setMaximumPoolSize(1);
dataSource.setJdbcUrl(JDBC_URL);
final Sql2o quicksy = new Sql2o(dataSource);
final Database database = new Database(ejabberd, quicksy);
final Database database = new Database(IN_MEMORY_DATABASE_CONFIGURATION);
final Payment payment = new Payment(TEST_USER, PaymentMethod.VOUCHER);
payment.setToken("test");
database.createPayment(payment);
......@@ -51,4 +60,9 @@ public class DatabaseTest {
assertNotNull(entry);
}
@Test
public void createEntryAddPhoneNumberAndSearch() {
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment