Add Copyright on Eclipse Automatically

In this way you can add the copyright to your newly created java files automatically in eclipse.

Go to preferences -> Java -> Code Style -> Code Templates, expands Code and select “New Java Files”, edit the template to add whatever copyright messages you want.

eclipse_copyright_isdjava

After you complete above step, you will see your newly created java classes as below,

package lk.pwc.ird.automation.exceltoxml;

/*
* Created By idewasuren001 - Nov 21, 2018
*/

public class Test{

}

Famous GIT Commands

This tutorial will give you an idea to use GIT as your source code repository. You can use tortoise GIT or command line to use git. This tutorial is targeted to command line users.

INSTALL GIT

This is very easy, just download and install git. You can just skip all the steps by clicking next button allowing default configurations.

after successful installation you can right click and select ‘Git bash here’ to enter git command line quickly.

GIT CLONE

This command used to download the source to your directory. Go to your desired folder and type below command in git bash.

git clone <git repository url>

GIT STATUS

This command will show you all the updates and files need to commit in your repository

git status

GIT ADD

This command used to add a newly created file to the repository

git add <file path with name>

GIT COMMIT

This command will commit your code to your local git repository. You also need to provide a comment for the commit.

git commit -m “message for the commit”

GIT PULL

This command will update your local repository from your hosted repository. go to your project directory and run below command in git bash.

git pull

GIT PUSH

This command will update hosted repository from your local repository. Go to your project directory and run below command in git bash.

git push

GIT BRANCH

This command will show all the branches available in your hosted repository.

git branch -a

GIT FETCH

This command will fetch all the branches available in the hosted repository to your local repository.

git fetch

GIT CHECKOUT

You can switch between branches using this command.

git checkout <branch name>

HOW TO MERGE BETWEEN BRANCHES

To merge branches you have to follow below steps. Ill tell this using an example. Assume you have two branches as ‘master’ and ‘dev’. You need to merge source from dev to master. Follow below steps, apply same steps according to your scenarios.

go the root directory and type below command in git bash.

git checkout dev

git pull

git checkout master

git pull

git merge –no-off –no-comment dev

Following this steps will merge source in your local git repository. If you push right away it will merge even in the hosted git repository.

 

 

 

 

 

 

 

Run Selenium on Jmeter

This tutorial is basically for testers who have their written Selenium test scripts. Also testers who wants to use their existing selenium test scripts to load and performance testing using Jmeter.

Jmeter is a powerful tool which you provide several functionalities to run your selenium scripts to get testing statistics.

Step 01

Export Selenium Script

You should have junit test cases to export from eclipse as a jar file.

  1. right click on the project
  2. select export
  3. export as jar file

Step 02

Run Script on Jmeter

  1. click on test plan and browse the jar file you need to add.
  2. right click on test plan and add a thread group.
  3. right click on thread group and add->sampler->Junit request
  4. Then you can select the classname and test methode to run on the jmeter.

Note: If you have existing test scripts written using other test frameworks like testng, so far jmeter doesn’t support testng but we could use beanshell sampler to execute testng test scripts.

but if you use testng as your test frameworks, you could do all the load and performance testing without using Jmeter 🙂

Jmeter Setup For Selenium

This tutorial will give you an idea about the jmeter and each step to install jmeter; instruction to setup for selenium automation testing to load and performance testing.

What is Jmeter ?

The Apache JMeter™ application is open source software, a 100% pure Java application designed to load test functional behavior and measure performance. It was originally designed for testing Web Applications but has since expanded to other test functions.

JMeter is not a browser, it works at protocol level. As far as web-services and remote services are concerned, JMeter looks like a browser (or rather, multiple browsers); however JMeter does not perform all the actions supported by browsers. In particular, JMeter does not execute the Javascript found in HTML pages. Nor does it render the HTML pages as a browser does (it’s possible to view the response as HTML etc., but the timings are not included in any samples, and only one sample in one thread is ever displayed at a time).

You can learn more about jmeter using this link.

Install Jmeter And Selenium Setup

Install Jmeter

Prerequisites

  1. Your computer needs to be installed Java 7 or above before proceeding to the Jmeter installation.  You can download the Java from this link.

