Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Web Service Implementation, Deploy and Test 课程名:以服务为中心的软件开发设计与实现.

Similar presentations

Presentation on theme: "1 Web Service Implementation, Deploy and Test 课程名:以服务为中心的软件开发设计与实现."— Presentation transcript:

1 1 Web Service Implementation, Deploy and Test 课程名:以服务为中心的软件开发设计与实现

2 2 Outline Web service implementation and deploy –REST –JAX-WS –XFire Web service test –Junit Web service invoke –Ajax

3 3 Xfire – A Java SOAP framework for development and consumption of web services

4 4 Writing A Web Service with Xfire - Interface package edu.thu.keg.core.webservice; public interface ArnetminerSearchWebService { /** * search for person list * @param keyword - E.g: Jiawei Han pos:professor aff:Illinois edu:ph.D all:computer * @param start_page - the start page number you want to return * @param num - the number of persons you want to return for each page * @return */ public PersonSearchResult person_search(String keyword, int start_page,int num); public PersonSearchResult person_search(String keyword); … } XFire Step1: Interface

5 5 Writing A Web Service with Xfire - Implementation public class ArnetminerSearchWebServiceImpl implements ArnetminerSearchWebService { public PersonSearchResult person_search(String keyword, int start_page,int num){ if(keyword == null || keyword.length() == 0) { return null; } QueryContactInfoService service = new QueryContactInfoService(); SearchResult results = service.queryContactInfo(keyword, start_page, num); return SearchResultConvertor.SearchResultToPersonSearchResult(results); } public PersonSearchResult person_search(String keyword){ if(keyword == null || keyword.length() == 0) { return null; } int start_page = 0; int num = 10; return person_search(keyword, start_page, num); } … } XFire Step2: Implementation

6 6 Writing A Web Service with Xfire - Configuration XFire Step3: Configuration

7 7 Writing A Web Service with Xfire - Configuration ArnetminerSearchWebService edu.thu.keg.core.webservice.ArnetminerSearchWebService edu.thu.keg.core.webservice.ArnetminerSearchWebSer viceImpl XFire Step4: Release

