groovy grails gr8ladies women techmakers: minneapolis
DESCRIPTION
An introduction to Groovy and Grails given at Women Techmakers: MinneapolisTRANSCRIPT
GROOVY, GRAILS,
AND GR8LADIES
Women Techmakers: MinneapolisMinneapolis, MNMarch 22, 2014
Setup
Prerequisites Java jdk
try ‘java –version’ to see if it’s already installed http://www.grails.org/Installation
To Install Groovy and Grails: Gvmtool.net
curl –s get.gvmtool.net | bash gvm install groovy gvm install grails
gvm use grails 2.3.4
Gr8Ladies
An organization for the support of women in the Gr8 stack community
Goals: Education Networking Professional Development
Contact Us @Gr8Ladies on twitter Gr8Ladies.org LinkedIn, Facebook search Gr8Ladies Contribute to our website via GitHub
The Gr8 Stack
Groovy – the language Grails – MVC framework Gradle –build automation tool
Used to build Android Apps Griffon – desktop app framework Spock – testing framework Many other minor frameworks and
development tools
Image via: http://blogs.vitoria-gasteiz.org/ti/2010/05/31/full-stack/
Groovy – The Language
Dynamic compiled language Flexible
optional typing default scope optional getters/setters
Rapid Development Integrates with existing JAVA libraries and
infrastructure #groovylang
Hello World
In Java,
In Groovy,
Special Functions in Groovy
Elvis Operator ?: In PHP:
In Groovy:
Ranges A collection of numbers defined by boundaries
Spread dot Applies a function to every element of a
collection and returns the result
Null safe user?.chapter?.name?.toUpperCase()
will return null if user or chapter is null and will not return an error
Groovy Truth Null, empty string, empty set, and 0 are all
false!
Image via: http://blogs.vitoria-gasteiz.org/ti/2010/05/31/full-stack/
Grails – the framework
MVC framework similar to Symphony in PHP, Django in Python,
or Ruby on Rails Convention over configuration #grailsfw
MVC Architecture
image from: http://www.mathiasbosman.be/wp-content/uploads/2014/02/mvc_role_diagram.png
The Layout
Model
Domain Classes Grails Object Relationship
Mapping(GORM) Uses Hibernate Class names map to tables Field names map to
columns Dynamic finders
User.findAllByGenderAndChapterIsNotNull(‘F’)
Events beforeInsert, afterUpdate,
beforeDelete Alternatives
Mongo and Redis
Controllers
Mapped to urls by name /user/list
UserController.list() Url Parameters map
to controller /user/show/id
UserController.show(id)
Views
GSP (Groovy Server Page)
Directory structure by convention UserController.list()
renders/views/user/list.gsp
Special tags for forms, links, looping and other tasks
Reference model data by ${model.attribute}
Tests
Lots of Options for writing tests Spock (default) JUnit Geb
• www.gebish.org• http://docs.spockframework.org/en/spock-0.7-groovy-2.0/• For Grails, in BuildConfig.groovy:
dependencies {test "org.seleniumhq.selenium:selenium-support:2.40.0"
test "org.seleniumhq.selenium:selenium-firefox-driver:2.40.0" test "org.gebish:geb-spock:0.9.2”
}plugins {
test ":geb:0.9.2" compile ":remote-control:1.4“ // For accessing the context of the
// grails app from tests// Spock is already included in Grails 2.3.6
}
package com.allison.pages
import geb.Page
class EmployeeListPage extends Page {
// the link that is navigated to // on the 'to' call static url = "employee/list"
// the closure that is verified on // the 'at' call static at = { pageTitle.text() == 'Employee List Page' }
// The navigator objects static content = { pageTitle(wait: true) { $('h2.title') } employeeLink(wait: true, to: EmployeeShowPage) { id -> $('a.employeeLink', 'id': "${id}") } }
// It is good practice to wrap all the // navigator objects in methods boolean employeeLinkIsPresent(Long id) { employeeLink(id).isDisplayed() }
EmployeeShowPage clickEmployeeLink(Long id) { employeeLink(id).click()
return browser.page }}
<html> <head> <title>Employee</title> </head> <body> <h2 class="title">Employee List Page</h2> <ul> <li> <a href="/gebDemo/employee/show/1" id="1” class="employeeLink"> Alice LastName </a> </li> <li> <a href="/gebDemo/employee/show/2" id="2” class="employeeLink"> Bob LastName2 </a> </li> <li> <a href="/gebDemo/employee/show/3" id="3” class="employeeLink"> Charles LastName3 </a> </li> </ul> </body> </html>
package com.allison.functional
import com.allison.pages.EmployeeListPageimport com.allison.remote.EmployeeRemoteControlimport geb.spock.GebReportingSpec
class firstSpec extends GebReportingSpec {
EmployeeRemoteControl remote = new EmployeeRemoteControl()
Long employeeId_1 Long employeeId_2 Long employeeId_3
def setup() { EmployeeRemoteControl remote =
new EmployeeRemoteControl() employeeId_1 =
remote.createEmployee('Alice', 'LastName') employeeId_2 =
remote.createEmployee('Bob', 'LastName2') employeeId_3 =
remote.createEmployee('Charles', 'LastName3')
}
CONTINUED
def 'A descriptive name for the feature'() {
given: 'Setup stuff' // Can be blank
when: 'The employee list page is navigated to' EmployeeListPage employeeListPage =
to EmployeeListPage then: 'The employee list page header is displayed' assert at(EmployeeListPage) // All statements in a
// then-block are implicitly // asserted
and: 'The employee is listed' assert employeeListPage.employeeLinkIsPresent(
employeeId_1)
when: 'The employee is clicked' employeeListPage.clickEmployeeLink(employeeId_1)
then: 'At the show employee page' assert at(EmployeeShowPage) }
def cleanup() { remote.deleteEmployee(employeeId_1) remote.deleteEmployee(employeeId_2) remote.deleteEmployee(employeeId_3) }
}
Setting Up the Environment
GVM! Groovy enVironment Manager
curl –s get.gvmtool.net | bash gvm install groovy gvm install grails
Use grails 2.3.4 Choose an IDE
IntelliJ Eclipse Netbeans
Creating An App
grails create-app appName auto generates app structure
grails create-domain-class User Creates domain class and test file
grails generate-all appName.User Auto generates views and controllers based on
the properties of the domain class
Deploying a grails app
Grails war Defaults to target/appName-0.1.war
Deploy to JVM Example via Amazon ElasticBeanstalk
Suggested Reading
Making Java Groovy – Ken Kousen Email [email protected] for coupon code
Programming Grails – Bert Beckwith (O’reilly animal book)
Groovy In Action - Dierk Koenig with Andrew Glover, Paul King, Guillaume Laforge and Jon Skeet
Many Tutorials available on grails.org
What Next?
Come to Groovy Users of Minnesota(GUM) every second Tuesday of the month at Smart Things
Join Gr8Ladies for hack sessions and contribute to the gr8ladies website on github
Gr8Ladies.org or @Gr8Ladies on twitter