Tutorial for Creating Resources in Java - Client

Similar documents
How to Create an Android Application using Eclipse on Windows 7

Vim, Emacs, and JUnit Testing. Audience: Students in CS 331 Written by: Kathleen Lockhart, CS Tutor

ReSTful OSGi Web Applications Tutorial. Khawaja Shams & Jeff Norris

CS506 Web Design and Development Solved Online Quiz No. 01

02267: Software Development of Web Services

Smooks Dev Tools Reference Guide. Version: GA

Android Studio Application Development

CERTIFIED MULESOFT DEVELOPER EXAM. Preparation Guide

Getting Started with the Internet Communications Engine

Introduction to Eclipse

How to develop your own app

Tutorial for Spring DAO with JDBC

Java 7 Recipes. Freddy Guime. vk» (,\['«** g!p#« Carl Dea. Josh Juneau. John O'Conner

WebSphere ESB Best Practices

Before you can use the Duke Ambient environment to start working on your projects or

Developing NFC Applications on the Android Platform. The Definitive Resource

Web Services API Developer Guide

Tutorial #1. Android Application Development Advanced Hello World App

ODROID Multithreading in Android

Slides prepared by : Farzana Rahman TESTING WITH JUNIT IN ECLIPSE

DataDirect XQuery Technical Overview

Getting Started with Android Programming (5 days) with Android 4.3 Jelly Bean

Developer Guide. Android Printing Framework. ISB Vietnam Co., Ltd. (IVC) Page i

Java Unit testing with JUnit 4.x in Eclipse

Android Application Development: Hands- On. Dr. Jogesh K. Muppala

IDS 561 Big data analytics Assignment 1

Consuming a Web Service(SOAP and RESTful) in Java. Cheat Sheet For Consuming Services in Java

IVI Configuration Store

CS 111 Classes I 1. Software Organization View to this point:

Copyright 2013 Consona Corporation. All rights reserved

Implementing a Web Service Client using Java

Hadoop Tutorial. General Instructions

Unit Testing. and. JUnit

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

Introduction to Testing Webservices

Android Development. 吳 俊 興 國 立 高 雄 大 學 資 訊 工 程 學 系

Building Web Services with Apache Axis2

Amazon Glacier. Developer Guide API Version

Custom SIS Integration Type Development. Jim Riecken Blackboard Learn Product Development

Overview of Web Services API

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL I)

Developing an EJB3 Application. on WebSphere 6.1. using RAD 7.5

XML & Databases. Tutorial. 2. Parsing XML. Universität Konstanz. Database & Information Systems Group Prof. Marc H. Scholl

Programming with Android: SDK install and initial setup. Dipartimento di Informatica: Scienza e Ingegneria Università di Bologna

WIRIS quizzes web services Getting started with PHP and Java

A Practical Guide to Test Case Types in Java

Ready, Set, Go Getting started with Tuscany

Report and Opinion 2014;6(7) Technical Specification for Creating Apps in Android. Kauser Hameed, Manal Elobaid

Aspect Oriented Programming. with. Spring

Programming with Android: SDK install and initial setup. Dipartimento di Informatica: Scienza e Ingegneria Università di Bologna

An Android-based Instant Message Application

A QUICK OVERVIEW OF THE OMNeT++ IDE

UI Performance Monitoring

Programming in C# with Microsoft Visual Studio 2010

Connecting to WebSphere ESB and WebSphere Process Server

Web Service Caching Using Command Cache

OpenEmbeDD basic demo

Listeners. Formats. Free Form. Formatted

Creating Web Services Applications with IntelliJ IDEA

Informatica Cloud Connector for SharePoint 2010/2013 User Guide

Arduino & Android. A How to on interfacing these two devices. Bryant Tram

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Creating a Simple, Multithreaded Chat System with Java

+ Introduction to JUnit. IT323 Software Engineering II By: Mashael Al-Duwais

Application Developer s Guide

Struts Tools Tutorial. Version: M5

Developing a Web Server Platform with SAPI Support for AJAX RPC using JSON

Licensed for viewing only. Printing is prohibited. For hard copies, please purchase from

Distribution and Integration Technologies

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

Android Environment SDK

A Sample OFBiz application implementing remote access via RMI and SOAP Table of contents

Introduction to Building Windows Store Apps with Windows Azure Mobile Services

DEVELOPING CONTRACT - DRIVEN WEB SERVICES USING JDEVELOPER. The purpose of this tutorial is to develop a java web service using a top-down approach.

Brekeke PBX Web Service

Java Access to Oracle CRM On Demand. By: Joerg Wallmueller Melbourne, Australia

Author: Sascha Wolski Sebastian Hennebrueder Tutorials for Struts, EJB, xdoclet and eclipse.

Building an Agile PLM Web Application with JDeveloper and Agile 93 Web Services

Beginning POJOs. From Novice to Professional. Brian Sam-Bodden

Introducing Apache Pivot. Greg Brown, Todd Volkert 6/10/2010

JAVA r VOLUME II-ADVANCED FEATURES. e^i v it;

Developing Web Services with Eclipse and Open Source. Claire Rogers Developer Resources and Partner Enablement, HP February, 2004

Upgrading a Microsoft Dynamics NAV 2009 R2 or Microsoft Dynamics NAV 2009 SP1 Database to Microsoft Dynamics NAV 2015

How To Use Query Console

E4 development: examples, methods and tools. Eclipse Con France 2014

Getting Started with Telerik Data Access. Contents

... Introduction... 17

Eclipse Rich Client Platform. Kai Tödter Karsten Becker et al. Organized by:

Java EE Web Development Course Program

Accessing Data with ADOBE FLEX 4.6

Tutorial Reference Manual. Java WireFusion 4.1

Mitigation Planning Portal MPP Reporting System

The presentation explains how to create and access the web services using the user interface. WebServices.ppt. Page 1 of 14

labs Attacking JAVA Serialized Communication By: Manish S. Saindane

CHAPTER 10: WEB SERVICES

Eclipse with Mac OSX Getting Started Selecting Your Workspace. Creating a Project.

JBoss Portlet Container. User Guide. Release 2.0

New Features for Sybase Mobile SDK and Runtime. Sybase Unwired Platform 2.1 ESD #2

Transcription:

Tutorial for Creating Resources in Java - Client Overview Overview 1. Preparation 2. Creation of Eclipse Plug-ins 2.1 The flight plugin 2.2 The plugin fragment for unit tests 3. Create an integration test for testing the backend resource 4. Deserializing the request response 4.1 Creating a class for FlightData 4.2 Create a content handler 4.3 Creating a unit test for the content handler 4.4 Add an XML File with Test Data 5. Add the Content Handler to the Integration Test 6. Final Words you Should not Miss 7. Further References 1. Preparation The complete code of the example can be downloaded at Perforce. Please use the code for further reference. In the following, we will focus on a quite simplified end-to-end example. 2. Creation of Eclipse Plug-ins At first, we will create Plug-ins for the sample code and a separate Plug-In fragment for tests. It is recommended to switch to the Plug-In Development perspective. 2.1 The flight plugin Create a new Eclipse plug-in com.sap.adt.xx.examples.flight (it's recommended to use your own namespace, so that you can also import the sample solution in parallel). Configure the plug-ins dependencies in the MANIFEST.MF file as follows: Within the plugin, you get a package 'com.sap.adt.xx.examples.flight'. 2.2 The plugin fragment for unit tests We will put all tests into a separate plugin fragment. Create a new Eclipse plug-in fragment com.sap.adt.xx.examples.flight.test. Enter com.sap.adt.xx.examples.flight as host plugin name. For the fragment which inherits the dependencies from the host plug-in, we need to define the following additional dependencies for the tests:

easymock is needed for UI testing which is not part of this example, so you do not have to add the dependency. Within the plugin, you get a package 'com.sap.adt.xx.examples.flight.test'. 3. Create an integration test for testing the backend resource Go to the test plugin and create a new Java class with the name ResourceExampleTests and add a new test method invokegetmethodonrestresource(). Now you should have the following coding: public class ResourceExampleTests { @Test public void invokegetmethodonrestresource() { Next, we have to register a JCo destination provider with our test helper. It will later be used to connect to the backend with the default connection settings to unit tests. So we put this code in a class with the @RunWithDestination annotation. The code looks like this: @RunWith(AdtIntegrationTest.class) @RunWithDestination(DestinationTestUtil.BAP) public class ResourceExampleTests { @Test public void invokegetmethodonrestresource() { Note: To organize your imports, press Ctrl+Shift+O. Now, it is time to write the code for invoking the method on the backend resource controller. This coding consists of 4 steps: 1. 2. 3. 4. Create a stateless connection for the backend. Create the URI (id) for the resource which you want to manipulate Create a rest resource for the system session and URI. This object offers HTTP methods to communicate with the backend. Call the GET method on the resource. In the end the necessary code looks like this:

// (imports skipped) @SuppressWarnings("restriction") @RunWith(AdtIntegrationTest.class) @RunWithDestination(DestinationTestUtil.BAP) public class ResourceExampleTests { @Test public void invokegetmethodonrestresource() throws Exception { // create resource factory IRestResourceFactory restresourcefactory = AdtCommunicationFrameworkPlugin.getDefault().getFactory().createRestResourceFactory(); // get destination from test utility String destination = DestinationTestUtil.getInstance().getDestinationId(); // set URI to a single flight resource URI flighturi = new URI( "/sap/bc/adt/examples/restflights/flights/aa/0017/20100916"); IRestResource flightresource = restresourcefactory.createresourcewithstatelesssession(flighturi, destination); // check if resource in the backend exists (HTTP response code OK = 200) IRestResponse response = flightresource.get( null, IRestResponse.class); assertequals(httpurlconnection.http_ok,response.getstatus()); The code shall compile now. Please run the unit test as a JUnit-Plugin-Test. If it fails, please check the table entries of the database table sflight in the backend. Adjust the URI so that an existing table entry will be selected. This code does not yet deserialize the response body which contains the flight XML data because we did not yet provide a content handler and a data object which represents a single flight. 4. Deserializing the request response In the backend, we have implemented a simple transformation which is used to deserialize and serialize the content data of the request or response, respectively. At the client side, we will use a specific content handler which is able to serialize and deserialize the xml flight data. First, we will define a simple FlightData class which represents a single flight instance. 4.1 Creating a class for FlightData Go to the plugin com.sap.adt.xx.examples.flight and create a new package com.sap.adt.xx.examples.flight.data. Create a new class FlightData and copy the code below.

// (imports skipped) public class FlightData { carrierid; connectionid; flightdate; planetype; price; currency; seatsmax; seatsoccupied; public FlightData() { public FlightData( String carrierid, String connectionid, String flightdate, String planetype, String price, String currency, String seatsmax, String seatsoccupied) { super(); this.carrierid = carrierid; this.connectionid = connectionid; this.flightdate = flightdate; this.planetype = planetype; this.price = price; this.currency = currency; this.seatsmax = seatsmax; this.seatsoccupied = seatsoccupied; You will get compiler warnings for the unused private attributes. Use the eclipse source code editor functionality (ALT-SHIFT-S) to add the following methods automatically: Add setters and getters for all attributes Add a tostring() method Add a hashcode() method Add an equals() method The warnings will disappear. We will now start to create a content handler with a corresponding unit test. 4.2 Create a content handler Go to the plugin com.sap.adt.xx.examples.flight and create a new class FlightDataContentHandler in package com.sap.adt.xx.examples.flight.data which implements the interface IContentHandler<FlightData>. Create stubs for all interface methods. The methods getsupportedcontenttype and getsupporteddatatype can be implemented easily (see below). For now, we leave the serialize and deserialize methods unimplemented.

// (imports skipped) public class FlightDataContentHandler implements IContentHandler<FlightData> { public FlightData deserialize(imessagebody body, Class<? extends FlightData> datatype) { // TODO Auto-generated method stub return null; public IMessageBody serialize(flightdata dataobject, Charset charset) { // TODO Auto-generated method stub return null; public String getsupportedcontenttype() { return AdtMediaType.APPLICATION_XML; public Class<FlightData> getsupporteddatatype() { return FlightData.class; 4.3 Creating a unit test for the content handler Go to the plugin fragment com.sap.adt.xx.examples.flight.test and create a new package com.sap.adt.xx.examples.flight.data. This package already exists in the "productive" Plug-in. Because we add tests for the *.data-plug-in, the tests should be part of the same package. In the new package, create class TestsFlightDataContentHandler.

@SuppressWarnings("restriction") public class TestsUnitFlightDataContentHandler { private static final String TESTFILE_XML = "testflight.xml"; private static final String PLANETYPE = "747-400"; private static final String CURRENCY = "USD"; private static final String CONNECTION_ID = "0017"; private static final String CARRIER_ID = "AA"; private static final String FLIGHT_DATE = "2010-09-16"; private static final String PRICE = "500.00"; private static final String SEATSMAX = "385"; private static final String SEATS_OCCUPIED = "375"; @Test public void deserialize() throws Exception { FlightDataContentHandler contenthandler = new FlightDataContentHandler(); IMessageBody messagebody = createmessagebody(testfile_xml); FlightData flightdata = contenthandler.deserialize(messagebody, FlightData.class); assertnotnull( "Flight data is null", flightdata); assertequals(carrier_id, flightdata.getcarrierid()); assertequals(connection_id, flightdata.getconnectionid()); assertequals(flight_date, flightdata.getflightdate()); assertequals(price, flightdata.getprice()); assertequals(currency, flightdata.getcurrency()); assertequals(planetype, flightdata.getplanetype()); assertequals(seatsmax, flightdata.getseatsmax()); assertequals(seats_occupied, flightdata.getseatsoccupied()); @SuppressWarnings("restriction") @Test public void serialize() throws Exception { FlightDataContentHandler contenthandler = new FlightDataContentHandler(); FlightData flightdata = new FlightData(CARRIER_ID, CONNECTION_ID, FLIGHT_DATE, PLANETYPE, PRICE, CURRENCY, SEATSMAX, SEATS_OCCUPIED); // Prepare expected string: IMessageBody expectedbody = createmessagebody(testfile_xml); String expectedxml = FileUtils.toString(expectedBody.getContent()).replaceAll( "[\n\r ]", ""); // Call the serialization to get the actual string: IMessageBody actbody = contenthandler.serialize(flightdata, null); String actualxml = FileUtils.toString(actBody.getContent()).replaceAll( "[\n\r ]", ""); // assertequals(expectedxml, actualxml); protected IMessageBody createmessagebody( String name) throws IOException { InputStream stream = getclass().getresourceasstream(name); if (stream == null) { fail( "No such file: '" + name + "' in " + getclass().getprotectiondomain().getcodesource().getlocation()); return null ; // never happens return new InputStreamMessageBody(AdtMediaType.APPLICATION_XML, stream); In order to avoid some warnings due to discouraged access, we have added the annotation @SuppressWarnings("restriction") because this is not productive code.

The code will compile but does not yet work. We will need a sample XML file. 4.4 Add an XML File with Test Data In the same package, create a new file "testflight.xml" with the following content: <?xml version='1.0' encoding='utf-8'?> <flights:flight xmlns:flights="http://www.sap.com/adt/examples/flights" carrierid= "AA" connectionid= "0017" date= "2010-09-16" price= "500.00" currency= "USD" planetype= "747-400" seatsmax= "385" seatsoccupied= "375" /> Execute the unit tests as a JUnit Test ("Debug as JUnit Test"). Of course, both tests will fail. Feel free to implement the serialize and deserialize methods now. Here is the code: public class FlightDataContentHandler implements IContentHandler<FlightData> {... private static final String FLIGHTS_ELEMENT = "flights"; private static final String ATTR_SEATSOCCUPIED = "seatsoccupied"; private static final String ATTR_SEATSMAX = "seatsmax"; private static final String ATTR_PLANETYPE = "planetype"; private static final String ATTR_CURRENCY = "currency"; private static final String ATTR_PRICE = "price"; private static final String ATTR_DATE = "date"; private static final String ATTR_CONNECTIONID = "connectionid"; private static final String ATTR_CARRIER_ID = "carrierid"; private static final String FLIGHT_ELEMENT = "flight"; private static final String HTTP_NAMESPACE = "http://www.sap.com/adt/examples/flights"; protected final AdtStaxContentHandlerUtility utility = new AdtStaxContentHandlerUtility();... public FlightData deserialize(imessagebody body, Class<? extends FlightData> datatype) { XMLStreamReader xsr = null; try { xsr = this.utility.getxmlstreamreader(body); FlightData flightdata = new FlightData(); for ( int event = xsr.next(); event!= XMLStreamReader.END_DOCUMENT; event = xsr.next()) { switch (event) { case XMLStreamReader.START_ELEMENT: if (HTTP_NAMESPACE.equals(xsr.getNamespaceURI()) && FLIGHT_ELEMENT.equals(xsr.getLocalName())) { flightdata.setcarrierid(getflightattribute(xsr, ATTR_CARRIER_ID)); flightdata.setconnectionid(getflightattribute(xsr, ATTR_CONNECTIONID)); flightdata.setflightdate(getflightattribute(xsr, ATTR_DATE)); flightdata.setprice(getflightattribute(xsr, ATTR_PRICE)); flightdata.setcurrency(getflightattribute(xsr, ATTR_CURRENCY)); flightdata.setplanetype(getflightattribute(xsr, ATTR_PLANETYPE)); flightdata.setseatsmax(getflightattribute(xsr, ATTR_SEATSMAX)); flightdata.setseatsoccupied(getflightattribute(xsr, ATTR_SEATSOCCUPIED)); break; return flightdata; catch (XMLStreamException e) { throw new ContentHandlerException(e.getMessage(), e); catch (NumberFormatException e) { throw new ContentHandlerException(e.getMessage(), e);

finally { if (xsr!= null) { this.utility.closexmlstreamreader(xsr); getflightattribute(xmlstreamreader xsr, String attributename) { String carrierid = xsr.getattributevalue( null, attributename); if (carrierid == null) { throw new ContentHandlerException( "Attribute" + attributename + "not set" ); return carrierid; public IMessageBody serialize(flightdata dataobject, Charset charset) { XMLStreamWriter xsw = null; try { xsw = this.utility.getxmlstreamwriterandstartdocument(charset, AdtStaxContentHandlerUtility.XML_VERSION_1_0); xsw.setprefix(flights_element, HTTP_NAMESPACE); xsw.writestartelement(http_namespace, FLIGHT_ELEMENT); xsw.writenamespace(flights_element, HTTP_NAMESPACE); xsw.writeattribute(attr_carrier_id, dataobject.getcarrierid()); xsw.writeattribute(attr_connectionid, dataobject.getconnectionid()); xsw.writeattribute(attr_date, dataobject.getflightdate()); xsw.writeattribute(attr_price, dataobject.getprice()); xsw.writeattribute(attr_currency, dataobject.getcurrency()); xsw.writeattribute(attr_planetype, dataobject.getplanetype()); xsw.writeattribute(attr_seatsmax, dataobject.getseatsmax()); xsw.writeattribute(attr_seatsoccupied, dataobject.getseatsoccupied()); xsw.writeendelement(); xsw.writeenddocument(); catch (XMLStreamException e) { throw new ContentHandlerException( null, e); finally { this.utility.closexmlstreamwriter(xsw); return this.utility.createmessagebody( this.getsupportedcontenttype());

Hopefully, your test result is OK now! 5. Add the Content Handler to the Integration Test Now, it is easy to modify the integration test we have created above and add the content handler. Modify the last lines of the code in the test class ResourceExampleTests in the following way:... FlightDataContentHandler flighthandler = new FlightDataContentHandler(); flightresource.addcontenthandler(flighthandler); // check if resource in the backend exists (HTTP response code OK = 200) FlightData flightdataread = flightresource.get( null, FlightData.class); assertequals( "AA", flightdataread.getcarrierid()); assertequals( "0017", flightdataread.getconnectionid()); You can see that we have registered the content handler by use of the addcontenthandler method of the resource. The get is now able to return an instance of FlightData instead of the IRestResponse. The code should compile. Start the test as a JUnit Plug-in test in the debug mode. The test result should be green. 6. Final Words you Should not Miss Congratulations, you successfully implemented your first resource controller and invoked it from Eclipse! The example described here is just for training. When writing client code, you should follow some basic rules: The client should never create any URI's or make any assumptions how they look like! Unlike in this example, please use backend services to get the URI to access the resource. E.g., the discovery service shall be used to get the URI to the collection /sap/bc/adt/examples/restflights/flights as an entry URI to execute an initial flight query. Please take a look at the complete example class TestsIntegrationFlights. For productive code, of course, you need to declare Plug-in dependencies correctly rather than ignoring warnings Try to avoid string literals and use constants in your test classes, too. We are constantly improving our documentation. Please let us know if you have any questions or find any errors! 7. Further References As mentioned under Preparation, you can take a look at the example code in the adt tools plugin in packages com.sap.adt.examples.flight and com.sap.adt.examples.flight.test which can be downloaded from the Perforce server. Example Class FlightManager CarrierData CarrierDataTable FlightData FlightDataContentHandler FlightDataTable FlightDataTableContentHandler Description The class implements a simple example view which can be displayed by use of CTRL-3 "flights". Data model for carriers (single carrier). Please note that the carrier data example is still based on asxml. For new resources, asxml must not be used any more Data model for carriers (collection). See above! Data model for flights. Corresponds with the example described here Content handler for flight data which is also part of the example above Data model for a list of flights. It is used by the integration test for the GET method of the collection resource Content handler for a list of flights

TestsIntegrationCarriers TestsIntegrationFlights TestsUnitFlightDataContentHandler TestsUnitFlightDataTableContentHandler testflight.xml testflights.xml Integration test for carrier query using the CarrierData and CarrierDataTable Integration test for flights. Example with create, read, update, delete (CRUD) and query on the collection resource Unit test for flight data content handler Unit test for flight data table content handler Sample XML for content handler unit test (single flight) Sample XML for content handler unit test (flight list)