feat: Database model & configuration
This commit is contained in:
parent
c087081d25
commit
ee6c6eda02
10 changed files with 209 additions and 0 deletions
21
pom.xml
21
pom.xml
|
@ -41,8 +41,29 @@
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 3rd party -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Database -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hsqldb</groupId>
|
||||||
|
<artifactId>hsqldb</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- Tests -->
|
<!-- Tests -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.junit.jupiter</groupId>
|
<groupId>org.junit.jupiter</groupId>
|
||||||
<artifactId>junit-jupiter</artifactId>
|
<artifactId>junit-jupiter</artifactId>
|
||||||
|
|
|
@ -2,8 +2,12 @@ package eu.ztsh.wymiana;
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
|
||||||
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
|
@EnableConfigurationProperties
|
||||||
|
@ConfigurationPropertiesScan
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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) {
|
||||||
|
|
||||||
|
}
|
|
@ -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;
|
||||||
|
|
||||||
|
}
|
29
src/main/java/eu/ztsh/wymiana/data/entity/UserEntity.java
Normal file
29
src/main/java/eu/ztsh/wymiana/data/entity/UserEntity.java
Normal 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;
|
||||||
|
|
||||||
|
}
|
|
@ -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> {
|
||||||
|
|
||||||
|
}
|
|
@ -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
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue