Skip to content

Commit

Permalink
Merge 68d97e2 into 2dc4c3d
Browse files Browse the repository at this point in the history
  • Loading branch information
musketyr committed Jul 28, 2021
2 parents 2dc4c3d + 68d97e2 commit 051c8d0
Show file tree
Hide file tree
Showing 11 changed files with 749 additions and 504 deletions.
2 changes: 2 additions & 0 deletions docs/guide/src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ include::{root-dir}/examples/micronaut-grails-example/src/test/groovy/micronaut/

There is an experimental generator for the JPA entities from GORM domain classes. You can use either from integration test or using Grails Console plugin

TIP: You can use `MicronautJdbcGenrator` instead of `MicronautJpaGenerator` to generate JDBC repositories instead of generic ones.

[source,groovy,indent=0,options="nowrap"]
.Integration Test Usage
----
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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 micronaut.grails.example

class Vehicle {

String name

String make
String model

static constraints = {
name maxSize: 255
make inList: ['Ford', 'Chevrolet', 'Nissan']
model nullable: true
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,10 @@ class GeneratorSpec extends Specification {
given:
File root = initRootDirectory()
when:
int generated = generator.generate(root)
generator.generate(root)
then:
noExceptionThrown()

generated == 1

when:
File entityFile = new File(root, 'micronaut/grails/example/User.groovy')
File repositoryFile = new File(root, 'micronaut/grails/example/UserRepository.groovy')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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 micronaut.grails.example

// tag::body[]
import com.agorapulse.micronaut.grails.jpa.generator.MicronautJdbcGenerator
import com.agorapulse.micronaut.grails.test.MicronautGrailsIntegration
import com.agorapulse.testing.fixt.Fixt
import org.springframework.beans.factory.annotation.Autowired
import spock.lang.Specification

@MicronautGrailsIntegration
class MicronautJdbcGeneratorSpec extends Specification {

Fixt fixt = Fixt.create(MicronautJdbcGeneratorSpec)

@Autowired MicronautJdbcGenerator generator

void 'generate domains'() {
given:
File root = initRootDirectory()
when:
generator.generate(root)
then:
noExceptionThrown()

when:
File entityFile = new File(root, 'micronaut/grails/example/Vehicle.groovy')
File repositoryFile = new File(root, 'micronaut/grails/example/VehicleRepository.groovy')
then:
entityFile.exists()
entityFile.text.trim() == fixt.readText('Vehicle.groovy.txt').trim()

repositoryFile.exists()
repositoryFile.text.trim() == fixt.readText('VehicleRepository.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[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package micronaut.grails.example

import groovy.transform.CompileStatic
import javax.annotation.Nullable
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.Pattern
import javax.validation.constraints.Size

@Entity
@CompileStatic
class Vehicle {

@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
Long id

@Version
Long version

@NotNull
@Pattern(regexp = 'Ford|Chevrolet|Nissan')
@Size(max = 255)
String make

@Nullable
@Size(max = 255)
String model

@NotNull
@Size(max = 255)
String name

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package micronaut.grails.example

import io.micronaut.data.jdbc.annotation.JdbcRepository
import io.micronaut.data.model.query.builder.sql.Dialect
import io.micronaut.data.repository.CrudRepository

@JdbcRepository(dialect = Dialect.MYSQL)
interface VehicleRepository extends CrudRepository<Vehicle, Long> {

}

0 comments on commit 051c8d0

Please sign in to comment.