From 7c3028eca2d309552e5e513a89f3102704e1ecf4 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Sat, 3 Aug 2019 00:57:00 +0200 Subject: [PATCH] fixs and move files --- build.gradle.kts | 6 --- resources/sql/functions/user/insert_user.sql | 18 -------- .../sql/migrations/0000-init_view.down.sql | 2 - .../sql/migrations/0000-init_view.up.sql | 5 --- src/fr/dcproject/Module.kt | 26 ------------ .../kotlin}/fr/dcproject/Application.kt | 4 +- .../kotlin}/fr/dcproject/Configuration.kt | 8 ++-- src/main/kotlin/fr/dcproject/Module.kt | 26 ++++++++++++ .../kotlin}/fr/dcproject/entity/Article.kt | 0 .../kotlin}/fr/dcproject/entity/Citizen.kt | 15 +++---- .../kotlin}/fr/dcproject/entity/User.kt | 7 ++-- .../fr/dcproject/repository/Article.kt | 0 .../kotlin}/fr/dcproject/routes/Article.kt | 0 .../kotlin}/fr/dcproject/routes/Paths.kt | 0 .../main/resources}/application.conf | 0 {resources => src/main/resources}/logback.xml | 0 .../main/resources}/sql/fixtures/01-user.sql | 0 .../resources}/sql/fixtures/02-citizen.sql | 0 .../resources}/sql/fixtures/03-workgroup.sql | 0 .../resources}/sql/fixtures/04-article.sql | 0 .../sql/fixtures/05-constitution.sql | 0 .../resources}/sql/fixtures/06-follow.sql | 0 .../resources}/sql/fixtures/07-comment.sql | 0 .../main/resources}/sql/fixtures/08-vote.sql | 0 .../functions/article/find_article_by_id.sql | 0 .../sql/functions/article/find_articles.sql | 0 .../find_last_article_by_version_id.sql | 0 .../sql/functions/article/upsert_article.sql | 0 .../functions/citizen/find_citizen_by_id.sql | 0 .../citizen/find_citizen_by_user_id.sql | 0 .../sql/functions/citizen/upsert_citizen.sql | 4 +- .../sql/functions/comment/comment.sql | 0 .../sql/functions/comment/edit_comment.sql | 0 .../create_title_in_constitution.sql | 0 .../constitution/find_constitution_by_id.sql | 0 .../find_constitution_title_by_id.sql | 0 .../find_constitution_titles_by_id.sql | 0 .../constitution/upsert_constitution.sql | 0 .../sql/functions/follow/follow.sql | 0 .../sql/functions/follow/unfollow.sql | 0 .../sql/functions/helpers/random_between.sql | 0 .../sql/functions/user/check_user.sql | 0 .../sql/functions/user/find_user_by_id.sql | 0 .../functions/user/find_user_by_username.sql | 0 .../sql/functions/user/insert_user.sql | 19 +++++++++ .../resources}/sql/functions/vote/vote.sql | 0 .../sql/migrations/0000-init_schema.down.sql | 0 .../sql/migrations/0000-init_schema.up.sql | 2 - .../main/resources}/sql/tests/article.sql | 2 +- .../main/resources}/sql/tests/citizen.sql | 2 +- .../main/resources}/sql/tests/comment.sql | 4 +- .../resources}/sql/tests/constitution.sql | 2 +- .../main/resources}/sql/tests/follow.sql | 4 +- .../main/resources}/sql/tests/user.sql | 2 +- .../main/resources}/sql/tests/vote.sql | 2 +- {test => src/test/kotlin}/ArticleRouteTest.kt | 40 ++++++++++++++++-- {test => src/test/kotlin}/RunCucumberTest.kt | 14 ++++++- {test => src/test/kotlin}/feature/Context.kt | 4 ++ {test => src/test/kotlin}/feature/Request.kt | 41 ++++++++++++++++--- .../test/resources}/feature/articles.feature | 17 ++++---- 60 files changed, 171 insertions(+), 105 deletions(-) delete mode 100644 resources/sql/functions/user/insert_user.sql delete mode 100644 resources/sql/migrations/0000-init_view.down.sql delete mode 100644 resources/sql/migrations/0000-init_view.up.sql delete mode 100644 src/fr/dcproject/Module.kt rename src/{ => main/kotlin}/fr/dcproject/Application.kt (95%) rename src/{ => main/kotlin}/fr/dcproject/Configuration.kt (51%) create mode 100644 src/main/kotlin/fr/dcproject/Module.kt rename src/{ => main/kotlin}/fr/dcproject/entity/Article.kt (100%) rename src/{ => main/kotlin}/fr/dcproject/entity/Citizen.kt (56%) rename src/{ => main/kotlin}/fr/dcproject/entity/User.kt (67%) rename src/{ => main/kotlin}/fr/dcproject/repository/Article.kt (100%) rename src/{ => main/kotlin}/fr/dcproject/routes/Article.kt (100%) rename src/{ => main/kotlin}/fr/dcproject/routes/Paths.kt (100%) rename {resources => src/main/resources}/application.conf (100%) rename {resources => src/main/resources}/logback.xml (100%) rename {resources => src/main/resources}/sql/fixtures/01-user.sql (100%) rename {resources => src/main/resources}/sql/fixtures/02-citizen.sql (100%) rename {resources => src/main/resources}/sql/fixtures/03-workgroup.sql (100%) rename {resources => src/main/resources}/sql/fixtures/04-article.sql (100%) rename {resources => src/main/resources}/sql/fixtures/05-constitution.sql (100%) rename {resources => src/main/resources}/sql/fixtures/06-follow.sql (100%) rename {resources => src/main/resources}/sql/fixtures/07-comment.sql (100%) rename {resources => src/main/resources}/sql/fixtures/08-vote.sql (100%) rename {resources => src/main/resources}/sql/functions/article/find_article_by_id.sql (100%) rename {resources => src/main/resources}/sql/functions/article/find_articles.sql (100%) rename {resources => src/main/resources}/sql/functions/article/find_last_article_by_version_id.sql (100%) rename {resources => src/main/resources}/sql/functions/article/upsert_article.sql (100%) rename {resources => src/main/resources}/sql/functions/citizen/find_citizen_by_id.sql (100%) rename {resources => src/main/resources}/sql/functions/citizen/find_citizen_by_user_id.sql (100%) rename {resources => src/main/resources}/sql/functions/citizen/upsert_citizen.sql (86%) rename {resources => src/main/resources}/sql/functions/comment/comment.sql (100%) rename {resources => src/main/resources}/sql/functions/comment/edit_comment.sql (100%) rename {resources => src/main/resources}/sql/functions/constitution/create_title_in_constitution.sql (100%) rename {resources => src/main/resources}/sql/functions/constitution/find_constitution_by_id.sql (100%) rename {resources => src/main/resources}/sql/functions/constitution/find_constitution_title_by_id.sql (100%) rename {resources => src/main/resources}/sql/functions/constitution/find_constitution_titles_by_id.sql (100%) rename {resources => src/main/resources}/sql/functions/constitution/upsert_constitution.sql (100%) rename {resources => src/main/resources}/sql/functions/follow/follow.sql (100%) rename {resources => src/main/resources}/sql/functions/follow/unfollow.sql (100%) rename {resources => src/main/resources}/sql/functions/helpers/random_between.sql (100%) rename {resources => src/main/resources}/sql/functions/user/check_user.sql (100%) rename {resources => src/main/resources}/sql/functions/user/find_user_by_id.sql (100%) rename {resources => src/main/resources}/sql/functions/user/find_user_by_username.sql (100%) create mode 100644 src/main/resources/sql/functions/user/insert_user.sql rename {resources => src/main/resources}/sql/functions/vote/vote.sql (100%) rename {resources => src/main/resources}/sql/migrations/0000-init_schema.down.sql (100%) rename {resources => src/main/resources}/sql/migrations/0000-init_schema.up.sql (99%) rename {resources => src/main/resources}/sql/tests/article.sql (98%) rename {resources => src/main/resources}/sql/tests/citizen.sql (97%) rename {resources => src/main/resources}/sql/tests/comment.sql (95%) rename {resources => src/main/resources}/sql/tests/constitution.sql (98%) rename {resources => src/main/resources}/sql/tests/follow.sql (93%) rename {resources => src/main/resources}/sql/tests/user.sql (96%) rename {resources => src/main/resources}/sql/tests/vote.sql (97%) rename {test => src/test/kotlin}/ArticleRouteTest.kt (66%) rename {test => src/test/kotlin}/RunCucumberTest.kt (64%) rename {test => src/test/kotlin}/feature/Context.kt (89%) rename {test => src/test/kotlin}/feature/Request.kt (71%) rename {testresources => src/test/resources}/feature/articles.feature (59%) diff --git a/build.gradle.kts b/build.gradle.kts index 1c78cad..5341ce3 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -47,9 +47,3 @@ dependencies { testImplementation("io.cucumber:cucumber-java8:4.3.1") testImplementation("io.cucumber:cucumber-junit:4.3.1") } - -kotlin.sourceSets["main"].kotlin.srcDirs("src") -kotlin.sourceSets["test"].kotlin.srcDirs("test") - -sourceSets["main"].resources.srcDirs("resources") -sourceSets["test"].resources.srcDirs("testresources") diff --git a/resources/sql/functions/user/insert_user.sql b/resources/sql/functions/user/insert_user.sql deleted file mode 100644 index b629b37..0000000 --- a/resources/sql/functions/user/insert_user.sql +++ /dev/null @@ -1,18 +0,0 @@ -create or replace procedure insert_user(inout resource json) language plpgsql as -$$ -declare - new_id uuid; -begin - insert into "user" (username, password, blocked_at) - select - username, - crypt(resource->>'plain_password', gen_salt('bf', 8)), - case when blocked_at is not null then now() else null end - from json_populate_record(null::"user", resource) - returning id into new_id; - - select find_user_by_id(new_id) into resource; -end; -$$; - --- drop procedure if exists insert_user(inout json); \ No newline at end of file diff --git a/resources/sql/migrations/0000-init_view.down.sql b/resources/sql/migrations/0000-init_view.down.sql deleted file mode 100644 index 3a46f2f..0000000 --- a/resources/sql/migrations/0000-init_view.down.sql +++ /dev/null @@ -1,2 +0,0 @@ --- User -drop view if exists user_lite; \ No newline at end of file diff --git a/resources/sql/migrations/0000-init_view.up.sql b/resources/sql/migrations/0000-init_view.up.sql deleted file mode 100644 index 354be37..0000000 --- a/resources/sql/migrations/0000-init_view.up.sql +++ /dev/null @@ -1,5 +0,0 @@ --- User -create or replace view user_lite as -select u.id, u.created_at, u.blocked_at, u.username -from "user" u -where u.blocked_at is null; \ No newline at end of file diff --git a/src/fr/dcproject/Module.kt b/src/fr/dcproject/Module.kt deleted file mode 100644 index 31627f9..0000000 --- a/src/fr/dcproject/Module.kt +++ /dev/null @@ -1,26 +0,0 @@ -package fr.dcproject - -import fr.postgresjson.connexion.Requester -import io.ktor.util.KtorExperimentalAPI -import org.koin.dsl.module -import java.io.File -import fr.dcproject.repository.Article as ArticleRepository - -val config = Config() - -@KtorExperimentalAPI -val Module = module { - - single { config } - - single { Requester.RequesterFactory( - host = config.host, - database = config.database, - username = config.username, - password = config.password, - port = config.port, - functionsDirectory = File(this::class.java.getResource("/sql/functions").toURI()) - ).createRequester() } - - single { ArticleRepository(get()) } -} diff --git a/src/fr/dcproject/Application.kt b/src/main/kotlin/fr/dcproject/Application.kt similarity index 95% rename from src/fr/dcproject/Application.kt rename to src/main/kotlin/fr/dcproject/Application.kt index 3da30de..3dbb4c0 100644 --- a/src/fr/dcproject/Application.kt +++ b/src/main/kotlin/fr/dcproject/Application.kt @@ -52,8 +52,8 @@ fun Application.module() { convert
{ decode { values, _ -> val id = values.singleOrNull()?.let { UUID.fromString(it) } - ?: throw InternalError("Cannot convert $values to Article") - get().findById(id) + ?: throw InternalError("Cannot convert $values to UUID") + get().findById(id) ?: throw InternalError("Article $values not found") } } } diff --git a/src/fr/dcproject/Configuration.kt b/src/main/kotlin/fr/dcproject/Configuration.kt similarity index 51% rename from src/fr/dcproject/Configuration.kt rename to src/main/kotlin/fr/dcproject/Configuration.kt index 7f345a2..2e6faaa 100644 --- a/src/fr/dcproject/Configuration.kt +++ b/src/main/kotlin/fr/dcproject/Configuration.kt @@ -1,14 +1,16 @@ package fr.dcproject import com.typesafe.config.ConfigFactory +import java.io.File class Config { private var config = ConfigFactory.load() + val sqlFiles = File(this::class.java.getResource("/sql").toURI()) val envName: String = config.getString("app.envName") val host: String = config.getString("db.host") - val database: String = config.getString("db.database") - val username: String = config.getString("db.username") - val password: String = config.getString("db.password") + var database: String = config.getString("db.database") + var username: String = config.getString("db.username") + var password: String = config.getString("db.password") val port: Int = config.getInt("db.port") } diff --git a/src/main/kotlin/fr/dcproject/Module.kt b/src/main/kotlin/fr/dcproject/Module.kt new file mode 100644 index 0000000..1d47780 --- /dev/null +++ b/src/main/kotlin/fr/dcproject/Module.kt @@ -0,0 +1,26 @@ +package fr.dcproject + +import fr.postgresjson.connexion.Connection +import fr.postgresjson.connexion.Requester +import fr.postgresjson.migration.Migrations +import io.ktor.util.KtorExperimentalAPI +import org.koin.dsl.module +import fr.dcproject.repository.Article as ArticleRepository + +val config = Config() + +@KtorExperimentalAPI +val Module = module { + + single { config } + + single { Connection(host = config.host, port = config.port, database = config.database, username = config.username, password = config.password) } + + single { Requester.RequesterFactory( + connection = get(), + functionsDirectory = config.sqlFiles.resolve("functions") + ).createRequester() } + + single { ArticleRepository(get()) } + single { Migrations(connection = get(), directory = config.sqlFiles) } +} diff --git a/src/fr/dcproject/entity/Article.kt b/src/main/kotlin/fr/dcproject/entity/Article.kt similarity index 100% rename from src/fr/dcproject/entity/Article.kt rename to src/main/kotlin/fr/dcproject/entity/Article.kt diff --git a/src/fr/dcproject/entity/Citizen.kt b/src/main/kotlin/fr/dcproject/entity/Citizen.kt similarity index 56% rename from src/fr/dcproject/entity/Citizen.kt rename to src/main/kotlin/fr/dcproject/entity/Citizen.kt index 28bda14..3d8f5d8 100644 --- a/src/fr/dcproject/entity/Citizen.kt +++ b/src/main/kotlin/fr/dcproject/entity/Citizen.kt @@ -3,22 +3,23 @@ package fr.dcproject.entity import fr.postgresjson.entity.EntityCreatedAt import fr.postgresjson.entity.EntityCreatedAtImp import fr.postgresjson.entity.UuidEntity +import org.joda.time.DateTime import java.util.* class Citizen( - id: UUID?, + id: UUID = UUID.randomUUID(), var name: Name?, - var birthday: String?, - var userId: String?, - var voteAnnonymous: Boolean?, - var followAnnonymous: Boolean?, + var birthday: DateTime?, + var userId: String? = null, + var voteAnnonymous: Boolean? = null, + var followAnnonymous: Boolean? = null, var user: User? ) : UuidEntity(id), EntityCreatedAt by EntityCreatedAtImp() { data class Name( - var civility: String?, + var firstName: String?, var lastName: String?, - var firstName: String? + var civility: String? = null ) } \ No newline at end of file diff --git a/src/fr/dcproject/entity/User.kt b/src/main/kotlin/fr/dcproject/entity/User.kt similarity index 67% rename from src/fr/dcproject/entity/User.kt rename to src/main/kotlin/fr/dcproject/entity/User.kt index 8e88d2c..b622f9c 100644 --- a/src/fr/dcproject/entity/User.kt +++ b/src/main/kotlin/fr/dcproject/entity/User.kt @@ -5,11 +5,10 @@ import org.joda.time.DateTime import java.util.* class User( - id: UUID?, + id: UUID? = UUID.randomUUID(), var username: String?, - var blockedAt: DateTime?, - override var createdAt: DateTime?, - override var updatedAt: DateTime? + var blockedAt: DateTime? = null, + var plainPassword: String? ) : UuidEntity(id), EntityCreatedAt by EntityCreatedAtImp(), EntityUpdatedAt by EntityUpdatedAtImp() diff --git a/src/fr/dcproject/repository/Article.kt b/src/main/kotlin/fr/dcproject/repository/Article.kt similarity index 100% rename from src/fr/dcproject/repository/Article.kt rename to src/main/kotlin/fr/dcproject/repository/Article.kt diff --git a/src/fr/dcproject/routes/Article.kt b/src/main/kotlin/fr/dcproject/routes/Article.kt similarity index 100% rename from src/fr/dcproject/routes/Article.kt rename to src/main/kotlin/fr/dcproject/routes/Article.kt diff --git a/src/fr/dcproject/routes/Paths.kt b/src/main/kotlin/fr/dcproject/routes/Paths.kt similarity index 100% rename from src/fr/dcproject/routes/Paths.kt rename to src/main/kotlin/fr/dcproject/routes/Paths.kt diff --git a/resources/application.conf b/src/main/resources/application.conf similarity index 100% rename from resources/application.conf rename to src/main/resources/application.conf diff --git a/resources/logback.xml b/src/main/resources/logback.xml similarity index 100% rename from resources/logback.xml rename to src/main/resources/logback.xml diff --git a/resources/sql/fixtures/01-user.sql b/src/main/resources/sql/fixtures/01-user.sql similarity index 100% rename from resources/sql/fixtures/01-user.sql rename to src/main/resources/sql/fixtures/01-user.sql diff --git a/resources/sql/fixtures/02-citizen.sql b/src/main/resources/sql/fixtures/02-citizen.sql similarity index 100% rename from resources/sql/fixtures/02-citizen.sql rename to src/main/resources/sql/fixtures/02-citizen.sql diff --git a/resources/sql/fixtures/03-workgroup.sql b/src/main/resources/sql/fixtures/03-workgroup.sql similarity index 100% rename from resources/sql/fixtures/03-workgroup.sql rename to src/main/resources/sql/fixtures/03-workgroup.sql diff --git a/resources/sql/fixtures/04-article.sql b/src/main/resources/sql/fixtures/04-article.sql similarity index 100% rename from resources/sql/fixtures/04-article.sql rename to src/main/resources/sql/fixtures/04-article.sql diff --git a/resources/sql/fixtures/05-constitution.sql b/src/main/resources/sql/fixtures/05-constitution.sql similarity index 100% rename from resources/sql/fixtures/05-constitution.sql rename to src/main/resources/sql/fixtures/05-constitution.sql diff --git a/resources/sql/fixtures/06-follow.sql b/src/main/resources/sql/fixtures/06-follow.sql similarity index 100% rename from resources/sql/fixtures/06-follow.sql rename to src/main/resources/sql/fixtures/06-follow.sql diff --git a/resources/sql/fixtures/07-comment.sql b/src/main/resources/sql/fixtures/07-comment.sql similarity index 100% rename from resources/sql/fixtures/07-comment.sql rename to src/main/resources/sql/fixtures/07-comment.sql diff --git a/resources/sql/fixtures/08-vote.sql b/src/main/resources/sql/fixtures/08-vote.sql similarity index 100% rename from resources/sql/fixtures/08-vote.sql rename to src/main/resources/sql/fixtures/08-vote.sql diff --git a/resources/sql/functions/article/find_article_by_id.sql b/src/main/resources/sql/functions/article/find_article_by_id.sql similarity index 100% rename from resources/sql/functions/article/find_article_by_id.sql rename to src/main/resources/sql/functions/article/find_article_by_id.sql diff --git a/resources/sql/functions/article/find_articles.sql b/src/main/resources/sql/functions/article/find_articles.sql similarity index 100% rename from resources/sql/functions/article/find_articles.sql rename to src/main/resources/sql/functions/article/find_articles.sql diff --git a/resources/sql/functions/article/find_last_article_by_version_id.sql b/src/main/resources/sql/functions/article/find_last_article_by_version_id.sql similarity index 100% rename from resources/sql/functions/article/find_last_article_by_version_id.sql rename to src/main/resources/sql/functions/article/find_last_article_by_version_id.sql diff --git a/resources/sql/functions/article/upsert_article.sql b/src/main/resources/sql/functions/article/upsert_article.sql similarity index 100% rename from resources/sql/functions/article/upsert_article.sql rename to src/main/resources/sql/functions/article/upsert_article.sql diff --git a/resources/sql/functions/citizen/find_citizen_by_id.sql b/src/main/resources/sql/functions/citizen/find_citizen_by_id.sql similarity index 100% rename from resources/sql/functions/citizen/find_citizen_by_id.sql rename to src/main/resources/sql/functions/citizen/find_citizen_by_id.sql diff --git a/resources/sql/functions/citizen/find_citizen_by_user_id.sql b/src/main/resources/sql/functions/citizen/find_citizen_by_user_id.sql similarity index 100% rename from resources/sql/functions/citizen/find_citizen_by_user_id.sql rename to src/main/resources/sql/functions/citizen/find_citizen_by_user_id.sql diff --git a/resources/sql/functions/citizen/upsert_citizen.sql b/src/main/resources/sql/functions/citizen/upsert_citizen.sql similarity index 86% rename from resources/sql/functions/citizen/upsert_citizen.sql rename to src/main/resources/sql/functions/citizen/upsert_citizen.sql index c9fd132..d903292 100644 --- a/resources/sql/functions/citizen/upsert_citizen.sql +++ b/src/main/resources/sql/functions/citizen/upsert_citizen.sql @@ -1,4 +1,4 @@ -create or replace procedure upsert_citizen(inout resource json) +create or replace function upsert_citizen(inout resource json) language plpgsql as $$ declare @@ -25,4 +25,4 @@ begin end; $$; --- drop procedure if exists insert_user(inout json); \ No newline at end of file +-- drop function if exists upsert_citizen(inout json); \ No newline at end of file diff --git a/resources/sql/functions/comment/comment.sql b/src/main/resources/sql/functions/comment/comment.sql similarity index 100% rename from resources/sql/functions/comment/comment.sql rename to src/main/resources/sql/functions/comment/comment.sql diff --git a/resources/sql/functions/comment/edit_comment.sql b/src/main/resources/sql/functions/comment/edit_comment.sql similarity index 100% rename from resources/sql/functions/comment/edit_comment.sql rename to src/main/resources/sql/functions/comment/edit_comment.sql diff --git a/resources/sql/functions/constitution/create_title_in_constitution.sql b/src/main/resources/sql/functions/constitution/create_title_in_constitution.sql similarity index 100% rename from resources/sql/functions/constitution/create_title_in_constitution.sql rename to src/main/resources/sql/functions/constitution/create_title_in_constitution.sql diff --git a/resources/sql/functions/constitution/find_constitution_by_id.sql b/src/main/resources/sql/functions/constitution/find_constitution_by_id.sql similarity index 100% rename from resources/sql/functions/constitution/find_constitution_by_id.sql rename to src/main/resources/sql/functions/constitution/find_constitution_by_id.sql diff --git a/resources/sql/functions/constitution/find_constitution_title_by_id.sql b/src/main/resources/sql/functions/constitution/find_constitution_title_by_id.sql similarity index 100% rename from resources/sql/functions/constitution/find_constitution_title_by_id.sql rename to src/main/resources/sql/functions/constitution/find_constitution_title_by_id.sql diff --git a/resources/sql/functions/constitution/find_constitution_titles_by_id.sql b/src/main/resources/sql/functions/constitution/find_constitution_titles_by_id.sql similarity index 100% rename from resources/sql/functions/constitution/find_constitution_titles_by_id.sql rename to src/main/resources/sql/functions/constitution/find_constitution_titles_by_id.sql diff --git a/resources/sql/functions/constitution/upsert_constitution.sql b/src/main/resources/sql/functions/constitution/upsert_constitution.sql similarity index 100% rename from resources/sql/functions/constitution/upsert_constitution.sql rename to src/main/resources/sql/functions/constitution/upsert_constitution.sql diff --git a/resources/sql/functions/follow/follow.sql b/src/main/resources/sql/functions/follow/follow.sql similarity index 100% rename from resources/sql/functions/follow/follow.sql rename to src/main/resources/sql/functions/follow/follow.sql diff --git a/resources/sql/functions/follow/unfollow.sql b/src/main/resources/sql/functions/follow/unfollow.sql similarity index 100% rename from resources/sql/functions/follow/unfollow.sql rename to src/main/resources/sql/functions/follow/unfollow.sql diff --git a/resources/sql/functions/helpers/random_between.sql b/src/main/resources/sql/functions/helpers/random_between.sql similarity index 100% rename from resources/sql/functions/helpers/random_between.sql rename to src/main/resources/sql/functions/helpers/random_between.sql diff --git a/resources/sql/functions/user/check_user.sql b/src/main/resources/sql/functions/user/check_user.sql similarity index 100% rename from resources/sql/functions/user/check_user.sql rename to src/main/resources/sql/functions/user/check_user.sql diff --git a/resources/sql/functions/user/find_user_by_id.sql b/src/main/resources/sql/functions/user/find_user_by_id.sql similarity index 100% rename from resources/sql/functions/user/find_user_by_id.sql rename to src/main/resources/sql/functions/user/find_user_by_id.sql diff --git a/resources/sql/functions/user/find_user_by_username.sql b/src/main/resources/sql/functions/user/find_user_by_username.sql similarity index 100% rename from resources/sql/functions/user/find_user_by_username.sql rename to src/main/resources/sql/functions/user/find_user_by_username.sql diff --git a/src/main/resources/sql/functions/user/insert_user.sql b/src/main/resources/sql/functions/user/insert_user.sql new file mode 100644 index 0000000..d8d20e4 --- /dev/null +++ b/src/main/resources/sql/functions/user/insert_user.sql @@ -0,0 +1,19 @@ +create or replace function insert_user(inout resource json) language plpgsql as +$$ +declare + new_id uuid; +begin + insert into "user" (id, username, password, blocked_at) + select + coalesce(t.id, uuid_generate_v4()), + t.username, + crypt(resource->>'plain_password', gen_salt('bf', 8)), + case when t.blocked_at is not null then now() else null end + from json_populate_record(null::"user", resource) t + returning id into new_id; + + select find_user_by_id(new_id) into resource; +end; +$$; + +-- drop function if exists insert_user(inout json); \ No newline at end of file diff --git a/resources/sql/functions/vote/vote.sql b/src/main/resources/sql/functions/vote/vote.sql similarity index 100% rename from resources/sql/functions/vote/vote.sql rename to src/main/resources/sql/functions/vote/vote.sql diff --git a/resources/sql/migrations/0000-init_schema.down.sql b/src/main/resources/sql/migrations/0000-init_schema.down.sql similarity index 100% rename from resources/sql/migrations/0000-init_schema.down.sql rename to src/main/resources/sql/migrations/0000-init_schema.down.sql diff --git a/resources/sql/migrations/0000-init_schema.up.sql b/src/main/resources/sql/migrations/0000-init_schema.up.sql similarity index 99% rename from resources/sql/migrations/0000-init_schema.up.sql rename to src/main/resources/sql/migrations/0000-init_schema.up.sql index 1644479..8969113 100644 --- a/resources/sql/migrations/0000-init_schema.up.sql +++ b/src/main/resources/sql/migrations/0000-init_schema.up.sql @@ -1,6 +1,4 @@ -- Users -create extension if not exists pgcrypto; - create table "user" ( id uuid default uuid_generate_v4() not null primary key, diff --git a/resources/sql/tests/article.sql b/src/main/resources/sql/tests/article.sql similarity index 98% rename from resources/sql/tests/article.sql rename to src/main/resources/sql/tests/article.sql index 93dd712..6b1b924 100644 --- a/resources/sql/tests/article.sql +++ b/src/main/resources/sql/tests/article.sql @@ -9,7 +9,7 @@ declare selected_article json; begin -- insert user for context - call insert_user(created_user); + select insert_user(created_user) into created_user; _user_id := created_user->>'id'; created_citizen := jsonb_set(created_citizen::jsonb, '{user}'::text[], jsonb_build_object('id', _user_id::text), true)::json; assert created_citizen#>>'{user, id}' = _user_id::text, format('userId in citizen must be the same as user, %s = %s', created_citizen#>>'{user, id}', _user_id::text); diff --git a/resources/sql/tests/citizen.sql b/src/main/resources/sql/tests/citizen.sql similarity index 97% rename from resources/sql/tests/citizen.sql rename to src/main/resources/sql/tests/citizen.sql index a7be3f4..cf17d46 100644 --- a/resources/sql/tests/citizen.sql +++ b/src/main/resources/sql/tests/citizen.sql @@ -8,7 +8,7 @@ declare selected_citizen json; begin -- insert user for context - call insert_user(created_user); + select insert_user(created_user) into created_user; _user_id := created_user->>'id'; created_citizen := jsonb_set(created_citizen::jsonb, '{user}'::text[], jsonb_build_object('id', _user_id::text), true)::json; assert created_citizen#>>'{user, id}' = _user_id::text, format('userId in citizen must be the same as user, %s = %s', created_citizen#>>'{user, id}', _user_id::text); diff --git a/resources/sql/tests/comment.sql b/src/main/resources/sql/tests/comment.sql similarity index 95% rename from resources/sql/tests/comment.sql rename to src/main/resources/sql/tests/comment.sql index b972081..55413e5 100644 --- a/resources/sql/tests/comment.sql +++ b/src/main/resources/sql/tests/comment.sql @@ -28,8 +28,8 @@ declare _comment_id uuid; begin -- insert user for context - call insert_user(created_user); - call insert_user(created_user2); + select insert_user(created_user) into created_user; + select insert_user(created_user2) into created_user2; created_citizen := jsonb_set(created_citizen::jsonb, '{user}'::text[], jsonb_build_object('id', created_user->>'id'), true)::json; -- insert new citizen for context diff --git a/resources/sql/tests/constitution.sql b/src/main/resources/sql/tests/constitution.sql similarity index 98% rename from resources/sql/tests/constitution.sql rename to src/main/resources/sql/tests/constitution.sql index 8f73638..e23fa46 100644 --- a/resources/sql/tests/constitution.sql +++ b/src/main/resources/sql/tests/constitution.sql @@ -42,7 +42,7 @@ declare $json$; begin -- insert user for context - call insert_user(created_user); + select insert_user(created_user) into created_user; _user_id := created_user->>'id'; created_citizen := jsonb_set(created_citizen::jsonb, '{user}'::text[], jsonb_build_object('id', _user_id::text), true)::json; assert created_citizen#>>'{user, id}' = _user_id::text, format('userId in citizen must be the same as user, %s = %s', created_citizen#>>'{user, id}', _user_id::text); diff --git a/resources/sql/tests/follow.sql b/src/main/resources/sql/tests/follow.sql similarity index 93% rename from resources/sql/tests/follow.sql rename to src/main/resources/sql/tests/follow.sql index 6c84a38..96af102 100644 --- a/resources/sql/tests/follow.sql +++ b/src/main/resources/sql/tests/follow.sql @@ -25,8 +25,8 @@ declare $json$; begin -- insert user for context - call insert_user(created_user); - call insert_user(created_user2); + select insert_user(created_user) into created_user; + select insert_user(created_user2) into created_user2; created_citizen := jsonb_set(created_citizen::jsonb, '{user}'::text[], jsonb_build_object('id', created_user->>'id'), true)::json; created_citizen2 := jsonb_set(created_citizen2::jsonb, '{user}'::text[], jsonb_build_object('id', created_user2->>'id'), true)::json; diff --git a/resources/sql/tests/user.sql b/src/main/resources/sql/tests/user.sql similarity index 96% rename from resources/sql/tests/user.sql rename to src/main/resources/sql/tests/user.sql index 8e5a5ea..146b51c 100644 --- a/resources/sql/tests/user.sql +++ b/src/main/resources/sql/tests/user.sql @@ -6,7 +6,7 @@ declare exist_user json; begin -- Insert user and check if username and password is correct - call insert_user(created_user); + select insert_user(created_user) into created_user; assert created_user->>'username' = 'george', 'username must be george'; assert created_user->>'password' is null, 'password must not be returned'; diff --git a/resources/sql/tests/vote.sql b/src/main/resources/sql/tests/vote.sql similarity index 97% rename from resources/sql/tests/vote.sql rename to src/main/resources/sql/tests/vote.sql index 172c536..7cea59e 100644 --- a/resources/sql/tests/vote.sql +++ b/src/main/resources/sql/tests/vote.sql @@ -26,7 +26,7 @@ declare $json$; begin -- insert user for context - call insert_user(created_user); + select insert_user(created_user) into created_user; created_citizen := jsonb_set(created_citizen::jsonb, '{user}'::text[], jsonb_build_object('id', created_user->>'id'), true)::json; -- insert new citizen for context diff --git a/test/ArticleRouteTest.kt b/src/test/kotlin/ArticleRouteTest.kt similarity index 66% rename from test/ArticleRouteTest.kt rename to src/test/kotlin/ArticleRouteTest.kt index c75caa4..cb257ed 100644 --- a/test/ArticleRouteTest.kt +++ b/src/test/kotlin/ArticleRouteTest.kt @@ -1,17 +1,51 @@ package fr.dcproject +import fr.postgresjson.migration.Migrations import io.ktor.http.ContentType import io.ktor.http.HttpHeaders import io.ktor.http.HttpMethod import io.ktor.http.HttpStatusCode +import io.ktor.locations.KtorExperimentalLocationsAPI import io.ktor.server.testing.handleRequest import io.ktor.server.testing.setBody import io.ktor.server.testing.withTestApplication -import kotlin.test.Test +import io.ktor.util.KtorExperimentalAPI +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.TestInstance +import org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS +import org.koin.core.context.startKoin +import org.koin.core.context.stopKoin +import org.koin.test.KoinTest +import org.koin.test.inject import kotlin.test.assertEquals import kotlin.test.assertTrue -class ArticleRouteTest { +@KtorExperimentalLocationsAPI +@KtorExperimentalAPI +@TestInstance(PER_CLASS) +class ArticleRouteTest: KoinTest { + private val migrations: Migrations by inject() + + @BeforeEach + fun beforeAll() { + startKoin { + modules(Module) + config.database = "test" + config.username = "test" + config.password = "test" + + } + migrations.run() + } + + @AfterEach + fun afterAll() { + migrations.forceAllDown() + stopKoin() + } + private val article: String = """{ "id" : "8e8dd0aa-2b2b-41e1-bff5-ea613c988774", "version_id" : "e3ec9ea8-87ac-46ac-8321-8f2bc8c687bc", @@ -39,7 +73,7 @@ class ArticleRouteTest { }""" @Test - fun testRoot() { + fun testRoute() { withTestApplication({ module() }) { handleRequest(HttpMethod.Get, "/articles").apply { assertEquals(HttpStatusCode.OK, response.status()) diff --git a/test/RunCucumberTest.kt b/src/test/kotlin/RunCucumberTest.kt similarity index 64% rename from test/RunCucumberTest.kt rename to src/test/kotlin/RunCucumberTest.kt index de6c753..4ff7dbd 100644 --- a/test/RunCucumberTest.kt +++ b/src/test/kotlin/RunCucumberTest.kt @@ -3,22 +3,32 @@ import cucumber.api.Scenario import cucumber.api.java8.En import cucumber.api.junit.Cucumber import feature.Context +import fr.dcproject.config +import fr.postgresjson.migration.Migrations import io.ktor.server.testing.TestApplicationEngine import io.ktor.server.testing.createTestEnvironment import org.junit.runner.RunWith +import org.koin.test.KoinTest +import org.koin.test.inject import java.util.concurrent.TimeUnit import feature.Context.Companion.current as contextCurrent @RunWith(Cucumber::class) @CucumberOptions(plugin = ["pretty"]) -class RunCucumberTest: En { +class RunCucumberTest: En, KoinTest { + private val migrations: Migrations by inject() init { Before(-1) { scenario: Scenario -> -// config.database = "dc-projectg-test" + config.database = "test" + config.username = "test" + config.password = "test" contextCurrent = Context(TestApplicationEngine(createTestEnvironment()) {}, scenario) + + migrations.run() } After { scenario: Scenario -> + migrations.forceAllDown() contextCurrent.engine.stop(0L, 0L, TimeUnit.MILLISECONDS) } } diff --git a/test/feature/Context.kt b/src/test/kotlin/feature/Context.kt similarity index 89% rename from test/feature/Context.kt rename to src/test/kotlin/feature/Context.kt index ab4555e..5960795 100644 --- a/test/feature/Context.kt +++ b/src/test/kotlin/feature/Context.kt @@ -3,10 +3,14 @@ package feature import cucumber.api.Scenario import fr.dcproject.module import io.ktor.application.Application +import io.ktor.locations.KtorExperimentalLocationsAPI import io.ktor.server.testing.TestApplicationCall import io.ktor.server.testing.TestApplicationEngine import io.ktor.server.testing.TestApplicationRequest +import io.ktor.util.KtorExperimentalAPI +@KtorExperimentalLocationsAPI +@KtorExperimentalAPI class Context( val engine: TestApplicationEngine, val scenario: Scenario diff --git a/test/feature/Request.kt b/src/test/kotlin/feature/Request.kt similarity index 71% rename from test/feature/Request.kt rename to src/test/kotlin/feature/Request.kt index 4c85e13..317ff6f 100644 --- a/test/feature/Request.kt +++ b/src/test/kotlin/feature/Request.kt @@ -1,8 +1,11 @@ package feature import com.google.gson.Gson -import cucumber.api.Scenario import cucumber.api.java8.En +import fr.dcproject.entity.Citizen +import fr.dcproject.entity.User +import fr.postgresjson.connexion.Requester +import fr.postgresjson.migration.Migrations import io.cucumber.datatable.DataTable import io.ktor.http.ContentType import io.ktor.http.HttpHeaders @@ -11,18 +14,46 @@ import io.ktor.http.HttpStatusCode import io.ktor.server.testing.TestApplicationCall import io.ktor.server.testing.TestApplicationEngine import io.ktor.server.testing.setBody +import org.joda.time.DateTime import org.junit.jupiter.api.Assertions.assertEquals import org.koin.test.KoinTest +import org.koin.test.inject import org.opentest4j.AssertionFailedError +import java.util.* import kotlin.test.asserter import feature.Context.Companion.current as currentContext class Request: En, KoinTest { + private val migrations: Migrations by inject() + private val requester: Requester by inject() init { - Before { scenario: Scenario -> - } +// Before { scenario: Scenario -> +// migrations.run() +// } +// +// After { scenario: Scenario -> +// migrations.forceAllDown() +// } - After { scenario: Scenario -> + When("I have citizen:") { body: DataTable -> + val user = User(username = "jaque", plainPassword = "azerty") + val data = body.asMap(String::class.java, String::class.java) + val citizen = Citizen( + id = UUID.fromString(data["id"]), + name = Citizen.Name(data["firstName"], data["lastName"]), + birthday = DateTime.now(), + user = user + ) + val test: TestApplicationEngine.() -> Unit = { + requester + .getFunction("insert_user") + .selectOne(user) + requester + .getFunction("upsert_citizen") + .selectOne(citizen) + } + + currentContext.engine.test() } When("I send a {string} request to {string} with body:") { method: String, uri: String, body: String -> @@ -55,7 +86,7 @@ class Request: En, KoinTest { Then("the response status code should be {int}") { statusCode: Int -> val call: TestApplicationCall = currentContext.call ?: throw AssertionFailedError("No call", statusCode, null) with(call) { - assertEquals(HttpStatusCode.fromValue(statusCode), response.status()) + assertEquals(HttpStatusCode.fromValue(statusCode), response.status(), response.content) } } diff --git a/testresources/feature/articles.feature b/src/test/resources/feature/articles.feature similarity index 59% rename from testresources/feature/articles.feature rename to src/test/resources/feature/articles.feature index 65a82d6..b3ced35 100644 --- a/testresources/feature/articles.feature +++ b/src/test/resources/feature/articles.feature @@ -4,11 +4,15 @@ Feature: articles routes When I send a "GET" request to "/articles" Then the response status code should be 200 - Scenario: The route for get article must response a 200 - When I send a "GET" request to "/articles/55a24426-139b-4ee7-b1e2-a3d016d66cc2" - Then the response status code should be 200 +# Scenario: The route for get article must response a 200 +# When I send a "GET" request to "/articles/55a24426-139b-4ee7-b1e2-a3d016d66cc2" +# Then the response status code should be 200 Scenario: The route for get article must response a 200 + Given I have citizen: + | id | 64b7b379-2298-43ec-b428-ba134930cabd | + | firstName | Jaque | + | lastName | Dupuis | When I send a "POST" request to "/articles" with body: """ { @@ -28,9 +32,4 @@ Feature: articles routes Then the response status code should be 200 And the response should contain object: | version_id | 09c418b6-63ba-448b-b38b-502b41cd500e | - | title | title2 | - When I send a "GET" request to "/articles/99afd1b1-3555-43c1-80a7-63c56e93d250" - Then the response status code should be 200 - And the response should contain object: - | id | 99afd1b1-3555-43c1-80a7-63c56e93d250 | - | title | title2 | \ No newline at end of file + | title | title2 | \ No newline at end of file