Mixins on Grails Domains Classes broken in upgrade to 2.2 - grails

We have a multiple Grails 2.0.3 domain classes that use the #Mixin annotation
like so:
#Mixin(PremisesMixin)
class Clinic {
Premises premises
String name
....
It works really well!
In trying to update to 2.2.2 the mixins don't seem to work. We use the fixtures plugin to bootstrap some data, and in the process of starting up we get errors related to the getters and setters that should injected by the mixins not being present.
I did Find there there are some issues with groovy mixins in more recent versions of grails, but there is a Grails specific replacement http://jira.grails.org/browse/GRAILS-9901
but changing to
#grails.util.Mixin(PremisesMixin)
class Clinic { ...
gives other errors.
Getter for property 'fax' threw exception; nested exception is java.lang.reflect.InvocationTargetException
So is there a way to utilize mixins on Grails domain classes in the latest version of grails or do I need to refactor my code to avoid them?
update:
the premises mixin which is in src/groovy looks like this:
class PremisesMixin implements Serializable {
private static final long serialVersionUID = 1L
static fields = ['addressLine1', 'addressLine2', 'city', 'county', 'state', 'postalCode', 'plus4', 'phone', 'latitude', 'longitude']
String getAddressLine1() { premises?.addressLine1 }
void setAddressLine1(String addressLine1) { premises?.addressLine1 = addressLine1 }
String getAddressLine2() { premises?.addressLine2 }
void setAddressLine2(String addressLine2) { premises?.addressLine2 = addressLine2 }
String getCity() { premises?.city }
void setCity(String city) { premises?.city = city }
...
String getPhone() { premises?.phone }
void setPhone(String phone) { premises?.phone = phone }
String getFax() { premises?.fax }
void setFax(String fax) { premises?.fax = fax }
...
// Workaround for open Groovy bug with Mixins https://issues.apache.org/jira/browse/GROOVY-3612
String toString() {
this as String
}
}
and Premises looks like this:
class Premises {
String addressLine1
String addressLine2
String city
String state
...
String county
String phone
String fax
Double latitude
Double longitude
}

It works for me in Grails 2.2.2 with the below setup:
#grails.util.Mixin(PremisesMixin)
class Clinic {
String name
static constraints = {
}
}
class Premises {
String fax
static constraints = {
fax nullable: true
}
}
class PremisesMixin {
//Without this a runtime error is thrown,
//like property 'premises' not found in Clinic.
Premises premises
void setFax(String fax) {
premises?.fax = fax
}
String getFax() {
premises?.fax
}
}
//Test Case
def clinic = new Clinic(name: "TestClinic")
clinic.premises = new Premises().save(flush: true, failOnError: true)
clinic.fax = "123456"
clinic.save(flush: true, failOnError: true)
Clinic.list().each{assert it.fax == '123456'}
Premises.list().each{assert it.fax == '123456'}
The logic for Mixin transformation has not been modified for 2.2.x version although I see modifications done on it in master branch, but the change is minute(generic class literals used).
Few Questions:
1. How was premises accessible in the mixin class? I do not see where it is defined in the Mixin class.
2. Actually when were you facing the error, run-app or during creation of Clinic(similar to what is done in test above)?

Related

Grails 3.x - tests from grails 2.5.x don't work

I have a domain class, which extends non-domain class. When my project was on Grails 2.5.3, the test worked fine.
#Mock(Activity)
class ActivitySpec extends Specification {
def "test"(){
expect:
new Activity(name: 'dfd').save()
}
}
The domain
class Activity extends DomainRestResource {
String name
String code
String description
static hasMany = [....]
static belongsTo = [... ]
static constraints = {
name maxSize: 50
....
}
static mapping = {
table name: 'tt_activity'
}
}
DomainRestResource is defined in src/main/groovy/com/...
DomainRestResource.groovy
abstract class DomainRestResource extends UniversalRestResource {
#Autowired
def connectionManager
#Autowired
def userActivityService
#Autowired
def dataSource
protected transient int limit
protected transient int offset
private transient String tableName
/*
many static methods and fields
and some logic
*/
}
And UniversalRestResource.groovy
abstract class UniversalRestResource {
/*
some logic
*/
abstract List<Object> findObjectsByQuery(String query, int limit, int offset)
/*and any others abstract methods*/
}
Now i'm upgrading the project to Grails 3.1.1 and everything works fine except all my tests.
Test result on grails 3.1.1:
org.grails.datastore.mapping.model.IllegalMappingException: Mapped identifier [id] for class [com.astaprime.rest.DomainRestResource] is not a valid property
at org.grails.datastore.mapping.model.config.GormMappingConfigurationStrategy.getIdentity(GormMappingConfigurationStrategy.java:887)
at org.grails.datastore.mapping.model.AbstractPersistentEntity.resolveIdentifier(AbstractPersistentEntity.java:196)
at org.grails.datastore.mapping.model.AbstractPersistentEntity.initialize(AbstractPersistentEntity.java:117)
at org.grails.datastore.mapping.model.AbstractPersistentEntity.getRootEntity(AbstractPersistentEntity.java:221)
at org.grails.datastore.mapping.model.AbstractMappingContext.initializePersistentEntity(AbstractMappingContext.java:271)
at org.grails.datastore.mapping.model.AbstractMappingContext.initialize(AbstractMappingContext.java:250)
at grails.test.mixin.domain.DomainClassUnitTestMixin.initializeMappingContext(DomainClassUnitTestMixin.groovy:93)
at grails.test.mixin.domain.DomainClassUnitTestMixin.mockDomains(DomainClassUnitTestMixin.groovy:87)
at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:153)
at org.spockframework.runtime.model.MethodInfo.invoke(MethodInfo.java:84)
at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:88)
at org.spockframework.runtime.extension.builtin.AbstractRuleInterceptor$1.evaluate(AbstractRuleInterceptor.java:37)
at grails.test.runtime.TestRuntimeJunitAdapter$1$2.evaluate(TestRuntimeJunitAdapter.groovy:46)
at org.spockframework.runtime.extension.builtin.TestRuleInterceptor.intercept(TestRuleInterceptor.java:38)
at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:87)
at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:88)
at org.spockframework.runtime.extension.builtin.AbstractRuleInterceptor$1.evaluate(AbstractRuleInterceptor.java:37)
at grails.test.runtime.TestRuntimeJunitAdapter$3$4.evaluate(TestRuntimeJunitAdapter.groovy:73)
at org.spockframework.runtime.extension.builtin.ClassRuleInterceptor.intercept(ClassRuleInterceptor.java:38)
at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:87)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
If remove "extends DomainRestResource" from domain, the test succeeds.
I cannot remove it from all my domain classes, it's very important for program logic.
Haw can i fix this? Thanx!
The solution is just upgrade Grails to version 3.1.2
The issue on Github

