SSIS – SQL Server Integration Services

SQL Server Integration Services

SSIS is a product included with Microsoft’s SQL Server and it is the Microsoft propose for Extract, Transform and Data load. It is also Microsoft’s recommend tool for data cleansing.

SSIS works by building packages, made up of tasks, that can move data around from source to destination and transform it along the way.

The Import and Export Wizard

Microsoft has produced a simple wizard to handle some of the most common and needed tasks: importing data to one SQL server database from one local or external database or exporting data from a one of our SQL Server databases. The Import and Export Wizard protects us from the complexity of SSIS while allowing us to move data between any of these data sources:

  1. SQL Server databases
  2. Microsoft Access databases
  3. Microsoft Excel worksheets
  4. Other OLE DB providers

Steps

To import some data using the Import and Export Wizard, follow these steps:

  1. Launch SQL Server Management Studio and log in to your server.
  2. Open a new query window.
  3. Create a new database with the name you want:
  1. CREATE DATABASE Name_DB
  1. Click the Execute toolbar button to create a new database.
  2. Expand the Databases node in Object Explorer
  3. Right-click on the Name_DB database and select Tasks
  1. Import Data
  1. Read the first page of the Import and Export Wizard and click Next.
  2. Select SQL Native Client for the data source and provide login information.
  3. Select the database we want as the source of the data to import.
  4. Because we’re importing data, the next page of the wizard will ask us the connection information for our new database, in this case is Name_DB database.
  5. Select Copy Data From One or More Tables or Views and click Next. (Note that if we only want to import part of a table we can use a query as the data source instead.)
  1. Select the Tables you want to import
  1. After select the tables, the wizard will automatically assign names for the target tables.
  2. Check Execute Immediately and click Next.
  3. Click Finish to perform the import. SQL Server will display progress as it performs the import.
  1. Now you have your database with all the imported data.

Working with Connection Managers

SSIS uses connection managers to integrate different data sources into packages. SSIS includes a variety of connection managers that allow you to move data around from place to place.

