Commit cd5ae61a authored by Daniel Gultsch's avatar Daniel Gultsch
Browse files

write test for payment processing and entry creation

parent a911525a
......@@ -92,6 +92,13 @@
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.20.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
......
......@@ -43,32 +43,53 @@ public class Database {
private static final Logger LOGGER = LoggerFactory.getLogger(Database.class);
private static final Database INSTANCE = new Database();
private static Database INSTANCE;
private static final Quirks QUIRKS;
private final Sql2o ejabberdDatabase;
private final Sql2o quicksyDatabase;
private Database() {
static {
HashMap<Class, Converter> converters = new HashMap<>();
Adapter.register(converters);
converters.put(Phonenumber.PhoneNumber.class, new PhoneNumberConverter());
converters.put(LocalDateTime.class, new LocalDateTimeConverter());
converters.put(UUID.class, new UUIDStringConverter());
Quirks quirks = new NoQuirks(converters);
this.ejabberdDatabase = createDatabase("ejabberd", quirks);
this.quicksyDatabase = createDatabase("quicksy", quirks);
QUIRKS = new NoQuirks(converters);
}
private Database() {
this(createDatabase("ejabberd"),createDatabase("quicksy"));
}
private static Sql2o createDatabase(String database, Quirks quirks) {
public Database(Sql2o ejabberdDatabase, Sql2o quicksyDatabase) {
this.ejabberdDatabase = ejabberdDatabase;
this.quicksyDatabase = quicksyDatabase;
setup(this.quicksyDatabase);
}
private static void setup(Sql2o quicksyDatabase) {
try(Connection connection = quicksyDatabase.open()){
connection.createQuery("CREATE TABLE IF NOT EXISTS `payments` ( `uuid` char(36) NOT NULL, `owner` varchar(191) DEFAULT NULL, `method` varchar(100) DEFAULT NULL, `token` varchar(255) DEFAULT NULL, `total` float DEFAULT NULL, `status` varchar(100) DEFAULT NULL, `created` timestamp NOT NULL, PRIMARY KEY (`uuid`))").executeUpdate();
connection.createQuery("CREATE TABLE IF NOT EXISTS `entries` ( `jid` varchar(191) NOT NULL, `phoneNumber` varchar(25) DEFAULT NULL, `verified` tinyint(1) DEFAULT '0', `attempts` int(11) DEFAULT NULL, PRIMARY KEY (`jid`))").executeUpdate();
}
}
private static Sql2o createDatabase(String database) {
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());
return new Sql2o(dataSource, quirks);
return new Sql2o(dataSource, QUIRKS);
}
public static Database getInstance() {
public static synchronized Database getInstance() {
if (INSTANCE == null) {
INSTANCE = new Database();
}
return INSTANCE;
}
......@@ -120,7 +141,7 @@ public class Database {
}
public boolean updatePaymentAndCreateEntry(Payment payment, Entry entry) {
try (Connection connection = this.quicksyDatabase.beginTransaction()) {
try (Connection connection = this.quicksyDatabase.beginTransaction(java.sql.Connection.TRANSACTION_SERIALIZABLE)) {
connection.setRollbackOnException(true);
connection.createQuery(CREATE_ENTRY).bind(entry).executeUpdate();
int count = connection.createQuery(MAKE_PAYMENT)
......
/*
* 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;
import com.zaxxer.hikari.HikariDataSource;
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;
import static junit.framework.TestCase.assertTrue;
public class DatabaseTest {
private static final String JDBC_URL = "jdbc:sqlite::memory:";
private static final Jid TEST_USER = Jid.of("test@example.com");
@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 Payment payment = new Payment(TEST_USER, PaymentMethod.VOUCHER);
payment.setToken("test");
database.createPayment(payment);
assertTrue(database.updatePaymentAndCreateEntry(payment, new Entry(TEST_USER)));
final Entry entry = database.getEntry(TEST_USER);
assertNotNull(entry);
}
}
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