Monday, July 18, 2011

Testing and Groovin'

I've come to the conclusion that writing unit tests in Java is such a pain. Specially when there are others languages that are better suited for writing test. Lately, I've been writing all my test using Groovy. I have been using it for writing unit tests and creating mock objects. I am using EasyMock for most of my programming time, but I recently found out that Groovy has its own mock object framework. Here are some example from the site. I think that they are pretty compelling.


Domain Layer:




Service Layer:







This is the class that we are going to test. As you noticed, we are using the ExchangeRateService class.


Tests:

Here we will need to mock the ExchangeRateService. We are going to use a few example that Groovy can provide us:
- You can often get away with simple maps or closures to build your custom mocks
- By using maps or expandos, we can incorporate desired behaviour of a collaborator very easily as shown here:



This is an example of using it with closure:



If we need the full power of a dynamic mocking framework, Groovy has a built-in framework which makes use of meta-programming to define the behaviour of the service. An example is shown here:

As I mentioned, just like any other mocking framework, Groovy has its own framework build-in: "MockFor" which defines the behavior of the collaborator.


Let me know how it goes and thanks for reading!

Wednesday, July 6, 2011

How to sell your idea to your boss - HBR article

I had the fortune of working with some great CIOs and CTOs. Some of them where very technical savvy, while others where very business oriented. The best technical managers, in my opinion, are those that can align business with technology. Unfortunately, I worked for some really bad managers. Working for someone like that its very challenging. The worse is when he manages a technical team (programmers, sysadmins, etc) and claim to have some technical expertise when he clearly does not. This type of manager is very dangerous when he starts weighting on architectural decisions, programming preferences, and others. I worked for just this type of manager once and I learned a lot. The biggest challenge for me was when I had to pitch an idea - technical methodology (Scrum, Kaban, Agile) or having to learn a different language (Ruby on Rails, Groovy, or JRuby). HBR wrote about a similar scenerio: How to sell your idea to your boss. Here is what the article talks about:

Main point:
  • You are a middle manager
  • You see the big picture but your boss does not. He is just focused on the here and now
  • What do you do if your boss wants you to stay in your place?

First and foremost, do your job: Make certain that you do everything you are asked to do. Onced you have stablished yourself as a credible performer, there are three things you can do to give your big idea a better chance to succeed:

1 - Align you initiatives with the corporate objectives:
  • Whatever you propose must complement your company's strategic direction.
  • Build a business case for your idea by showing how your idea does not conflict with current priorities, but in fact supports them by planning for the future

2 - Work Through Your Boss:
  • Do not go around your boss
  • Walking through your plan and get his feedback
  • Incorporate his ideas if they are viable
  • Find ways for your boss to get some credit

3 - Build coalitions:
Things get done in organizations because people pull together to get the work done. The same goes for driving initiatives. Enlist the support of peers to help you get your idea of the ground. Leverage your customers; these are the people who will benefit from your idea. Frame your idea around serving their needs more comprehensively.

While I have seen these steps work, I have to admit that some bosses cannot be led. These are bosses who are typically very insecure in their positions and feel that creativity from below is a thread to their powers. Managers who step on others do not deserve to be in positions of authority, but as long as they wield authority as a weapon, their direct reports are best advised to keep their heads own and do as hey are told - that is, until they can find another outlet or opportunity for their talents and skills.

Management, when it works, is a reciprocal process. Bosses set direction, set objectives, and follow up to ensure that work is completed on time and on budget. Employees must ensure that they do what their boss asks then to do. But managers and employees work best when they understand both that good ideas can come from anywhere in the organization and that companies can capitalize on them are the ones that will succeed. Success depends upon those who not only think creatively but also have the skills to put their ideas into action.

Another key element in pitching new ideas is how they are framed. Nobel Prize - winning psychologist Daniel Kahneman and his collegues Amos Tversky demostrated that when choices are framed as gains versus no gains, decision makers tend to be risk averse. When choices are are framed as losses versus no losses, however, decisions makers tend to be risk seeking. So if you pitch idea by emphasizing what the company can gain (new customers, more profits, higher market share, etc) as a result of implementing the new idea, you may actually be making it less likely that the idea will be accepted. If you pitch it by emphasizing what the company will avoid loosing (ground to the competition, lower market share, etc) by implementing the new idea, however, you may find your boss more receptive to the idea.

Friday, July 1, 2011

Automation, monitor with Nagios, and passive checks


The first thing that we did during my previous company (a start-up) was to automate everything. Granted, there are some stuff that you cannot automate for whatever reasons, but you should try to automate as much as possible because of scalability. Once an automated process is setup, then the only thing pending is to make sure if it's running fine and if there are any warnings, if it is not running, etc. Nagios is a great tool for monitoring these type of things. There is almost an infinite amount of things that you can do with it. Previously, it was a pain to install, but lately, the installation is very straight forward.

In Nagios, there are two types of checks: active checks and passive checks. Active checks is a constant check - very much like heartbeat in high-availability architectures. The example that I always tell managers is a constant "are you OK?" conversation between the Nagios server and its clients. This is great if you need to have consistent check on a particular process. For example, uptime, drive space, CPU load, memory usage are processes that should be monitored every "n" time (every 5 minutes). For those process that are asynchronous and should be triggered by a particular event, then we use passive checks. In my case I had the following requirements:
  • Client(s) can provide me a file at anytime between 9 am - 6:30 pm
  • The file should contain a specific format
  • If the format is not valid, we need to contact the client
  • If the format is correct, persist it into the DB
  • Once it is in the DB, then we launch another process and performed some statistical calculations
Passive checks can be for a host or a particular service. In this example, I will covered the steps to configure a service.
  1. Install Nagios
  2. Configure a particular service for this component
  3. Create an external application that checks the state of the application (in my case I used Groovy and shell script - groovysh)
  4. Write to an external command file

This is the picture of the process for Nagios:


There are a few configurations that needs to be enabled to have the passive checks work in the nagios configuration (/usr/local/nagios/etc/nagios.cfg). Make sure that the followings are set to "1" (enable):
- accept_passive_service_checks=1
- check_external_commands=1

There should also be a "command_file" with some type of path. For example: command_file=/usr/local/nagios/var/rw/nagios.cmd

Then, configure the service check and enable the passive_checks_enabled. This will be done in the host configuration file (localhost in my case):
vi /usr/local/nagios/etc/objects/localhost.cfg



Restart Nagios:
sudo -i service nagios restart

You should be able to see the "asynch_client_files" service in the localhost. The next step would be to check if the passive check is working via the command file. The way that Nagios knows about any passive events is by writing into a file (nagios.cmd). The following parameters are needed:

where...

timestamp is the time in time_t format (seconds since the UNIX epoch) that the service check was perfomed (or submitted). Please note the single space after the right bracket.
host_name is the short name of the host associated with the service in the service definition
svc_description is the description of the service as specified in the service definition
return_code is the return code of the check (0=OK, 1=WARNING, 2=CRITICAL, 3=UNKNOWN)
plugin_output is the text output of the service check (i.e. the plugin output)

Temporarly, you can get the timestamp by doing the following command in linux: date +%s

To execute the test, we do the following in the terminal screen:
In this case, I had to login as root:

If you go back to the Nagios site: http://localhost/nagios you should be able to see that the new change has been done and now the service shows a status of "OK" with the comment "The security master is up to date"

The only thing missing is configuring an application that sets the status of the page. You can use the language of your choice and use crontab or something else to execute the application.