From 477330826e9c7cd53cf3d14b90ef37e42b5e851c Mon Sep 17 00:00:00 2001 From: Vladimir Orany Date: Wed, 28 Jul 2021 15:32:51 +0200 Subject: [PATCH] Test generating from Micronaut (#19) * example of using the generator with Micronaut * added forgotten test fixtures * Revert "added forgotten test fixtures" This reverts commit 2ee3c5edc6215bf678c5319b28beb99357eb36bd. * added javadoc (fixed violation) --- docs/guide/src/docs/asciidoc/index.adoc | 8 ++ .../micronaut-grails-domain-library.gradle | 11 +++ .../domain/MicronautGeneratorSpec.groovy | 75 +++++++++++++++++++ .../src/test/resources/application-test.yml | 7 ++ .../MicronautGeneratorSpec/Manager.groovy.txt | 26 +++++++ .../ManagerRepository.groovy.txt | 9 +++ gradle.properties | 2 +- 7 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 examples/micronaut-grails-domain-library/src/test/groovy/com/agorapulse/micronaut/grails/domain/MicronautGeneratorSpec.groovy create mode 100644 examples/micronaut-grails-domain-library/src/test/resources/application-test.yml create mode 100644 examples/micronaut-grails-domain-library/src/test/resources/com/agorapulse/micronaut/grails/domain/MicronautGeneratorSpec/Manager.groovy.txt create mode 100644 examples/micronaut-grails-domain-library/src/test/resources/com/agorapulse/micronaut/grails/domain/MicronautGeneratorSpec/ManagerRepository.groovy.txt diff --git a/docs/guide/src/docs/asciidoc/index.adoc b/docs/guide/src/docs/asciidoc/index.adoc index 7f4b34c0..0a842725 100644 --- a/docs/guide/src/docs/asciidoc/index.adoc +++ b/docs/guide/src/docs/asciidoc/index.adoc @@ -161,6 +161,12 @@ TIP: You can use `MicronautJdbcGenrator` instead of `MicronautJpaGenerator` to g include::{root-dir}/examples/micronaut-grails-example/src/test/groovy/micronaut/grails/example/GeneratorSpec.groovy[tags=body] ---- +[source,groovy,indent=0,options="nowrap"] +.Micronaut Usage +---- +include::{root-dir}/examples/micronaut-grails-domain-library/src/test/groovy/com/agorapulse/micronaut/grails/domain/MicronautGeneratorSpec.groovy[tags=body] +---- + [source,groovy,indent=0,options="nowrap"] .Grails Console Usage @@ -168,6 +174,8 @@ include::{root-dir}/examples/micronaut-grails-example/src/test/groovy/micronaut/ ctx.micronautJpaGenerator.generate(new File('/path/to/generated/sources')) ---- + + The current generator supports * Constraints diff --git a/examples/micronaut-grails-domain-library/micronaut-grails-domain-library.gradle b/examples/micronaut-grails-domain-library/micronaut-grails-domain-library.gradle index 23ef085e..f86c0a72 100644 --- a/examples/micronaut-grails-domain-library/micronaut-grails-domain-library.gradle +++ b/examples/micronaut-grails-domain-library/micronaut-grails-domain-library.gradle @@ -46,4 +46,15 @@ dependencies { annotationProcessor "io.micronaut:micronaut-inject-java" compile "io.micronaut:micronaut-runtime-groovy" compile "io.micronaut:micronaut-validation" + + testCompile project(':micronaut-grails-jpa-generator') + + testCompile 'com.agorapulse.testing:fixt:0.2.1.1' + + testCompile 'io.micronaut:micronaut-inject-groovy' + testCompile 'org.mariadb.jdbc:mariadb-java-client:2.7.3' + testCompile "org.testcontainers:mariadb:$testcontainersVersion" + testCompile 'com.github.docker-java:docker-java-api:3.2.8' + testCompile 'com.github.docker-java:docker-java-transport-zerodep:3.2.8' + testCompile 'net.java.dev.jna:jna:5.8.0' } diff --git a/examples/micronaut-grails-domain-library/src/test/groovy/com/agorapulse/micronaut/grails/domain/MicronautGeneratorSpec.groovy b/examples/micronaut-grails-domain-library/src/test/groovy/com/agorapulse/micronaut/grails/domain/MicronautGeneratorSpec.groovy new file mode 100644 index 00000000..3b97896b --- /dev/null +++ b/examples/micronaut-grails-domain-library/src/test/groovy/com/agorapulse/micronaut/grails/domain/MicronautGeneratorSpec.groovy @@ -0,0 +1,75 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * Copyright 2020 Vladimir Orany. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.agorapulse.micronaut.grails.domain + +// tag::body[] +import com.agorapulse.micronaut.grails.jpa.generator.MicronautJpaGenerator +import com.agorapulse.testing.fixt.Fixt +import io.micronaut.context.ApplicationContext +import org.grails.datastore.gorm.validation.constraints.eval.DefaultConstraintEvaluator +import org.grails.orm.hibernate.HibernateDatastore +import spock.lang.AutoCleanup +import spock.lang.Specification + +/** + * Example specification generating JPA entities from GORM entities. + */ +class MicronautGeneratorSpec extends Specification { + + Fixt fixt = Fixt.create(MicronautGeneratorSpec) + + @AutoCleanup ApplicationContext context = ApplicationContext.run() + + MicronautJpaGenerator generator = new MicronautJpaGenerator( + context.getBean(HibernateDatastore), + new DefaultConstraintEvaluator() + ) + + void 'generate domains'() { + given: + File root = initRootDirectory() + when: + generator.generate(root) + then: + noExceptionThrown() + + when: + File entityFile = new File(root, 'com/agorapulse/micronaut/grails/domain/Manager.groovy') + File repositoryFile = new File(root, 'com/agorapulse/micronaut/grails/domain/ManagerRepository.groovy') + then: + entityFile.exists() + entityFile.text.trim() == fixt.readText('Manager.groovy.txt').trim() + + repositoryFile.exists() + repositoryFile.text.trim() == fixt.readText('ManagerRepository.groovy.txt').trim() + } + + private static File initRootDirectory() { + File root = new File(System.getProperty('java.io.tmpdir'), 'micronaut-data-model') + + if (root.exists()) { + root.deleteDir() + } + + root.mkdirs() + + return root + } + +} +// end::body[] diff --git a/examples/micronaut-grails-domain-library/src/test/resources/application-test.yml b/examples/micronaut-grails-domain-library/src/test/resources/application-test.yml new file mode 100644 index 00000000..06046e46 --- /dev/null +++ b/examples/micronaut-grails-domain-library/src/test/resources/application-test.yml @@ -0,0 +1,7 @@ +dataSource: + dbCreate: "create-drop" + url: "jdbc:tc:mariadb:10.5.10:///test?useSSL=false" + driverClassName: "org.testcontainers.jdbc.ContainerDatabaseDriver" + dialect: org.hibernate.dialect.MySQL5InnoDBDialect + username: "username" + password: "password" diff --git a/examples/micronaut-grails-domain-library/src/test/resources/com/agorapulse/micronaut/grails/domain/MicronautGeneratorSpec/Manager.groovy.txt b/examples/micronaut-grails-domain-library/src/test/resources/com/agorapulse/micronaut/grails/domain/MicronautGeneratorSpec/Manager.groovy.txt new file mode 100644 index 00000000..dc3b1e67 --- /dev/null +++ b/examples/micronaut-grails-domain-library/src/test/resources/com/agorapulse/micronaut/grails/domain/MicronautGeneratorSpec/Manager.groovy.txt @@ -0,0 +1,26 @@ +package com.agorapulse.micronaut.grails.domain + +import groovy.transform.CompileStatic +import javax.persistence.Entity +import javax.persistence.GeneratedValue +import javax.persistence.GenerationType +import javax.persistence.Id +import javax.persistence.Version +import javax.validation.constraints.NotNull +import javax.validation.constraints.Size + +@Entity +@CompileStatic +class Manager { + + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) + Long id + + @Version + Long version + + @NotNull + @Size(max = 255) + String name + +} diff --git a/examples/micronaut-grails-domain-library/src/test/resources/com/agorapulse/micronaut/grails/domain/MicronautGeneratorSpec/ManagerRepository.groovy.txt b/examples/micronaut-grails-domain-library/src/test/resources/com/agorapulse/micronaut/grails/domain/MicronautGeneratorSpec/ManagerRepository.groovy.txt new file mode 100644 index 00000000..6bbce7b4 --- /dev/null +++ b/examples/micronaut-grails-domain-library/src/test/resources/com/agorapulse/micronaut/grails/domain/MicronautGeneratorSpec/ManagerRepository.groovy.txt @@ -0,0 +1,9 @@ +package com.agorapulse.micronaut.grails.domain + +import io.micronaut.data.annotation.Repository +import io.micronaut.data.repository.CrudRepository + +@Repository +interface ManagerRepository extends CrudRepository { + +} diff --git a/gradle.properties b/gradle.properties index 5fa0fb3a..5abc306e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -28,7 +28,7 @@ groovyVersion = 2.5.8 spockVersion = 1.3-groovy-2.5 awsSdkVersion = 1.11.656 awsSdk2Version = 2.9.24 -testcontainersVersion = 1.12.1 +testcontainersVersion = 1.15.3 groovyClosureSupportVersion = 0.6.0 kordampVersion=0.46.0 nexusPluginVersion=1.0.0