Installation Steps

  1. Download Jmeter file from this link. (download binaries zip to install on windows and other tgz to install on linux. Also you download the source codes as well since it’s open source 🙂 )
  2. Once you download the zip or tgz file, extract it in you computer on preferred location.
  3. Go to the extracted root folder, then redirect to bin, open ApacheJmeter.jar (You may ask running platform when it’s open at the first time, select java)

That’s all Enjoy Jmeter.

Untitled

Install Plugin Manager in Jmeter

My opinion is installing plugin manager in jmeter is a must. We could easily install plugins though the plugin manger without confusing jar files copy pasting. Please go through below easy step to install plugin manager in Jmeter.

  1. Download the plugin manager Jar file using this link.
  2. After installing the jar files, paste in the following path, <jmeter root folder>/lib/ext
  3. restart the jmeter.
  4. verify plugin manager installation once jmeter restarted; go to the Options menu->plugin managerplugin_menuplugin_manager

Install Selenium Webdriver Plugin

  1. Open plugin manager.
  2. navigate to the available plugin tab.
  3. search selenium webdriver and tick on selenium webdriver support plugin.
  4. press install and restart jmeter button. sel_web_support_plugin

Configure Chrome/Firefox Driver

To run the selenium testscripts we need to configure chrome or firefox drivers in Jmeter. For that you need to create a testplan; file->new; then you could create a new test plan.

Now you have to create a thread group for that; right click on newly created test plan->add->Threads(Users)->Thread group

create_thred_group

Now you can create a configuration for Chrome or firefox. In here chrome configuration is illustrated, You could do the firefox and other browser configuration by following the same steps.

To create the configuration file; right click on previously created thread group->add->config element->Chrome driver config

chrome_driver_config_path

Once you created the chrome driver config, you will see a window as below. Navigate to the chrome tab and insert the chrome driver exe file path.

If you don’t have the chrome diver exe file; you can download from this link.

chrome_driver_config

Now you have successfully created the configuration for chrome.

Create a Listener

You could create a listener to listen the results of our test cases. It gives the end results of our test cases. Mostly used to get statistics for performance test results.

There are several listeners available in Jmeter, we use tree view listener for our exercise.

right click on previously created thread group->add->listener->View Results Tree

tree_listener_path

Create a Webdriver Sampler

We could create a webdriver sampler and directly write selenium test scripts through Jmeter, else if you have pre-written selenium scripts you could export it to a jar file and run it through the Jmeter. For more go to this link.

For this exercise, we will create a webdriver sampler and execute a simple selenium script.

to create a webdriver sampler right click on previously created thread group->add->sampler->WebDriver Sampler

web_driver_sampler_path

driver_sampler

Running the Script

Before running the script make sure, you have followed all the steps above. Your final setup should be like below,

driver_sampler

In the webdriver sampler, you could write your selenium script and run it by pressing green color arrow button (start button).

You can change the thread properties in Thread Group and run the scripts in different ways.

thread_properties

Also by opening the View Results Tree, you could see the end results of your test plan.

That’s all Friends. Happy Testing.

 

 

Install TestNG On Eclipse Oxygen

What is TestNG ?

TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionalities that make it more powerful and easier to use

such as:

  • Annotations.
  • Run your tests in arbitrarily big thread pools with various policies available (all methods in their own thread, one thread per test class, etc…).
  • Test that your code is multithread safe.
  • Flexible test configuration.
  • Support for data-driven testing (with @DataProvider).
  • Support for parameters.
  • Powerful execution model (no more TestSuite).
  • Supported by a variety of tools and plug-ins (Eclipse, IDEA, Maven, etc…).
  • Embeds BeanShell for further flexibility.
  • Default JDK functions for runtime and logging (no dependencies).
  • Dependent methods for application server testing.

For more information visit this link.

Why TestNG ?

TestNG has more functionalities when compared to other testing frameworks. Also it supports well on Selenium. It supports latest platforms and frameworks as well (Java 7 and above). Last but not least due to the rich annotations we could code our scripts very easily.

How to install TestNG on Eclipse