grails 2.2.2 service don't know domain model: "No signature of method"

I have a grails-plugin called "listadmin" there is a domain model "Liste":
package listadmin
class Liste {
String internal_name
String public_name
Boolean edtiable = true
Boolean visible = true
static hasMany = [eintrage : ListenEintrag]
static constraints = {
internal_name(unique : true , blank : false);
}
String toString() {
"${public_name}"
}
}
I have service called "SECO_ListenService" in the same module (grails-plugin):
package listadmin
class SECO_ListenService {
def getEntriesOfList(String intnalListName) {
def aList = Liste.findByInternal_name(intnalListName)
return aList
}
}
Now I try to call this service from an other module (grails-plugin) called "institutionadmin". The SECO_ListenService should return a list of strings for an select of a domain model in the inistitutionadmin:
package institutionadmin
import listadmin.SECO_ListenService
class Einrichtung {
Long einrichtungs_type
Long type_of_conzept
int anzahl_gruppen
int anzahl_kinder_pro_Gruppe
String offnungszeiten
static hasMany = [rooms : Raum]
static constraints = {
def aList = []
def sECO_ListenService = new SECO_ListenService()
aList=sECO_ListenService.getEntriesOfList("einrichtung_type")
einrichtungs_type(inList: aList)
}
}
If I try to run this application with the both modules. I get the following error:
Caused by MissingMethodException: No signature of method:
listadmin.Liste.methodMissing() is applicable for argument types: ()
values: []
It seemed to be that the service class don't know the "Liste"-domain-model. But I don't know where the error is. I also tried to call other standard methods like "findAll" but without any success.
Has anybody an idea where my mistake could be?
To get a service in a static context you need to access the grailsApplication spring bean. This can be done thought Holders. Example:
class MyService {
List<String> getAvailable() {
return ['A','B','C']
}
}
class MyDomainClass {
String something
static constraints = {
something inList: getSomethingList()
}
static List<String> getSomethingList() {
def myService = Holders.grailsApplication.mainContext.getBean('myService')
return myService.getAvailable()
}
}

