1/16/2016 1 Jersey Test Framework: TDD for Web Services Brian Westrich McWest Corp. JavaOne 10/4/2011 Slides:

Slides:



Advertisements
Similar presentations
Pierre-Johan CHARTRE Java EE - JAX-RS - Pierre-Johan CHARTRE
Advertisements

Server Access The REST of the Story David Cleary
WEB SERVICES. FIRST AND FOREMOST - LINKS Tomcat AXIS2 -
Introduction to Maven 2.0 An open source build tool for Enterprise Java projects Mahen Goonewardene.
A Toolbox for Blackboard Tim Roberts
Francisco Gonzalez Mario Rincon.  Apache CXF is an open source services framework.  CXF helps you build and develop services using frontend programming.
Web Service Ahmed Gamal Ahmed Nile University Bioinformatics Group
SE 370: Programming Web Services Week 4: SOAP & NetBeans Copyright © Steven W. Johnson February 1, 2013.
Streaming NetCDF John Caron July What does NetCDF do for you? Data Storage: machine-, OS-, compiler-independent Standard API (Application Programming.
Remote Unit Testing Brian Pruitt-Goddard Alex Riordan.
Servlets and a little bit of Web Services Russell Beale.
Peoplesoft: Building and Consuming Web Services
Microsoft ® Official Course Interacting with the Search Service Microsoft SharePoint 2013 SharePoint Practice.
Web Services with Apache CXF Part 2: JAXB and WSDL to Java Robert Thornton.
© Internna Technologies 1 IWebMvc Features, Possibilities & Goals.
Chapter 10 EJB Concepts of EJB Three Components in Creating an EJB Starting/Stopping J2EE Server and Deployment Tool Installation and Configuration of.
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
Apache Chemistry face-to-face meeting April 2010.
1 Lecture 22 George Koutsogiannakis Summer 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES.
Testing. What is Testing? Definition: exercising a program under controlled conditions and verifying the results Purpose is to detect program defects.
1 3. Implementing Web Services 1.Create SOAP proxy interfaces and WSDL based service descriptions 2.Register/publish services 3.Stores service descriptions.
AUTOBUILD Build and Deployment Automation Solution.
|Tecnologie Web L-A Anno Accademico Laboratorio di Tecnologie Web Introduzione ad Eclipse e Tomcat
Or, Hey can’t we just do it using HTTP for the envelope?
Spring Framework. Spring Overview Spring is an open source layered Java/J2EE application framework Created by Rod Johnson Based on book “Expert one-on-one.
Web Services with Apache CXF Part 2: JAXB and WSDL to Java Robert Thornton.
Nadir Saghar, Tony Pan, Ashish Sharma REST for Data Services.
Ch 2 – Application Assembly and Deployment COSC 617 Jeff Schmitt September 14, 2006.
Distributed Programming CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Introduction to Spring Remoting Simplifying.
1 Seminar on Service Oriented Architecture Principles of REST.
Web Services with Apache CXF Part 2: JAXB and WSDL to Java Robert Thornton.
XML and Web Services (II/2546)
Preliminary Ocean Project Page 1 WGISS SG May 15, C. Caspar G. Tandurella P. Goncalves G. Fallourd I. Petiteville Preliminary Ocean Project Phase.
INT-9: Implementing ESB Processes with OpenEdge ® and Sonic ™ David Cleary Principal Software Engineer.
1 Java Servlets l Servlets : programs that run within the context of a server, analogous to applets that run within the context of a browser. l Used to.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Spring MVC Essentials Getting started.
RESTful Microservices In Java With Jersey Jakub Podlešák Software Engineer Oracle, Application Server Group September 29, 2014 Copyright © 2014, Oracle.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
Lecture IV: REST Web Service
Java Programming: Advanced Topics 1 Enterprise JavaBeans Chapter 14.
Presentation Title Subtitle DSpace UI Prototype 7 Spring, Angular.js, and the DSpace REST API.
UNDERSTANDING YOUR OPTIONS FOR CLIENT-SIDE DEVELOPMENT IN OFFICE 365 Mark Rackley
Google Code Libraries Dima Ionut Daniel. Contents What is Google Code? LDAPBeans Object-ldap-mapping Ldap-ODM Bug4j jOOR Rapa jongo Conclusion Bibliography.
Apache Solr Dima Ionut Daniel. Contents What is Apache Solr? Architecture Features Core Solr Concepts Configuration Conclusions Bibliography.
Modern Development Technologies in SharePoint SHAREPOINT SATURDAY OMAHA APRIL, 2016.
1 PSI/PhUSE Single Day Event – SAS Applications – June 11, 2009 SAS Drug Development from the Inside Magnus Mengelbier Director.
Enterprise Java Beans. Contents  Understanding EJBs  Practice Section.
The Holmes Platform and Applications
Java Web Services Orca Knowledge Center – Web Service key concepts.
CS3220 Web and Internet Programming RESTful Web Service
JRA2: Acceptance Testing senarious
Data Virtualization Tutorial: Introduction to SQL Script
Node.js Express Web Applications
Sabri Kızanlık Ural Emekçi
z/Ware 2.0 Technical Overview
Web Services-JAX-RPC JAX-RPC enables a Web Service endpoint to be developed using either a Java Servlet or Enterprise JavaBeans (EJB) component model.
Overall Architecture and Component Model
Node.js Express Web Services
WEB API.
Chapter 9 Web Services: JAX-RPC, WSDL, XML Schema, and SOAP
ESIS Consulting LLC (C) ESIS Consulting LLC. All rights reserved
Developing and testing enterprise Java applications
CS4961 Software Design Laboratory Understand Aquila Backend
Consuming Web Services with 2E Generated Objects
#01# ASP.NET Core Overview Design by: TEDU Trainer: Bach Ngoc Toan
Chengyu Sun California State University, Los Angeles
SDMX IT Tools SDMX Registry
Presentation transcript:

1/16/ Jersey Test Framework: TDD for Web Services Brian Westrich McWest Corp. JavaOne 10/4/2011 Slides: Code:

2 Outline Tool survey Jersey Test Framework (JTF) primer Test Driven Design/Development (TDD) with JTF Intermediate Jersey via JTF Q&A / Discussion

3 Web services: What do you use? Remote calls: RMI, EJB 2, EJB 3, Web Services, other Web Services type: SOAP, REST REST implementation: Restlet, Jersey, Other

4 Web Services: What tools do you use? Transfer format: XML, JSON Serialization: JAXB, XStream, JibX, Burlap, text, other

5 What is Jersey? Reference implementation for JAX-RS (RESTful Web Services for Java) Straightforward API Production ready Major version numbers mirror JAX-RS versions Version 1.0 released October 13, 2008 Current version: (Sept 16, 2011) 2.0 will be released with JEE7

6 What is Jersey Test Framework (JTF)? Framework for testing Jersey Web Services Runs as “JUnit” test Automatically starts/stops “test” app server, deploys your services Available since Jersey version 1.4 User guide:

7 Types of Web Service testing TypeDescriptionBenefitsDrawbacks MockedCall service class methods directly from tests FastestDoes not test Jersey etc) JTFUse Jersey Test Framework Faster, Tests Jersey service annotations Does not fully simulate production service use (e.g. character encoding) FunctionalCall live services using tools such as soapUI Represen- tative Slowest, Requires test environment, Tests are laborious to write, Tests are brittle

