-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Добавлен модуль hw05-orm-gradle
- Loading branch information
1 parent
87f16ab
commit 075777e
Showing
37 changed files
with
973 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
77
hw05-example-gradle/src/main/groovy/ru/otus/homework/Main.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
4
hw05-example-gradle/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
12 changes: 12 additions & 0 deletions
12
hw05-example-gradle/src/main/resources/db/migration/V1.0.0__init_schema.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); |
68 changes: 68 additions & 0 deletions
68
hw05-example-gradle/src/test/groovy/ru/otus/homework/DbServiceClientImplTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
hw05-example-gradle/src/test/groovy/ru/otus/homework/EntityClassMetaDataImplTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
|
||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
hw05-example-gradle/src/test/groovy/ru/otus/homework/EntitySQLMetaDataImplTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
Oops, something went wrong.