BootStrap.groovy content doesn't update the content

I'm new to Grails and study it via InfoQ's "Getting Started With Grails" book.
Running through it I've faced a problem with the BootStrap.groovy file:
I've edited it as the book says.
Run-app it - and the data I've entered into BootStrap.groovy has been shown.
I've updated the bootstrapping values but they are not changed.
No errors are shown in the console.
What I've tried:
exchanging def's places;
set Runner's class value to nullables;
changed .save(failOnError:true) to .save(flush:true).
Even after I've changed the Race class' bootstrapping values it looks like it hasn't been changed.
Looks like the value is taken from somewhere else than the BootStrap.groovy?
I use Grails 2.2.1 with a PostgreSQL DB.
The classes' code is below:
package racetrack
class Runner {
static constraints = {
firstName(blank:false)
lastName(blank:false)
dateOfBirth(nullable:true)
gender(inList:["M","F"])
address(nullable:true)
city(nullable:true)
state(nullable:true)
zipcode(nullable:true)
email(email:true)
}
static hasMany = [registrations:Registration]
/*static mapping = {
sort "email"
}*/
String firstName
String lastName
Date dateOfBirth
String gender
String address
String city
String state
String zipcode
String email
String toString(){
"${firstName} ${lastName} (${email})"
}
}
and
package racetrack
class BootStrap {
def init = { servletContext ->
def begun = new Runner(firstName:"Marathon",
lastName:"Runner",
dateOfBirth:"",
gender:"M",
address:"",
city:"",
state:"",
zipcode:"",
email:"me#me.ru"
)
begun.save(flush:true)
def zabeg = new Race(name:"Run SPB",
startDate:new Date() + 360*30,
city:"Moscow",
state:"Moscow",
cost:10.0,
distance:42.0,
maxRunners:10)
zabeg.save(flush:true)
}
def destroy = {
}
}
EDIT: could this be happening due to any generate-* scripts been run?
Race.groovy:
package racetrack
class Race {
static hasMany = [registrations:Registration]
String name
Date startDate
String city
String state
BigDecimal distance
BigDecimal cost
Integer maxRunners
static constraints = {
name(blank:false, maxSize:50)
startDate(validator: {
return (it >= new Date())
}
)
city()
state(inList:["Moscow", "Volgograd", "SPb", "NN"])
distance(min:0.0)
cost(min:0.0, max:100.0)
maxRunners(min:0, max:100000)
}
static mapping = {
sort "startDate"
}
BigDecimal inMiles(){
return distance*0.6214
}
String toString(){
return "${name}, ${startDate.format('MM/dd/yyyy')}"
}
}
BootStrap should be in grails-app/conf instead. Refer this.
Try to print the errors of the new instance you save by adding
print begun.errors
See if somethings comes up.
You need to remove
def index() { }
from the controllers.
I had the same issue. Now it is fixed.

Grails - testing custom validator on domain class issue

