quinta-feira, 22 de agosto de 2013

Graiils Service Layer, Simple Testing, others

- Service Layer

    - src/services.  Used to add core application logic.
    - Declarative transactions
        -  static transactional = false
        or
        @Transactional
        class BookService {
        or
        @Transactional(readOnly = true)
        def listBooks()
        @Transactional   // read-write
        def updateBook() {
        def deleteBook() { //Not transactional
    - AutoWiring
        class AuthorController {
            def authorService
        }
    - Services from Java
        In a class Java     private BookStore store;
- Testing
    MockFor ->   def control = mockFor(SearchService)
    control.demand.searchWeb { String q -> ['mock results'] }

    @TestFor(SimpleController)
    class SimpleControllerTests {      
        void testSearch() {
        def control = mockFor(SearchService)
        control.demand.searchWeb { String q -> ['mock results'] }
        control.demand.static.logResults { List results ->  }
        controller.searchService = control.createMock()
        controller.search()
        assert controller.response.text.contains "Found 1 results"
    }
       
    ex controller:
        assert response.text == 'hello'
        redirect action: 'hello'                                 --> assert response.redirectedUrl == '/simple/hello'
        render view: "homePage", model: [title: "Hello World"]     --> assert view == "/simple/homePage";  assert model.title == "Hello World"
        render template:"snippet"                                 -->  assert response.text == 'contents of template'  or
                                                                    views['/simple/_snippet.gsp'] = 'mock contents'
                                                                    controller.display()
                                                                    assert response.text == 'mock contents'
        def renderXml() {                                        controller.renderXml()  
            render(contentType:"text/xml") {        -->            assert "<book title='Great'/>" == response.text  
                book(title:"Great")                                 assert "Great" == response.xml.@title.text()
            }
        def renderJson() {                            -->            controller.renderJson()
            render(contentType:"text/json") {                    assert '{"book":"Great"}' == response.text
            book = "Great"                                         assert "Great" == response.json.book  
        }
       
    - Testing constraints
        static constraints = {
            title blank: false, unique: true
            author blank: false, minSize: 5
        }
       
        def existingBook = new Book(
            title: "Misery",
            author: "Stephen King")

        mockForConstraintsTests(Book, [existingBook])          
       
        // validation should fail if both properties are null
        def book = new Book()
        assert !book.validate()
        assert "nullable" == book.errors["title"].code
        assert "nullable" == book.errors["author"].code
           
    - Integrating test
        Tests many layers, usually from service. using     class MyServiceTests extends GroovyTestCase {
    - Funcional Test
        - Invoque HTTP request against the running application and verify the resultant behaviour.
        - Ex: Selenium
- Internationalization (I18n)
    - files message(_ptBR).properties inside grails-app/i18n
    - Reading messages
        <g:message code="my.localized.content" args="${ ['Juan', 'lunes'] }" /> --> my.localized.content=Hola, Me llamo {0}. Hoy es {1}.
- Security
    - Authentication with filter
        grails create-filters security. In filteer verify if there is a session.user otherwise send to login.
    - Better use spring Security.
- Plugin (TODO)
- Web Service
    - Provides API of a application.
    - REST (Not a technology, but a architecture) Involves sending simple XML or JSON combined with URL partterns
        - ex of CRUD: GET(retrieving), PUT(creating), POST(updating), DELETE(detele)
            "/product/$id"(controller: "product") {
                action = [GET: "show", PUT: "update", DELETE: "delete", POST: "save"]
            }
    - SOAP
        Uses plugin of CXF, Axis2
- Grails and Spring
- Grails and Hibernate
    Create a hibernate.cfg.xml file in your project's grails-app/conf/hibernate
- Scaffold
    - Scaffolding lets you auto-generate a whole application for a given domain class including:
        - The necessary views
        - Controller actions for create/read/update/delete (CRUD) operations
     - static scaffold = true
   

Nenhum comentário:

Postar um comentário