Monday, March 12, 2018

JDBC-SQL Connection in JMeter

  Hi Guys, this is my notes on connecting to SQL instance through JMeter. For this example I am using latest version of JMeter that is 4.0 and SQL server 2017. We also need the latest SQL JDBC driver jar, I have downloaded the JDBC Driver 6.0.
 1.Open your JMeter instance and add a JDBC config element which helps for setting up the connection to sql server.


2.Now give all the details like DB connection pool name (which is important when using the JDBC sampler),
SQL server URL name, Driver Class ( I am using com.microsoft.sqlserver.jdbc.SQLServerDriver as I am using latest JDBC driver, if your are using JDBC driver less than 5.0 version then you must use the other one that is com.microsoft.jdbc.sqlserver.SQLServerDriver),Username and Password.

3.Now one thing is pending that we need to make sure that the JMeter is able to access the Microsoft JDBC driver so for this purpose we can include the jar file in TestPlan, where we have the option to browse and include that jar files.

4.Now the setup is done, it is time to add the JDBC request sampler along with the required query and donot forget the DB pool connection name which we assigned in JDBC configuration.

5.Finally add listener and run it.

Tuesday, February 27, 2018

Public Static Void main explained

This is my notes on public static void main method which we use regularly in our Java Code. I just literally used it without knowing why it is structured like that, so I started looking for this. Below are my points, Please do correct me if I am wrong.
Before going into deep what exactly is happening let us see the structure of this method.
PUBLIC  STATIC  VOID  MAIN (STRING A[])
Here
Public keyword is one of the Access Modifiers which  are available in Java
Static keyword is used for class-method binding
Void specifies the return type
Main is the method name
String A[] : String represents datatype and A represents the variable name

Basically when we execute the “.java” code it creates a “.class” file with the same name which we have given for “.java” code. When we try to execute this class file the JVM instance will takes the class name and by defaults searches for main method.

Here it will not create any object to that class instead it searches for the main method which is bound to that class, so for this purpose we need to specify the “Static” keyword  because “Static” keyword helps to bind the method to that particular class instead to the object of that class.
Just binding is not sufficient but also we need to take care of its accessibility, so for this purpose we use the “public” keyword which specifies that this method can be accessible by other classes also.
So now we can access the main method directly using the class itself.
For example, consider the below sample java code.

public class Sample {
public static void main(String a[])
{
System.out.println("Welcome to codeanyway blog");
}
}
As we specified the static keyword now the JVM can access the main() as Sample.main() directly.
     Now there is another constraint in Java that is every method should have a return type. We specified it void as we are not returning any data in this method.
    We are giving parameters to main method because it facilitates us to pass data during execution.



Sunday, February 25, 2018

UNIX Commands

 This is my personal notes of UNIX commands with practise snapshots. Hope it might be helpful for others also, please correct if there are any wrongs in the commands.

Commands:

1.Uname: This command is used to get the details about OS details and the kernel version details. This command is useful if you don't know any details about the OS on which you are working.
Options:
·        -a: This option is useful if you want all the information of the system
·        -s: This option prints only the kernel name
·        -r:  This option prints only the kernel release details
·        -v: This option prints the kernel version details
·        -m: This option prints the details of the machine hardware
·        -p:  This option prints the processor details
·        -n: This option prints the network node hostname
·        -i: This option prints the details about non-portable hardware network

To get a better view of these commands I took the snapshots of my linuxpractiseinstance.

-a:


-s:
-r:
-v:
-n:



Sunday, December 17, 2017

Java Code to process Soap Request

 Hi guys, welcome to my blog. This post contains a sample java code to send soap request with XML post body. Recently in our project, we got a different requirement that we need to send soap request using Java code. For this example demo, I am using sample request available on this website.

In this example below are the details which I am going to use.

URL - www.dneonline.com/calculator.asmx
POST Body-
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <Add xmlns="http://tempuri.org/">
      <intA>1</intA>
      <intB>2</intB>
    </Add>
  </soap:Body>
</soap:Envelope>

Headers-
Content-Type:text/xml; charset=utf-8
Content-Length:length
SOAPAction:"http://tempuri.org/Add"

Below is the Java code for sending the above request,


package com.srikanth.SoapRequestExample;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class SoapRequest {
  
  public static void main(String a[]){
   try {
    int a1=3;
    int b=4;
    String requestXML="<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><Add xmlns=\"http://tempuri.org/\"><intA>"+a1+"</intA><intB>"+b+"</intB></Add> </soap:Body></soap:Envelope>";
    
    //We are creating and instance for URL class which is pointing to our specified url path
    URL url = new URL("http://www.dneonline.com/calculator.asmx");
    
    //Now we are opening the url connection  using the URLConnection class
    URLConnection connection = url.openConnection();
    
    //Converting the connection HTTP
    //If you want to convert to HTTPS use HttpsURLConnection
    HttpURLConnection httpConn = (HttpURLConnection) connection;
    
    //Below are the statements that gives the header details for the url connection
    httpConn.setRequestProperty("Content-Length", "length");
    httpConn.setRequestProperty("Content-Type","text/xml");
    httpConn.setRequestProperty("SOAPAction","http://tempuri.org/Add");
    httpConn.setRequestMethod("POST");
    
    httpConn.setDoOutput(true);
    httpConn.setDoInput(true);

    //Sending the post body to the http connection
    OutputStreamWriter out=new OutputStreamWriter(httpConn.getOutputStream());
    out.write(requestXML);
    out.close();
    
    InputStream newIP=httpConn.getInputStream(); 
    String temp;
    String tempResponse = "";
    String responseXML;
    
    // Read the response and write it to standard out.
    InputStreamReader isr = new InputStreamReader(newIP);
    BufferedReader br = new BufferedReader(isr);
    
    
    // Create a string using response from web services
    while ((temp = br.readLine()) != null) {
     tempResponse = tempResponse + temp;
     
    }
    responseXML = tempResponse;
    System.out.println(responseXML);
    //this.outputResponse=responseXML;
    br.close();
    isr.close();
    
    }
   catch (java.net.MalformedURLException e) {
    System.out.println("Error in postRequest(): Secure Service Required");
    e.printStackTrace();
   
   } catch (Exception e) {
    System.out.println("Error in postRequest(): " + e.getMessage());
    }
   
  }
  
}

      Most of the explanation for the above code is given in form of comments.

Note: We need to use a try-catch block to capture the exception as the URLConnection/ HttpURLConnection will throw an exception.


After successful execution, we can see the following result in the console.

Outut Response Body:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <AddResponse
            xmlns="http://tempuri.org/">
            <AddResult>7</AddResult>
        </AddResponse>
    </soap:Body>
</soap:Envelope>