|
Even though we created simple test programs during the development stage, these programs really didn’t exercise the Web Services enough to test their availability and whether the methods behaved properly. Another aspect to consider during testing is the ability of your server to handle the load from the demand put on your Web Services. We begin by examining requests and responses.
With all the technology available, it’s easy to forget that XML is underneath. Here’s a request and response from the Money Exchange Web Service to jar your memory. Remember that the request and response can be viewed in WebServicesStudio and through the tools Apache provides in both SOAP and Axis. Luckily all the tools mentioned in this book work fairly well together because of the compatible WSDL the tools generate. If, however, you ever had problems communicating with a particular service, examining the request and response is a great way to look for inconsistencies. As Web Services mature, you’ll find that more and more software will become compatible and the problems with making different requests and response will disappear.
The following SOAP request is similar to many requests viewed thus far in the book except that this is the first service that sends a float to the method. You’ll notice that the quantity tag contains a type definition for xsd:float.
<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap= "http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc= "http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns= "http://localhost:8080/axis/MoneyExchange.jws" xmlns:types= "http://localhost:8080/ axis/MoneyExchange.jws/encodedTypes" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd= "http://www.w3.org/2001/XMLSchema"> <soap:Body soap:encodingStyle= "http://schemas.xmlsoap.org/soap/encoding/"> <tns:returnUSDollarEquiv> <countryName xsi:type="xsd:string"> Argentina </countryName> <quantity xsi:type="xsd:float">350</quantity> </tns:returnUSDollarEquiv> </soap:Body> </soap:Envelope>
The response then returns the equivalent in US dollars as a double to the client.
<?xml version="1.0" encoding="utf-8"?> <soapenv:Envelope xmlns:soapenv= "http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd= "http://www.w3.org/2001/XMLSchema" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Body> <ns1:returnUSDollarEquivResponse soapenv:encodingStyle= "http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1= "http://localhost:8080/axis/MoneyExchange.jws"> <returnUSDollarEquivReturn xsi:type="xsd:double"> 93.205 </returnUSDollarEquivReturn> </ns1:returnUSDollarEquivResponse> </soapenv:Body> </soapenv:Envelope>
WebServicesStudio allows you to make requests and responses to the server without having to actually create a client, as mentioned in Chapter 9. Figure 11.3 shows WebServiceStudio calling the methods of the Money Exchange Web Service. Using this tool allows you to test the methods and view requests and responses without having to write a program. For complete testing, however, you’ll want to have a command line program or some Web pages that can test the Web Service in an automated fashion and gather statistics on which methods might be failing.
Figure 11.3: Using WebServicesStudio to test one of the Money Exchange methods.
This is a very handy tool, as long as the Web Service generates WSDL.
Testing is something you can never do enough of, especially in a cross-platform environment. The first thing you need to test is your ability to communicate with the Web Service through the use of simple test programs as shown in previous sections of this chapter. Once that phase is complete, you need to develop a test strategy that allows you to test all the various platforms and consumer types you make available to your customer base.
Consider the following Java code. The code tests each method to see whether it returns the correct value for the attributes sent to the method. If the correct value returns, the test program prints out a message saying that the test PASSED. It also prints out messages when tests fail. This makes it easy to search a log file looking for failures. The code in this client is simply the client code used in the previous examples but here it checks each conversion to see if the proper value is returned.
Import localhost.*; public class FullTestMoneyExchange { public static void main(String [] args) throws Exception { //init value passed back from service double returnedValue = 0; String currencyName = null; MoneyExchangeServiceLocator myMoneyExchangeLocation = new MoneyExchangeServiceLocator();
localhost.MoneyExchange myMoneyExchange = myMoneyExchangeLocation.getMoneyExchange(); //get currency name for all countries currencyName = myMoneyExchange.returnCurrencyName ("Argentina"); if (currencyName.equals("Peso")) { System.out.println("Argentina Money Name Test PASSED " + currencyName); } else { System.out.println ("Argentina Money Name Test Failed " + currencyName); } currencyName = myMoneyExchange.returnCurrencyName("Australia"); if (currencyName.equals("Dollar")) { System.out.println("Australia Money Name Test PASSED " + currencyName); } else { System.out.println("Australia Money Name Test Failed " + currencyName); } currencyName = myMoneyExchange.returnCurrencyName("China"); if (currencyName.equals("Renminbi")) { System.out.println("China Money Name Test PASSED " + currencyName); } else { System.out.println("China Money Name Test Failed " + currencyName); } currencyName = myMoneyExchange.returnCurrencyName("Japan"); if (currencyName.equals("Yen")) { System.out.println("Japan Money Name Test PASSED " + currencyName); } else { System.out.println("Japan Money Name Test Failed " + currencyName); } currencyName = myMoneyExchange.returnCurrencyName("Mexico"); if (currencyName.equals("Peso")) { System.out.println ("Mexico Money Name Test PASSED " + currencyName); } else { System.out.println("Mexico Money Name Test Failed" + currencyName); } currencyName = myMoneyExchange.returnCurrencyName("Swiss"); if (currencyName.equals("Franc")) { System.out.println("Swiss Money Name Test PASSED " + currencyName); } else { System.out.println("Swiss Money Name Test Failed " + currencyName); } currencyName = myMoneyExchange.returnCurrencyName("UK"); if (currencyName.equals("Pound")) { System.out.println("Swiss Money Name Test PASSED " + currencyName); } else { System.out.println("Swiss Money Name Test Failed " + currencyName); } currencyName = myMoneyExchange.returnCurrencyName("France"); if (currencyName.equals("No match")) { System.out.println ("No Match Money Name Test PASSED " + currencyName); } else { System.out.println("Swiss Money Name Test Failed " + currencyName); } //get equiv returnedValue = myMoneyExchange.returnForeignEquiv ("Argentina",1000); if (returnedValue == 3755.0) { System.out.println("Converstion to Argentinian Money PASSED " + returnedValue); } else { System.out.println("Converstion to Argentinian Money Failed " + returnedValue); } returnedValue = myMoneyExchange.returnForeignEquiv ("Australia",1000); if (returnedValue == 1830.0) { System.out.println("Converstion to Australian Money PASSED " + returnedValue); } else { System.out.println("Converstion to Australian Money Failed " + returnedValue); } returnedValue = myMoneyExchange.returnForeignEquiv("China",1000); if (returnedValue == 8290.0) { System.out.println("Converstion to Chinese Money PASSED " + returnedValue); } else { System.out.println("Converstion to Chinese Money Failed " + returnedValue); } returnedValue = myMoneyExchange.returnForeignEquiv("Japan",1000); if (returnedValue == 81.0) { System.out.println("Converstion to Japanese Money PASSED " + returnedValue); } else { System.out.println("Converstion to Japanese Money Failed " + returnedValue); } returnedValue = myMoneyExchange.returnForeignEquiv("Mexico",1000); if (returnedValue == 10120.0) { System.out.println("Converstion to Mexican Money PASSED " + returnedValue); } else { System.out.println("Converstion to Mexican Money Failed " + returnedValue); } returnedValue = myMoneyExchange.returnForeignEquiv("Swiss",1000); if (returnedValue == 1478.2) { System.out.println("Converstion to Swiss Money PASSED " + returnedValue); } else { System.out.println("Converstion to Swiss Money Failed " + returnedValue); } returnedValue = myMoneyExchange.returnForeignEquiv("UK",1000); if (returnedValue == 637.84) { System.out.println("Converstion to UK Money PASSED" + returnedValue); } else { System.out.println("Converstion to UK Money Failed " + returnedValue); } returnedValue = myMoneyExchange.returnForeignEquiv("France",1000); if (returnedValue == -1) { System.out.println("No match Test PASSED" + returnedValue); } else { System.out.println("Not match Test Failed " + returnedValue); } //repeat for other methods } }
Note | When testing a Web Service or any other type of code, you want to not only test expected values but also what happens when you pass something that is out of range. You’ll notice that there are two calls in the previous example that send France as the name of the country; the code, however, doesn’t support this but still returns something to the method to let the program know. Supporting things that might cause an exception or out of range error are just as important to test as is seeing whether you receive the expected result. |
Note | This code is written as a command line application, but if you plan on supporting JSP and ASP.NET Web pages, you need to have tests that reside in those types of applications as well. |
Note | Testing methods and the different types of consumers are important, but so is knowing how many requests per second the server can handle. So by creating consumers that live in your supported consumers and then calling them through test software such as the Apache Group’s JMeter™, you can gauge whether your server hardware will support your deployment. |
|