Day-3 Sessions
Microservice Architecture with Grails 2 - Jeff Brown
My notes
Testing Driven Development with Spock - Venkat Subramaniam
My notes
My notes
My notes
Microservice Architecture with Grails 2 - Jeff Brown
Microservice Architecture with Grails 2 - Jeff Brown |
My notes
- Microservices, Spring Boot, Grails 3
- Microservices by Martin Fowler - a good place to start http://martinfowler.com/articles/microservices.html
- Versioning resources e.g Employee management system, Contract management system as 2 micro services. One system evolves into a different version. Within grails apps the resource can be versioned.
- “people/v1”(resources:’book’, namespace:’v1’)
- “people/v2”(resources:’book’, namespace:’v2’)
- “people”(version: 1.0, resources:’book’, namespace:’v1’) //looks for http header
- “people”(version: 2.0, resources:’book’, namespace:’v2’)
- Controllers can be name-spaced. There can be any number of BookControllers with the same name but in different packages, versioned in the same application.
- another way Accept-Version:1.0 can be in the header with the same url
- Default is the highest version number if left out in the header
- Domain class Resource annotation @Resource(uri=’automobiles’) class Car{} //this will create a CarController that extends grails.rest.RestfulController. queryForResource() method is the one called everytime a resource retrieve request is received. It can be overridden. getObjectToBind() is another method you may need to override.
- REST Client Builder RestBuilder()
- plugins { compile: “:rest-client-builder:2.0.3” }
- TIP: In groovy underscores can be put into numbers: e.g. 200_000
- 3 small grails applications bank, car, webui, each running on a different ports 8080, 8082, 8084. When grails starts on a port it uses another post to listen to some details that it needs to get on the running application. e.g when run on 8080, 8081 is taken too.
- webui app sends a rest req (build a RestBuilder() and send rest request and get json responce) to the bank app and gets the list of banks and displays in a form for searching cars app. Gets the cars price and compares prices and list only cars that the bank can offer loan based on maxLoanPrice that the bank offers.
- TIP: If you are building apps that do not need any plugin, remove it to make the application size reduced. e.g. Hibernate, The Bankui app is not using hibernate at all.
Testing Driven Development with Spock - Venkat Subramaniam
Testing Driven Development with Spock - Venkat Subramaniam |
- Automated testing: Software crisis, time and disciple, This code is not testable = The design sucks, lack of discipline, lack of knowing how to do it, value it as much it not more than design, how do you know the code that worked before works now, JDD - Jesus Driven development ;)
- TDD, BDD
- Spock
- Groovy to test Java, Spock to test Groovy, spock is groovier than groovy in lot many ways
- Example:
- Canary testing : a simple test method expect: true == true ;)
- spock can be used to test code written in any language for jvm as it uses the compiled code
- One of the TDD principles: You don’t write code until a test fails ;)
- Very short feedback cycle. groovysh/groovyconsole to play with code right away. Java repple - being considered for Java 9. They already got it: groovysh ;)
- multiple unrelated assets in a test method: problem, you don’t know where it failed
- Spock undrills multiple asserts and gives nice feedback (where: block)
- where : tabular pattern of test data with | construct or << construct
- expect: exceptions, kind of exception, exception message etc.
- expect:
- Stub vs. Mock. Mock is useful to mockout behavior, stub is useful to stubout the stub
- Spell checker: Java spell checkers, which one to use? mocking, when you can mock, why to worry about whick spell checker to brng in, integration issues etc.
- Injections: Constructor based, setter based, factory
- Create an interface file SpellChecker: boolean isCorrect(String word)
- void setUp(){ spellChecker = Mock(SpellChecker; scrambler.spellChecker = spellChecker)
- Mocking: can be problamatic, The more coupling you create, the more mocking you need to do. Use mocking very sparingly. More mocking is a design smell. Use light mocking.
- TODO: blogged: “Knock out before you mock out”
- Spock summary: very light, nice, applies AST transformations nicely.
- Fun: do you use maven? nobody uses maven, maven uses you
Advanced GORM: Beyond relational - Graeme Rocher |
- GORM Internals
- Visible to Java code, as it is into bytecode through AST Transformations
- Built by Travis: https://travis-ci.org/grails/grails-data-mapping
- GORM Standalone
- Easy initialization of GORM outside grails
- Boot pluguin
- @Entity - gorm tag, applies AST transformations and make sthe domain class rich model class
- GORM for Hinernate standalone
- DEMO: a simple gradle project with spock tests, grails-datasource-gorm-hibernate dependency
- Tips & Tricks
- Where queries, compilation checks in where closure, powerful and flexible.
- Subqueries
- Hibernate Column Formula, Column Readers and Writers
- Asynchronous GORM: in GORM any method can become async
- Unit testing GORM
- dependenc: grails-datastore-test-support
- Provides HibernateTestMixin
- Annotate spock test spec class with @TestMixin(HibernateTestMixen)
My notes
- Appium http://appium.io
- Opensource test automation framework
- Uses WebDriver JSONWireProtocol https://code.google.com/p/selenium/wiki/JsonWireProtocol
- Architecture
- Appium Java client (selinium), Gradle, Groovy, Spock
- Appium Java client: Wrapper of Selenium 2.x, Uses GSON (Google JSON)
- Groovy, Spock, Gradle
- dependency : appium (transitive: selenium)
- http://www.genymotion.com Genymotion Emulator - definitely the way to go
- iOS is a simulator, Genymotion emulator is a full android system
- 2 spock tests testing both iOS and Android
- Test iOS and Android with one test and helpers: using Groovy traits
- Test using @Unroll
- LocaleSpec Test Demo
- CI
- Jenkins for Android, XCodeBots for iOS
- Sauce Labs
Java 8 for Groovy Programmers - Venkat Subramaniam
My notes
My notes
- When you write code in Groovy, the compiler works for you. When you write code in Java, you work for the compiler ;)
- Iteration: each in Groovy is forEach in Java 8
- null is synonymous to word evil ;)
- collect in Groovy is stream() in Java 8
List<Integer> values = Arrays.asList(1,2,3,4,5)
values.stream().map(e-> e*2).forEach(System.out::println); //java 8
def values = [1,2,3,4,5]
values.collect{2 * it}.each{println it}
values.stream().filter(e->).findFirst().orElse(0)); //to avoid null when not found...groovy find returns null
- findAll
- inject - Groovy
def values = [1,2,3,4]
println values.inject(0) {c, e -> c + e} //starts with 0, adds 1, takes the result adds 2 and goe on.
reduce - Java 8
values.stream().reduce(0, (c,e) -> c + e));
map gathers the data, reduce scatters the data
- def names = [‘Jake’, ‘Sara’, ‘Pete’]
Groovy: join method of collections separated by commans
:)
Make it a law: Arrest people for writing code like for(int i=0; i<....)
Java 8:
names.stream().map(String::toUpperCase).collect(joining(“, “))); //joining is a static method from Collectors class
- Unfamiliar things in Java 8 that are really nice (late in the party) and not there in Groovy
- default method: List has been around for awhile. we called stream on List.
- A method you can implement in interface is default method.
- You can override default method, if you don’t override, it is available automatically. Abstract base classes can have state, fields. Interfaces cannot.
- Any method in the class hierarchy takes precedence over the interface default method.
- two interfaces with same default methods: throws error.
- stream is implememnted using default method in Java 8
- Java guys got promoted to Architects so that they don’t have to work any more ;)
- Groovy interface at this time cannot have default method. You cannot create interfaces in Groovy as Groovy needs to evelve. Groovy can use Java interface with default methods.
- Groovy has traits, give stateability.
- Why default methods in interfaces? Without it, you will be writing utilities.
- Nice to Use, easy to Abuse them ;)
- Be willing to use, not to abuse ;)
- String::toUppercase // is method reference
- IntelliJ IDEA: There are two kinds of programmers, those who have IDEA and those who don’t have IDEA ;)
- Method references is Venkat’s most favorite in Java 8
- Lazy evaluation
- Two types of methods: intermediate operation, terminal operation
- println values.collect{
println “collecting $it”
it * 2}.findAll {
println “picking $it”
it > 6}.inject(0) {c, e-> c + e } //
Java 8 filter is like groovy find on collection
reduce is a terminal operation, mapand filter are intermediate operations.
Lazy in Java is Efficient ;)
Lambda expression function component in Groovy is very inefficient. In Java 8, it is extremely efficient.
- Infinite Stream - you can create a collection that is infinite in length. Lambdas give lazy evaluation, so you can postpone.
Stream.iterate(1, e -> e + 1)
.limit(10) //without this it runs forever ;)
.collect(toList())); //infinite stream of 1,2,3,4….
- Parallel collections
- in Groovy you can use GPers to do this
- in Java 8, stream() to parallelStream() //concurrent
- try { Thread.sleep(1000);} catch(Exception e) {} //add delay
- InvokeDynamic: One of the most exciting features of Java 8
- means look the other way. Started as a way to support dynamic languages in Java 7.
- In java 8,
- Java introduced anonymous inner classes in 1999, when people were crying for lambdas…
- compile a class with inner class, compile take a look at the .class….buy one, get one free ;) it’s not inner class ;)
- It is this feature that made java 8 made performing better
- InvokeDynamic: method binding happens at run time. It is just a binding to the method. Doesn’t incur creating the object (anonymous or what so ever), doesn’t involve object creation, so fast. Happens at the bytecode level.
- When code is compiled, lambda expression calls become InvokeDynamic
No comments:
Post a Comment