I'm learning grails from Grails - getting started by Jason Rudolph book.
My domain class looks like that:
class Race {
String name;
Date startDateTime
String city
String state
Float distance
Float cost
Integer maxRunners = 10000
static hasMany = [registrations: Registration]
static constraints = {
name(maxSize: 50, blank: false)
startDateTime(validator: {
return it > new Date()
})
city(maxSize: 30, blank: false)
state(inList: ['GA', 'NC', 'SC', 'VA'], blank: false)
distance(min: 3.1f, max: 100f)
cost(min: 0f, max: 999.99f)
}
String toString() { "${this.name} : ${this.city}, ${this.state}" }
}
I want to test the custom validation of startDateTime field. Test looks like that:
class RaceTests extends GrailsUnitTestCase {
protected void setUp() {
super.setUp()
}
protected void tearDown() {
super.tearDown()
}
void testCustomDateValidation() {
def race = new Race()
race.startDateTime = null
assertFalse(race.validate())
}
}
Test looks similar to the one from book I mentioned earlier. But I'm getting
groovy.lang.MissingMethodException: No signature of method: racetrack.Race.validate() is applicable for argument types: () values: []
I'm stuck and didn't find any solution :/ Any help will be appreciated.
You're missing the mockForConstraintsTests() call. The common pattern is to do this in setUp()
protected void setUp() {
super.setUp()
mockForConstraintsTests(Race)
}
For details: http://mrhaki.blogspot.com/2009/04/unit-testing-constraints-in-domain.html
You should not use unit tests or mocking to test domain classes. Grails does create a unit test for domain classes and this should be changed. Move the class to the same package and folder under test/integration and change the base class to GroovyTestCase and you'll have a proper test that runs with the in-memory database and tests persistence, not the mocking framework.

How to handle Many-To-Many In Grails without belongsTo?

I need to create a many-to-many relationship in Grails.
I have a "Question" domain and a "Tag" domain.
A Question can have 0 or more tags. A Tag can have 0 or more Questions.
If I put a "hasMany" on each sides, it gives me an error saying I need a "belongTo" somewhere.
However, adding a belongsTo means that the owner must exist...
Like I said, a Tag could have 0 questions, and a Question could have 0 tags.
There is no concept of an owner, it's a many-to-many!
What am I supposed to do?
you can do this (please see the code below). but does it make sense to have a question tag with out both a question and a tag?
package m2msansbt
class Question {
String toString() { return name }
String name
static hasMany=[questionTags:QuestionTag]
static constraints = {
}
}
package m2msansbt
class Tag {
String toString() { return name }
String name
static hasMany=[questionTags:QuestionTag]
static constraints = {
}
}
package m2msansbt
class QuestionTag {
Question question
Tag tag
static QuestionTag link(Question question,Tag tag) {
QuestionTag questionTag=QuestionTag.findByQuestionAndTag(question,tag)
if (!questionTag) {
questionTag = new QuestionTag()
question?.addToQuestionTags(questionTag)
tag?.addToQuestionTags(questionTag)
questionTag.save()
}
return questionTag
}
static void unlink(Question question,Tag tag) {
QuestionTag questionTag=QuestionTag.findByQuestionAndTag(question,tag)
if (questionTag) {
question?.removeFromQuestionTags(questionTag)
tag?.removeFromQuestionTags(questionTag)
questionTag.delete()
}
}
static constraints = {
}
}
import m2msansbt.*
class BootStrap {
def init = { servletContext ->
Question q1=new Question(name:'q1')
Tag t1=new Tag(name:'t1')
Tag t2=new Tag(name:'t2')
q1.save()
t1.save()
t2.save()
QuestionTag q1t1=QuestionTag.link(q1,t1)
q1t1.save()
QuestionTag q1t2=QuestionTag.link(q1,t2)
q1t2.save()
q1.save()
t1.save()
}
def destroy = {
}
}
If your main concern is the cascading delete, you can take a look at 5.5.2.9 in the grails docs to manually disable it for the mapping.
I haven't tried it, but I think the mappedBy property can be used to solve this.
Checkout the Taggable Plugin.
It seems to solve the problem you are having and you could look at the source code if you want to see how they modeled the relationship. BTW this plugin was originally created by Graeme Rocher who is the lead developer on Grails.
This works for me on Grails 2.4.4. Add a "belongsTo" with just the classname.
class Question {
String toString() { return name }
String name
static hasMany=[tags:Tag]
static constraints = {
}
}
class Tag {
String toString() { return name }
String name
static hasMany=[questions:Question]
static belongsTo = Question
static constraints = {
}
}

Resources