Sunday, 14 December 2014

6. Why do we test web services?


Why do we Test Web services?

ans: 1. To Validate service is working fine or not.
2. Whether the given request is sending response or not.
3. To Validate HTTP Protocols.
4. 


Different sorts of testing


As with any other application, there are different sorts of testing you can carry out on web services:

Proof-of-concept testing

Web services are a new type of software. Because of this, you will need to understand if the architecture you have chosen for your web service is the correct one. There are many different choices to be made - which tool vendor to use, which programming language and which database backend, for example. If you can clarify and resolve these issues before you start developing, or early on in the development life cycle, then you will save a lot of time and money further on down the line. Because most of the questions you need to resolve will concern scalability issues (Will the architecture we’re using really cope with 1,000 simultaneous users?), a proof-of-concept test is normally a cut-down load test (see below). There’s no need to run it on powerful hardware, or get exact answers; your aim is to answer the question, "Am I going in the right direction?".

Functional testing

This is to ensure that the functionality of the web service is as expected. If you have a web service that divides two numbers, does it give the expected result? If you pass in a 0 as the denominator, does it handle this correctly? Does your web service implement security/authentication as it is meant to? Does your web service support all the communications protocols it is meant to? Because your web service can be accessed by clients that you can’t control, what happens if they make requests you aren’t expecting? Bounds testing and error checking are especially important.

Regression testing

A regression test is normally a cut-down version of a functional test. Its aim is to ensure that the web service is still working between builds or releases. It assumes that some area of functionality was working in the past, and its job is to check that it still does. If your development team has changed the method that divides two numbers, does that method still work? Does the method that multiplies two numbers still work? Is the performance still acceptable? Since regression testing is, by its nature, a repetitive task then it must be automated. This is true for traditional applications and websites; it is even truer for UI-less web services.

Load/stress testing

This White Paper will concentrate on load and stress testing. The aim of load/stress testing is to find how your web service scales as the number of clients accessing it increases. You have carried out functional and regression testing, so you know that your web service will cope with a single user. What you need to know now is if it will cope with 10, 100 or 1,000 users, or how many users it will cope with. If you double the number of users, do response times stay the same? If you double the number of servers running your web service, does its capacity double? The following section in this White Paper will go into more details about these issues.


5. What are the types of Web Services?

Web services can be implemented in various ways. Web services can be classified as “Big” web services and “RESTful” web services.

