Grails 2.2.4 testing what does assert means and response url - grails

I do not understand clearly the code behind the "assert". Please tell me what does those mean.
I already put the params attribute above my code . I did not describe detail.
void testSave() {
controller.save()
println 'testSave() from Controller'
assert model.serviceAccountInstance != null
assert view == '/serviceAccount/create'
response.reset()
populateValidParams(params)
controller.save()
println params.passWord
assert response.redirectedUrl == '/serviceAccount/show/1'
assert controller.flash.message != null
assert ServiceAccount.count() == 1
}
Than you for your answer.

Related

How to check the Groovy assert return?

My assert statement
assert actualObject==expectedObject : "Verify Status"
How can I validate the return statement(TRUE or FALSE) of the above assert statement?
Like
if(WebUI.verifyEqual(actualObject, expectedObject)==true){
My purpose:
if it's True I will do something, else I throw the assert error
How can I validate the return statement(TRUE or FALSE) of the above
assert statement?
My purpose: if it's True I will do something, else I throw the assert
error
That is what will happen if you do this:
assert actualObject==expectedObject : "Verify Status"
EDIT:
#daggett left a comment below indicating "i believe he don't want to throw error". If that is the case the question isn't clear but you can do that simply with an if:
if(actualObject==expectedObject) {
// do something
} else {
// don't throw an error
}
Or:
if(WebUI.verifyEqual(actualObject, expectedObject)) {
// do something
} else {
// don't throw an error
}

Grails generated controller test fails on delete

All of the generated controller tests for my Grails 3.1.8 application fail only when trying to delete a domain instance.
The error is:
java.lang.NullPointerException: Cannot invoke method fire() on null object
at gov.usda.fs.forecast.traits.UpdateMetadataTrait$Trait$Helper.afterDelete(UpdateMetadataTrait.groovy:30)
at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:216)
at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:201)
at org.grails.datastore.gorm.events.DomainEventListener.invokeEvent(DomainEventListener.java:259)
at org.grails.datastore.gorm.events.DomainEventListener.afterDelete(DomainEventListener.java:176)
at org.grails.datastore.gorm.events.DomainEventListener.onPersistenceEvent(DomainEventListener.java:96)
at org.grails.datastore.mapping.engine.event.AbstractPersistenceEventListener.onApplicationEvent(AbstractPersistenceEventListener.java:47)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:166)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:138)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:381)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:335)
at org.grails.datastore.mapping.engine.EntityPersister.firePostDeleteEvent(EntityPersister.java:340)
at org.grails.datastore.mapping.engine.NativeEntryEntityPersister.deleteEntity(NativeEntryEntityPersister.java:131)
at org.grails.datastore.mapping.engine.EntityPersister.delete(EntityPersister.java:262)
at org.grails.datastore.mapping.core.AbstractSession.delete(AbstractSession.java:728)
at org.grails.datastore.gorm.GormInstanceApi.delete_closure9(GormInstanceApi.groovy:268)
at groovy.lang.Closure.call(Closure.java:426)
at org.grails.datastore.mapping.core.DatastoreUtils.execute(DatastoreUtils.java:310)
at org.grails.datastore.gorm.AbstractDatastoreApi.execute(AbstractDatastoreApi.groovy:37)
at org.grails.datastore.gorm.GormInstanceApi.delete(GormInstanceApi.groovy:267)
at org.grails.datastore.gorm.GormEntity$Trait$Helper.delete(GormEntity.groovy:194)
at gov.usda.fs.forecast.InvoiceStatusController.$tt__delete(InvoiceStatusController.groovy:91)
at gov.usda.fs.forecast.InvoiceStatusController.delete_closure4(InvoiceStatusController.groovy)
at groovy.lang.Closure.call(Closure.java:426)
at groovy.lang.Closure.call(Closure.java:442)
at grails.transaction.GrailsTransactionTemplate$2.doInTransaction(GrailsTransactionTemplate.groovy:96)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
at grails.transaction.GrailsTransactionTemplate.execute(GrailsTran
Here is the test code:
void "Test that the delete action deletes an instance if it exists"() {
when:"The delete action is called for a null instance"
request.contentType = FORM_CONTENT_TYPE
request.method = 'DELETE'
controller.delete(null)
then:"A 404 is returned"
response.redirectedUrl == '/invoiceStatus/index'
flash.message != null
when:"A domain instance is created"
response.reset()
populateValidParams(params)
def invoiceStatus = new InvoiceStatus(params).save(flush: true)
then:"It exists"
InvoiceStatus.count() == 1
// The test fails when issuing the delete
when:"The domain instance is passed to the delete action"
controller.delete(invoiceStatus)
then:"The instance is deleted"
InvoiceStatus.count() == 0
response.redirectedUrl == '/invoiceStatus/index'
flash.message != null
}
I get the same error on every generated test in the same spot. Any clues would be greatly appreciated.

Grails: Clear errors when passing object to view

I am checking if my user has entered the same password twice in controller:
if(params.password1 != "" || params.password2 != "") {
// change password
if(params.password1 != params.password2) {
user.errors.rejectValue('password', 'noMatch')
}
user.password = params.password1
}
if (user.errors.errorCount > 0 || !user.save()) {
println user.errors.errorCount
render(view: "edituser", model: [userInstance: user])
return
}
I get the correct user error count displayed in console so my if-clause works and Grails does not re-validate the object by using double pipe.
When I have a look into my GSP I get the following message:
grails.validation.ValidationErrors: 0 errors
It seams like Grails clears all errors or uses an other object. It's curious, because my custom validator in my domain class works fine...

Geb 'at null' when asserting page

I'm trying to write a simple Geb/Spock test using Grails but I am receiving the following test failure.
| Failure: login works correctly(...UserAuthAcceptanceSpec)
| Condition not satisfied:
at HomePage
|
null
I can follow the test through with a debugger using a browser and can see that the application works as expected and the correct heading is being shown. However, the test is failing when I try to invoke the at checker. Can anyone tell me why the final assertion in the test might be failing and why the 'at' checker appears to be null?
Here is my code: (Geb v0.9.0, Grails 2.2.2)
Spock Specification:
class UserAuthAcceptanceSpec extends GebReportingSpec {
def "login works correctly"() {
given: "the correct credentials"
def theCorrectUsername = "admin"
def theCorrectPassword = "password"
when: "logging in"
to LoginPage
username = theCorrectUsername
password = theCorrectPassword
submitButton.click() //([HomePage, LoginPage])
then: "the welcome page is shown"
heading =~ /(?i)Welcome.*/ // <- same as 'at' checker in HomePage
and: "the 'at' checker works"
at HomePage // <- fails
}
LoginPage:
class LoginPage extends Page {
final String path = "/login/auth"
static content = {
heading(required: false, wait:true) { $("h1").text() }
username { $("input", name:"j_username") }
password { $("input", name:"j_password") }
submitButton { $("input", id:"submit") }
}
static at = {
title =~ /Login.*/
}
}
HomePage:
class HomePage extends Page {
final String path = "/"
static content = {
heading(required: false, wait:true) { $("h1").text() }
}
static at = {
heading =~ /(?i)Welcome.*/
}
}
The at checker should use ==~ rather than =~.
Geb's implicit assertions mean the statements:
heading1 =~ /(?i)Welcome.*/
heading2 ==~ /(?i)Welcome.*/
effectively become:
assert (heading1 =~ /(?i)Welcome.*/) == true // [1]
assert (heading2 ==~ /(?i)Welcome.*/) == true // [2]
[2] will evaluate to a boolean and pass/fail as expected, whereas [1] evaluates to a java.util.regex.Matcher causing the failure.
See the Groovy Regex FAQ for an explanation of the difference between the two syntaxes.

How to write a conditional if for user privilege in symfony 1.4 using sfGuard plugin

I am trying to write a condition that will check for a specific parameter and the user credentials. If the user does not have one of two credentials and passes the correct parameter, they will get forwarded to a secure login screen.
This is what I did have that worked:
if ($request->getParameter('profile_type') == 'foo' && !$this->getUser()->isAdmin()) {
return $this->redirect('sfGuardAuth/secure');
}
But I have added a new user group so I need something like this:
if ($request->getParameter('profile_type') == 'foo' && (!$this->getUser()->isAdmin() || !$this->getUser()->isFaculty()) {
return $this->redirect('sfGuardAuth/secure');
}
So if you are not admin OR faculty, you get the redirect.
Is this a syntax or logic issue?
UPDATE
This is working, but it seems wrong logically
if (($request->getParameter('profile_type') == 'foo') && (!$this->getUser()->isAdmin() && !$this->getUser()->isFaculty())) {
return $this->redirect('sfGuardAuth/secure');
}
(!$this->getUser()->isAdmin() || !$this->getUser()->isFaculty())
should be
!($this->getUser()->isAdmin() || $this->getUser()->isFaculty())
Update, with more explanation: you want to redirect the user if both isAdmin and isFaculty return false, and not redirect if either is yes.
If both are false:
!(false || false) == !false == true => redirect
!(true || false) == !true == false => no redirect
Just to answer the question, this is what I got to work:
if (($request->getParameter('profile_type') == 'foo') && (!$this->getUser()->isAdmin() && !$this->getUser()->isFaculty())) {
return $this->redirect('sfGuardAuth/secure');
}
I am not sure how the OR/AND operator is working to get the desired result, but it works, so that is an ok compromise.

Resources