Rest API testing using Grails REST Client Builder - grails

I am writing test cases to verify the response from REST call. I am doing the following:
RestResponse response = rest.get("http://localhost:8080/dynamic-content-services/api/"
But I want to use the in-memory database. I know localhost will not work but what will be the URL in that case?
This is my test environment
test {
dataSource {
driverClassName = "org.h2.Driver"
username = "sa"
password = ""
dbCreate = "update"
url = "jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE"
}
}

you can have a look to this post I wrote. It might help you out:
http://aruizca.com/integrated-vs-functional-testing-how-to-test-rest-apis-in-grails-using-spock/
Look at the functional testing part. The default H2 configuration for test should work out of the box.

Related

Using REST APIs

I am new to gerrit. I am using gerrit V. 2.6 . I want to use gerrit REST APIs in my python script. But not able to figure out how to use it. I tried below code but getting errors.
curl --digest --user user:password http://server/a/changes/path/to/project~branch~change_id/rebase
getting error :
401 Authorization Required
Authorization Required
This server could not verify that you
are authorized to access the document
requested. Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.
Am I missing something.??
Are you using the correct username:password combination? This isn't your network password - it is the HTTP password that gerrit generates. You can find it by going to Settings->HTTP Password. If the password box is blank, click the button to have Gerrit generate a new password.
You may try using pygerrit. https://pypi.python.org/pypi/pygerrit/0.2.1
I think it has some APIs to easily access gerrit.
As #Ramraj mentioned, you can try using pygerrit or pygerrit2.
And I provide some examples that how I use gerrit REST APIs in my python script.
Here is the code.
auth = HTTPBasicAuth(username, password)
rest = GerritRestAPI(url='http://review.xxxxxx.com:8080', auth=auth)
Query changes by change number.
info = rest.get("/changes/?q=change:{}".format(change_number))
change_id = info[0]['change_id']
subject = info[0]['subject']
Query changes by commit id.
info = rest.get("/changes/?q=commit:{}".format(commit_id))
change_id = info[0]['change_id']
subject = info[0]['subject']
Revert a change.
headers = {'content-type': 'application/json'}
query = "/changes/" + str(change_number) + "/revert"
my_data = {"message": "{}".format("Revert "+str(subject))}
rest.post(query, data=json.dumps(my_data), timeout=30, headers=headers)
Review a change
headers = {'content-disposition': 'attachment', 'content-type': 'application/json'}
query = "/changes/" + str(change_number) + "/revisions/current/review"
my_data = { "labels": {"Code-Review": "+2", "Verified": "+1"} }
rest.post(query, data=json.dumps(my_data), timeout=30, headers=headers)

Can I make a Grails web app using db2 jdbc driver not commit?

My situation: I work with a lot of RPG programmers who have created files on the IBM-i in a way that does not create a journal. I've created a Grails app which uses a db2 jdbc driver to connect to a file and update, insert, etc. I'm getting an error:
com.ibm.db2.jdbc.app.DB2DBException: MYFILE in MYLIB not valid for operation.
Cause . . . . . : The reason code is 3 . Reason codes are:
...blah blah blah...
3 -- MYFILE not journaled, no authority to the journal, or the journal state is *STANDBY. Files with an RI constraint action of CASCADE, SET NULL, or SET DEFAULT must be journaled to the same journal.
...blah blah blah...
I know that I could start journaling the file with STRJRNPF, but I'd rather not keep up with it (no scolding please). Is there a parameter for the db2 jdbc connection url that I can set to let it know not to try to commit?
Here's my current connection info:
dataSource
{
dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''
pooled = true
url = "jdbc:db2:*local;naming=system;libraries=LIBS;errors=full"
driverClassName = "com.ibm.db2.jdbc.app.DB2Driver"
username = "user"
password = "pass"
dialect = org.hibernate.dialect.DB2400Dialect.class
}
EDIT: Here is what I've tried:
url = "jdbc:db2:*local;naming=system;libraries=LIBS;errors=full;transaction isolation=none"
Finally, this did end up having to do with Grails/Hibernate. Here is what the datasource ended up looking like:
dataSource
{
dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''
pooled = true
url = "jdbc:db2:*local;naming=system;libraries=LIBS;errors=full;transaction isolation=none"
driverClassName = "com.ibm.db2.jdbc.app.DB2Driver"
username = "user"
password = "pass"
dialect = org.hibernate.dialect.DB2400Dialect.class
properties{
defaultTransactionIsolation = 0
}
}
Thanks to #Buck Calabro 's comments and this question.
What do you think "keep up with it" entails? By default, the system will do a lot of journal management for you. Welcome to IBM i. ;-)

Need an example of groovy-wslite POST with multiple parameters

I need to make a secure POST to https://example.com/api/login with parameters Username and Password. I'm trying to use the wslite plugin with Grails, but I can't figure out the syntax. I've checked the unit tests at github, but none of them have given me what I need. Maybe I'm just failing to connect the dots.
Anyway, does anyone have an example of multiple parameters to a POST? Thanks!
Try following example to post your request.
def client = new RESTClient("https://example.com/api/login")
// for testing only!
client.httpClient.sslTrustAllCerts = true
def response = client.post() {
charset "UTF-8"
urlenc username: "test", password: "test" // here you can provide your params as a map
}

Cannot connect to mysql from grails

I am using wamp server, it comes with mysql 5.5.16 and I am trying use the mysql from grails 1.3.7. In my Buildconfig.groovy file i have added "runtime 'mysql:mysql-connector-java:5.1.6'" under dependencies and my datasource.groovy looks like this:
dataSource {
dbCreate = "create-drop" // one of 'create', 'create-drop','update'
pooled = true
driverClassName = "com.mysql.jdbc.Driver"
username = "root"
password = ""
url = "jdbc:mysql://localhost:3306/moviestore_dev"
}
but when i refresh dependencies or run the app, i get error=>
:: mysql#mysql-connector-java;5.1.6: not found
I can connect to the database but the data in the database remains as long as the app is running, the next time I run the app, all the data are gone !
I have no idea what is happening, please help !
Thanks
problem is in
dbCreate = "create-drop"
use
dbCreate = "update"
• create-drop: Drops and re-creates the database schema on each application load
• create: Creates the database on application load
• update: Creates and/or attempts an update to existing tables on application load
• [blank]: Does nothing

trouble viewing h2 db used in grails project

I'm trying to have a look at the tables generated in h2 db used in Grails project, but something's amiss.
I connect to the browser console at http://127.0.1.1:8082/ but all that's there to browse is INFORMATION_SCHEMA and Users. How do I get tho the tables used/generated by the app?
Just started building out the app and only few domain classes are in place and I'm trying to get a feel for working h2. Prior to that I've been using PostgreSql in all projects so this is very unnerving for the moment.
Thanks in advance
Are you using the right JDBC URL when logging in?
The default in grails is jdbc:h2:mem:devDB.
When an non-existing URL is given, like jdbc:h2:blabla, an empty database is created, with the default INFORMATION_SCHEMA and Users as you described.
Make sure you connect to the URL where your grails application stores its tables. You can find the URL in $GRAILS_PROJECT/config/DataSource.groovy, after the url definition.
environments {
development {
dataSource {
pooled = false
logSql = false
username = "sa"
password = ""
dialect = "com.hp.opr.hibernate.dialect.H2Dialect"
driverClassName = "org.h2.Driver"
dbCreate = "create-drop"
url = "jdbc:h2:mem:devDB;DB_CLOSE_DELAY=-1;MVCC=TRUE"
}
}
}
If you're using 2.0 the web console is enabled by default in dev mode and can be enabled in other environments: http://grails.org/doc/2.0.0.M2/guide/conf.html#databaseConsole
If you're not using 2.0 yet you can install the http://grails.org/plugin/dbconsole plugin or follow the link to my blog post and set it up yourself if you want to customize the url (or if you're using Grails pre-1.3.6 since the plugin has an artificial version restriction to 1.3.6+)
... so in datasource I changed the url to: url = "jdbc:h2:rswDb" (removing the 'mem' part and changing the name of the db). Then 3 db files showed up in the root dir of the project.
Next, in db console set the jdbc url to: jdbc url: jdbc:h2:~/work/web/rsw/rswDb
... and when I hit 'connect' all the tables were there!
Thanks again!

Resources