Connection Manager Description
ADO Connecting to ADO objects such as a Recordset.
ADO.NET Uses the .Net Provider to connect to SQL Server 2005 or other connection exposed through managed code (like C#) in a custom task
Analysis Services Connecting to an Analysis Services database or cube.
Excel Connecting to an Excel worksheet.
OLEDB Used to connect to any data source requiring a OLEDB connection (SQL Server 2000 for example)
Flat File Connecting to delimited or fixed width flat files.
FTP Connecting to a FTP data source.
MSMQ Connecting to a Microsoft Message Queue.

SSIS Packages:

The Import and Export Wizard is  simple  to use, but it is just a little sample of the functionality provided by the SSIS. To really appreciate the full power of SSIS, you’ll need to use the Business Intelligence Development Studio to build an SSIS package. A package is a collection of SSIS objects including:

  1. Connections to data sources.
  2. Data flows, which include the sources and destinations that extract and load data, the transformations that modify and extend data, and the paths that link sources, transformations, and destinations.
  3. Control flows, which include tasks and containers that execute when the package runs. You can organize tasks in sequences and in loops.
  4. Event handlers, which are work flows that runs in response to the events raised by a package, task, or container.

Steps

To Create a project:

  1. Launch Visual Studio 2005, 2008.
  2. Create a Project
  3. Select Business Intelligence Projects
  1. Select the Integration Services Project template. You will have your empty SSIS project

To add some connection managers to your package, follow these steps:

  1. Right click on the Connection Managers  Tab and it will display a pop up menu
  1. It will display the Connection Manager dialog box
  2. Select New
  3. The provider select SQL Native Client
  1. Select the Server Name
  2. Select Windows authentication or SQL Server authentication
  3. Select your database
  4. Click OK
  5. On the Connection OLE DB manager dialog box Click ok
  6. Now you will have an OLE DB connection

Control Flows

This is where you tell the SSIS what the package is going to do. You create your control flow, as creating an aspx page,  by dragging and dropping controls from the toolbox to the surface, and then dragging and dropping connections between the objects.

The objects available to Drop are divided in four groups:

  1. Tasks: Different activities than an SSIS package can do, such as execute SQL statements or transfer  objects from one SQL Server to another SQL Provider.
  2. Maintenance Plan tasks: are a special group of tasks that can handle jobs such as checking  the database integrity.
  3. The Data Flow Task is a general purpose task for ETL (extract, transform, and load) operations on data.
  4. Containers: Objects to hold a group of tasks, such as the Data Flow object.

Steps:

To add control flows to the package:

  1. Make sure you are in the SSIS project, and on the Control Flow Tab is selected
  1. Drag a Data Flow object from the toolbox to the and drop it on the designer page
  1. This is a simple Idea about how can you create the Control Flow, yo can add more object and connect them selecting the green arrow and dropping on the object you want to connect.

Data Flows

This is where you specify the details of any Data Flow tasks that you’ve added on the Control Flow tab. Data Flows are made up of various objects that you drag and drop from the Toolbox:

  1. Data Flow Sources: The data get into the system using this.
  2. Data Flow Transformations: Allow to manipulate the data in different ways.
  3. Data Flow Destinations: You send the transformed data to this places.

Steps:

This example is a simple use of the data flows, we are going to convert data from one type to an other.

  1. Select the Data Flow tab
  2. On the Data Flow Task combo select the Data flow you want to use. In this example we are going to use the “Data Flow Taks” created before
  1. Drag an OLE DB source from the toolbox and drop it on the designer
  1. double Click to the source and it will display one configuration dialog box
  2. The OLE DB Connection manager will display the connection we added before
  3. select the table or the view you want to use.
  4. select “Columns” from the list of  the left side
  5. It will display the table columns
  6. Select the columns you want to use on the work flow
  1. Drag and Drop a Data Conversion Control
  2. Drag the green arrow from the OLE DB source and drop it on the Data Conversion Control
  1. Double left click to the Data Conversion object and it will display one dialog box
  1. Select the columns you want to manipulate, or to be more specific the columns you want to convert
  2. Input Column is the selected column, output alias is the data converted on the type selected on the data type column.
  1. now Drag and Drop an OLE DB Destination
  1. Drag and drop the green arrow from the Data Conversion object to the OLE DB Destination
  2. Double click to the object to display the configuration dialog box
  3. select the OLE DB connection manager, remember you can add all the connection you need on the connection manager explained before. We are using the same because we are converting the data and saving in other table on the same database. You could easily do it between two different databases.
  4. now go to the list from the left side and select “mappings” it will display the converted data
  1. Now test it and you have you first SSIS project.

.

Conclusion

 

This is an application that we can use for data migration, integration, it eases the task of importing and exporting information from different sources, allowing us to define different processes to manipulate, verify and transform the data before loading it into one or  several destinations. It allows us to migrate data between heterogeneous databases in a relatively easy and reliable way.

The work flow organization makes creating and defining the different processes, rules and  stages that the information must pass through from source to destination an easier task to tackle. Its integration with the Visual Studio IDE makes working with SSIS a natural task to any experienced developer, making the initial learning curve a lot easier to handle.

Posted in .NET, software development | Tagged , , , | Leave a comment

Enterprise Service Bus

Enterprise Service Bus

An enterprise service bus (ESB) is a combined software architecture that provides essential services to complex architectures through a messaging system (the bus) based on the standards and responds to events. Developers typically implement a ESB using technologies of middleware infrastructure products based on accepted standards.

A ESB generally provides an abstraction layer built upon an implementation of an enterprise messaging system that enables integration experts exploit the value of messaging without writing code. Unlike the case with traditional enterprise application integration (EAI) based on a monolithic battery on architecture, hub and spoke, an enterprise service bus is built on basis functions which are divided into their constituent parts, with a distributed implementation when it becomes necessary, so that they work harmoniously on demand.

A ESB itself does not implement a service-oriented architecture (SOA) but provides the features by which it can be implemented. A ESB should be based on standards and provide flexibility, providing coverage to different means of transport that are capable of implementing both traditional patterns of SOA business architecture enriched with OSA 2.0. The ESB is to isolate the coupling between the service requested and the means of transport. Most ESB providers incorporate principles of SOA and allow independent message formats.

Definitions and Scope

There is no agreement on whether to define an enterprise service bus as a style of architecture as a software product or as a group of software products. While it is true that the use of a ESB involves some adjustment to a specific architecture, the term “enterprise service bus” almost always refers to the software infrastructure that enables this architecture and, in essence, is considered to ESB as a platform for a service-oriented architecture.

An Enterprise Service Bus (ESB) involves flows related concepts such as transformation and routing in a Service Oriented Architecture. A ESB can also provide an abstraction for endpoints.This gives flexibility in the layer of abstraction and easy connection between the services.

ESB Architecture

Using the word “bus” comes from the bus carrying the bits between devices of a computer. The enterprise service bus provides a similar function at a higher level of abstraction. In an enterprise architecture that makes use of a ESB an application will communicate via the bus, which acts as a “splitter message”(message broker) between applications. This approach has the advantage of reducing the number of connected point-to-point needed to allow it to communicate an application. This, in turn, simplifies the “impact assessment”(Impact Analysis)of most of the software. Reducing the number of contact points in a given application, the process of adapting a system to changes of one of its components is made easier.

ESB and software

architecture in a complex, the ESB represents the software element that mediates between enterprise applications and enables communication between them. Ideally, the ESB would have to be able to replace all direct contact with the applications on the bus, so that all communication takes place via the bus. To achieve this objective, the bus must encapsulate the functionality offered by its component applications in a meaningful way. This usually happens with the introduction of a standard business messages. The message model defines a standard set of messages received and transmitted ESB . When a ESB receives a message, routed to the appropriate application. It often happens that as this application has been developed without the same message model, the ESB will have to transform the message into a format compatibility(legacyformat)that the application is able to interpret. An “adapter” software performs the task of making these changes (as does a physical adapter). There is no agreement on whether to consider this adapter as a constituent of ESB or not.

The ESB are based on the precise connection of an enterprise message model and the functionality offered by applications. If the message model does not encapsulate all the functionality of the applications, then other applications that want that functionality may have to go around the bus and call directly to the unpaired applications. This is violating all the principles outlined above and despises many of the advantages of using a ESB.

Most relevant features

The term “Enterprise Service Bus” serves as a catch-all term for a set of capabilities that can deploy systems differently. There is no consensus on whether to consider a ESB as a tangible product or an architectural style and how exactly a ESB should be implemented (for example, centralized(broker or hub) or decentralized end points)). (smart For example, some experts say AOS SOAP + WS-Addressing is the bus. Either way seems agreed to accept some core capabilities as functions of a ESB:

In addition, a ESB should have the following characteristics:

  1. general agnosticism about operating systems and programming languages, for example, provide interoperability between Java and. NET
  2. general use of XML as a standard communication language
  3. support for Web services standards
  4. support number of MEP (message exchange patterns) (eg, request / response asynchronous request / response synchronous, send-and-forget, publish / subscribe)
  5. adapters to enable integration of support systems, possibly based on standards such as ( Java_EE_Connector_Architecture | JCA),
  6. a standardized security model to authorize, authenticate and audit the use of ESB
  7. facilitating the transformation of data formats and values, including transformation services (often via XSLT or XQuery between the sending application formats receiving and implementation
  8. validation of schemes for sending and receiving messages
  9. can apply business rules consistently
  10. enriched messages from other sources
  11. of division and combination of multiple messages and exceptions handling
  12. the provision of a unified abstraction multilayer
  13. or processing routing messages conditionally, based on a non-centralized policy (no need for a centralized system of normal)
  14. and retention queuing messages if applications are temporarily unavailable

Conclusions

Main Benefits

  1. Accommodation from existing systems faster and cheaper
  2. Greater flexibility, easier to change if new requirements.
  3. Based on Standards
  4. Scale point solutions to enterprise deployment Bus (distributed).
  5. Service types ready-to-operate(ready-to-use)predefined
  6. Mayor settings instead of having to code integration.
  7. No central rules engine without center divider
  8. incremental patches instant turn-off time, the company does “refactoring.”

Main disadvantages

  1. Normally requires Enterprise Message Model, which requires additional administration.
  2. Requires ongoing management of message versions to ensure the purported benefit of a flexible coupling. An administration incorrect, inadequate or incomplete versions of message can lead to a match instead of the intended strict matching flexible.
  3. Normally requires more hardware for a simple messaging system point-to-point.
  4. Expertise is required in the analysis of middleware to configure, manage and operate a ESB.
  5. Mayor overall latency caused by messages that go through the extra layer of ESB , especially when compared with the communications point-to-point. The higher latency is also triggered by an extra XML processing (ESB typically uses XML as a language of communication).
  6. The ESB becomes a single element failure.
  7. Although ESB systems may require significant effort to implement, they do not produce a commercial value without the subsequent development services that take advantage of the created infrastructure.

Here is a video expanding on the ESB characteristics

Posted in software development | Tagged | Leave a comment

NHibernate, O/R mapping for .NET

NHibernate, O/R mapping for .NET

O/R Mapping

The Object/Relational Mapping is a technique to translate the data between two different and most likely incompatible representation systems. We usually mean by this to convert an object model domain to a representation in a relational database management system

These two representations are clearly different, while an object is usually composed of non-scalar attributes (other objects, collection of objects, etc.) while a database table is usually a collection of scalar values (char, ints, etc.) For example: A “Person” object may contain multiple “Address” objects and each “Address” object may contain reference to object representations for “Country” and “Region”, while a Person object may be referenced as a single entity, one variable, on the object oriented environment it needs multiple tables, attributes and relationships to represent the same information on a relational database.

The O/R Mapping tools attempt to do this “translation” of the objects model to a relational model to a Relation Database Model created and dictated by our “Domain Model”. Our main goal by doing this mapping is to translate the object model in a form that makes possible to save our objects to a relational database, keeping the ability to easily query and retrieve the objects to be used again on the object oriented environment and maintain all of its relationships and properties during the translation.

NHibernate

NHibernate is a free open source software solution for Microsoft .NET that provides a framework to map an object domain model to some of the most popular relation database systems. Created as a port of Java’s Hibernate, NHibernate maintains the same functionality, principles, design and even the same documentation largely applies to both.

NHibernate is available for download from the project’s sourceforge page on both source code and binary distributions. To start using it we simply need to add references to NHibarnet’s DLLs from our .NET projects. The binary distribution includes a “Required_Bins” directory with all the DLLs we need to start using NHibernate (other useful DLLs are also included on different directories).

NHibernate’s main configuration and mappings are both configured using XML files, these XML files define which objects and attributes will be translated to a representation in the database. In the NHinbernate’s configuration file it is possible to define the target database and the way to connect to it this makes extremely easy to change database technologies, in the best case scenario a migration to a different technology would involve just changing a couple of lines.

Supported RDBMS

  1. Microsoft SQL Server 2005/2000
  2. Oracle
  3. Microsoft Access
  4. Firebird
  5. PostgreSQL
  6. DB2 UDB
  7. MySQL
  8. SQLite

NHibernate is a perfect match to apply Domain Driven Design. For development NHibernate recommends to apply Test Driven Development, it is not only used by NHibernate itself but it also includes the necessary libraries to work with the NUnit framework.

NHibernate lends itself to use the Repository Pattern to get access to the data, this to accomplish a decoupling of the underlying technology  (database), make testing easier and to facilitate the replacement of any component.

Resources:

Tutorial “Your First NHibernate based application” (be careful while using 64 bits systems and the SQL Compact Edition version. It is safer to use the SQL Server Express edition)

NHibernate Community Page.

“How-Tos” for NHibernate

NHibernate FAQ (blog with many tips, tutorials and answers to some of the most common NHibernate issues)

Fluent NHibernate

Posted in .NET, Uncategorized | Tagged , , , , | Leave a comment

Custom Single Sign On between Websites

What is single-sign on?

Single sign on is a way to control access to multiple, related but independent systems after being asked for credentials just once. This way the system ask the user for his credentials just once and that is enough to make them valid on a multitude of other systems.

Benefits

By implementing single sign on we will basically make our user’s life easier, instead of having them to sign up and maintain user accounts and passwords for each possible system, this may result in a simpler way for them to manage their password, fewer support calls about missing or forgotten passwords and if they do happen they tend to be easier to answer since there will be just one place and method to manage them.

The user account is easier to manage since in case we wish to deny access to the user we only have to worry about one place, instead of tracking down a multitude of ways he could access again.

By having just one security infrastructure and model we are able to secure and manage it more carefully.

Problems

Achieving the level of a true “Single Sign On” across the company’s systems using one unified security infrastructure is a monumental task that may include expensive solutions, having to rewrite a great deal of authentication processes on the different applications we want to use.

Another problem is that with just a set of credentials the user is able to access many resources, making any event where the user credentials become compromised.

Standard Solutions

There a few systems that promise to provide a single sign on solution, we will not be focusing on them today but they certainly provide richer options that integrate with a number of products, their trade-off is that they are sometimes hard to configure, require extra infrastructure, special programs and sometimes change they way the users interact with the applications.

Some of the most popular are:

  1. Kerberos
    • Is a network authentication method designed to operate on a client-server fashion, where the clients authenticate with the server on a secure manner even when the traffic is going trough an insecure network. The authentication works both ways, so that the clients can be sure they are authenticating against the correct server.
  2. Active Directory
    • Standard solution on Windows environments, one domain controller authenticates the user and many other applications that are aware of this fact will not prompt for authentication a second time.
  3. CAS
    • Central Authentication Server, is a single sign on protocol for the web, it may use Kerberos, Active Directory or other type of databases as its backend to manage the database of users. Different web applications may use a CAS server for authentication, CAS provides the application with an authentication ticket that the application is able to validate. This saves the application the need to implement its own authentication mechanism.

Even when these provide a more robust authentication mechanism we will be focusing on a much more simpler case, when we have 2 websites, each with its own authentication mechanism but the user needs to use both and we want to provide a seamless transition between the two.

Custom Solution between websites

The example we’ll show here is between two sites we control of but that do not share any authentication infrastructure. The method we’ll use is based on a secret key shared between the sites. We assume that the user on the origin site has an equivalent account on the target site, using the same account name

Custom Single Sign On diagram

Example:

For this example we are going to use 2 ASP.NET web sites. Both of them implement authentication through FormsAuthentication and have the login page configured to be at ~/Login.aspx.

How it works

We are going to have one SSO related page on each site. On the origin site we’ll have an “sso out”, this page will take a target URL, build up a hashed authentication token with this parameters and redirect the user to the target site.

On the target site there is a second “sso in” page that will take the authentication token, the username and target URL and verify them by deriving a second authentication token, this way we can make sure the information has not been tampered with.

sso-out.aspx.cs:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace OriginSite
{
public partial class sso_out : System.Web.UI.Page
{
String targetSiteSsoUrl = "http://localhost:8990/sso.aspx";
String SECRET_KEY = "MySSO";
protected void Page_Load(object sender, EventArgs e)
{
string destinationUrl = Request.Params.Get("destinationUrl");
string UserName = User.Identity.Name;
string destination = targetSiteSsoUrl + "?uname=" +  HttpUtility.HtmlEncode(UserName) + "&destinationUrl=" +  HttpUtility.HtmlEncode(destinationUrl);
string message = UserName + "|" + destinationUrl;
string hash = getHash(message);
destination += "&hash=" + hash;
Response.Redirect(destination);
}

protected string getHash(string message)
{
HMACSHA1 sha1 = new HMACSHA1(Encoding.Default.GetBytes(SECRET_KEY));
string hash = BitConverter.ToString(sha1.ComputeHash(Encoding.Default.GetBytes(message))).Replace("-", "");
return hash;
}
}
}

Our secrete shared key is: “MySSO”

String SECRET_KEY = "MySSO";

We receive a parameter named “destinationUrl”, this is the URL at the target site we wish to reach. We take the username out of the current Identity Object and together with the target’s site SSO address and destination URL we create the message to hash in order to create our authentication token.

string UserName = User.Identity.Name;
string destination = targetSiteSsoUrl + "?uname=" +  HttpUtility.HtmlEncode(UserName) + "&destinationUrl=" +  HttpUtility.HtmlEncode(destinationUrl);
string message = UserName + "|" + destinationUrl;
string hash = getHash(message);

We will be doing the hashing using our secret key and the HMACSHA1 cryptography class.


protected string getHash(string message)

{

HMACSHA1 sha1 = new HMACSHA1(Encoding.Default.GetBytes(SECRET_KEY));
string hash = BitConverter.ToString(sha1.ComputeHash(Encoding.Default.GetBytes(message))).Replace("-", "");

return hash;

}

On the target site:

We receive 2 parameters via the URL, plus we also get the hash. We get:

  1. uname: The user name of the account who wants to get in.
  2. destinationUrl: The final url to which we are headed.
  3. hash: We receive the hash calculated by the origin site, we will use uname and destinationUrl to calculate the hash one more time and compare them verify that the information has not been tampered with.

sso.aspx.cs:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography;
using System.Text;
using System.Web.Security;

namespace TargetSite
{
 public partial class sso : System.Web.UI.Page
 {
 public string destination = "NOT SET";
 public string hash;
 private string userName;
 public string Verified;
 public string DerivedHash;

 protected void getParameters()
 {
 if (Request.Params.Get("destinationUrl") != null)
 {
 destination = HttpUtility.HtmlDecode(Request.Params.Get("destinationUrl"));
 }
 else
 {
 destination = "/";
 }

 if (Request.Params.Get("uname") != null)
 {
 userName = HttpUtility.HtmlDecode(Request.Params.Get("uname"));
 }

 if (Request.Params.Get("hash") != null)
 {
 hash = HttpUtility.HtmlDecode(Request.Params.Get("hash"));
 }
 }

 public bool verifyParameters()
 {
 string message = userName + "|" + destination;
 string CalculatedHash = getHash(message);
 DerivedHash = getHash(message);
 return CalculatedHash.Equals(hash);
 }

 protected string getHash(string message)
 {
 string SECRET_KEY = "MySSO";
 HMACSHA1 sha1 = new HMACSHA1(Encoding.Default.GetBytes(SECRET_KEY));
 string CalcultedHash = BitConverter.ToString(sha1.ComputeHash(Encoding.Default.GetBytes(message))).Replace("-", "");
 return CalcultedHash;
 }

 protected void Page_Load(object sender, EventArgs e)
 {
 getParameters();
 if (verifyParameters())
 {
 Verified = "Hash Verified";
 FormsAuthentication.SetAuthCookie(userName, true);
 Response.Redirect(destination);
 }
 else
 {
 Verified = "Hash Failed";
 }

 }
 }
}

We first will get the destination url, user name and hash out of the parameters as described on the getParameters() mehotd


protected void getParameters()
 {
 if (Request.Params.Get("destinationUrl") != null)
 {
 destination = HttpUtility.HtmlDecode(Request.Params.Get("destinationUrl"));
 }
 else
 {
 destination = "/";
 }
 .......

}

Next we will derive a new hash out of the plain text information and compare it to the one we received on the initial request. The getHash method uses the same secret key and procedure to calculate the hash, if the data on the request is correct and has not been tampered with then we can continue with the authentication.


protected string getHash(string message)
 {
 string SECRET_KEY = "MySSO";
 HMACSHA1 sha1 = new HMACSHA1(Encoding.Default.GetBytes(SECRET_KEY));
 string CalcultedHash = BitConverter.ToString(sha1.ComputeHash(Encoding.Default.GetBytes(message))).Replace("-", "");
 return CalcultedHash;
 }

We simply will use the userName and destination as parameters to create a message to be hashed, if the data is correct then both hashes must match.


public bool verifyParameters()
 {
 string message = userName + "|" + destination;
 string CalculatedHash = getHash(message);
 DerivedHash = getHash(message);
return CalculatedHash.Equals(hash);
}

If the hashes match we have verified that the user is authenticated on the origin site and we can grant him access to our site, we may now log him in and redirect the user to the final destination.


if (verifyParameters())
 {
 Verified = "Hash Verified";
 FormsAuthentication.SetAuthCookie(userName, true);
 Response.Redirect(destination);
 }
 else
 {
 Verified = "Hash Failed";
 }

Conclusions

This is a very simple and basic method to facilitate our users to pass from one site to another site, its main advantage it is its simplicity, a couple of pages is all we need to move between the sites. However, it has some problems, the way we are building the hash currently lends itself to “playback” attacks, since this token never expires. We can improve on it by adding a time variable into the mix, so that the token is valid only for a few minutes at most. Scalability is another issue, you can get away with this setup if your users needs to move just among a handful of sites, after that, adding new sites and handling more shared keys between them may get difficult, if you get to this point a more robust solution like CAS may be needed.

Posted in .NET, ASP, C#, software development | Tagged , , | Leave a comment

Using Google Maps, Geocoding and #.NET

Google Maps using C# .NET

The Google Maps API lets us embed Google Maps in our own web pages with JavaScript. The API provides a number of utilities for manipulating maps and adding content to the map through a variety of services, allowing us to create robust maps applications on our website.

Google Map Key

We must have a Google Account to get a Maps API key, and our API key will be connected to our Google Account.

  1. There is no limit on the number of page views you may generate per day using the Maps API.
  2. There is a limit on the number of geocode requests per day. The Limit is set on 50,000 queries per day, Google allows to increment the limit with direct contact with Google.
  3. The Maps API does not include advertising.
  4. The service must be freely accessible to end users.
  5. You may not alter or obscure the logos or attribution on the map.
  6. Google will upgrade the APIs periodically.

The site to get our own Key:

http://code.google.com/apis/maps/signup.html

Developing the Library

Create a new project or add a new class where we are going to place all the Google Maps methods, function, etc:

1. Add Google Map Key:

It’s a good idea to develop to get our Key, so it will be easier to change it and use it on different google queries.

public static string GeocoderKey()
{
//Google Map Key by the moment is just for localhost, we have to change it.
return "ABQIAAAAKji2yEvnKQltot2jp7sguBT2yXp_ZAY8_ufC3CFXhHIE1NvwkxQMXl5qiRVRMsOEG5s9HrQqdXPl1w";
}

2. The Query Builder

The three parameters received forthis function could be used for different purposes, such as the three parameters could be: City, State, Country or Postal Code, State, Country. This is going to be explained later.

While building the query  we call the function that has our API Key, the API Key is needed on every request.

The query will be executed and the result is a XML file, that is going to be saved on a XML Document (Object) which we are going to use and manipulate.

public static XmlNodeList GoogleQuery(string text1, string text2, string text3)

{

XmlDocument xDoc = new XmlDocument();

xDoc.Load("http://maps.google.com/maps/geo?q=" +  HttpUtility.UrlEncode(text1) + "," + HttpUtility.UrlEncode(text2) + "," +  HttpUtility.UrlEncode(text3) +  "&output=xml&sensor=false&key=" + GeocoderKey());

XmlNodeList personas = xDoc.GetElementsByTagName("Response");

XmlNodeList list = ((XmlElement)personas[0]).GetElementsByTagName("Placemark");

return list;

}

The result is a XmlNode with the information that google retrieves, it could be all the needed data, the geographic data or the error that google retrieves: like no more queries, or wrong data inserted on querystring.

An example of the XML file retrieved by the API:

<kml>
 <Response>
   <name>12 Calle,Guatemala,Guatemala</name>
   <Status>
     <code>200</code>
     <request>geocode</request>
   </Status>
   <Placemark id="p1">
     <address>12 Calle, <a class="zem_slink" title="Guatemala City" rel="geolocation" href="http://maps.google.com/maps?ll=14.6133333333,-90.5352777778&spn=0.1,0.1&q=14.6133333333,-90.5352777778%20%28Guatemala%20City%29&t=h">Guatemala City</a>, Guatemala</address>
     <AddressDetails Accuracy="6">
       <Country>
         <CountryNameCode>GT</CountryNameCode>
         <CountryName>Guatemala</CountryName>
         <AdministrativeArea>
           <AdministrativeAreaName>Guatemala</AdministrativeAreaName>
           <Locality>
             <LocalityName>Guatemala</LocalityName>
             <Thoroughfare>
               <ThoroughfareName>12 Calle</ThoroughfareName>
             </Thoroughfare>
           </Locality>
         </AdministrativeArea>
       </Country>
     </AddressDetails>
     <ExtendedData>
       <LatLonBox north="14.6042849" south="14.5979896" east="-90.5088887" west="-90.5254774"/>
     </ExtendedData>
     <Point>
       <coordinates>-90.5170290,14.6009959,0</coordinates>
     </Point>
   </Placemark>
 </Response>
</kml>

3. Data Manipulation:

Geolocation:

Geolocation refers to the identification of the geographic location of a user or computing device via a variety of data collection mechanisms. Most geolocation services use network routing addresses or internal GPS devices to figure this location.

Location:

Is a position or point in physical space that something occupies on the Earth’s surface, the Solar System, or mankinds physically reachable universe. An Absolute location can often be designated using a specific pairing of latitude and longitude, a Cartesian coordinate grid.

3.1. Geographic Location from an address:

The library request the geolocation coordinates for a specific address, the three needed values are the city, state and country:

First the attribute accuracy is checked to determine if the retrieved file has the requested values or if it just has the error information. to check we use the following check:

node.GetAttribute("Accuracy") == "6"

The number 6 was determined by Google, it means that the query was executed successfully and it retrieves the needed data. Check status codes for more info.

We send the follow parameters to the query builder method:

  1. City: This parameter is optional, but if a more specific location is needed, it will be requested.
  2. State: This parameter is not optional.
  3. Country: This parameter isn’t optional, It is needed to be more specific and to use it as limit, google will search just in the selected country.

XmlNodeList coordinates = nodo.GetElementsByTagName("coordinates");

XmlNodeList LatLonBox = nodo.GetElementsByTagName("LatLonBox");

string test = coordinates[0].InnerText;

char[] delimiterChars = { ',' };

string[] words = test.Split(delimiterChars);

i++;

foreach (XmlElement nodo2 in LatLonBox) //the data for the bounding boxes

{

if (i < 2)

{

LatLon[0] = words[0];//latitude

LatLon[1] = words[1];//longitude

LatLon[2] = nodo2.GetAttribute("north");

LatLon[3] = nodo2.GetAttribute("south");

LatLon[4] = nodo2.GetAttribute("east");

LatLon[5] = nodo2.GetAttribute("west");

}

else

{

LatLon[0] = "The postcode does not exist";

LatLon[1] = "";

LatLon[2] = "";

LatLon[3] = "";

LatLon[4] = "";

LatLon[5] = "";

}

}

We use the  XMLNode list retrieved to select the latitude and longitude node that has the following info:

  1. Latitude
  2. Longitude
  3. North
  4. South
  5. East
  6. West

The data is saved on a string list that could be displayed as the code wanted, for example:

Latitude:   -72.1179169

Longitude: 42.4328338

North:        42.4845309

South:       42.3566919

East:        -72.0235210

West:       -72.2051740

3.2. Geographic Location from postal/zip code:

The library requests the Geolocation coordinates for a postal code, it needs to send the exact postal code and the country, the state is optional.:

As before, we first check the “Accuracy” attribute to verify if we got the level of information we need, for what we are looking for we need for it to return a “6″ you can  Check the status codes for more info on google’s webiste.

node.GetAttribute("Accuracy") == "6"

We send the following parameters to the query builder method:

  1. Postal Code: This parameter is required, since it is basis of what we are looking for.
  2. State: This parameter is  optional, it could be sent as null or empty value.
  3. Country: This parameter is required. It limits google’s query to a single country.

Once again we use the XMLNode object returned to get, on the latlong node, the coordinates we need to locate the postal code.

  1. North
  2. South
  3. East
  4. West
  5. <coordinates>-90.5401602,14.6037439,0</coordinates>

and the data is saved on a string list that could be displayed as the code wanted, for example:

3.3. Status Codes

The “status” field within the Geocoding response object contains the status of the request, and may contain debugging information to help you track down why Geocoding is not working. The “status” field may contain the following values:

  1. “OK” indicates that no errors occurred; the address was successfully parsed and at least one geocode was returned.
  2. “ZERO_RESULTS” indicates that the geocode was successful but returned no results. This may occur if the geocode was passed a non-existent address or alatlng in a remote location.
  3. “OVER_QUERY_LIMIT” indicates that you are over your quota.
  4. “REQUEST_DENIED” indicates that your request was denied, generally because of lack of a sensor parameter.
  5. “INVALID_REQUEST” generally indicates that the query (address or latlng) is missing.

4. Using the data

The retrieved points can be used for different purposes, for example:

  1. Bounding Box: For this is needed four points: south, north, east and west. This could be used to know all the geographic points inside the box. To know the limits for a city, state, country.
  2. Geolocation: To know the exact geolocation (latitude and longitude) for the postal code or the address.
  3. Display point on map: The latitude and longitude could be used to display the address or postal code on one google map:

This is just a brief overlook of what you are able to accomplish by using one of Google’s easiest to use APIs, with just a few details about the place you want to find you are able to get enough information to feed into a map to display on your page. You get enough information to build a “bounding box” around your point of interest that can help you or your users narrow down searches.

Posted in .NET, ASP, C#, software development | Tagged , , | Leave a comment

Unit Test in .NET

Testing Frameworks

Our daily work require testing, and what is better that have it automated? So… here’s a quick example of the tools we use a Viaro for .NET development. We will be testing a pretty simple library that contains some basic arithmetic operations. Addition, multiplication and a power of function.

Visual Studio Testing Framework

Microsoft has included a unit testing Framework since Visual Studio 2005 called the “Visual Studio Unit Testing Framework” the tests created using this framework can be executed from the Visual Studio interface or from the command line using the MSTest utility.

How to create and run tests

1. Create a new Test Project

2. Create a new Test Class

3. Add your own test methods.

Complete Test Methods Listing:

[TestMethod]
public void TestAddition()
{
    int a = 2;
    int b = 2;
    int result = Core.Operations.sum(a, b);
    Assert.AreEqual(4, result, "The addition was wrong.");
}

[TestMethod]
public void TestArrayAddition()
{
    int[] arr = new int[ ]{1, 9, 5, 3, 2};
    int result = Core.Operations.sum(arr);
    Assert.AreEqual(20, result, "The Array Addition is not correct.");
}

[TestMethod]
public void TestPower()
{
    int x = 3;
    int a = 9;
    int result = Operations.power(a,x);
    Assert.AreEqual(729, result);
    result = Operations.power(10, 0);
    Assert.AreEqual(1, result);
    Assert.AreEqual(5, Operations.power(5, 1));
    Assert.AreEqual(12, Operations.power(12, -1));
}

[TestMethod]
public void TestTimes()
{
    Assert.AreNotEqual(1, Operations.times(1, -1));
    Assert.AreEqual(10, Operations.times(2, 5));
    Assert.AreEqual(25, Operations.times(-5, -5));
}

Running Tests

The tests are run from the Visual Studio interface. When on a Test project you will see some additional buttons to run the tests:
The buttons include options to run and debug all the tests on the current context (the same class or file you are working on) or to run all the tests on the solution. The results appear on a new panel while the tests are running:

NUnit

Nunit is an open source unit testing framework for .NET. It is able to handle all of .NET languages. Originally a port of JUnit to .NET it is now on its sixth major release, written entirely on C# it is able to take advantage of many of the newer .NET features and it is specially heavy on reflection, this makes really easy to write tests for it.

You can get NUnit from its homepage: http://www.nunit.com/index.php?p=download

How to create and run tests

1. Create a new class library project to hold the test code

2. Add a reference to the nunit.framework library from your project.

3. Create a C# class file and reference your test. Here is an example of how to set up a class to be recognized by the framework as containing NUnit tests.

4. You will need to build your project, since the NUnit gui will need to load the resulting dll.
5. Now open the NUnit GUI and then choose “Open New Project” from the  file menu. Browse to your test projects resulting DLL and open it. Click “RUN” to run all the tests on the project.

Complete Listing of the test file used:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Core;

namespace NunitTests
{
    [TestFixture]
    public class NunitTests
    {
        [Test]
        public void TestAddition()
        {
            int a = 2;
            int b = 2;
            int result = Operations.sum(a, b);
            Assert.AreEqual(4, result, "The addition was wrong.");
        }

        [Test]
        public void TestArrayAddition()
        {
            int[] arr = new int[] { 1, 9, 5, 3, 2 };
            int result = Core.Operations.sum(arr);
            Assert.AreEqual(20, result, "The Array Addition is not correct.");
        }

        [Test]
        public void TestPower()
        {
            int x = 3;
            int a = 9;
            int result = Operations.power(a, x);
            Assert.AreEqual(729, result);
            result = Operations.power(10, 0);
            Assert.AreEqual(1, result);
            Assert.AreEqual(5, Operations.power(5, 1));
            Assert.AreEqual(12, Operations.power(12, -1));
        }

        [Test]
        public void TestTimes()
        {
            Assert.AreNotEqual(1, Operations.times(1, -1));
            Assert.AreEqual(10, Operations.times(2, 5));
            Assert.AreEqual(25, Operations.times(-5, -5));
        }
    }
}

Pros & Cons

    • NUnit is able to run tests on 32 and 64 bits mode, while the Visual Studio’s built in testing framework is able to test only on 32 bits mode.
    • NUnit tends to perform faster.
    • NUnit allows TestFixtures, the classes that contain the tests, to be abstract, so you can inherit TestFixture classes.
    • NUnit is able to run test in parallel (only Visual Studio 2010 is able to do this)
    • NUnit main focus is unit testing, MSTests does general testing with the option to do unit testing.
    • MSTest is able load the applications configuration and have access to its properties.
    • MSTest performs better for tests that involve a database connection.
    • MSTest have a better loggin integration and provides lots of statistics and reports about the tests.
    • MSTest allows to choose in what order the tests will be run, with the special “OrderedTests” type.
    • MSTest allows to do performance and benchmarking testing, while NUnit is not capable of do this.
<pre style=’color:#000000;background:#f1f0f0;’><span style=’color:#400000; font-weight:bold; ‘>using</span> System<span style=’color:#806030; ‘>;</span>
<span style=’color:#400000; font-weight:bold; ‘>using</span> System<span style=’color:#806030; ‘>.</span>Collections<span style=’color:#806030; ‘>.</span>Generic<span style=’color:#806030; ‘>;</span>
<span style=’color:#400000; font-weight:bold; ‘>using</span> System<span style=’color:#806030; ‘>.</span>Linq<span style=’color:#806030; ‘>;</span>
<span style=’color:#400000; font-weight:bold; ‘>using</span> System<span style=’color:#806030; ‘>.</span>Text<span style=’color:#806030; ‘>;</span>
<span style=’color:#400000; font-weight:bold; ‘>using</span> NUnit<span style=’color:#806030; ‘>.</span>Framework<span style=’color:#806030; ‘>;</span>
<span style=’color:#400000; font-weight:bold; ‘>using</span> Core<span style=’color:#806030; ‘>;</span>

<span style=’color:#400000; font-weight:bold; ‘>namespace</span> NunitTests
<span style=’color:#806030; ‘>{</span>
<span style=’color:#806030; ‘>[</span>TestFixture<span style='color:#806030; '>]</span>
<span style=’color:#400000; font-weight:bold; ‘>public</span> <span style=’color:#400000; font-weight:bold; ‘>class</span> NunitTests
<span style=’color:#806030; ‘>{</span>
<span style=’color:#806030; ‘>[</span>Test<span style='color:#806030; '>]</span>
<span style=’color:#400000; font-weight:bold; ‘>public</span> <span style=’color:#400000; font-weight:bold; ‘>void</span> TestAddition<span style=’color:#806030; ‘>(</span><span style=’color:#806030; ‘>)</span>
<span style=’color:#806030; ‘>{</span>
<span style=’color:#400000; font-weight:bold; ‘>int</span> a <span style=’color:#806030; ‘>=</span> <span style=’color:#c00000; ‘>2</span><span style=’color:#806030; ‘>;</span>
<span style=’color:#400000; font-weight:bold; ‘>int</span> b <span style=’color:#806030; ‘>=</span> <span style=’color:#c00000; ‘>2</span><span style=’color:#806030; ‘>;</span>
<span style=’color:#400000; font-weight:bold; ‘>int</span> result <span style=’color:#806030; ‘>=</span> Operations<span style=’color:#806030; ‘>.</span>sum<span style=’color:#806030; ‘>(</span>a<span style=’color:#806030; ‘>,</span> b<span style=’color:#806030; ‘>)</span><span style=’color:#806030; ‘>;</span>
Assert<span style=’color:#806030; ‘>.</span>AreEqual<span style=’color:#806030; ‘>(</span><span style=’color:#c00000; ‘>4</span><span style=’color:#806030; ‘>,</span> result<span style=’color:#806030; ‘>,</span> <span style=’color:#800000; ‘>”</span><span style=’color:#e60000; ‘>The addition was wrong.</span><span style=’color:#800000; ‘>”</span><span style=’color:#806030; ‘>)</span><span style=’color:#806030; ‘>;</span>
<span style=’color:#806030; ‘>}</span>

<span style=’color:#806030; ‘>[</span>Test<span style='color:#806030; '>]</span>
<span style=’color:#400000; font-weight:bold; ‘>public</span> <span style=’color:#400000; font-weight:bold; ‘>void</span> TestArrayAddition<span style=’color:#806030; ‘>(</span><span style=’color:#806030; ‘>)</span>
<span style=’color:#806030; ‘>{</span>
<span style=’color:#400000; font-weight:bold; ‘>int</span><span style=’color:#806030; ‘>[</span><span style='color:#806030; '>]</span> arr <span style=’color:#806030; ‘>=</span> <span style=’color:#400000; font-weight:bold; ‘>new</span> <span style=’color:#400000; font-weight:bold; ‘>int</span><span style=’color:#806030; ‘>[</span><span style='color:#806030; '>]</span> <span style=’color:#806030; ‘>{</span> <span style=’color:#c00000; ‘>1</span><span style=’color:#806030; ‘>,</span> <span style=’color:#c00000; ‘>9</span><span style=’color:#806030; ‘>,</span> <span style=’color:#c00000; ‘>5</span><span style=’color:#806030; ‘>,</span> <span style=’color:#c00000; ‘>3</span><span style=’color:#806030; ‘>,</span> <span style=’color:#c00000; ‘>2</span> <span style=’color:#806030; ‘>}</span><span style=’color:#806030; ‘>;</span>
<span style=’color:#400000; font-weight:bold; ‘>int</span> result <span style=’color:#806030; ‘>=</span> Core<span style=’color:#806030; ‘>.</span>Operations<span style=’color:#806030; ‘>.</span>sum<span style=’color:#806030; ‘>(</span>arr<span style=’color:#806030; ‘>)</span><span style=’color:#806030; ‘>;</span>
Assert<span style=’color:#806030; ‘>.</span>AreEqual<span style=’color:#806030; ‘>(</span><span style=’color:#c00000; ‘>20</span><span style=’color:#806030; ‘>,</span> result<span style=’color:#806030; ‘>,</span> <span style=’color:#800000; ‘>”</span><span style=’color:#e60000; ‘>The Array Addition is not correct.</span><span style=’color:#800000; ‘>”</span><span style=’color:#806030; ‘>)</span><span style=’color:#806030; ‘>;</span>
<span style=’color:#806030; ‘>}</span>

<span style=’color:#806030; ‘>[</span>Test<span style='color:#806030; '>]</span>
<span style=’color:#400000; font-weight:bold; ‘>public</span> <span style=’color:#400000; font-weight:bold; ‘>void</span> TestPower<span style=’color:#806030; ‘>(</span><span style=’color:#806030; ‘>)</span>
<span style=’color:#806030; ‘>{</span>
<span style=’color:#400000; font-weight:bold; ‘>int</span> x <span style=’color:#806030; ‘>=</span> <span style=’color:#c00000; ‘>3</span><span style=’color:#806030; ‘>;</span>
<span style=’color:#400000; font-weight:bold; ‘>int</span> a <span style=’color:#806030; ‘>=</span> <span style=’color:#c00000; ‘>9</span><span style=’color:#806030; ‘>;</span>
<span style=’color:#400000; font-weight:bold; ‘>int</span> result <span style=’color:#806030; ‘>=</span> Operations<span style=’color:#806030; ‘>.</span>power<span style=’color:#806030; ‘>(</span>a<span style=’color:#806030; ‘>,</span> x<span style=’color:#806030; ‘>)</span><span style=’color:#806030; ‘>;</span>
Assert<span style=’color:#806030; ‘>.</span>AreEqual<span style=’color:#806030; ‘>(</span><span style=’color:#c00000; ‘>729</span><span style=’color:#806030; ‘>,</span> result<span style=’color:#806030; ‘>)</span><span style=’color:#806030; ‘>;</span>
result <span style=’color:#806030; ‘>=</span> Operations<span style=’color:#806030; ‘>.</span>power<span style=’color:#806030; ‘>(</span><span style=’color:#c00000; ‘>10</span><span style=’color:#806030; ‘>,</span> <span style=’color:#c00000; ‘>0</span><span style=’color:#806030; ‘>)</span><span style=’color:#806030; ‘>;</span>
Assert<span style=’color:#806030; ‘>.</span>AreEqual<span style=’color:#806030; ‘>(</span><span style=’color:#c00000; ‘>1</span><span style=’color:#806030; ‘>,</span> result<span style=’color:#806030; ‘>)</span><span style=’color:#806030; ‘>;</span>
Assert<span style=’color:#806030; ‘>.</span>AreEqual<span style=’color:#806030; ‘>(</span><span style=’color:#c00000; ‘>5</span><span style=’color:#806030; ‘>,</span> Operations<span style=’color:#806030; ‘>.</span>power<span style=’color:#806030; ‘>(</span><span style=’color:#c00000; ‘>5</span><span style=’color:#806030; ‘>,</span> <span style=’color:#c00000; ‘>1</span><span style=’color:#806030; ‘>)</span><span style=’color:#806030; ‘>)</span><span style=’color:#806030; ‘>;</span>
Assert<span style=’color:#806030; ‘>.</span>AreEqual<span style=’color:#806030; ‘>(</span><span style=’color:#c00000; ‘>12</span><span style=’color:#806030; ‘>,</span> Operations<span style=’color:#806030; ‘>.</span>power<span style=’color:#806030; ‘>(</span><span style=’color:#c00000; ‘>12</span><span style=’color:#806030; ‘>,</span> <span style=’color:#806030; ‘>-</span><span style=’color:#c00000; ‘>1</span><span style=’color:#806030; ‘>)</span><span style=’color:#806030; ‘>)</span><span style=’color:#806030; ‘>;</span>
<span style=’color:#806030; ‘>}</span>

<span style=’color:#806030; ‘>[</span>Test<span style='color:#806030; '>]</span>
<span style=’color:#400000; font-weight:bold; ‘>public</span> <span style=’color:#400000; font-weight:bold; ‘>void</span> TestTimes<span style=’color:#806030; ‘>(</span><span style=’color:#806030; ‘>)</span>
<span style=’color:#806030; ‘>{</span>
Assert<span style=’color:#806030; ‘>.</span>AreNotEqual<span style=’color:#806030; ‘>(</span><span style=’color:#c00000; ‘>1</span><span style=’color:#806030; ‘>,</span> Operations<span style=’color:#806030; ‘>.</span>times<span style=’color:#806030; ‘>(</span><span style=’color:#c00000; ‘>1</span><span style=’color:#806030; ‘>,</span> <span style=’color:#806030; ‘>-</span><span style=’color:#c00000; ‘>1</span><span style=’color:#806030; ‘>)</span><span style=’color:#806030; ‘>)</span><span style=’color:#806030; ‘>;</span>
Assert<span style=’color:#806030; ‘>.</span>AreEqual<span style=’color:#806030; ‘>(</span><span style=’color:#c00000; ‘>10</span><span style=’color:#806030; ‘>,</span> Operations<span style=’color:#806030; ‘>.</span>times<span style=’color:#806030; ‘>(</span><span style=’color:#c00000; ‘>2</span><span style=’color:#806030; ‘>,</span> <span style=’color:#c00000; ‘>5</span><span style=’color:#806030; ‘>)</span><span style=’color:#806030; ‘>)</span><span style=’color:#806030; ‘>;</span>
Assert<span style=’color:#806030; ‘>.</span>AreEqual<span style=’color:#806030; ‘>(</span><span style=’color:#c00000; ‘>25</span><span style=’color:#806030; ‘>,</span> Operations<span style=’color:#806030; ‘>.</span>times<span style=’color:#806030; ‘>(</span><span style=’color:#806030; ‘>-</span><span style=’color:#c00000; ‘>5</span><span style=’color:#806030; ‘>,</span> <span style=’color:#806030; ‘>-</span><span style=’color:#c00000; ‘>5</span><span style=’color:#806030; ‘>)</span><span style=’color:#806030; ‘>)</span><span style=’color:#806030; ‘>;</span>
<span style=’color:#806030; ‘>}</span>
<span style=’color:#806030; ‘>}</span>
<span style=’color:#806030; ‘>}</span>
</pre>

Posted in .NET, C#, software development | Tagged , , | Leave a comment

Test Driven Development

Test Driven Development is technique for software development that places the testing activities along with the other development activities, it makes them a part of the code and routine of the developer and it is of great help to catch programming and logic errors quickly, minimizing the effect and cost of them.

By using Test Driven Development the developer is committing to follow a pattern, some people refer to this pattern a red/green/refactor pattern. Where Red means a failed test and green a test that passed. The pattern includes the following steps:

  1. Before implementing new functionality, write a test for what you will be creating and test the behavior that is expected. The test will fail since the functionality is yet to be implemented.
  2. Implement the code until the behavior you are expecting is met. This will assure you that both the test and implementation code are in concordance and correct. It does not matters if the code is not up to the standards set by you or your project.
  3. Refactor and polish the code so that it achieves the standards specified on your project and by using the related test (or tests) you can be sure that you are not breaking anything.

Creating tests for your functionality and making sure they succeed has the benefit of relieving stress, giving you the piece of mind that the project’s progress is going in the right direction. Writing the tests first helps you keep focused and limit the scope of the work you are doing, you should not add any code that it is not required to make a test succeed. Writing the tests before anything is implemented also helps you get into rhythm, if you are not coding then you should be writing new tests, if there is a test that fails you should work on making it succeed, if you just made a test succeed you should be refactoring the code it made it succeed.

Unit Tests

Unit tests are so called because they test one unit of code, if the same unit (for example a method) needs just one test or a hundred it makes no difference. It is recommendable to avoid as reasonable possible to create tests that depend on a chain of processes and modules since this may misdirect and increase the scope for the search of an error. Unit test that depend on external services (web services, database connections, etc.) may run slow and present a series of problems not related to the code it is being tested, using mock objects and classes to represent these services is recommended on these cases.

Integration Tests

The integration tests come after the Unit Tests, they test grouping of units, assemblages, representing major design items possibly involving multiple modules that have already been unit tested. The test cases are design in a such a way as to verify that the modules interact correctly.

There are many other types of test that exists and that we use at Viaro, although lately we have been doing a lot of these for our .NET customers.

Posted in software development | Tagged , , | Leave a comment