Continuous Integration,delivery,deploy

seaside-06

Continuous Integration,continuous delivery and continuous deploy are terms used a lot into software world. But I think, in most cases, are used in the wrong way, by me in the first place.

I have read the guide to dev ops of DZone and I found a very simple and clear definitions of these things:

Continuous Integration is a software development practice in which you build and test software every time a developer pushes code to the application.

Continuous Delivery is a software engineering approach in which continuous integration, automated testing, and automated deployment capabilities allow software to be developed and deployed rapidly, reliably, and repeatedly with minimal human intervention. Still, the deployment to production is defined strategically and triggered manually.

Continuous Deployment is a software development practice in which every code change goes through the entire pipeline and is put into production automatically, resulting in many production deployments every day. It does everything that Continuous Delivery does, but the process is fully automated; there’s no human intervention at all.

So, at first sight, these are the definitions of the same thing, but it isn’t. Continuous integration stops at test stage, continuous delivery make a more step and stops at manually deployment to production. Continuous Deployment makes the last step and deploy automatically software to production.

I think is important to keep in mind these differences in order to make the right choose in your software production chain.

Advertisements

Relational vs NoSql Database

montagne-rocciose-1000x441

What is a NoSql database? and how is different from a relational database? these are some frequent ansewes when somebody start to study the NoSql Database.

First of all: what is a relational database? Relational database is a collection of data items organized as a set of tables, the table describe the relationship between datas stored. The lenguage used to manage this kind of database is SQL (Structured Query Language).

NoSql database approach data management without tables but with other data models. This kinds of databases are very useful for very large and distribuited data sets. This database family seeks to solve the scalabilty and big data performance issues that relational databases weren’t designed to address.

In the last times, enterprise solutions are focused on manage a large amount and inhomogeneous data, so it’s clear why this types of databases are grown.

There are a lot of NoSql databases, the most famous are

  • MongoDb
  • Elastic
  • Neo4j
  • OrientDb

Every one are designed for specific goal.

In this posts serie i what to write on the graph database standard de facto: Neo4j DB.

What is a graph database? A graph management system (Graph database) is a NoSql database management system with Create, Read, Update and Delete (CRUD) methods that expose a graph data model.

In the next posts we’ll go deeper into graph db world, and we’ll use Neo4j for some tests.

how test a kotlin class?

flweekendbreakers_t5

TDD is a modern software development process based on creation of automatic test before write code, so modern programming can’t be called in this way without test the software. In this post i want to write something about testing software written in kotlin .

Ok, after this little presentation let’s start to write some code: in the previous post on kotlin the problem was connect to mongo db and write some data. Now i want to complicate this example and the challenge is write a class (data class) and then save it into the database.

In kotlin write a data class is very simple (can find here), my data class for this example is this:

data class Person (val name:String, val surname:String, val age:Int)

Testing this class is very simple, we can test it in the same java way: with junit!

On IntelliJ create test can be done directly from the class:

Schermata 2016-05-22 alle 21.41.20

Schermata 2016-05-22 alle 21.44.54

We can select which methods to test and the IDE create the skeleton for the test class:

Schermata 2016-05-22 alle 21.48.29

I saved this test class in a test folder to keep everything organized. It’s important to configure the test folder for the project:

Schermata 2016-05-22 alle 21.55.26

Schermata 2016-05-22 alle 21.55.44

Now, all it’s configured, and now i can test my class! Run the test class and the result is something like this:

Schermata 2016-05-22 alle 21.58.28.png

gatsby-is-already-doing-great-at-the-box-office-despite-harsh-reviews      All Green! Great!

 

Kotlin exercise 1: Connect to MongoDB -part 2-

beach-beautiful-beautiful-beach_1600x1200_93247

In the first part of this exercise we have seen how connect to a MongoDb with Java. Now that is clear how the connection can be performed, we can try to do the same with Kotlin.
Ok, no waste other time, start coding:


import com.mongodb.BasicDBObject
import com.mongodb.MongoClient
import com.mongodb.MongoException
import java.net.UnknownHostException
import java.util.*

/**
 * Created by Claudio on 01/05/16.
 * This is a main to test mongoDB connection in kotlin
 */

fun main(args: Array<String>) {

    try
    {
        val mongo = MongoClient("localhost", 27017)

        val db = mongo.getDB("testDB")

        val table = db.getCollection("person")

        //Reflection example
        val person = Person("Jon","Doe",20);

        val data = person.javaClass;


        //Insert document
        val document = BasicDBObject()
        document.put("name", "mkyong")
        document.put("age", 30)
        document.put("createdDate", Date())
        table.insert(document)

        /**** Find and display ****/
        val searchQuery = BasicDBObject();
        searchQuery.put("name", "mkyong");

        val cursor = table.find(searchQuery);

        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }
    }
    catch (e: UnknownHostException) {
        e.printStackTrace ( );
    } catch (e: MongoException) {
        e.printStackTrace();
}

}
  

Sorry but the kotlin code isn’t highlighted

How we see the code is similar with Java example and the output is equal:

Schermata 2016-05-04 alle 21.10.01

This code can be converted from Java code with the intellij IDE function called: “Convert java code to kotlin code”. This can be reached from the search tool or vith copy and paste of java code to kotlin file:

Cattura

For exercise the “Main.kt” code is was written from scratch.

In this little exercice I learned how connect to MongoDb and how code something more difficult to “hello world” in Kotlin.

A little step for mankind, a big step for a developer!

featured

Kotlin exercise 1: Connect to MongoDB -part 1-

3_maldive

After the firts posts on Kotlin, i want to creating something more difficult. And i chose to try a database connection in Kotlin.

In this exercise i chose to connect my little application to a Mongo database. This choice is driven by a book that i started to read in  these days: “MongoDb in action”, because i want to know more about this type of database. So i decided to try to use both new things: kotlin and MongoDB.

This exercise is organized in this way: first part is a java test connection and operations with MongoDB then the same operations are written in Kotlin.

Ok, let’s start with code:

Firt of all create the instance of mongodb client and create te DB:

import com.mongodb.*;

import java.net.UnknownHostException;
import java.util.Date;

/**
 * Created by Claudio on 30/04/16.
 */
public class Main {

    public static void main(String[] args) {

        try {
            MongoClient mongo = new MongoClient( "localhost" , 27017 );

            DB db = mongo.getDB("testDB");

            DBCollection table;
            table = db.getCollection ("clienti");

            //Insert document
            BasicDBObject document = new BasicDBObject();
            document.put("name", "mkyong");
            document.put("age", 30);
            document.put ("createdDate", new Date ( ));
            table.insert(document);

            /**** Find and display ****/
            BasicDBObject searchQuery = new BasicDBObject();
            searchQuery.put("name", "mkyong");

            DBCursor cursor = table.find(searchQuery);

            while (cursor.hasNext()) {
                System.out.println(cursor.next());
            }

        } catch (UnknownHostException e) {
            e.printStackTrace ( );
        } catch (MongoException e) {
            e.printStackTrace();
        }
    }

}

this is a little example to show how to:

  • connect to MongoDB (line 14)
  • create or get database (lines 16-19)
  • create a document (lines 22-26)
  • get insered data (lines 29-32)
  • show on standart output the datas (34-35)

The result of running this code is:

Schermata 2016-05-04 alle 21.09.20

The next part is focused on the Kotlin equivalent code.

 

What is great in Kotlin? Data class!

article-2202821-150213ff000005dc-976_634x391

One of the great things in Kotlin is the Data class.

When you are working with a data class, you spend a lot of time to write getters and setters and equal operator and so on…

Kotlin show all his power in this situations, in fact you can write only something like this:


data class Person (val name: String, val age:Int)  //Formattare

And.. it’s all!

If you want to add a new parameter, you can add and then all it’s ok! You don’t waste your time to write new getters and setters and modify the equal operator.

Now, we want to use this data class: it’s so simple!


fun getUser(): User {
    return User("Alex", 1)
}

fun main(args: Array<String>) {
    val user = getUser()
    println("hello my name is  ${user.name}, and I'm ${user.id}")

    // or

    val (name, id) = getUser()
    println("hello my name is $name, and I'm $id")

    // or

    println("hello my name is ${getUser().component1()}, and I'm ${getUser().component2()}")
}

I think this is a cool feature of kotlin because, in this way, the code is less verbose than other lenguages.

How to set up kotlin into android project

In this post I want to show how to start an android project with some kotlin classes:

Start android studio and install the kotlin plugin: go to “search everywhere” and write plugin. In Plugins select kotlin and install it.

Schermata 2016-03-30 alle 18.11.37

Schermata 2016-03-30 alle 18.13.02.png

After this, create a new Android project and then, into search everywhere, type Configure kotlin and select Configure kotlin in project. This operation modify your build.grandle and add some rows:

Schermata 2016-03-30 alle 18.13.43

Schermata 2016-03-30 alle 18.19.18.png

After this operation it’s possible to convert java code into kotlin code, so it’s possible to convert an activity written in java, into activity written in kotlin: Select the activity to convert and then select  “Convert java file to kotlin file” from Code menu

Schermata 2016-03-30 alle 18.20.43.png

 

Now we can start to write our kotlin code in this android project.