·         1 Big web services
·         2 RESTful Web Services
Big web services
Big web services are based on SOAP standard and often contain a WSDL to describe the interface that the web service offers. The details of the contract may include messages, operations, bindings, and the location of the web service.
Big web services includes architecture to address complex non-functional requirements like transactions, security, addressing, trust, coordination, and also handles asynchronous processing and invocation.
The SOAP message format and the WSDL interface definition language have gained widespread adoption in traditional enterprises.
SOAP based Web Services is a great solution when you need,
§  Asynchronous processing
§  Reliability
§  Stateful operations – If the application needs contextual information and conversational state management then SOAP 1.2 has the additional specification in the WS* structure to support those things (Security, Transactions, Coordination, etc).
RESTful Web Services
RESTful web services are based on the way how our web works. Our very own world wide web (www) – the largest distributed application – is based on an architectural style called REST – Representational State Transfer. REST is neither a standard nor a protocol. It is just an architectural style like say for example client-server architecture (client-server is neither a standard nor a protocol). Web services following this architectural style are said to be RESTful Web services.
So what is this REST? According to Roy Fielding who coined this term,
“Representational State Transfer is intended to evoke an image of how a well-designed Web application behaves: Presented with a network of web pages (a virtual state-machine), the user progresses through an application by selecting links (state transitions), resulting in the next page (representing the next state of the application) being transferred to the user and rendered for their use.”
In the web, everything is identified by resources. When we type a URL in the browser we are actually requesting a resource present on the server. A representation of the resource (normally a page) is returned to the user which depicts the state of the application. On clicking any other link, the application transfers state with the new representation of the resource. Hence the name Representational State Transfer.
REST-style architecture follows this concept and consists of clients and servers. Clients initiate requests to servers; servers process requests and return appropriate responses. Requests and responses are built around the transfer of representations of resources which are identified by URI (Uniform Resource Identifier).
RESTful web services are based on HTTP protocol and its methods mainly PUT, GET, POST, and DELETE. These web services are better integrated with HTTP than SOAP-based services are, and as such do not require XML SOAP messages or WSDL service definitions.
Because RESTful web services use existing well-known standards (HTTP, XML, URI, MIME) and have a lightweight infrastructure that allows services to be built with minimal tooling, developing RESTful web services is inexpensive and thus has a very low barrier for adoption.
RESTful Web Service HTTP methods
A RESTful web services is a collection of resources. For example, consider an office has deployed a web services to get a list of employees and to get individual employee data for use with other departments. The web service makes available a URL to a ‘list of employees’ resource. For example, a client would use this URL to get the employee list:
http://www.example.com/myoffice/employees
On sending a request to that particular URL, the client would receive the following document.
<employees xmlns:xlink="http://www.w3.org/1999/xlink">
<employee xlink:href="http://www.example.com/myoffice/employee/234">234</employee>
<employee xlink:href="http://www.example.com/myoffice/employee/235">235</employee>
<employee xlink:href="http://www.example.com/myoffice/employee/236">236</employee>
<employee xlink:href="http://www.example.com/myoffice/employee/237">237</employee>
</employees>
The above document contains the links to get detailed info about each employee. This is a key feature of REST. The client transfers from one state to the next by examining and choosing from among the alternative URLs in the response document.
To get individual employee information, the web service makes available a URL to each employee resource. For example, to get employee information whose id is 237, the client may send a request to the following URL:
http://www.example.com/myoffice/employee/237
And the response document containing the employee information may be as follows:
<employee xmlns:xlink="http://www.w3.org/1999/xlink">
<id>237</id>
<firstname>xyz</firstname>
<lastname>abc</lastname>
<address>123 ABC St</address>
<salary>3344.56</salary>
</employee>
http://theopentutorials.com/totwp331/wp-content/uploads/types-of-web-services_2510/4-rest-ws.jpg?c3a9c1
We have seen the use of HTTP GET method to get the information. In the same way, we can use the other HTTP methods like POST, PUT and DELETE.
The logical meaning of these HTTP methods for the URL http://www.example.com/myoffice/employees is as follows,
§  When a HTTP POST request is sent to the above URL with an employee data, the data will be added to the employee list.
§  When a HTTP PUT request is sent to the above URL with a list of employees then the original list will be modified with this employee list.
§  When a HTTP DELETE request is sent to the above URL then the entire list of employees will be deleted.
Similarly for the URL http://www.example.com/myoffice/employee/237 the actions may be interpreted as follows,
§  When a HTTP POST request is sent to the above URL, treat the addressed member as a collection in its own right and create a new entry in it.
o    For example consider a situation where the employee works in a particular department and the URL http://www.example.com/myoffice/dept/A1205 represents the list of employees working in department A1205. So a POST request to this URL with employee data will add an employee data to that particular department.
§  When a HTTP PUT request is sent to the above URL then modify that particular employee with the new request data or create if employee does not exist.
§  When a HTTP DELETE request is sent to the above URL then delete that particular employee.
In this REST form of communication, the service producer and service consumer should have a mutual understanding of the context and content (XML) being passed along. Because there is no WSDL to describe the web services interface, both parties must agree on the schemas that describe the data being exchanged and on ways to process it meaningfully.
A RESTful design may be appropriate when,
§  The web services are completely stateless.
§  The data that the web service returns is not dynamically generated and can be cached.
o    The caching infrastructure that web servers provide can be leveraged to improve performance. However, the developer must take care because such caches are limited to HTTP GET method for most servers.
§  The service producer and service consumer have a mutual understanding of the context and content being passed along.
§  Bandwidth is particularly important and needs to be limited.
o    REST is particularly useful for limited-profile devices, such as PDAs and mobile phones, for which the overhead of headers and additional layers of SOAP elements on the XML payload must be restricted.
§  Web service delivery or aggregation into existing web sites is to be enabled.


Full Forms

WS 

     *     Web Service 

WSDL

  • WSDL stands for Web Services Description Language
  • WSDL is an XML-based language for describing Web services.
  • WSDL is a W3C recommendation

SOAP

  • SOAP stands for Simple Object Access Protocol
  • SOAP is an XML based protocol for accessing Web Services.
  • SOAP is based on XML
  • SOAP is a W3C recommendation

UDDI

  • UDDI stands for Universal Description, Discovery and Integration
  • UDDI is a directory service where companies can search for Web services.
  • UDDI is described in WSDL
  • UDDI communicates via SOAP

RDF

  • RDF stands for Resource Description Framework
  • RDF is a framework for describing resources on the web
  • RDF is written in XML
  • RDF is a W3C Recommendation.

REST 


    * REST stands for Representational State Transfer. (It is sometimes spelled "ReST".) It relies on a stateless, client-server, cacheable communications protocol -- and in virtually all cases, the HTTP protocol is used.

JMS

 * The Java Message Service (JMSAPI is a Java Message Oriented Middleware (MOM) API[1] for sending messages between two or more clients. JMS is a part of the Java Platform, Enterprise Edition, and is defined by a specification developed under the Java Community Process as JSR 914.[2] It is a messaging standard that allows application components based on the Java Enterprise Edition (Java EE) to create, send, receive, and read messages. It allows the communication between different components of adistributed application to be loosely coupled


AMF:

 * Autoscript Message Format.


JDBC :

  1. JDBC is a Java database connectivity technology (Java Standard Edition platform) from Oracle Corporation. This technology is an API for the Java programming language that defines how a client may access a database. It provides methods for querying and updating data in a database.
HTML :

  • HTML stands for Hyper Text Markup Language
  • A markup language is a set of markup tags
  • HTML documents are described by HTML tags
  • Each HTML tag describes different document content

4. Web Services Example:

Web Services Example:
Based on the Web Service Architecture we will create following two components as a part of Web Services implementation
  • Service provider or publisher:
This is the provider of the web service. The service provider implements the service and makes it available on the Internet or intranet.
We will write and publish a simple web Service using .NET SDK.
  • Service requestor or consumer
This is any consumer of the web service. The requestor utilizes an existing web service by opening a network connection and sending an XML request.
We will also write two Web Service requestors: one Web-based consumer (ASP.NET application) and another Windows application-based consumer.
Following is our First Web Service example which works as a service provider and exposes two methods (add and SayHello) as Web Services to be used by applications. This is a standard template for a Web Service. .NET Web Services use the .asmx extension. Note that a method exposed as a Web Service has the WebMethod attribute. Save this file as FirstService.asmx in the IIS virtual directory (as explained in configuring IIS; for example, c:\MyWebSerces).
FirstService.asmx
  <%@ WebService language="C" class="FirstService" %>
 
  using System;
  using System.Web.Services;
  using System.Xml.Serialization;
 
  [WebService(Namespace="http://localhost/MyWebServices/")]
  public class FirstService : WebService
  {
      [WebMethod]
      public int Add(int a, int b)
      {
          return a + b;
      }
 
      [WebMethod]
      public String SayHello()
      {
          return "Hello World";
      }
  }
To test a Web Service, it must be published. A Web Service can be published either on an intranet or the Internet. We will publish this Web Service on IIS running on a local machine. Let's start with configuring the IIS.
  • Open Start->Settings->Control Panel->Administrative tools->Internet Services Manager.
  • Expand and right-click on [Default Web Site]; select New ->Virtual Directory.
  • The Virtual Directory Creation Wizard opens. Click Next.
  • The "Virtual Directory Alias" screen opens. Type the virtual directory name—for example, MyWebServices—and click Next.
  • The "Web Site Content Directory" screen opens. Here, enter the directory path name for the virtual directory—for example, c:\MyWebServices—and click Next.
  • The "Access Permission" screen opens. Change the settings as per your requirements. Let's keep the default settings for this exercise. Click the Next button. It completes the IIS configuration. Click Finish to complete the configuration.
To test that IIS has been configured properly, copy an HTML file (for example, x.html) in the virtual directory (C:\MyWebServices) created above. Now, open Internet Explorer and type http://localhost/MyWebServices/x.html. It should open the x.html file. If it does not work, try replacing localhost with the IP address of your machine. If it still does not work, check whether IIS is running; you may need to reconfigure IIS and Virtual Directory.
To test our Web Service, copy FirstService.asmx in the IIS virtual directory created above (C:\MyWebServices). Open the Web Service in Internet Explorer (http://localhost/MyWebServices/FirstService.asmx). It should open your Web Service page. The page should have links to two methods exposed as Web Services by our application. Congratulations; you have written your first Web Service!!!

Testing the Web Service

As we have just seen, writing Web Services is easy in the .NET Framework. Writing Web Service consumers is also easy in the .NET framework; however, it is a bit more involved. As said earlier, we will write two types of service consumers, one Web- and another Windows application-based consumer. Let's write our first Web Service consumer.

Web-Based Service Consumer

Write a Web-based consumer as given below. Call it WebApp.aspx. Note that it is an ASP.NET application. Save this in the virtual directory of the Web Service (c:\MyWebServices\WebApp.axpx).
This application has two text fields that are used to get numbers from the user to be added. It has one button, Execute, that, when clicked, gets the Add and SayHello Web Services.
 
WebApp.axpx
  <%@ Page Language="C#" %>
  <script runat="server">
  void runSrvice_Click(Object sender, EventArgs e)
  {
      FirstService mySvc = new FirstService();
      Label1.Text = mySvc.SayHello();
      Label2.Text = mySvc.Add(Int32.Parse(txtNum1.Text),
                    Int32.Parse(txtNum2.Text)).ToString();
  }
  </script>
  <html>
  <head>
  </head>
  <body>
  <form runat="server">
    <p>
        <em>First Number to Add </em>:
        <asp:TextBox id="txtNum1" runat="server"
             Width="43px">4</asp:TextBox>
    </p>
    <p>
        <em>Second Number To Add </em>:
        <asp:TextBox id="txtNum2" runat="server"
             Width="44px">5</asp:TextBox>
    </p>
    <p>
        <strong><u>Web Service Result -</u></strong>
    </p>
    <p>
        <em>Hello world Service</em> :
        <asp:Label id="Label1" runat="server"
             Font-Underline="True">Label</asp:Label>
    </p>
 
    <p>
        <em>Add Service</em> :
        & <asp:Label id="Label2" runat="server"
               Font-Underline="True">Label</asp:Label>
    </p>
    <p align="left">
        <asp:Button id="runSrvice" onclick="runSrvice_Click"
             runat="server" Text="Execute"></asp:Button>
    </p>
  </form>
  </body>
  </html>
After the consumer is created, we need to create a proxy for the Web Service to be consumed. This work is done automatically by Visual Studio .NET for us when referencing a Web Service that has been added. Here are the steps to be followed:
  • Create a proxy for the Web Service to be consumed. The proxy is created using the wsdl utility supplied with the .NET SDK. This utility extracts information from the Web Service and creates a proxy. Thus, the proxy created is valid only for a particular Web Service. If you need to consume other Web Services, you need to create a proxy for this service as well. VS .NET creates a proxy automatically for you when the reference for the Web Service is added. Create a proxy for the Web Service using the wsdl utility supplied with the .NET SDK. It will create FirstSevice.cs in the current directory. We need to compile it to create FirstService.dll (proxy) for the Web Service.
c:> WSDL http://localhost/MyWebServices/
 
                FirstService.asmx?WSDL
c:> csc /t:library FirstService.cs
  • Put the compiled proxy in the bin directory of the virtual directory of the Web Service (c:\MyWebServices\bin). IIS looks for the proxy in this directory.
  • Create the service consumer, which we have already done. Note that I have instantiated an object of the Web Service proxy in the consumer. This proxy takes care of interacting with the service.
  • Type the URL of the consumer in IE to test it (for example, http://localhost/MyWebServices/WebApp.aspx).

Windows Application-Based Web Service Consumer

Writing a Windows application-based Web Service consumer is the same as writing any other Windows application. The only work to be done is to create the proxy (which we have already done) and reference this proxy when compiling the application. Following is our Windows application that uses the Web Service. This application creates a Web Service object (of course, proxy) and calls the SayHello and Add methods on it.
WinApp.cs
  using System;
  using System.IO;
 
  namespace SvcConsumer{
  class SvcEater
  {
      public static void Main(String[] args)
      {
          FirstService mySvc = new FirstService();
 
          Console.WriteLine("Calling Hello World Service: " +
                             mySvc.SayHello());
          Console.WriteLine("Calling Add(2, 3) Service: " +
                             mySvc.Add(2, 3).ToString());
      }
  }
  }
Compile it using c:>csc /r:FirstService.dll WinApp.cs. It will create WinApp.exe. Run it to test the application and the Web Service.


3. Web Services – Architecture


Web Services – Architecture:
There are two ways to view the web service architecture.
·         The first is to examine the individual roles of each web service actor.
·         The second is to examine the emerging web service protocol stack.

1. Web Service Roles
There are three major roles within the web service architecture:
  • Service provider:
This is the provider of the web service. The service provider implements the service and makes it available on the Internet.
  • Service requestor
This is any consumer of the web service. The requestor utilizes an existing web service by opening a network connection and sending an XML request.
  • Service registry
This is a logically centralized directory of services. The registry provides a central place where developers can publish new services or find existing ones. It therefore serves as a centralized clearinghouse for companies and their services.

2. Web Service Protocol Stack
A second option for viewing the web service architecture is to examine the emerging web service protocol stack. The stack is still evolving, but currently has four main layers.
  • Service transport
This layer is responsible for transporting messages between applications. Currently, this layer includes hypertext transfer protocol (HTTP), Simple Mail Transfer Protocol (SMTP), file transfer protocol (FTP), and newer protocols, such as Blocks Extensible Exchange Protocol (BEEP).
  • XML messaging
This layer is responsible for encoding messages in a common XML format so that messages can be understood at either end. Currently, this layer includes XML-RPC and SOAP.
  • Service description
This layer is responsible for describing the public interface to a specific web service. Currently, service description is handled via the Web Service Description Language (WSDL).
  • Service discovery
This layer is responsible for centralizing services into a common registry, and providing easy publish/find functionality. Currently, service discovery is handled via Universal Description, Discovery, and Integration (UDDI).

As web services evolve, additional layers may be added, and additional technologies may be added to each layer.