8 Supported “test” app servers JTF in-memory: FAST, no full web services stack (e.g. character encoding) Standalone app servers: slower, more representative  Grizzly  JTF HTTP  Glassfish  External (app server of your choice)

9 Getting started with JTF Maven: Non-Maven: See Jersey user guide section 7 Core “Test” app server

10 Sample web service Time Service Purpose: return current time Data format: milliseconds since 1/1/1970

11 TDD mantra Red: write a failing test Green: make it pass Clean: refactor For more on TDD, including cheatsheet and dojo, see

12 RED

13 GREEN 1) Descend from JerseyTest. 2) Write test code that calls the Jersey service. Notes: client() returns an automatically configured client. Test does not compile since “Time” DTO does not exist. Jersey will automatically call JAXB to unmarshal XML into object.

14 GREEN (cont.) 3) Write XSD that generates dto. Code now compiles, but test fails (fast!) as service does not exist. dto name dto package

15 GREEN (cont.) Stack trace shows how JTF works...

16 GREEN (cont.) 4) Write service class Jersey automatically calls JAXB to marshal object into XML.

17 GREEN (cont.) 5) Declare location of service class. Registers all classes in package that annotation

18 GREEN (cont.) 6) Test passes (in less than a second!).

19 GREEN (review) 1 Note: Automatic JAXB marshalling/unmarshalling inherit from JerseyTest 2. register server resources 3. get client 4. specify path 5. select verb

20 CLEAN Example refactor: Extract path to a constant This example refactor... keeps test in sync with service helps users locate service (Eclipse F3) assumes caller in same classloader as service (e.g. thick client jar, service test code). server client

21 JTF tests as sample clients (executable documentation) Example: get without parameters Path Return type

22 JTF tests as sample clients Example: get with header Providing a sample client is more important as service API complexity increases Header name

23 Other JTF testable Web Service features* Parameters (path, query, post/put form or dto) Verbs (get, post, put, delete) Content types (xml, html, pdf,...) Filters (client) Exception mapping Serialization methods (JAXB, JibX, Castor,...) For sample code, see code.mcwest.com/jersey-example For names of classes of sample code, see slide notes

24 Serialization A key element of web services Jersey integrated with JAXB serialization, can use other serialization methods

25 XStream: Simplified object serialization Convert object to xml Convert xml back to object XStream specific xml

26 Serialization research using JTF Try to use of XStream instead of JAXB  Will XStream simplify Jersey WS serialization?

27 XStream: client deserialization Service returns XML Must unmarshall XML manually dto

28 XStream: dto Manually coded XStream data transfer object (dto) (JAXB generates dto from XSD)  Xstream leads to added dto maintenance effort

29 Xstream: namespace Manual name space configuration Needed for each return type JAXB uses namespace declared in the XSD XStream leads to duplicate maintenance of namespace info

30 XStream: server side serialization Must manually marshall object into XML

31 Manual coding: JAXB XStream For Jersey WS, XStream requires much more manual coding than JAXB

32 Serialization research using JTF Intended simplification would have added end- to-end complexity JTF eases Web Service architectural decisions

Optional Slides: Start

34 Nested XSDs A more serious concern of using Xstream in web services

35 Nested XSDs Reusable XSD (file = jtf-tdd-nested-namespaced-time.xsd)

36 Nested XSDs Example reuse in another XSD. Generated Java object will contain nested XSD namespace in package import statement.

37 Xstream limitation re: nested XSDs No support for namespaces on nested XSDs Must use chameleon XSDs

38 Chameleon XSD Example (file = jtf-tdd-nested-chameleon-time.xsd) Does not declare a namespace

39 Chameleon XSD reuse “include” (like a macro or ‘C’ include) Chameleon assumes namespace of file it is included into

40 Chameleon XSDs: Problems Type name cannot be qualified by namespace Think “Java class in default package” Leads to unwieldly names Item -> InputBatchItem_v3_2 Generated Java class has no record of origin (package) of chameleon code Name collision potential Two chameleon XSDs with same name can’t be imported into same “parent” XSDs

41 Chameleon XSDs: Problems “Ultimately, namespaces, like packages, are there for a reason, which is to locally group a set of types and set them off as being constituent of a given general idea. If you can't put your types in a namespace, you may need to reconsider your design. If you could put your types in a namespace, but choose not to in the hopes of gaining flexibility, expect interoperability and collision problems sooner or later.” Eben Hewitt (2009) Java SOA Cookbook (O'Reilly and Associates)

42 XStream: no nested namespaces SpringSource's position on XStream for WS  Note that XStream is an XML serialization library, not a data binding library. Therefore, it has limited namespace support. As such, it is rather unsuitable for usage within Web services. – ws/site/reference/html/oxm.html ws/site/reference/html/oxm.html Problems easily demonstrated using JTF JTF can help demonstrate rationale for architectural decisions

43 Advanced server resource init Initialize server resources with classes: when don't want to load classes in a package for a specific test (e.g. avoid declaration of duplicate contexts) Initialize server resources with objects: create objects and initialize yourself (e.g. inject mocks) (e.g. use a prepopulated Spring bean as a service resource) For working code, see ResourceRegistrar.class in sample code repository (jersey-example/src/test/java/com/mcwest/jersey/example/resource/ResourceRegistrar.java)

JTF wish list Spring servlet capabilities (e.g. filter registration) Easier setup of custom message body readers/writers (e.g. for specialized content types)

Optional Slides: End

46 Summary Jersey Test Framework enables... TDD of web services: RED – GREEN – CLEAN Executable documentation for Web services Quick learning of Jersey features Sound architectural choices

47 Download Slides and Code Slides Link: Code (Mercurial) Host: Repository: jersey-example

48 Jersey Test Framework: TDD for Web Services Brian Westrich McWest Corp. JavaOne 2011 Slides: Code: