Skip to content

Commit

Permalink
feat: Добавлен модуль hw05-orm-gradle
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanKondrashkov committed Nov 24, 2024
1 parent 87f16ab commit 075777e
Show file tree
Hide file tree
Showing 37 changed files with 973 additions and 1 deletion.
12 changes: 12 additions & 0 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ jobs:
java-version: '17'
distribution: 'temurin'

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4

- name: Set up QEMU
uses: docker/setup-qemu-action@v4

- name: Build Docker images
run: docker-compose -f docker-compose.yml build

- name: Start Docker Compose services
run: docker-compose -f docker-compose.yml up -d

# Configure Gradle for optimal use in GitHub Actions, including caching of downloaded dependencies.
# See: https://github.com/gradle/actions/blob/main/setup-gradle/README.md
- name: Setup Gradle
Expand Down
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ allprojects {
dependency("org.junit.jupiter:junit-jupiter:${Versions.junit_jupiter}")
dependency("org.apache.poi:poi:${Versions.apache_poi}")
dependency("org.apache.poi:poi-ooxml:${Versions.apache_poi}")
dependency("com.zaxxer:HikariCP:${Versions.hikari}")
dependency("org.postgresql:postgresql:${Versions.postgres}")
dependency("org.flywaydb:flyway-core:${Versions.flyway}")
}
}

Expand Down
3 changes: 3 additions & 0 deletions buildSrc/src/main/groovy/Versions.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ interface Versions {
String guava = '31.1-jre'
String junit_jupiter = '5.9.0'
String apache_poi = '4.0.0'
String hikari = '5.0.1'
String postgres = '42.5.1'
String flyway = '9.12.0'
}
2 changes: 2 additions & 0 deletions hw05-example-gradle/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM amazoncorretto:17-alpine-jdk
COPY hw05-example-gradle/build/libs/*.jar app.jar
36 changes: 36 additions & 0 deletions hw05-example-gradle/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
plugins {
id 'idea'
id 'groovy'
id 'com.github.johnrengelman.shadow'
}

group 'ru.otus.homework'
version '1.0-SNAPSHOT'

repositories {
mavenCentral()
}

dependencies {
implementation localGroovy()
implementation 'com.zaxxer:HikariCP'
implementation 'org.postgresql:postgresql'
implementation 'org.flywaydb:flyway-core'
testImplementation 'org.junit.jupiter:junit-jupiter'
implementation project(':hw05-orm-gradle')
}

test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
}
}

shadowJar {
archiveBaseName.set('hw05-example-gradle-fatJar')
archiveVersion.set('0.1')
manifest {
attributes 'Main-Class': 'ru.otus.homework.Main'
}
}
77 changes: 77 additions & 0 deletions hw05-example-gradle/src/main/groovy/ru/otus/homework/Main.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package ru.otus.homework

import javax.sql.DataSource
import org.flywaydb.core.Flyway
import ru.otus.homework.model.Client
import ru.otus.homework.model.Manager
import ru.otus.homework.repository.DataTemplate
import ru.otus.homework.repository.DataTemplateJdbc
import ru.otus.homework.repository.DbExecutorImpl
import ru.otus.homework.service.DbServiceClientImpl
import ru.otus.homework.service.DbServiceManagerImpl
import ru.otus.homework.mapper.EntitySQLMetaDataImpl
import ru.otus.homework.mapper.EntityClassMetaDataImpl
import ru.otus.homework.datasource.DriverManagerDataSource
import ru.otus.homework.sessionmanager.TransactionRunnerJdbc

static void main(String[] args) {
Properties properties = new Properties()
ClassLoader classLoader = getClass().getClassLoader()
File file = new File(classLoader.getResource("application.properties").getFile())
file.withDataInputStream {
properties.load(it)
}

final String URL = properties."datasource.url"
final String USER = properties."datasource.username"
final String PASSWORD = properties."datasource.password"
final String DRIVER = properties."datasource.driver-class-name"

def dataSource = new DriverManagerDataSource(URL, USER, PASSWORD, DRIVER)
flywayMigrations(dataSource)
def transactionRunner = new TransactionRunnerJdbc(dataSource)
def dbExecutor = new DbExecutorImpl()

def entityClassMetaDataClient = new EntityClassMetaDataImpl<>(Client.class)
def entitySQLMetaDataClient = new EntitySQLMetaDataImpl<>(entityClassMetaDataClient)
def dataTemplateClient = new DataTemplateJdbc<>(
dbExecutor: dbExecutor,
entitySQLMetaData: entitySQLMetaDataClient,
entityClassMetaData: entityClassMetaDataClient
)

def dbServiceClient = new DbServiceClientImpl(transactionRunner, dataTemplateClient as DataTemplate<Client>);
def clientAfterSave = dbServiceClient.saveClient(new Client(name: "dbServiceFirst"))
def clientSecond = dbServiceClient.saveClient(new Client(name: "dbServiceSecond"))
def clientSecondSelected = dbServiceClient.getClient(clientSecond.getId())
def clientForUpdate = clientSecondSelected
clientForUpdate?.setName("New name for client")
def clientAfterUpdate = dbServiceClient.saveClient(clientForUpdate)

def entityClassMetaDataManager = new EntityClassMetaDataImpl<>(Manager.class)
def entitySQLMetaDataManager = new EntitySQLMetaDataImpl<>(entityClassMetaDataManager)
def dataTemplateManager = new DataTemplateJdbc<>(
dbExecutor: dbExecutor,
entitySQLMetaData: entitySQLMetaDataManager,
entityClassMetaData: entityClassMetaDataManager
)

def dbServiceManager = new DbServiceManagerImpl(transactionRunner, dataTemplateManager as DataTemplate<Manager>)
def managerAfterSave = dbServiceManager.saveManager(new Manager(label: "ManagerFirst", param1: "param1"))
def managerSecond = dbServiceManager.saveManager(new Manager(label: "ManagerSecond", param1: "param2"))
def managerSecondSelected = dbServiceManager.getManager(managerSecond.getNo())
def managerForUpdate = managerSecondSelected
managerForUpdate?.setLabel("New Label for manager")
managerForUpdate?.setParam1("New param for manager")
def managerAfterUpdate = dbServiceManager.saveManager(managerForUpdate)
}

private static void flywayMigrations(DataSource dataSource) {
def flyway = Flyway.configure()
.dataSource(dataSource)
.baselineVersion('0')
.baselineOnMigrate(true)
.locations("classpath:/db/migration")
.load()
flyway.migrate()
}
4 changes: 4 additions & 0 deletions hw05-example-gradle/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
datasource.username=root
datasource.password=root
datasource.url=jdbc:postgresql://localhost:6542/todo
datasource.driver-class-name=org.postgresql.Driver
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
DROP TABLE IF EXISTS Client, Manager;

CREATE TABLE IF NOT EXISTS Client (
id SERIAL,
name VARCHAR(50)
);

CREATE TABLE IF NOT EXISTS Manager (
no SERIAL,
label VARCHAR(50),
param1 VARCHAR(50)
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package ru.otus.homework

import org.junit.jupiter.api.Test
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import ru.otus.homework.model.Client
import ru.otus.homework.mapper.EntitySQLMetaDataImpl
import ru.otus.homework.mapper.EntityClassMetaDataImpl
import ru.otus.homework.repository.DataTemplateJdbc
import ru.otus.homework.repository.DbExecutorImpl
import ru.otus.homework.service.DBServiceClient
import ru.otus.homework.service.DbServiceClientImpl
import ru.otus.homework.datasource.DriverManagerDataSource
import ru.otus.homework.sessionmanager.TransactionRunnerJdbc

class DbServiceClientImplTest {
Client client
DBServiceClient serviceClient

final String USER = "root"
final String PASSWORD = "root"
final String URL = "jdbc:postgresql://localhost:6542/todo"
final String DRIVER = "org.postgresql.Driver"

@BeforeEach
void init() {
def dataSource = new DriverManagerDataSource(URL, USER, PASSWORD, DRIVER)
def entityClassMetaData = new EntityClassMetaDataImpl<>(Client.class)
def entitySQLMetaData = new EntitySQLMetaDataImpl(entityClassMetaData)
def dbExecutor = new DbExecutorImpl()
def runnerJdbc = new TransactionRunnerJdbc(dataSource)
def templateJdbc = new DataTemplateJdbc(
dbExecutor: dbExecutor,
entitySQLMetaData: entitySQLMetaData,
entityClassMetaData: entityClassMetaData
)

client = new Client(name: 'Djon')
serviceClient = new DbServiceClientImpl(runnerJdbc, templateJdbc)
}

@AfterEach
void tearDown() {
client = null
serviceClient = null
}

@Test
void saveClient() {
def saveClient = serviceClient.saveClient(client)

assert saveClient.id != null
assert saveClient.name == 'Djon'
}

@Test
void getClient() {
def saveClient = serviceClient.saveClient(client)

assert saveClient.id != null
assert saveClient.name == 'Djon'

saveClient = serviceClient.getClient(saveClient.id)

assert saveClient.id != null
assert saveClient.name == 'Djon'
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package ru.otus.homework

import org.junit.jupiter.api.Test
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import ru.otus.homework.model.Client
import ru.otus.homework.mapper.EntityClassMetaDataImpl

class EntityClassMetaDataImplTest {
EntityClassMetaDataImpl<Client> entityClassMetaData

@BeforeEach
void init() {
entityClassMetaData = new EntityClassMetaDataImpl<>(Client.class)
}

@AfterEach
void tearDown() {
entityClassMetaData = null
}

@Test
void getIdField() {
def id = entityClassMetaData.getIdField()

assert id.name == 'id'
assert id.getType() == Integer
}

@Test
void getName() {
def name = entityClassMetaData.getName()

assert name == 'Client'
}

@Test
void getAllFields() {
def fields = entityClassMetaData.getAllFields()

assert fields.size() == 2
assert fields.get(0).name == 'id'
assert fields.get(1).name == 'name'

}

@Test
void getFieldsWithoutId() {
def fields = entityClassMetaData.getFieldsWithoutId()

assert fields.size() == 1
assert fields.get(0).name == 'name'

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package ru.otus.homework

import org.junit.jupiter.api.Test
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import ru.otus.homework.model.Client
import ru.otus.homework.mapper.EntitySQLMetaDataImpl
import ru.otus.homework.mapper.EntityClassMetaDataImpl

class EntitySQLMetaDataImplTest {
Client client
EntitySQLMetaDataImpl entitySQLMetaData

@BeforeEach
void init() {
client = new Client(id: 1, name: 'Djon')
def entityClassMetaData = new EntityClassMetaDataImpl<>(Client.class)
entitySQLMetaData = new EntitySQLMetaDataImpl(entityClassMetaData)
}

@AfterEach
void tearDown() {
client = null
entitySQLMetaData = null
}

@Test
void getSelectAllSql() {
def sql = entitySQLMetaData.getSelectAllSql()

assert sql == 'SELECT * FROM Client'
}

@Test
void getSelectByIdSql() {
def sql = entitySQLMetaData.getSelectByIdSql(client.id)

assert sql == 'SELECT * FROM Client WHERE id = 1'
}

@Test
void getInsertSql() {
def sql = entitySQLMetaData.getInsertSql(client)

assert sql == 'INSERT INTO Client (name) VALUES(?)'
}

@Test
void getUpdateSql() {
def sql = entitySQLMetaData.getUpdateSql(client)

assert sql == 'UPDATE Client SET name = ? WHERE id = 1'
}
}
20 changes: 20 additions & 0 deletions hw05-orm-gradle/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
plugins {
id 'groovy'
}

group 'ru.otus.homework'
version '1.0-SNAPSHOT'

repositories {
mavenCentral()
}

dependencies {
implementation localGroovy()
implementation 'com.zaxxer:HikariCP'
}

tasks.register('copyResources', Copy) {
from "${projectDir}/src/main/resources"
into "${buildDir}/classes/java/main/resources"
}
Loading

0 comments on commit 075777e

Please sign in to comment.