8 8 Writing A Web Service with Xfire - Client in Java public class ArnetminerSearchWebServiceClient { public static void main(String args[]) throws Exception { String url = "http://localhost:8080/Arnetminer/services/ArnetminerSearchWebService"; Service serviceModel = new ObjectServiceFactory().create(ArnetminerSearchWebService.class); ArnetminerSearchWebService service = (ArnetminerSearchWebService)new XFireProxyFactory(). create(serviceModel, url); assert(service != null); /* System.out.println(id);*/ System.out.println("1. test person search..."); System.out.println("\t querying jie tang..."); PersonSearchResult results1 = service.person_search("jie tang"); System.out.println("Got " + results1.getResultCount() + " results!"); System.out.println("1. test person search..."); System.out.println("\t querying jie tang..."); PersonSearchResult results1_1 = service.person_search("jie tang",1,5); System.out.println("Got " + results1_1.getResultCount() + " results!"); } Step5: Test

9 9 Unit Testing Framework for the Java Programming Language - JUnit

10 10 Test suites Obviously you have to test your code to get it working in the first place –You can do ad hoc testing (running whatever tests occur to you at the moment), or –You can build a test suite (a thorough set of tests that can be run at any time) Disadvantages of a test suite –It’s a lot of extra programming This is true, but use of a good test framework can help quite a bit –You don’t have time to do all that extra work False—Experiments repeatedly show that test suites reduce debugging time more than the amount spent building the test suite Advantages of a test suite –Reduces total number of bugs in delivered code –Makes code much more maintainable and refactorable This is a huge win for programs that get actual use!

11 11 JUnit JUnit is a framework for writing tests –JUnit was written by Erich Gamma (of Design Patterns fame) and Kent Beck (creator of XP methodology) –JUnit uses Java’s reflection capabilities (Java programs can examine their own code) –JUnit helps the programmer: define and execute tests and test suites formalize requirements and clarify architecture write and debug code integrate code and always be ready to release a working version –JUnit is not yet (as far as I know) included in Sun’s SDK, but an increasing number of IDEs include it –BlueJ, JBuilder, and Eclipse now provide JUnit tools

12 12 Structure of a JUnit test class Here’s what you do to test a class named Fraction : public class FractionTest extends junit.framework.TestCase { –This is the unit test for the Fraction class; it declares (and possibly defines) values used by one or more tests public FractionTest() { } –This is the default constructor protected void setUp() –Creates a test fixture by creating and initializing objects and values protected void tearDown() –Releases any system resources used by the test fixture public void testAdd(), public void testToString(), etc. –These methods contain tests for the Fraction methods add(), toString(), etc. (note how capitalization changes)

13 13 Assert methods I Within a test, –Call the method being tested and get the actual result –assert what the correct result should be with one of the provided assert methods –These steps can be repeated as many times as necessary An assert method is a JUnit method that performs a test, and throws an AssertionFailedError if the test fails –JUnit catches these Errors and shows you the result static void assertTrue(boolean test ) static void assertTrue(String message, boolean test ) –Throws an AssertionFailedError if the test fails –The optional message is included in the Error static void assertFalse(boolean test ) static void assertFalse(String message, boolean test ) –Throws an AssertionFailedError if the test fails

14 14 Example: Counter class For the sake of example, we will create and test a trivial “counter” class –The constructor will create a counter and set it to zero –The increment method will add one to the counter and return the new value –The decrement method will subtract one from the counter and return the new value We write the test methods before we write the code –This has the advantages described earlier –Depending on the JUnit tool we use, we may have to create the class first, and we may have to populate it with stubs (methods with empty bodies) Don’t be alarmed if, in this simple example, the JUnit tests are more code than the class itself

15 15 JUnit tests for Counter public class CounterTest extends junit.framework.TestCase { Counter counter1; public CounterTest() { } // default constructor protected void setUp() { // creates a (simple) test fixture counter1 = new Counter(); } protected void tearDown() { } // no resources to release public void testIncrement() { assertTrue(counter1.increment() == 1); assertTrue(counter1.increment() == 2); } public void testDecrement() { assertTrue(counter1.decrement() == -1); } } Note that each test begins with a brand new counter This means you don’t have to worry about the order in which the tests are run

16 16 The Counter class itself  public class Counter { int count = 0; public int increment() { return ++count; } public int decrement() { return --count; } public int getCount() { return count; } } Is JUnit testing overkill for this little class? The Extreme Programming view is: If it isn’t tested, assume it doesn’t work You are not likely to have many classes this trivial in a real program, so writing JUnit tests for those few trivial classes is no big deal Often even XP programmers don’t bother writing tests for simple getter methods such as getCount() We only used assertTrue in this example, but there are additional assert methods

17 17 Assert methods II assertEquals( expected, actual ) assertEquals(String message, expected, actual ) –This method is heavily overloaded: expected and actual must be both objects or both of the same primitive type –For objects, uses your equals method, if you have defined it properly, as public boolean equals(Object o) —otherwise it uses == assertSame(Object expected, Object actual ) assertSame(String message, Object expected, Object actual ) –Asserts that two objects refer to the same object (using == ) assertNotSame(Object expected, Object actual ) assertNotSame(String message, Object expected, Object actual ) –Asserts that two objects do not refer to the same object

18 18 Assert methods III assertNull(Object object ) assertNull(String message, Object object ) –Asserts that the object is null assertNotNull(Object object ) assertNotNull(String message, Object object ) –Asserts that the object is null fail() fail(String message ) –Causes the test to fail and throw an AssertionFailedError –Useful as a result of a complex test, when the other assert methods aren’t quite what you want

19 19 The assert statement Earlier versions of JUnit had an assert method instead of an assertTrue method –The name had to be changed when Java 1.4 introduced the assert statement There are two forms of the assert statement: –assert boolean_condition ; –assert boolean_condition : error_message ; –Both forms throw an AssertionFailedError if the boolean_condition is false –The second form, with an explicit error message, is seldom necessary When to use an assert statement: –Use it to document a condition that you “know” to be true –Use assert false; in code that you “know” cannot be reached (such as a default case in a switch statement) –Do not use assert to check whether parameters have legal values, or other places where throwing an Exception is more appropriate

20 20 Junit in eclipse - Setup In Eclipse –Create a new project –Open project’s property window (File -> Properties) –Select: Java build path –Select: libraries –Add Library –Select Junit Select the type 3.x or 4.x

21 21 Create a new JUnit test case File –New Junit Test Case –Set the parameters: »Junit 3.x or 4.x »name of the class »etc. –Finish Eclipse Menu File Edit Source Refactor Navigate Search Project Run Window Help

22 22 Run as JUnit Test Run –Run As Junit Test Eclipse Menu File Edit Source Refactor Navigate Search Project Run Window Help

23 23 Red / Green Bar expected but was Fail Pass

24 24 Unit Testing Web Services Web services as a presentation layer of sorts –Tested using an approach analogous to testing GUI –Separating the presentation layer from the application logic –Each method and behavior of the web service interface should have corresponding tests To validate how the service methods work "over the wire," it's not sufficient just to create an instance of the service interface class and invoke its methods directly. The service methods must be validated by calling them via the client interface, the same way they are called in the live application.

25 25 Unit Testing Web Services: An example public class MyServiceTest extends TestCase { public void setUp() { String URI = "http://localhost/services/MyService"; //... create service endpoint and connect to URI... MyService service = endpoint.getMyService(); } public void testGetDoc() { service.putDoc( "test" ); assertEquals( "test", service.getDoc() ); } public void tearDown() { service.destroy(); }

26 26 Web service invoke - Ajax

27 27 What is Ajax? AJAX (shorthand for asynchronous JavaScript and XHTML) is a group of interrelated web development techniques used on the client-side to create interactive web applications or rich Internet applications.

28 28 Simple Ajax function Ajax_Call(myurl, callfunc){ if (window.XMLHttpRequest) { req = new XMLHttpRequest(); } else if (window.ActiveXObject) { req = new ActiveXObject("Microsoft.XMLHTTP"); } if( req ) { var localurl; var isrefresh=true; if(isrefresh) { localurl = addTimeStamp1(myurl); } else{ localurl = myurl; }"GET", localurl, true); req.onreadystatechange = callfunc; req.send(null); } 客户端 buttonAjax_call 服务器端 webserver 1 23 http requesthttp response callfunc 4 UI 5

29 29 Ajax Web Services (1) First invoke a Web Service by: function sayHello(name, container) { var call = new WS.Call('/AjaxWS/services/HelloWorld'); var nsuri = 'http://example'; var qn_op = new WS.QName('sayHello',nsuri); var qn_op_resp = new WS.QName('sayHelloResponse',nsuri); call.invoke_rpc( qn_op, new Array( {name:'name',value:name} ), null, function(call,envelope) { var ret = envelope.get_body().get_all_children()[0]. get_all_children()[0].get_value(); container.innerHTML = ret; $('soap').innerHTML = arguments[2].escapeHTML(); } ); }

30 30 Ajax Web Services (2) Then Calling the sayHello function Result: Please refer to the source code: OR

31 31 Ajax is not just Web Service… document.write(unescape("%3Cscript src='‘ type='text/javascript'%3E%3C/script%3E")); arnet_socialGraph("jie tang",900,650);

32 32 WS call is not just ajax… document.write(unescape("%3Cscript src='' type='text/javascript'%3E%3C/script%3E")); person_block_inline("jie tang", -1, -1, 90, 60);

33 33 Summary

34 34 Summary As a distributed computing architecture, Web Services are the most important implementation for SOA; Web Services: Flexible machine-machine interaction; SOAP is a light weight replacement for complicated distributed object technology which we use to wrap Web Services message; The function and interface of Web Services is open by WSDL and the interaction between Web Services is presented by BPEL.

35 35 Further Question? SOAP? WSDL? BPEL? UDDI?

36 36 Reference A list of web service specifications. SOA & Webservices. webservices WebServices. Ueli Wahli,Thomas Kjaer…“WebSphere Version 6 Web Services Handbook Development&Deployment”, /redbooks/pdfs/sg246461.pdf,2005 Stan Kleijnen,Srikanth Raju.An Open Web Services Archiecture.NewYork: ACM Press,2003 XML. XML Schema. WSDL. Tony Andrews, Francisco Curbera. Business Process Execution Language for Web Services Version 1.1. ibmdl/pub/software/dw/specs/ws-bpel/ws-bpel.pdf The Stencil Group.” The Evolution of UDDI, White Paper”

37 37 References Reference WebSite: http://ww.junit.org JUnit 4.0 in 10 minutes Quick and easy introduction Ted Husted, Vincent Massol. JUnit in Action. Book. Kent Beck, Eric Gamma, Test Infected: Programmers Love Writing Tests. Bryan Dollery. JUnit and Its Extensions Make Unit Testing Your Java Code Much Easier. Test First Demo: ml ml An early look at JUnit 4, junit4.html junit4.html Unit Test Web Service,

38 38 Links Roy Felding’s dissertation – SOAP vs REST comparison paper – Security for REST web services presentation by Mark O’Neill of Vordel – Popular REST Frameworks in Java –Restlet ( –Axis 2.0 ( –Jboss RESTEasy ( –Apache CXF ( Popular REST Frameworks in other lang. –Ruby on REST( –Rest in Python ( –.NET Familiar RESTful services –TeraGrid Info Services ( –Amazon Web Services ( –Yahoo! Web Services ( –Google Web Services ( Accessing REST services –Python:

39 39 Thanks! HP:

40 40 Appendix

41 41 Terminology A test fixture sets up the data (both objects and primitives) that are needed to run tests –Example: If you are testing code that updates an employee record, you need an employee record to test it on A unit test is a test of a single class A test case tests the response of a single method to a particular set of inputs A test suite is a collection of test cases A test runner is software that runs tests and reports results An integration test is a test of how well classes work together –JUnit provides some limited support for integration tests

42 42 What is soapUI? soapUI is an application for inspecting, invoking, developing, simulating/mocking and functional/load/compliance testing of web services over HTTP. More on soapUI on Process –In order to begin testing a service we need to have a WSDL file describing it. –Next you will need to create the.xsd files needed in the WSDL description so that soapUI can validate the schema our services responses.

43 43 Creating Tests I Click to start soapUI Now lets create a WSDL project. Go to the menu bar and click File -> New WSDL project From there you can give the project a name. Because of path problems its not recommended to add a WSDL file yet. Click ok and the project will be created

44 44 Creating Tests II Right click on the newly created Project and choose Add WSDL from URL. Then enter something like file:./request.wdsl In windows this may not work. If this is the case then choose Add WSDL from file. Then agree to Create default Requests for all operations. Then you will see that a request was created based on your WSDL description

45 45 Creating Tests III The example requests accuracy is determined by the detail of the definitions in the xsd files You will notice that it’s not quite like the real thing and modifications are required. You may modify it or you can simple copy paste an example request inside the tag.

46 46 Creating Tests IV Right click on the service interface icon and choose Generate Testsuite in order to create the container for the test cases to come This will create the container for the test cases to be defined.

47 47 Creating Tests V To create a new test case right click on the newly created Testsuite icon and choose New TestCase. Give it a name and the new test case is created.

48 48 Creating tests VI To add a request to the test case right click on the example request and choose Add to Testcase The test suite and test cases can be manipulated by right clicking on their icons and selecting their corresponding editors.

49 49 Creating tests VII The request window should now appear. Click on the green play button on the upper left side to send the request to the service. You can set the service address in the text bar on the top of the window. The response will appear on the right hand side By right clicking the space below the window you can add assertions- tests for the response

50 50 Creating Tests VIII Choose schema validation and in the next message box choose the response WSDL file soapUI will then validate the response. In the same manner one can add soap fault assertions XPath matches or simple contains matches

51 51 Creating Tests IX XPath matches are one of the most useful features of soapUI Choose the XPath matches. Then click on declare. Add your XPath expression and click Select from current. The result of the XPath expression on the response will appear on the expected result box If it is what you were going for click save and there you are!

52 52 Creating Tests X In the same manner one can add tests for the response not being a soap fault or just to check if a certain string is part of the result For more in depth testing and for database access the use of a Groovy script is needed…

53 53 Survey on WS Testing – arch/work- on%20paper/WS%20testing/WS%20Testing%20Su rvey%2010.doc

Download ppt "1 Web Service Implementation, Deploy and Test 课程名:以服务为中心的软件开发设计与实现."

Similar presentations

Ads by Google