feat: Database model & configuration

This commit is contained in:
Piotr Dec 2024-05-21 22:40:44 +02:00
parent c087081d25
commit ee6c6eda02
Signed by: stawros
GPG key ID: F89F27AD8F881A91
10 changed files with 209 additions and 0 deletions

View file

@ -2,8 +2,12 @@ package eu.ztsh.wymiana;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@SpringBootApplication
@EnableConfigurationProperties
@ConfigurationPropertiesScan
public class Main {
public static void main(String[] args) {

View file

@ -0,0 +1,54 @@
package eu.ztsh.wymiana.data.config;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.hsqldb.persist.HsqlProperties;
import org.hsqldb.server.Server;
import org.hsqldb.server.ServerAcl;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
@Configuration
public class DatabaseConfiguration {
/**
* Create HSQL Database instance that is accessible from outside of application
*
* @return server instance
* @throws ServerAcl.AclFormatException when instantiating ServerAcl
* @throws IOException when instantiating ServerAcl
*/
@Bean
public Server hsqlServer(HsqlDbProperties properties) throws ServerAcl.AclFormatException, IOException {
var server = new Server();
var props = new HsqlProperties();
props.setProperty("server.database.0", String.format("mem:%s", properties.name()));
props.setProperty("server.dbname.0", properties.name());
props.setProperty("server.no_system_exit", true);
props.setProperty("server.port", properties.port());
server.setProperties(props);
server.start();
return server;
}
/**
* As we manually create database instance, we need also create datasource instance.
* HSQL instance is passed to force spring to create {@link Server} before pool.
*
* @param dataSourceProperties spring.datasource.* properties
* @return datasource instance
*/
@Bean
public HikariDataSource hikariDataSource(DataSourceProperties dataSourceProperties, Server ignore) {
var config = new HikariConfig();
config.setJdbcUrl(dataSourceProperties.getUrl());
config.setUsername(dataSourceProperties.getUsername());
config.setPassword(dataSourceProperties.getPassword());
config.setDriverClassName(dataSourceProperties.getDriverClassName());
return new HikariDataSource(config);
}
}

View file

@ -0,0 +1,8 @@
package eu.ztsh.wymiana.data.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties("hsqldb")
public record HsqlDbProperties(String name, int port) {
}

View file

@ -0,0 +1,23 @@
package eu.ztsh.wymiana.data.entity;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "currency")
public class CurrencyEntity {
@Id
String pesel;
@Id
String symbol;
Double amount;
}

View file

@ -0,0 +1,29 @@
package eu.ztsh.wymiana.data.entity;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "user")
public class UserEntity {
@Id
String pesel;
String name;
String surname;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "pesel")
List<CurrencyEntity> currencies;
}

View file

@ -0,0 +1,8 @@
package eu.ztsh.wymiana.data.repository;
import eu.ztsh.wymiana.data.entity.UserEntity;
import org.springframework.data.repository.CrudRepository;
public interface UserRepository extends CrudRepository<UserEntity, String> {
}

View file

@ -0,0 +1,13 @@
hsqldb:
name: db
port: 9090
spring:
datasource:
username: sa
password:
url: jdbc:hsqldb:hsql://localhost:${hsqldb.port}/${hsqldb.name}
driver-class-name: org.hsqldb.jdbc.JDBCDriver
jpa:
hibernate:
ddl-auto: create

View file

View file

@ -0,0 +1,49 @@
package eu.ztsh.wymiana.data.repository;
import eu.ztsh.wymiana.data.entity.CurrencyEntity;
import eu.ztsh.wymiana.data.entity.UserEntity;
import jakarta.transaction.Transactional;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest
@ExtendWith(SpringExtension.class)
class UserRepositoryTest {
private final UserRepository userRepository;
@Autowired
UserRepositoryTest(UserRepository userRepository) {
this.userRepository = userRepository;
}
@BeforeEach
void prepareTest() {
userRepository.deleteAll();
}
@Test
@Transactional
@DisplayName("Basic insert & get test")
void basicTest() {
var pesel = "00281018264";
var entity = new UserEntity(pesel, "Janina", "Kowalska",
List.of(new CurrencyEntity(pesel, "PLN", 20.10)));
userRepository.save(entity);
assertThat(userRepository.findById(pesel))
.isNotEmpty()
.get()
.usingRecursiveComparison()
.isEqualTo(entity);
}
}