Step 1

  1. Open Eclipse – if you don’t have eclipse you download it from this link.
  2. go to the help menu and click on install new software

Step 2

  1. a pop up window will open, click on add button as below, 
  2. another pop up window will open as below, enter  “TestNG” and type “http://beust.com/eclipse/” as location. Click OKNote : You can find the location url in this link.
  3. You will come back to the previous window, but with testng option.
  4. check the testng and click on next button.

Step 03

  1. Read the agreement and accept.
  2. if you see any security warning press ok. This warning is populated from eclipse due to it’s policy on third party installations. 
  3. After installation, eclipse need to be restarted. Press yes on below window.

Step 04

  1. After restarting the eclipse, verify testng is installed correctly as below.

That’s all friends, Enjoy automation testing.

 

Spring File Down-loader

File download is very common to any type of web application. Let’s see how this done in spring framework and java. This is pretty easy and you can add/remove code segments as you want. The given code segments are written as I thought and comments will give you an idea about each code.

Let’s go then, Please follow below simple steps to create your own file down-loader;

Step 1:

Create the spring controller.

  @RequestMapping(value = "/downloadFile", method = RequestMethod.GET)
  public void getUserProfilePage(@RequestParam("fileName") String fileName,
  HttpServletRequest request, HttpServletResponse response) {
    FileDownloadManager downloadManager = new FileDownloadManager();
    try {
      downloadManager.downloadFile(fileName, response);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

Step 2:

Create a java Class “DownloadManager” and create below method.

public void downloadFile(String fileName, HttpServletResponse response) throws IOException {
    // reads input file from an absolute path
    try {
      //get fixed file path from the property file of the project
      String filePath = CommonUtil.getValueFromFile("application", "file.upload.location") + fileName;
      File downloadFile = new File(filePath);
      FileInputStream inStream = new FileInputStream(downloadFile);

      // gets MIME type of the file
      String mimeType = new MimetypesFileTypeMap().getContentType(downloadFile);
      if (mimeType == null) {
        // set to binary type if MIME mapping not found
        mimeType = "application/octet-stream";
      } else if (StringUtils.equals(FilenameUtils.getExtension(filePath), "pdf")
          || StringUtils.equals(FilenameUtils.getExtension(filePath), "PDF")) {
        mimeType = "application/pdf";
      }
      System.out.println("MIME type: " + mimeType);

      // modifies response
      response.setContentType(mimeType);
      response.setHeader("Content-Disposition", "inline; filename=" + downloadFile.getName());
      response.setContentLength((int) downloadFile.length());

      // if you want to forces download you can use below
      // String headerKey = "Content-Disposition";
      // String headerValue = String.format("attachment; filename=\"%s\"", downloadFile.getName());
      // response.setHeader(headerKey, headerValue);

      // obtains response's output stream
      OutputStream outStream = response.getOutputStream();

      byte[] buffer = new byte[4096];
      int bytesRead = -1;

      while ((bytesRead = inStream.read(buffer)) != -1) {
        outStream.write(buffer, 0, bytesRead);
      }

      inStream.close();
      outStream.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

For mime type; you can use different java libraries like MimeMagic, Tika or use URLConnection. But I have used simple MimetypesFileTypeMap which is available form java 1.7 as I know.

Now you can enjoy the file down-loader by accessing the spring controller via browser. Please contact me for further information.

Generating QR Code

Step 01:

Add below to your maven repository.

<!-- https://mvnrepository.com/artifact/net.glxn/qrgen -->
<dependency>
    <groupId>net.glxn</groupId>
    <artifactId>qrgen</artifactId>
    <version>1.4</version>
</dependency>

Step 02:

Add below java util class in to your project and use it as you want. Please note this util class is coded as I wanted, you can amend the code as you want; anyway I added comments to describe the code for you.

package lk.pwc.cult.fund.common.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

//importing libraries
import net.glxn.qrgen.QRCode;
import net.glxn.qrgen.image.ImageType;

public class QRCodeUtil {

  //pass a string generated code to embed in the qr code
  public String retrieveQRCodeImageName(String code) {

    try {
      //generating the qr code in png format and get to Byte Array Output Stream
      ByteArrayOutputStream out = QRCode.from(code).to(ImageType.PNG).stream();

      //get file path from the property file to save the created qr code png
      String filePath = CommonUtil.getValueFromFile("application", "qrcode.file.location");
      //generate a random string to save the qr coded png file
      String fileName = UUID.randomUUID().toString() + ".png";
      String imagePath = filePath + fileName;

      //this set of code uses to create the specific file path if it's not exist
      File theDir = new File(filePath);
      if (!theDir.exists()) {
        try {
          theDir.mkdirs();
          System.out.println("DIR created");
        } catch (SecurityException se) {
          se.printStackTrace();
        } catch (Exception e) {
          e.printStackTrace();
        }
      }

      FileOutputStream fout = new FileOutputStream(new File(imagePath));

      //Use file output stream to write the qr code png image to a file
      fout.write(out.toByteArray());
      fout.flush();
      fout.close();

      return imagePath;
    } catch (FileNotFoundException e) {
      e.printStackTrace();
      return null;
    } catch (IOException e) {
      e.printStackTrace();
      return null;
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }

  }

}

MongoDB

MongoDB is arise in the mid-2000s under NoSQL community. Dwight Merriman and Eliot Horowitz, who had encountered development and scalability issues with traditional relational database approaches while building Web applications at DoubleClick, an Internet advertising company that is now owned by Google Inc. According to Merriman, the name of the database was derived from the word humongous to represent the idea of supporting large amounts of data. Merriman and Horowitz helped form 10Gen Inc. in 2007 to commercialize MongoDB and related software. The company was renamed MongoDB Inc. in 2013. The database was released to open source in 2009 and is available under the terms of the Free Software Foundation’s GNU AGPL Version 3.0 commercial license.

MongoDB allow to use collections and documents instead of using tables and rows as in relational databases. Documents comprise key-value pairs. Collections contain sets of documents and functions just like database schema in relational databases.

Below are few characteristics of the MongoDB:

  • Written in: C++
  • Main point: Retains some friendly properties of SQL. (Query, index)
  • License: AGPL (Drivers: Apache)
  • Protocol: Custom, binary (BSON)
  • Master/slave replication (auto failover with replica sets)
  • Sharding built-in
  • Queries are javascript expressions
  • Run arbitrary javascript functions server-side
  • Better update-in-place
  • Uses memory mapped files for data storage
  • Performance over features
  • Text search integrated
  • GridFS to store big data + metadata
  • Has geospatial indexing
  • Data center aware

Install MongoDB

MongoDB 3 is copied to 10.161.121.8 server (D:\SHARED\LRIS_RESOURCES). Get a copy of the file(mongodb-win32-x86_64-2008plus-ssl-3.0.7-signed) and install it in your pc.

Note: install in the D:\mongodb folder for continue this guide.

Configure MongoDB

create two folders “data” and “log” in the D:\mongodb folder. Again create mongo.config file and copy paste below text.

##store data here
dbpath=D:\mongodb\data
##all output go here
logpath=D:\mongodb\log\mongo.log
##log read and write operations
diaglog=3

 

Run Mongo DB

Now you will have the below folder structure in path D:\mongodb

Capture3

run command prompt with administrator privileges, use cd command to navigate D:\mongodb folder. Then type below command to run the mongo service.

mongod --config D:\mongodb\mongo.config

 

Install Mongo DB as a windows service

You can add mongo DB service as a windows service using below command. Before executing below command make sure to use cd command to navigate D:\mongodb\bin folder.

"D:\mongodb\bin\mongod.exe" --config "D:\mongodb\mongo.config" --install

 

Access Mongo DB console

open command prompt and direct to D:\mongodb\bin folder using “cd” command. Execute below command to start the console.

mongo.exe

output:

Capture

Creating a snapshot

open command prompt and direct to D:\mongodb\bin folder using “cd” command.

mongodump --db &lt;db_name&gt;

output:

Capture1.png

There are tons of option you can use with mongodump in here.

Restore a backup

open command prompt and direct to D:\mongodb\bin folder using “cd” command.

mongorestore --db &lt;db_name&gt; &lt;dump_location&gt;

output:
drop database
Capture
restore the dump file(bson)
Capture21

There are tons of option you can use with mongorestore in here.

Latest MongoDB manual 3.0 is here.

 

MongoDB client

For people who doesn’t like command prompt can use different client software to access the server. MongoChef is one of the well known software which is support for latest MongoDB versions.

You can get a copy of the software from the 10.161.121.8 server (D:\SHARED\LRIS_RESOURCES).

 

Note : For people who are interested feel free to access MongoDB open test instance in 10.161.121.11 server. The service is running on port 27017. 

 

Application Hosting Using Docker

Docker_container_engine_logo

What is Docker ?

https://www.docker.com

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package. By doing so, thanks to the container, the developer can rest assured that the application will run on any other Linux machine regardless of any customized settings that machine might have that could differ from the machine used for writing and testing the code.

In a way, Docker is a bit like a virtual machine. But unlike a virtual machine, rather than creating a whole virtual operating system, Docker allows applications to use the same Linux kernel as the system that they’re running on and only requires applications be shipped with things not already running on the host computer. This gives a significant performance boost and reduces the size of the application.

And importantly, Docker is open source. This means that anyone can contribute to Docker and extend it to meet their own needs if they need additional features that aren’t available out of the box.

Docker Architecture

Docker consist with containers and images, We can run images on different containers. for more architectural information please refer this link.

docker_architecture

The Docker daemon
The Docker daemon runs on a host machine. The user uses the Docker client to interact with the daemon.

The Docker client
The Docker client, in the form of the docker binary, is the primary user interface to Docker. It accepts commands and configuration flags from the user and communicates with a Docker daemon. One client can even communicate with multiple unrelated daemons.

Inside Docker
To understand Docker’s internals, you need to know about images, registries, and containers.

Docker images
A Docker image is a read-only template with instructions for creating a Docker container. For example, an image might contain an Ubuntu operating system with Apache web server and your web application installed. You can build or update images from scratch or download and use images created by others. An image may be based on, or may extend, one or more other images. A docker image is described in text file called a Dockerfile, which has a simple, well-defined syntax. For more details about images, see How does a Docker image work?.

Docker images are the build component of Docker.

Docker containers
A Docker container is a runnable instance of a Docker image. You can run, start, stop, move, or delete a container using Docker API or CLI commands. When you run a container, you can provide configuration metadata such as networking information or environment variables. Each container is an isolated and secure application platform, but can be given access to resources running in a different host or container, as well as persistent storage or databases. For more details about containers, see How does a container work?.

Docker containers are the run component of Docker.

Docker registries
A docker registry is a library of images. A registry can be public or private, and can be on the same server as the Docker daemon or Docker client, or on a totally separate server. For more details about registries, see How does a Docker registry work?

Docker registries are the distribution component of Docker.

Docker services
A Docker service allows a swarm of Docker nodes to work together, running a defined number of instances of a replica task, which is itself a Docker image. You can specify the number of concurrent replica tasks to run, and the swarm manager ensures that the load is spread evenly across the worker nodes. To the consumer, the Docker service appears to be a single application. Docker Engine supports swarm mode in Docker 1.12 and higher.

Docker services are the scalability component of Docker.

Install Docker

Install on Windows – https://docs.docker.com/engine/installation/windows/

Install on Ubuntu – https://docs.docker.com/engine/installation/linux/ubuntulinux/

Note : Enable virtualization on your laptop/computer before continue docker. You can enable this option by accessing the boot menu of your computer.

How to use Docker in our projects?

Eg : Host ui-web / service-web / mysql (host two war files which is depends on one another with mysql server using docker engine. ui-web is the angular app, service-web is the spring boot application, mysql is the database server. angular app calls services on spring boot application and spring boot application access mysql database server)

Step 01 – Add Dockerfile to the angular application/ui-web

Add below code list in a file. File name should be exactly Dockerfile (no extension).

FROM jboss/wildfly
ADD ui-web.war /opt/jboss/wildfly/standalone/deployments/

Step 02 – Add Dockerfile to the Spring boot application/service-web

Add below code list in a file. File name should be exactly Dockerfile (no extension).

FROM jboss/wildfly
ADD service-web.war /opt/jboss/wildfly/standalone/deployments/

Step 03 – Create a yml file

Add below code list in a file. File name could be differ. extension shoule be .yml

db:
 image: orchardup/mysql
 environment:
 MYSQL_USER: &lt;db username&gt;
 MYSQL_PASSWORD: &lt;db password&gt;
 MYSQL_DATABASE: &lt;db name&gt;
 ports:
 - "3306"

services-web:
 build: &lt;path to the docker file eg: service/. (service is a directory, dot represent docker file)&gt;
 links:
 - db:mysql #(since service web access mysql server this is the link to access mysql image)
 ports:
 - "8080"

ui-web:
 build: ui-web/.
 links:
 - services-web:services-web #(since ui-web access service-web controller, this is the link)
 ports:
 - "8080"

Step 04 – Execute yaml file

&lt;yaml file name&gt; up

Now we have created all the images we want. You can list down created images using below command.

docker images

Result :
docker_images

Step 05 – Start mysql Server

docker run -e MYSQL_ROOT_PASSWORD=admin -p 3306:3306 --name mysql -d orchardup/mysql:latest

-e –> Set an environment variable (can be used multiple times)
-name –> Assign a name to the container
-d –> detache mode enable to run process background
orchardum/mysql –> image name
:latest –> tag name

Note: you can use any type of mysql client to access the database server to create databases or run scripts.
For more information, please refer this link.

Step 06 – Start service-web/spring boot application

docker run -p 8080:8080 --link mysql:mysql &lt;service web image name&gt;

Note : link should be set to mysql container as above.

Step 07 – Start ui-web/Angular application

docker run -p 8080:8080 --link service-web:service-web &lt;ui web image name&gt;

Note : link should be set to mysql container as above.

Note: After step 07, you may be able to access the system as you expected.

Note:

You can list your exising containers using below command.

docker ps -a

Result :
docker_all_containers

You can list your running container using below command.

docker ps

Result:
docker_active_containers

Using Volume with Docker

Docker container is volatile, data will be deleted if container is deleted. So we need volume to store container data.

A data volume is a specially-designated directory within one or more containers that bypasses the Union File System. Data volumes provide several useful features for persistent or shared data:

Volumes are initialized when a container is created. If the container’s base image contains data at the specified mount point, that existing data is copied into the new volume upon volume initialization. (Note that this does not apply when mounting a host directory.)

Data volumes can be shared and reused among containers.
Changes to a data volume are made directly.
Changes to a data volume will not be included when you update an image.
Data volumes persist even if the container itself is deleted.
Data volumes are designed to persist data, independent of the container’s life cycle. Docker therefore never automatically deletes volumes when you remove a container, nor will it “garbage collect” volumes that are no longer referenced by a container.
For more information, please refer this link.

Create Volume for Container Mysql

Use below command to create a volume for mysql container,

docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=admin -v <volume name>:<location for the backup> --name mysql orchardup/mysql:latest

eg:

docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=admin -v dbbackup:/var/lib/mysql --name mysql orchardup/mysql:latest

Note: when you create a volume, even though you deleted the container; you can create another container using an existing volume using above command. Then the container will be injected from the existing volume(stored data in the volume, in this scenario mysql database backup).

You can list your existing volumes under /var/lib/docker/volumes

Other Useful Commands for Docker

To delete containers,

docker rm -f <comntainer name>

To delete Images,

docker rmi -f <image name>

Note: You can use -f to force delete the container or image.

WildFly/Jboss Server Configuration

WildFly is the open source series of the jboss server. Latest WildFly version is 9.0.

If you are planning to use WildFly, below configuration will be very useful.

when you add the server double click the server and do the below configurations.

Publishing: choose “Automatically publish after a build event”. I like to change the publishing interval to 1 second too.
Application Reload Behavior: check the “Customize application reload …” checkbox and edit the regex pattern to \.jar$|\.class$
Then,

When enabled option Automatically publish when resource change then changes inside *.html, *.xhtml files are immediately reflected as soon as you refresh the browser.
To make hot deployment work for *.jsp files too, then you should inside ${wildfly-home}/standalone/configuration/standalone.xml make following change:

replace with: