The requirement
I'm trying to run my JavaScript tests in a custom test phase based in the functional test phase. Basically it needs to:
Startup embedded Tomcat
Open a controller
Check the result of the executed tests
What I've done
First, I created my custom test phase, based on this post. So my _Events.groovy looks like
includeTargets << new File("${basedir}/scripts/_RunJavaScriptUiTests.groovy")
eventConfigureTomcat = { tomcat ->
tomcat.connector.setAttribute("compression", "on")
tomcat.connector.setAttribute("compressableMimeType", "text/html,text/xml,text/plain,application/javascript")
tomcat.connector.port = serverPort
}
eventAllTestsStart = {
phasesToRun << "uijs"
}
uijsTests = ["uijs"]
uijsTestPhasePreparation = {
functionalTestPhasePreparation()
}
uijsTestPhaseCleanUp = {
functionalTestPhaseCleanUp()
}
eventTestPhaseEnd = { phase ->
if( phase == "uijs" ) {
runJavaScriptUiTests()
}
}
Next, I decided to use PhantomJS to open my page and analyze the executed tests. So I used this in the RunJavaScriptUiTests.groovy script
target(runJavaScriptUiTests:"Running Siesta tests") {
event("StatusUpdate", ["Siesta test phase..."])
//this is the script that evaluates the result of the tests
File script = new File("web-app/js/siesta/siesta-phantomjs-runner.js")
String home = System.getenv("PHANTOMJS_HOME")
if(!home) {
throw new RuntimeException("PHANTOMJS_HOME must be set.")
}
String executable = "${home}bin${File.separator}phantomjs"
String port = System.getProperty("server.port","8080")
String url = "http://localhost:$port/insoft-ext-ui/siesta" //url of my tests
println "Running Phantomjs ${executable} ${script.absolutePath} "
try {
ant.exec(executable: executable, outputproperty: "cmdOut", failonerror: 'true', errorproperty: "cmdErr") {
arg(value: script.absolutePath)
arg(value: url)
}
}catch(e) {
println "ERROR: $e"
throw e
}
try {
String output = "${ant.project.properties.cmdOut}"
println output
}catch(e) {
event("StatusError",["Exception $e"])
}
}
I can see that the functionalTestPhasePreparation runs, because this starts up my application correctly. I can also see that the phantomjs command is correct, when it prints:
Running: /desenv/phantomjs-1.9.2/bin/phantomjs /desenv/java/projetos/insoft-ext-ui/web-app/js/siesta/siesta-phantomjs-runner.js http://localhost:8080/insoft-ext-ui/siesta
But this gives me the groovy.lang.MissingPropertyException
groovy.lang.MissingPropertyException: No such property: org.codehaus.grails.INCLUDED_JS_LIBRARIES for class: org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:50)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:273)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:53)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at org.codehaus.groovy.grails.web.filters.JavascriptLibraryFilters$_closure1_closure2_closure3.doCall(JavascriptLibraryFilters.groovy:27)
at org.codehaus.groovy.grails.web.filters.JavascriptLibraryFilters$_closure1_closure2_closure3.doCall(JavascriptLibraryFilters.groovy)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
For full Stacktrace see here.
The interesting is that if I just do
grails test run-app
phantomjs /desenv/java/projetos/insoft-ext-ui/web-app/js/siesta/siesta-phantomjs-runner.js http://localhost:8080/insoft-ext-ui/siesta
The script works and I don't get any exception.
The question
Why MissingPropertyException is thrown? I looked at JavascriptLibraryFilters and didn't find a reason for it.
EDIT
About the Tomcat
I'm using the embedded Tomcat that comes with Grails, but enabling compressing in the _Events.groovy:
eventConfigureTomcat = { tomcat ->
tomcat.connector.setAttribute("compression", "on")
tomcat.connector.setAttribute("compressableMimeType", "text/html,text/xml,text/plain,application/javascript")
tomcat.connector.port = serverPort
}
I do not have a direct solution, but I can help you research this.
The source of your problem is apparently org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper, which is applied in your Tomcat environment, which explains why your code works standalone.
Other issues which refer to this same Spring class exist on Stack Overflow. Most of them are problems regarding incorrect multi-part request processing. This would lead me to believe PhantomJS is making multi-part calls without the appropriate casting or interfaces for your environment. I suspect a change to either your Tomcat or Grails configuration may be required.
Here are several of the SO questions to which I refer:
SO: Uploading file throws No signature of method exception (in getFile() method)
SO: uploading a file in grails
SO: Communication between Signed Applet and server side Controller
SO: java.lang.IllegalStateException: Standard argument type [org.springframework.web.multipart.MultipartHttpServletRequest]
Here is a potentially relevant bug on Grails / CXF:
Spring Security bug, referring to a CXF bug, which says "To enable MTOM on CXF you have to disable Grails' multipart handling by setting the option grails.web.disable.multipart=true in Config.groovy"
Please provide any details regarding your Tomcat / Grails settings and/or confirm that you have investigated these potential issue paths so that we may discount them.
Hopefully this answer points you or others in the right direction for a proper solution.
Related
I'm trying to utilize jOOQ's ability to generate from Liquibase files. My file structure is as follows:
C
- dev
-- testproject
--- src/main/resources
---- db
----- changelog.xml
In order to reference this file from the jOOQ configuration, I have the following in my build.gradle.kts:
jooq {
configurations {
create("main") {
jooqConfiguration.apply {
generator.apply {
database.apply {
name = "org.jooq.meta.extensions.liquibase.LiquibaseDatabase"
properties.add(Property().apply {
key = "rootPath"
value = "C:/dev/testproject/src/main/resources/db/"
})
properties.add(Property().apply {
key = "scripts"
value = "changelog.xml"
})
}
}
}
}
}
}
I'm using plugin version 7.1.1 and have the following dependencies:
dependencies {
implementation("org.liquibase:liquibase-core:4.8.0") // I tried removing this, no change
jooqGenerator("org.postgresql:postgresql:42.3.2")
jooqGenerator("org.jooq:jooq-meta-extensions-liquibase:3.17.2")
jooqGenerator(files("src/main/resources")) // I don't think this is necessary
}
When I try to run jooqGenerate, the error I get is:
Caused by: liquibase.exception.ChangeLogParseException: The file changelog.xml was not found in
Specifying files by absolute path was removed in Liquibase 4.0. Please use a relative path or add '/' to the classpath parameter.
at liquibase.parser.core.xml.XMLChangeLogSAXParser.parseToNode(XMLChangeLogSAXParser.java:82)
at liquibase.parser.core.xml.AbstractChangeLogParser.parse(AbstractChangeLogParser.java:15)
at liquibase.Liquibase.getDatabaseChangeLog(Liquibase.java:369)
Notice how it doesn't say which directories it looked in. As far as I can tell, the resource accessor is not receiving the rootPath from the configuration. The relevant output from Liquibase is here. Again, it should say it looked in the rootPath, but it doesn't print anything else, so there must be no directories searched.
Not sure if this is helpful, but the jOOQ configuration file in build/tmp/generateJooq definitely has the rootPath:
<property>
<key>rootPath</key>
<value>C:/dev/testproject/src/main/resources/db/</value>
</property>
I'm not sure where I'm going wrong. I've also tried the following values of scripts without setting rootPath and seen the same behavior:
C:/dev/testproject/src/main/resources/db/changelog.xml
src/main/resources/db/changelog.xml
/src/main/resources/db/changelog.xml
classpath:src/main/resources/db/changelog.xml
classpath:/src/main/resources/db/changelog.xml
This was causing the problem (or rather, the confusion):
jooqGenerator(files("src/main/resources"))
Apparently, this sets the classpath of the jooqGenerator task to be src/main/resources! So, knowing that, I fixed my configuration to look like this:
database.apply {
name = "org.jooq.meta.extensions.liquibase.LiquibaseDatabase"
properties.add(Property().apply {
key = "scripts"
value = "classpath:db/changelog.xml"
})
}
Everything is working nicely now.
I want to use interfaces for both client and server in the same android app. Usecase is to run a okhttpmockwebserver serving gRPC requests within the same app the client is running in. For this i created two library projects with their own wire configuration for client and server similar to those
wire {
kotlin {
includes = ['com..caompany.android.proto.*']
out "${buildDir}/protos"
rpcCallStyle = 'suspending'
rpcRole = 'client'
}
}
wire {
kotlin {
includes = ['com..company.android.proto.*']
out "${buildDir}/protos"
rpcCallStyle = 'suspending'
rpcRole = 'server'
}
}
Executing the wire-gradle-plugin fails with this exception:
com.company.android.proto.HelloReply$Companion$ADAPTER$1 is defined multiple times.
Caused by: com.android.tools.r8.CompilationFailedException: Compilation failed to complete, origin: .../com/company/android/proto/HelloReply$Companion$ADAPTER$1.dex
It would help me if wire could either
Generate all classes and interfaces at once including server and client role or
Exclude the generation of class files, only generating service interfaces for client or server
Is there a workaround i can achieve a similar result without gradle plugin support?
You can have multiple kotlin blocks at the same time. Wire will throw if you generate the same class twice so you need to define the rule as unique between both.
You need one block which will generate client role interfaces. You need one block to generate server roles interfaces. Lastly, you need to generate regular types in yet another block, or in one of them (but not both).
Something like this
wire {
kotlin {
includes = ['all.services.or.package']
rpcCallStyle = 'suspending'
rpcRole = 'client'
}
kotlin {
includes = ['all.services.or.package']
rpcCallStyle = 'suspending'
rpcRole = 'server'
}
kotlin {
excludes = ['all.services.or.package']
rpcRole = 'none'
}
}
I'm working on a small Spring Boot application in Kotlin and now I want to secure it using JJWT. Roughly speaking, I am translating this tutorial to my use case: https://jakublesko.com/spring-security-with-jwt/
In the project I have this AuthenticationFilter:
class JwtAuthenticationFilter(val authManager: AuthenticationManager) : UsernamePasswordAuthenticationFilter() {
// other stuff omitted for brevity
override fun successfulAuthentication(request: HttpServletRequest?, response: HttpServletResponse?, chain: FilterChain?, authResult: Authentication?) {
val user = authResult?.principal as User
val roles = user.authorities
.stream()
.map(GrantedAuthority::getAuthority)
val signingKey = JWT_SECRET.toByteArray()
val token = Jwts.builder()
.signWith(SignatureAlgorithm.HS512, Keys.hmacShaKeyFor(signingKey))
.setHeaderParam("typ", TOKEN_TYPE) // validate that "typ" does not actually mean "type"
.setIssuer(TOKEN_ISSUER)
.setAudience(TOKEN_AUDIENCE)
.setSubject(user.username)
.setExpiration(Date(System.currentTimeMillis() + 864000000))
.claim("rol", roles)
.compact()
response?.addHeader(TOKEN_HEADER, TOKEN_PREFIX + token)
}
}
When I post to the authentication URL that is supposed to issue a token though, I receive:
java.lang.NoSuchMethodError: io.jsonwebtoken.SignatureAlgorithm.getMinKeyLength()I
at io.jsonwebtoken.security.Keys.hmacShaKeyFor(Keys.java:69) ~[jjwt-api-0.10.7.jar:na]
I can debug into the successfulAuthentication method and see that it is called with reasonable parameters. What catches my eye is the "I" after the parenthesis at the end of the getMinKeyLength()I. My googling skills apparently do not suffice to find a reason why it is there, but I strongly suspect it is related to reflection & calling Java libs from Kotlin code.
Is anyone around who can tell me how to fix this? I have run out of guesses.
I stumbled upon the same issue. The reason for this is most probably a dependency conflict. Check if you have any other dependency using jjwt. For us it was com.twilio.sdk.
You can do it using
mvn dependency:tree -Dverbose
After you've identified the conflicting dependency you can either match your jjwt version or exclude it from the dependency.
to run java code from Kotlin, please put the java sources under the folder
main/java
Also, your kotlin sources are located under main/kotlin
EDIT: Please let me be clear, I'm asking how to do this in Grails using Spring Dependency Injection, and NOT Grails' metaclass functionality or new().
I have a grails service that is for analyzing log files. Inside the service I use the current time for lots of things. For unit testing I have several example log files that I parse with this service. These have times in them obviously.
I want my service, DURING UNIT TESTING to think that the current time is no more than a few hours after the last logging statement in my example log files.
So, I'm willing to this:
class MyService {
def currentDate = { -> new Date() }
def doSomeStuff() {
// need to know when is "right now"
Date now = currentDate()
}
}
So, what I want to be able to do is have currentDate injected or set to be some other HARDCODED time, like
currentDate = { -> new Date(1308619647140) }
Is there not a way to do this with some mockWhatever method inside my unit test? This kind of stuff was super easy with Google Guice, but I have no idea how to do it in Spring.
It's pretty frustrating that when I Google "grails dependency injection" all I find are examples of
class SomeController {
// wow look how amazing this is, it's injected automatically!!
// isn't spring incredible OMG!
def myService
}
It feels like all that's showing me is that I don't have to type new ...()
Where do I tell it that when environment equals test, then do this:
currentDate = { -> new Date(1308619647140) }
Am I just stuck setting this property manually in my test??
I would prefer not to have to create a "timeService" because this seems silly considering I just want 1 tiny change.
Groovy is a dynamic language, and as such it allows you to do almost what you're asking for:
class MyServiceTests extends GrailsUnitTestCase {
def testDoSomeStuff() {
def service = new MyService()
service.currentDate = { -> new Date(1308619647140) }
// assert something on service.doSomeStuff()
}
}
Keep in mind this only modifies the service instance, not the class. If you need to modify the class you'll need to work with the metaClass. Take a look at this post by mrhaki.
Another option would be to make the current date a parameter to doSomeStuff(). That way you wouldn't need to modify your service instance.
Thanks for the help guys. The best solution I could come up with for using Spring DI in this case was to do the following in
resources.groovy
These are the two solutions I found:
1: If I want the timeNowService to be swapped for testing purposes everywhere:
import grails.util.GrailsUtil
// Place your Spring DSL code here
beans = {
if (GrailsUtil.environment == 'test') {
println ">>> test env"
timeNowService(TimeNowMockService)
} else {
println ">>> not test env"
timeNowService(TimeNowService)
}
}
2: I could do this if I only want this change to apply to this particular service:
import grails.util.GrailsUtil
// Place your Spring DSL code here
beans = {
if (GrailsUtil.environment == 'test') {
println ">>> test env"
time1(TimeNowMockService)
} else {
println ">>> not test env"
time1(TimeNowService)
}
myService(MyService) {
diTest = 'hello 2'
timeNowService = ref('time1')
}
}
In either case I would use the service by calling
timeNowService.now().
The one strange, and very frustrating thing to me was that I could not do this:
import grails.util.GrailsUtil
// Place your Spring DSL code here
beans = {
if (GrailsUtil.environment == 'test') {
println ">>> test env"
myService(MyService) {
timeNow = { -> new Date(1308486447140) }
}
} else {
println ">>> not test env"
myService(MyService) {
timeNow = { -> new Date() }
}
}
}
In fact, when I tried that I also had a dummy value in there, like dummy = 'hello 2' and then a default value of dummy = 'hello' in the myService class itself. And when I did this 3rd example with the dummy value set in there as well, it silently failed to set, apparently b/c timeNow blew something up in private.
I would be interested to know if anyone could explain why this fails.
Thanks for the help guys and sorry to be impatient...
Since Groovy is dynamic, you could just take away your currentDate() method from your service and replace it by one that suits your need. You can do this at runtime during the setup of your test.
Prior to having an instance of MyService instantiated, have the following code executed:
MyService.metaClass.currentDate << {-> new Date(1308619647140) }
This way, you can have a consistent behavior across all your tests.
However, if you prefer, you can override the instance method by a closure that does the same trick.
Let me know how it goes.
Vincent Giguère
What's the best way to build environment-specific web.xml entries in grails?
I need to make certain modifications for production only, as they break running locally.
Any thoughts?
You can create scripts/_Events.groovy with an event handler for the 'WebXmlEnd' event which is fired once Grails and the plugins have finished making their changes. Update the XML with plain search/replace or via DOM methods by parsing the XML and write out the updated file:
import grails.util.Environment
eventWebXmlEnd = { String filename ->
if (Environment.current != Environment.PRODUCTION) {
return
}
String content = webXmlFile.text
// update the XML
content = ...
webXmlFile.withWriter { file -> file << content }
}
Here's the solution that's i'm using, from the guy over at death-head.ch
first install the templates
grails install-templates
then customize the web.xml you'll find in src/templates/war/web.xml. I chose to make a web_dev.xml and a web_prod.xml and delete the web.xml. I wanted web_prod.xml to contain a security-constraint block. anyway...
Place the following in BuildConfig.groovy:
// #########################################################
// ## Can't use environment switching block because BuildConfig doesn't support it.
// ## #url http://jira.grails.org/browse/GRAILS-4260
// ## So use this workaround:
// ## #url http://death-head.ch/blog/2010/09/finally-solved-the-base-authentication-in-grails/
// #########################################################
switch ("${System.getProperty('grails.env')}") {
case "development":
if (new File("/${basedir}/src/templates/war/web_dev.xml").exists()) {
grails.config.base.webXml = "file:${basedir}/src/templates/war/web_dev.xml"
}
break;
default:
if (new File("/${basedir}/src/templates/war/web_prod.xml").exists()) {
grails.config.base.webXml = "file:${basedir}/src/templates/war/web_prod.xml"
}
break;
}
Good luck!
I've never tried it, but it should be possible to specify the grails.config.base.webXml parameter in BuildConfig.groovy dependant on the current environment.
There's a list of available BuildConfig settings here
EDIT
Actually, due to this issue, this isn't a way forward :-( Maybe passing the property like:
grails -Dgrails.config.base.webXml=/path/to/web.xml
Is all that's possible?