Grails log4j configuration - grails

I've repeatedly had problems with the DSL introduced by Grails in version 1.1 for configuring Log4J. My current configuration looks like this:
log4j = {
debug 'com.mypackages'
appenders {
console name: 'stdout', layout: pattern(conversionPattern: '%d{dd-MM-yyyy HH:mm:ss,SSS} %5p %c{1} - %m%n')
rollingFile name: 'hibeFile', file: "hibeFile", maxFileSize: '500KB'
}
// By default, messages are logged at the error level to both the console and hibeFile
root {
error 'stdout', 'hibeFile'
additivity = true
}
}
The intention here is:
Log com.mypackages at the debug level and all others at the error level
Log all output to a file named hibeFile and the console
This works fine when I run the application or the integration tests. However, when I run the unit tests no logging appears either in the console, or in the "System.out" or "System.err" links shown in the Grails test report. How can I see my logs when running unit tests?
Thanks,
Don

AFAIK, when running a Grails unit test, the whole logging is not available via log4j, the log.xxxx calls in domain classes etc. are just mocked using
mockLogging(ClassUnderTest, true)
The "true" stands for "enable debug". In order to do so, the unit test class must extend GrailsUnitTestCase.
If you use mockController(class), it implicitly calls mockLogging(class, false) and therefore you get no debug logging. For details check out the grails sources, esp GrailsUnitTestCase and MockUtils.
In opposite to the above, in an integration test the whole machinery is started and log4j is available.

Here is a thought, you didn't ask this exactly but the same question was asked on the grails user group. I am posting my answer here as well, to spread the knowledge.
If you are explicitly saying def log = org.apache.commons.logging.LogFactory.getLog(this) in your classes rather than relying on dependency injection as was explained on the grails user group you can mock the getLog on the LogFactory.
The below is taken from grails.tests.MockUtils.mockLogging and modified to return the logger.
class LoggingEnabledTestCase extends GrailsUnitTestCase {
protected void setUp() {
super.setUp()
registerMetaClass(org.apache.commons.logging.LogFactory)
org.apache.commons.logging.LogFactory.metaClass.'static'.getLog = {instance ->
// This is taken from grails.tests.MockUtils and slightly changed to return a logger.
// Get the name of the class + the last component of the package
// (if it the class is in a package).
def pos = instance.class.name.lastIndexOf('.')
if (pos != -1) pos = instance.class.name.lastIndexOf('.', pos - 1)
def shortName = instance.class.name.substring(pos + 1)
// Dynamically inject a mock logger that simply prints the
// log message (and optional exception) to stdout.
def mockLogger = [
fatal: {String msg, Throwable t = null ->
println "FATAL (${shortName}): $msg"
if (t) {
println " Exception thrown - ${t.message}"
}
},
error: {String msg, Throwable t = null ->
println "ERROR (${shortName}): $msg"
if (t) {
println " Exception thrown - ${t.message}"
}
},
warn: {String msg, Throwable t = null ->
println "WARN (${shortName}): $msg"
if (t) {
println " Exception thrown - ${t.message}"
}
},
info: {String msg, Throwable t = null ->
println "INFO (${shortName}): $msg"
if (t) {
println " Exception thrown - ${t.message}"
}
},
debug: {String msg, Throwable t = null ->
println "DEBUG (${shortName}): $msg"
if (t) {
println " Exception thrown - ${t.message}"
}
},
trace: {String msg, Throwable t = null -> },
isFatalEnabled: {-> true},
isErrorEnabled: {-> true},
isWarnEnabled: {-> true},
isInfoEnabled: {-> true},
isDebugEnabled: {-> true},
isTraceEnabled: {-> false}] as Log
return mockLogger
}
}
protected void tearDown() {
super.tearDown()
}
}

Using Grails 1.1.1 and an approximation to your setup, I have a unit test called FooTests.groovy
After running grails test-app, I am able to see the output from the test in the directory:
./test/reports/plain
specifically in the files, as appropriate:
TEST-com.mypackages.FooTests-err.txt
TEST-com.mypackages.FooTests-out.txt
TEST-com.mypackages.FooTests.txt
Note that I see no output in the hibeFile. I'm not sure, but I suspect a previous poster is correct in that unit tests don't receive the logging setup.

Related

How to WebFluxTest whit ReactiveSecurityContexHolder

Here a simple Class reading the Principal user from the spring security context:
public Mono<Void> getAndCheckAccessRights(Integer agencyKey) {
return ReactiveSecurityContextHolder.getContext()
.map(securityContext -> getAccessRights(agencyKey, securityContext.getAuthentication().getName()))
.switchIfEmpty(Mono.defer(() -> {
log.error("No security context found!");
throw new AuthorizationException("No security context found!");
}))
.flatMap(accessRightsDtoMono -> checkAccessRights(accessRightsDtoMono))
.then();
}
private Mono<AccessRightsDto> getAccessRights(Integer agencyKey, String bensl) {
return dataServiceWebClient.get()
.uri("/access_rights/" + agencyKey + "/" + bensl)
.retrieve()
.bodyToMono(AccessRightsDto.class)
.switchIfEmpty(Mono.defer(() -> {
log.error("No user found!");
throw new AuthorizationException("No user found!");
}));
}
While testing it does not what it is supposed to do, execution just jump code lines over without executing method streams in .map or .flatMap,
Logs are not printed and there is no debug logging on any level, test just runs as everithing is terminated correctly, i dont have any clue whatsovewer why this happen:
#WebFluxTest(AccessRightService.class)
...
#Test
#WithMockUser
void getAndCheckAccessRights_NOT_AUTHORIZED() throws JsonProcessingException {
AccessRightsDto testAccessRightsDto = AccessRightsDto
.builder(123456789, "test", "test", PUBLISH, PUBLISH, PUBLISH, PUBLISH, PUBLISH,
PUBLISH, PUBLISH, PUBLISH, NO_ACCESS)
.build();
MockResponse response = new MockResponse();
response.setResponseCode(HttpStatus.OK.value()).setBody(objectMapper.writeValueAsString(testAccessRightsDto));
mockWebServer.enqueue(response);
assertThrows(AuthorizationException.class, () -> accessRightService.getAndCheckAccessRights(123456789));
}
Off course when running the application it just work correctly as expected, testing is strange!
App running with spring boot 2.2.2 and okhttp3 mockwebserver.
My bad, I forgot to put.block() after getAndCheckAccessRights which propagate the input of getting the object, whitout that nothing will happen.
It is actually not that clear how does it works in case of Netty, because it does not accept .block()'s but on tests is ok to call it.

Verifying Jenkins calls while testing pipeline code

I am writing a Jenkins pipeline library, and am having some difficulties with mocking/validating an existing Jenkins pipeline step.
I am using jenkins-spock by homeaway to unit test, but I think my problem is more Spock related.
import com.homeaway.devtools.jenkins.testing.JenkinsPipelineSpecification
import com.company.pipeline.providers.BuildLogProvider
class PublishBuildLogSpec extends JenkinsPipelineSpecification {
BuildLogProvider buildLogProvider = Mock()
PublishBuildLog publishBuildLog
def setup () {
publishBuildLog = new PublishBuildLog(buildLogProvider: buildLogProvider)
explicitlyMockPipelineStep('writeFile')
}
def "Gets the log file contents for a specific job and build"() {
when:
"the call method is executed with the jobName and buildNumber parameters set"
publishBuildLog.call("JOBNAME", "42")
then:
"the getBuildLog on the buildLogProvider is called with those parameters"
1 * buildLogProvider.getBuildLog("JOBNAME", "42")
}
def "the contents of log file is written to the workspace"() {
given:
"getBuildLog returns specific contents"
def logFileText = "Example Log File Text"
buildLogProvider.getBuildLog(_, _) >> logFileText
when:
"publishBuildLog.call is executed"
publishBuildLog.call(_, _)
then:
"the specific contents is passed to the writeFile step"
1 * getPipelineMock("writeFile").call([file: _ , text: logFileText])
}
}
This is my unit test. I am attempting to say that writeFile is called with the text matching the contents of logFileText, ignoring what the other parameters are. I have tried numerous combinations, but always seem to get the same or similar response to response of:
Too few invocations for:
1 * getPipelineMock("writeFile").call([file: _ , text: "Example Log File Text"]) (0 invocations)
Unmatched invocations (ordered by similarity):
1 * (explicit) getPipelineMock("writeFile").call(['file':'filename', 'text':'Example Log File Text'])
This is to test this class
import com.company.pipeline.providers.BuildLogProvider
class PublishBuildLog {
BuildLogProvider buildLogProvider = new BuildLogProvider()
void setBuildLogProvider(BuildLogProvider buildLogProvider) {
this.buildLogProvider = buildLogProvider
}
def call(def jobName, def buildNumber) {
def contents = buildLogProvider.getBuildLog(jobName, buildNumber)
writeFile(file: "filename", text: contents)
}
}
I am at a loss as to how to validate this call. I have a lot of experience with Java and Junit, but I am relatively new to Spock.
How can I verify this?
For me your test passes. But there is one thing I find strange: You use jokers in a when: block where you should really use concrete parameters like in the first feature method:
when: "publishBuildLog.call is executed"
publishBuildLog.call(_, _)
Instead you should write:
when: "publishBuildLog.call is executed"
publishBuildLog.call("JOBNAME", "42")
For me this works just fine if I use this as a dummy class in order to make the code compile (because you did not provide the source code):
class BuildLogProvider {
def getBuildLog(def jobName, def buildNumber) {}
}

How to use spock to test a service with real transactions in Grails application?

I am using Grails 2.4.4 and want to apply spock tests in my project.
Below is the MemberService:
#Transactional
class MemberService {
def lastMember = null
def login (userId, password) {
def member = Member.findByLoginEmail(userId)
if(!member) return LoginResult.NO_SUCH_MEMBER
if (member.isLocked) return LoginResult.MEMBER_IS_LOCKED
log.debug("member.password is [${member.passwd}], input password is [${password}]")
if (!member.passwd.equals(password)) return LoginResult.PASSWORD_ERROR
switch(member.validateResult) {
case "FAILED":
return LoginResult.VAILDATE_FAILED
case "WAIT":
return LoginResult.WAIT_VALIDATE
}
member.lasLoginTime = new Timestamp(System.currentTimeMillis())
member.lastLoginPlatform = "WEB"
member.loginFailCount = 0
member.save()
lastMember = member
return LoginResult.SUCCESS
}
enum LoginResult {
SUCCESS,
NO_SUCH_MEMBER,
PASSWORD_ERROR,
MEMBER_IS_LOCKED,
VAILDATE_FAILED,
WAIT_VALIDATE
}
enum ValidateResult {
SUCCESS,
FAILED,
WAIT
}
}
MemberServiceSpec is as below:
#TestFor(MemberService)
#Mock(Member)
class MemberServiceSpec extends Specification {
def memberService
def setup() {
memberService = new MemberService()
}
void "test something"() {
when:
println "service is ${service}"
def result = memberService.login("aa#mymail.com", "123456")
println "result is ${result}"
then:
result == MemberService.LoginResult.SUCCESS
}
}
The test result is as below:
Testing started at 15:15 ...
|Loading Grails 2.4.4
|Configuring classpath
.
|Environment set to test
....................................
|Running without daemon...
..........................................
|Compiling 1 source files
.
|Running 2 unit tests...|Running 2 unit tests... 1 of 2
--Output from test something--
1. setup
service is cusine_market.MemberService#54c425b1
Enter MemberService.login----------------userId=[aa#mymail.com]
result is NO_SUCH_MEMBER
Failure: |
test something(cusine_market.MemberServiceSpec)
|
Condition not satisfied:
result == MemberService.LoginResult.SUCCESS
| |
| false
NO_SUCH_MEMBER
at cusine_market.MemberServiceSpec.test something(MemberServiceSpec.groovy:32)
Condition not satisfied:
result == MemberService.LoginResult.SUCCESS
| |
| false
NO_SUCH_MEMBER
Condition not satisfied:
result == MemberService.LoginResult.SUCCESS
| |
| false
NO_SUCH_MEMBER
at cusine_market.MemberServiceSpec.test something(MemberServiceSpec.groovy:32)
|Completed 1 unit test, 1 failed in 0m 6s
.Tests FAILED
I confirm that the user exists in database.
Could anyone tell me why MemberService can not find the user "aa#mymail.com" in database? I also tried the below line
memberService = Mock(MemberService);
The result is the same. However, if I run-app, the service does find the user.
Mocking
In your test you're testing MemberService, which means you don't need to mock it. Just refer to it via service.
Test Data
This looks like unit test, not the integration one. So it doesn't use your database at all.
What you need to do is to create the Member instance manually in test. You have it in your #Mock annotation, which is good.
Now, create the object, preferably in a given block:
void "test something"() {
given:
new Member(loginEmail: 'aa#mymail.com', password: '123456', ...).save(failOnError: true, flush: true)
when:
println "service is ${service}"
def result = service.login("aa#mymail.com", "123456")
println "result is ${result}"
then:
result == MemberService.LoginResult.SUCCESS
}
I added failOnError: true to make sure the object has actually been created. You - of course - need to provide all required properties in Member constructor and make sure the data you provide correspond to the one you provide to login method.
Also, you need to ensure the Member object is initialized in a way that fulfills the path you want to reach. For example, if you want to reach LoginResult.SUCCES state, you need to set member.isLocked to false, etc.
BONUS
When you get this test to work, you may want to take a look into Build Test Data Plugin. It makes creating test data - like your Member object - much easier.

run something async in Grails 2.3

In My Grails service, there is a part of a method I wish to run asynchronously.
Following, the doc for 2.3.x http://grails.org/doc/2.3.0.M1/guide/async.html
I do
public class MyService {
public void myMethod() {
Promise p = task {
// Long running task
}
p.onError { Throwable err ->
println "An error occured ${err.message}"
}
p.onComplete { result ->
println "Promise returned $result"
}
// block until result is called
def result = p.get()
}
}
However, I want to execute mine without any blocking. The p.get() method blocks. How do I execute the promise without any sort of blocking. I don't care if myMethod() returns, it is a kinda of fire and forget method.
So, according to the documentation if you don't call .get() or .waitAll() but rather just make use of onComplete you can run your task without blocking the current thread.
Here is a very silly example that I worked up in the console to as a proof of concept.
import static grails.async.Promises.*
def p = task {
// Long running task
println 'Off to do something now ...'
Thread.sleep(5000)
println '... that took 5 seconds'
return 'the result'
}
p.onError { Throwable err ->
println "An error occured ${err.message}"
}
p.onComplete { result ->
println "Promise returned $result"
}
println 'Just to show some output, and prove the task is running in the background.'
Running the above example gives you the following output:
Off to do something now ...
Just to show some output, and prove the task is running in the background.
... that took 5 seconds
Promise returned the result

Definition of page component template '$' of 'ApplicationSummaryPage' is invalid, params must be either a Closure, or Map and Closure

Below is my Geb Page, Spec and Error. I am not sure where and what the issue is. When I remove the below from the ApplicationSummaryPage then I doesn't get this error.
ds(wait: true)
{ module DatasourceInformationRow, $("table.ic-table-creditReportProduct table tr", it) }
class ApplicationSummaryPage extends Page
{
static url = "application-summary/application-summary.jsf"
static at =
{ assert title == "Application Summary" }
static content =
{
menu
{ module MenuModule }
userPanel
{module UserPanelModule }
ds(wait: true)
{ module DatasourceInformationRow, $("table.ic-table-creditReportProduct table tr", it) }
applicationSummaryForm
{ $("#applicationSummary_form") }
workItemDiv
{$("div", id: "consumerWorkItems:workItemPanel")}
workItemsSection
{workItemDiv.find (".ui-panel-title").text()}
resubmitLink
{$("a",id: "loginForm")}
overrideDecisionLink
{$("a",id: "loginForm")}
addNoteLink
{$("a",id: "loginForm")}
}
}
Spec
class SearchSpec extends BaseUiSpec
{
def setup()
{
login("manager")
}
def "cccc"()
{
when: "I search"
to SearchPage
at SearchPage
applicationid.value "10002000000010000"
searchButton.click()
at SearchPage
waitFor
{ searchResultsData }
println "------------"+ searchResults(0).ApplicationId.text()
searchResults(0).ApplicationId.click(ApplicationSummaryPage)
Thread.sleep 5000
at ApplicationSummaryPage
println "-----???"+ ds
}
}
Error
geb.waiting.WaitTimeoutException: condition did not pass in 15.0 seconds (failed with exception)
at geb.waiting.Wait.waitFor(Wait.groovy:126)
at geb.content.PageContentTemplate.create(PageContentTemplate.groovy:117)
at geb.content.PageContentTemplate.get(PageContentTemplate.groovy:98)
at geb.content.NavigableSupport.getContent(NavigableSupport.groovy:43)
at geb.content.NavigableSupport.propertyMissing(NavigableSupport.groovy:127)
at geb.Browser.propertyMissing(Browser.groovy:175)
at geb.spock.GebSpec.propertyMissing(GebSpec.groovy:55)
at test.SearchSpec.cccc(SearchSpec.groovy:33)
Caused by: geb.error.InvalidPageContent: Definition of page component template '$' of 'ApplicationSummaryPage' is invalid, params must be either a Closure, or Map and Closure (args were: [class java.lang.String, null])
at geb.content.PageContentTemplateBuilder.throwBadInvocationError(PageContentTemplateBuilder.groovy:69)
at geb.content.PageContentTemplateBuilder.methodMissing(PageContentTemplateBuilder.groovy:51)
at groovy.lang.GroovyObjectSupport.invokeMethod(GroovyObjectSupport.java:44)
at com.equifax.ic.testing.framework.ui.pages.applicationmanagement.ApplicationSummaryPage._clinit__closure2_closure5(ApplicationSummaryPage.groovy:24)
at com.equifax.ic.testing.framework.ui.pages.applicationmanagement.ApplicationSummaryPage._clinit__closure2_closure5(ApplicationSummaryPage.groovy)
at geb.content.PageContentTemplate.invokeFactory(PageContentTemplate.groovy:134)
at geb.content.PageContentTemplate.create_closure1(PageContentTemplate.groovy:103)
at geb.content.PageContentTemplate.create_closure1(PageContentTemplate.groovy)
at geb.waiting.Wait.waitFor(Wait.groovy:115)
... 7 more
The problem was that i wan't passing the index for ds. The corrected version is below
println "-----???"+ ds(0)
Got the response on Geb mailing list. Posting here for others.

Resources