Monday, May 1, 2017

Testing an Express JS Application

We can use mocha, should, and supertest to test an express.js application. Mocha is used in creating test cases when unit testing, and executing them in a serial manner. Should is used to ensure that the response body is an Object, and have a property named _id. Supertest is used to test and ensure that the http calls are working properly.

As the first step in doing this, install mocha, should, and supertest by giving the commands:

    npm install mocha --save-dev
    npm install should --save-dev
    npm install supertest --save-dev

Then, make a new file with the name format <noun-name>.route.test.js. For example, it can be something like employee.route.test.js, and afterwards, go to the package.json file, and under "scripts", add:

   "test":"mocha \"./*.test.js\""

Then, import these three modules to the newly created file by using the require command.

Since we need the application for unit testing, we need to import that file as well. In order to do this, first go to the JavaScript file which has the application, and at the end of the file add the following line of code, so we could import the application later on, and use it.

   module.exports=app;

What we do here is, export the application made out of express, so that we could use it in unit testing. Note that app used in the above command should be the variable name given to the application made out of express using the command: const app=express();

After this step, import this file in the test file by giving the command(consider application file name to be server.js):  
   const app=require('./server');

Next step is to create the application using supertest by giving the command:
 const agent=request.agent(app);

Note that the name given in the place of the word request in the above command should be the variable name given when importing supertest using the line: const request=require('supertest');

What comes next is creating the basic structure for unit testing. The following command is given next in order to do that. Actually, what happens here is, a test suite is created. Ideally, a file should have only one test suite, but there can be cases where there is more than one test suite in a file.

  describe('Employee route test', ()=>{
    //place code here
});

Now we have to create a sample employee object to be used in testing the application. The following code will carry out that task, and it should be placed inside describe() where I have indicated by placing the comment: place code here.

 const employee={
   name:'Mary',
   email:'mary@gmail.com',
   tel:'1111111'
};

it() is used in creating test cases. Several test cases can be placed inside the test suite as we normally do. Now let's place our first test case inside the test suite. In this test case, we will be testing the POST rest method which we are using to insert an employee. The following test case code should appear below the above piece of code inside describe() in the file.

  it('should add new employee', (done)=>{
     agent.post('/employees')
         .send(employee) //parameters to be sent in the request body
        //which in this case is the sample employee object created           //above to test the inserting part of the application
         .expect(201)  //usual ok=>200 so if we have not added               //success response as 201, it is 200, depending on what you         //have used in the GET implementation in the application             //file.  
        //next comes the assertion part.
        .end(function(err, res){
           if(err){
               return done(err);
           }

           res.body.should.be.an.Object().and.have.property('_id');
           //this line expects and ensures that the body is an                  //Object and have a property named _id.

           done(); //used to signal mocha that the test case has                //been executed and that it can shift to the next test              //case in line to continue testing.
        });

      after(done=>{
           EmployeeModel.remove().then(()=>done()).catch(done);
      });
 })

The test case can now be tested to see whether it passes the test. In the same way, test cases to test other http methods can be written and tested in this test suite.


Wednesday, April 19, 2017

Spring Boot

Spring Boot is a framework which uses convention over configuration, and it supports Microservices. Spring Boot was developed to minimize the configurations. Property file contains most of the configurations, and have key-value pairs. Utility classes are used to access them.

How to set up a Spring-Boot Project


  • Create a project using Maven.
  • Setting up the parent of the project is the next step. The parent will be spring-boot-starter-parent. Inside the project folder, there will be a file created named pom.xml. Open it, and above the dependencies tag, add the following piece of code.
      <parent>

       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>1.5.2.RELEASE</version>
    </parent>

  • Setting up the starter is the next step. Add the following code under the dependencies tag in pom.xml in order to do this.
    <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

  • In order to have support for packaging we need to include a maven plugin, and in order to do this, include the following piece of code under the dependencies tag in the pom.xml file.
     <build>
         <plugins>
             <plugin>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
             </plugin>
         </plugins>
     </build>

  • The main class of your project must be modified to be a Spring Boot Application. Add the following line of code inside your public static void main method. Please make sure that you change the ClassName word in italics in the code to the name of your main class.
        SpringApplication.run(ClassName.class, args);
  • Create a folder named model inside your package. Add a class named Employee.java which will contain two attributes id and message, the getters and setters of the two attributes.
private long id;
        private String message;

          //getter of message
        public String getMessage() {
                return message;
        }
//getter of id
public long getID(){
return id;
}

          //setter of message
        public void setMessage(String message) {
                this.message = message;
        }
//setter of id
public void setID(long ID){
this.id=ID;
}

  • Create a folder named controller inside your package. Add a class named EmployeeController.java which will contain a method that will handle GET requests.
    private final AtomicLong count=new AtomicLong();

    @RequestMapping(name = "/employee")
    public Employee welcoming(@RequestParam(value = "employeeName",                defaultValue = "Worker") String name) {

            Employee employee = new Employee();
            employee.setMessage("Welcome "+name);
    employee.setID(count.incrementAndGet());   
            return employee;   
    }

AtomicLong is used because then we can make use of the method incrementAndGet() to get the id attribute incremented whenever a client calls this GET method. If we use something like int, we cannot get this done because the value will not change and increment, but it will always remain the same.

value of @RequestParam is the key of the key-value pair sent along with the GET request, and defaultValue will be the default value placed in the case that the client does not pass a value.

incrementAndGet() method which belongs to the AtomicLong class increments the value and returns it.

We can test this using POSTMAN after running the project by giving the two following commands:
  • mvn clean compile package
  • java -jar target/<jar file created>.jar
Replace <jar file created> the Executable Jar File created. It will be most probably be created inside a folder called target in your project. If target folder is not there, you do not have to use target/ part in the command.

A better way to do this will be having a separate class for handling the service. In that case, the only task of the controller class will be to redirect requests to the correct service, and give the output.


Thursday, April 13, 2017

Angular JS

Angular JS is a front end web application framework which is widely used in Single Page Applications. The content of a single page application is dynamically added to the web page, that is, the web page is not refreshed, but only the content of the web page is changed using JavaScript when we need to change. When we need to change, we just push the new changed div to the web page without reloading the page with the changed divs. Since the client has the logic, the browser does the pushing and removing of the divs whenever necessary. Angular JS has the model view controller architecture.

The Angular JS Directives are used to introduce angular features to the already existing html tags. ng-app, and ng-controller are two such Angular JS directives.

Steps to create a Simple Angular JS app
  • As the first step, download and install Angular JS if you don't have it already. The link of the website from which it can be downloaded is given below.
                           https://angularjs.org

When downloading, you can choose the zip version to download.

  • Create an html file named index.html. Later, we will add our Angular JS app to this.
  • As the next step, let's create the module. The module acts as the container for all angular elements like controllers and directives. Create a js file named app.module, and add the following piece of code.
                   angular.module('EmployeeApp', []);

  • Let's create the controller now. Create a js file named main.controller, and add the following piece of code. 
         angular.module('EmployeeApp').controller('MainController', ['$scope',
                            ($scope)=>{
                                   $scope.count=0;

                                   $scope.incrementCount=()=>{
                                        return $scope.count++;
                                    }
         ]);


$scope is used to inject dependencies which controls the view to the controller.

  • Next step would be to add the following code inside your head tag in the html file. Make sure that you have the <base href="/app/"> tag in your head tag, placed above the script files mentioned below.  And also make sure that you are giving the correct paths to the below mentioned script files.
        <script src="angular-1.6.3\angular.js"></script>

        <script src="app.module.js"></script>
        <script src="main.controller.js"></script>

  • Change the body tag using the ng-app directive as shown below. 
        <body ng-app="EmployeeApp">

  • Add the following code to place the div with the controller inside the body tag of the html file.
           <div>
                  <h2>Welcome</h2>
                 <div ng-controller="MainController">
                        <button ng-click="incrementCount()">Increase Count</button>
                        <span>Count: {{count}}</span>
                 </div>
           </div>

As we press the button, the incrementCount function will be called, and the count will be incremented.
  • We can test this by building up an ExpressJS server side application, and running on localhost. The page will be loaded, and you will be able to see that when we press the button, the count will be incremented.

Friday, April 7, 2017

Maven

Maven is a tool used in managing projects and automating the development process. It can be used to set up a project easily. Maven uses "Convention over Configuration" which means the developer does not have to do a lot of decision making and he does not have to configure manually since the build tool will manage it. Maven manages alot of things like the builds and dependencies.

Using Maven is alot easier than using some of the other build tools like Ant.

Why choose Maven over Ant?
In Ant, we have to manually copy all the dependencies to the project location when setting up the project, whereas in Maven, all the dependencies are downloaded recursively and automatically during the project set up from a central repository where all the files are stored. This in turn reduces the project set up time too.

Another advantage would be what was mentioned at the top: Maven uses "Convention over Configuration" whereas in Ant, we have to configure alot of stuff manually.

How to setup Maven
Maven can be configured at any place, and use the following link provided to download the zip file of Maven:

http://redrockdigimark.com/apachemirror/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.zip

After downloading, you have to configure two environment variables.

To do this, search environment variables in your computer, and choose Edit the system environment variables-->Click Environment Variables... from the window that appears, then under the system variables:

  • Add New variable named MAVEN_HOME or M2_HOME and for the value give the path to the home directory of Maven in your extracted file.
  • Select and Edit the Path variable, and add New path to the bin folder in your extracted Maven home directory. 
As the next step, we will check whether Maven is working now by giving the command:
       mvn -v
This should work now, given that the Java environment configurations have already been done. If you get a problem configure JAVA_HOME by giving path to Java installed home directory and add the path to the bin folder of Java folder to the Path variable.

If it works properly you should get an output which includes Maven home, Java version, and Java home with some other details.

How to create a simple project using Maven
Give the following command to create a project using Maven:

       mvn archetype:generate -DgroupId=com.student.learn 
 -DartifactId=learnMaven -DarchetypeArtifactId=maven-archetype-          quickstart

Note that archetype is a templating toolkit used to create projects of the same kind.
  • groupId will be the name of the package of your project.
  • artifactId will be the name of the project.
  • archetypeArtifactId indicates which archetype to be used when setting up the project through its name.
Give appropriate values to these three attributes as you want.

Saturday, April 1, 2017

Restful Services and Express.js with MongoDB

Express.js is a Node.js web application framework.

GET and POST are two of the main Restful Services. In addition to GET and POST, some of the other main services are PUT and DELETE. In this post, I'll be focussing only on GET and POST.

GET is used to fetch already existing resources whereas POST is used to insert a new resource. If I put it according to the example I'm going to use in coding, GET will fetch already existing employee details whereas POST will be used to insert a new employee record/document.

To test the following code, we can use Postman which is a Chrome Desktop Application-that helps in building APIs-to simulate the GET and POST services.



  • Create a JavaScript file named employee.model.


The strict notation will be applied when we start the code by giving the following line of code. The strict notation ensures that the programmer does not use any incorrect syntax or any syntax that may change along with time as JavaScript continues to evolve.

         'use strict';

Using the require command, we will import the mongoose module required to connect with the
MongoDB.

         const mongoose=require('mongoose');

Since we are planning to handle the employee GET and POST requests, we have to maintain 
employee information in the database. The following set of code lines are used to create the 
structure or schema of the information we are going to store in the database.

         const EmployeeSchema=new mongoose.Schema({
                      name:String,
                      email:String,
                      tel:String
         });


Then, we are creating a model using our schema.

const Employee=mongoose.model('Employee', EmployeeSchema);

Using module.exports, we are giving the things that are to be exported whenever we require 
or import this file from another file.

module.exports=Employee;

  • Create another JavaScript file named employees.
       'use strict';
Using the require command, jmport all the necessary modules.

       const express=require('express');
       const bodyParser=require('body-parser');

       const mongoose=require('mongoose');
The next particular line of code sets the promise type to be used by mongoose. In this case, 
by saying global.Promise, we are indicating it to use the native promises.

       mongoose.Promise=global.Promise;
mongoose.connect() is used to connect to MongoDB, and if an error occurs at a given point 
for this line of code, instead of using localhost, you can try using 127.0.0.1 which will work just
fine. 

       mongoose.connect('mongodb://localhost:27017/employees', err=>{
           if(err)   //handles any errors that occur when connecting to the database
           {
               console.log(err);
               process.exit(1);
           }
       });

Using the line below, we will import the employee.model file we created before with the 
employee schema on it.

       const EmployeeModel=require('./employee.model');

The line below is used to create an application out of express.

       const app=express();

express.static() is used to give the location where the server can find the static files like html, 
css, and js to serve to the client directly.

       app.use(express.static(__dirname));
bodyParser.json() is used to accept JSON objects in the request body.

       app.use(bodyParser.json());

The following command handles the GET restful service of the type or path or route /employees,
using which the client will request details of all the employees. The find() command will be 
used to search for all the documents with the employee details from the MongoDB. exec() 
command must be used along with find() to execute the query.
then() handles what needs to be done with the query results. In this case, res.json() sends the 
result in the response made for the request. catch() is used to handle errors that can occur.

app.get('/employees', (req, res)=>{
    EmployeeModel.find().exec().then(employees=>{
        res.json(employees);
    }).catch(err=>{
        console.error(err);
        res.sendStatus(500);
    });
});

The following command handles the POST restful service of the type or path or route 
/employees. By accessing the request body through req.body, we will save the parameters
passed by the client which contains the information of the new employee to be added to the
database, and then using the save() method, we will save the details to the database. Note 
that the client does not need to provide the _id in the request body since MongoDB will be 
adding it by itself.

As in the GET service handling, then() will deal with the result from carrying out the operation, 
while catch will do the error handling. In this case, the output that the client will get at the end 
the operation is the newly added details of the employee.

app.post('/employees', (req, res)=>{
    const employee=new EmployeeModel(req.body);
    employee.save().then(employee=>{
        res.json(employee);
    }).catch(err=>{
        console.error(err);
        res.sendStatus(500);
    });
});

Using app.listen(), we are making a server which will listen to the requests from port 3000. The
error status 500 which we send represents an internal error which simply depicts that the 
server is not able to fulfil the request due to an unexpected condition that it came across. 

app.listen(3000, err=>{
    if(err){      //does the error handling
        console.error(err);
        return;
    }
    console.log('app listening on port 3000');
});

Monday, March 20, 2017

Introduction to Node.js

Node.js is asynchronous just like JavaScript because of its Non-Blocking I/O nature, that is, the I/O operations are done in separate threads in the C++ thread pool upon which Node.js and JavaScript is built, while the main thread continues to execute the rest of the program. Since the main thread is not blocked for I/O operations, it is said to have a Non-Blocking I/O nature. The I/O operations will finish and inform the main thread, but the main thread will switch to the tasks in the completed queue only when it is free.

Let's start learning Node.js by printing "Hello World" in the console, just like when we started learning other programming and scripting languages.

To print "Hello World" in the console, type the simple command:

console.log("Hello World!");

We can run the file in the command prompt by giving the command: 
node <filename with extension as js> 
For example, if the filename is HelloFirst, then the command should be like: node HelloFirst.js

How to Read from a File and Write to a File

Firstly, we have to import the fs module in order to do the reading, and writing to a file. We can do so by using the require command, and storing the returned value in a variable. For example:

const fs=require('fs');

Keyword "const" makes the variable final, that is, the value stored in such a variable cannot be changed later.

Since we need a file to read from, we can create a text file in the same folder which contains our Node.js file with a sentence in it. For example, we can create a text file named firstfile.txt and add a sentence like "I love coding" and save it. Later on, we will be reading this file using Node,js.

After creating the file, we have to give the location of the file which the reading is supposed to be done from. The system variable __dirname is used to get the directory name of the directory in which the Node.js file is, and using this, we can give the location of the file. We can use this variable since the file we are intending to read from is also located in the same folder as the Node.js file. The following code achieves this part:

const readFromFile = __dirname + '/firstfile.txt';

We also have to give the location where the new file to which we are writing whatever read from the other file is to be stored. We can make use of the variable __dirname for this too if we are planning to save the new file in the same folder as the other two files(Node.js file and the text file). This particular part is achieved by the code below:

const writeToFile = __dirname + '/copiedfile.txt';

As the next step, we create a read stream-which allows you to read data from a source-to the file we are reading from, which is the source file:

const streamForReading = fs.createReadStream(readFromFile);

Since we need a stream to use in order to write whatever read, we will create a write stream-which allows you to write data to a destination-to the file we are writing to, which is the destination file:

const streamForWriting = fs.createWriteStream(writeToFile);

As the final step, we will pipe the read stream to the write stream. Piping simply means that we are passing or feeding the output of one method (reading in this case) as the input to another method (writing in this case), so it is like creating a pipe connection between two methods where the end of one pipe connects to the beginning of another pipe. The code for pipe-lining:

streamForReading.pipe(streamForWriting);

If we go and check the folder with the Node.js file and the text file now, we'll be able to find the new file (named copiedfile.txt in this case) created there with the same content as the original text file we had.







Tuesday, March 14, 2017

Basics of MongoDB

MongoDB is a NoSQL database, which means it is not like a relational database where data is handled using relations. It can be used to handle unstructured data like big data.

In MongoDB, a collection acts more like a table, while a document is more like a record or tuple.

Basic Commands
The following are some of the basic commands used in MongoDB:
  • The use command can be used to create and access a database. For example,
              use companyDB;

The above command will create a database if there is no database existing with the name companyDB. If there is a database already existing with the name companyDB, it can be accessed using this command. If such a database already exists, a new database will not be created from that name.

  • The insert command can be used to insert a document to a collection.
                          db.employee.insert({
                       "name" : "Alice",
                       "dept" : ["Accounting", "Marketing"]
            });

The above command will create a collection named employee, and insert the document with the above data to it, if such a collection is not already existing, but in the case where such a collection already exists(collection named employee in this case), then, the document is inserted into the already existing collection.

Each document is uniquely identified using _id. We can provide _id while inserting the document. If we do not provide it while inserting the document, the MongoDB will assign a value to _id by itself, and that will be the case for the above example; the _id will assigned by the MongoDB since we have not provided it.

If we try to add the same document with the same details again(for example, if we try to add the above one again), the addition will be successful since the MongoDB will provide the new document with a new _id value and add it to the database.

  • The find command can be used to get the details of a document.
                             db.employee.find({"name":"Alice"})

This command returns all the fields in the document with their values. Any field can be used with the find command to retrieve documents with a matching field and value. _id field can also be used with the find command, but for that, we have to know the _id. If there is more than one document with matching values for the search field, then all those documents will be returned.

  • The update command can be used to update data in a document.
     db.employee.update({"name":"Alice"}, {$push:{"dept":"Research"}})

Using the first parameter passed, the MongoDB will search for documents with a matching value for that particular field provided, and using the second parameter, it will update the relevant field with the value to be updated. $push is used to insert a value to an array. In this example, "dept" is an array if we take a look back at the insert command. Therefore, it is used in updating the existing array.


  • The remove command can be used to remove or delete a document.
          db.employee.remove({"name":"Alice"})

This command will remove any number of documents with a matching field and value as the one provided. In this case, any document having the field "name" with its value as "Alice" will be deleted. 





Friday, March 10, 2017

Second Year Project at SLIIT 2016

As our second year project, we developed a Business Management System for the hand loom company in Sri Lanka named Suntex Weaving Industries (PVT) Ltd. It is a stand-alone application implemented using Java.

The need for the development of this software arose when the company decided to computerize their whole system, as most of their tasks were carried out manually, and their computer at that moment handled only basic financial details, and some of the special orders.

We, as a team of eight, divided the scope of the project among us, and handled eight different functionalities, namely:
  • Employee Management
  • Material Management
  • Distribution Management
  • Production Management
  • Order Management
  • Services Management
  • Sales Management
  • Financial Management
Out of these eight functionalities, the one I handled and implemented was Sales Management.

Sales Management
Areas I worked on:
  • Details of products sold by the company in their branches(Includes the current quantity in stock and reorder levels as well).
  • Details of daily sales.
  • Details of all payments(Includes only Full Payments since Half Payments can be done only with Orders). 
  • Generation of Invoice.
  • Calculation of Total Sales for the day or month or year provided by the user.
  • Generation of Records.
Add, Update, Search, and Delete was done for all Products, Sales, and Payments.

In addition to handling Sales Management, I handled the logging in to the system, and the logged in details, and I also worked as the team leader in this project, trying my best to ensure that the time is well-managed by everyone in the team, and putting in my best effort make this a successfully completed project.

Benefits of the system we developed:
There are lots of benefits of using the software developed by us over continuing the manual process in the company, as the system we developed is faster, efficient, and it reduces paperwork and redundancy of data. It is easy to handle data and records using this software as it is less complicated, and is less time-consuming compared to the manual handling of data and records.

Technologies we used:
  • Langauge - Java
  • IDE - NetBeans 8.0
  • Database Management System - MySQL
  • Server - Wamp Server
  • Operating System- Microsoft Windows 10 / 8