Vikas Gupta: Software architect

Capturing methods arguments with Mockito

Posted by Vikas Gupta on September 22, 2012

In my project, we developed an API for searching on the lines of Hibernate Restrictions API. It was for Solr search. A sample code looked something like

 public SearchResults searchApprovedRecipes(String searchText, Integer pageNum) {
 SolrSearchServiceImpl.PageCounter pageCounter = new SolrSearchServiceImpl.PageCounter(pageNum);

 SearchCriteria searchCriteria = new SearchCriteria(SearchCriteria.SearchType.RECIPE);
 searchCriteria.add(SearchCriteria.SearchField.NAME, searchText);
 searchCriteria.add(SearchCriteria.SearchField.STATUS, WorkflowStatus.APPROVED.name(), WorkflowStatus.EDITED.name());
 searchCriteria.setMaxResults(pageCounter.getMaxResults());
 searchCriteria.setStart(pageCounter.getStart());

 SearchResults searchResults = getRestTemplate().postForObject(getSearchUrl(), searchCriteria, SearchResults.class);

 return searchResults;

Now, if you talk about unit testing, there is nothing much to test, except mocking calls to rest template. The main purpose of this method is to build SearchCriteria. So, if somehow, we can mock SearchCriteria, then we could test that.

Mockito comes with a concept of capturing the arguments passed to the mocked calls (in this case, call to RestTemplate would be mocked), using ArgumentCaptor, which captures argument values for further assertion.

Following is the unit test case of the above method.

@Test
 public void testSearchApprovedRecipes() {
 ArgumentCaptor<SearchCriteria> searchCriteriaArgumentCaptor = ArgumentCaptor.forClass(SearchCriteria.class);
 String test = "test";
 solrSearchService.searchApprovedRecipes(test, 1);
 verify(restTemplate, times(1)).postForObject(eq(SEARCH_URL), searchCriteriaArgumentCaptor.capture(), eq(SearchResults.class));

SearchCriteria searchCriteria = searchCriteriaArgumentCaptor.getValue();
 Assert.assertEquals(2, searchCriteria.getSearchFields().size());
 Assert.assertEquals(SearchCriteria.SearchType.RECIPE, searchCriteria.getSearchType());
 Assert.assertEquals(test, searchCriteria.get(SearchCriteria.SearchField.NAME).get(0));
 Assert.assertTrue(searchCriteria.get(SearchCriteria.SearchField.STATUS).contains( WorkflowStatus.APPROVED.name()));
 Assert.assertTrue(searchCriteria.get(SearchCriteria.SearchField.STATUS).contains( WorkflowStatus.EDITED.name()));
 }

In the above code, you can see that first we created a ArgumentCaptor for the SearchCriteria class, and then, while mocking call to the RestTemplate, instead of passing the SearchCriteria object, we passed the ArgumentCaptor and all the changes made to the SearchCriteria inside the method are captured in the ArgumentCaptor. Now, once the method calls return, we can have assert statements against the ArgumentCaptor. This way, we can test the SearchCriteria.

Posted in TDD | Leave a Comment »

Cleared Cloudera Certification for Apache Hadoop

Posted by Vikas Gupta on September 2, 2012

Today, I cleared Cloudera Certification for Apache Hadoop. I scored 90%. In this blog, I would like to share my preparation and test experience.

Preparation Tips

I started learning Hadoop in June, although I had been procrastinating it for a long time. I bought Hadoop: The Definitive Guide (HTDG) and started reading it. I must admit that I found this book at too much to handle and got a bit distracted by the details mentioned in this book. So, I searched another book and found Hadoop in Action. I must say that hands on approach of this book got me started and I enjoyed trying out the examples provided with this book. I studied first 6 chapters of this book and started feeling confident about writing Hadoop code.

Although, I could write hadoop programs, being an detailed oriented person, I was missing the details provided in the HTDG. So, I picked that up once again and started reading it with zeal and was able to follow the details this time and actually started enjoying the intricacies of the Hadoop framework mentioned in this book. For certification, I read first 8 chapters of this book. I recommend everyone to read Chapeter 11, Pig and Chapter 14, Case Studies to get better equipped and pratical insights about Hadoop.

So, in the end, I would say, for the certification reading the Hadoop: The Definitive guide is enough if you understand the concepts mentioned in this book. Opionally, one may buy Hadoop in Action, as it provides better code samples specially for Joins and Chaining jobs.

Examination Experience

I gave the examination at NIIT Centre at Preet Vihar as it was easily accessible from my home. After the initial setup, which included creating my id and login on the pearson website, I was directed to the computer on which my exam was setup.

As the exam started and I clicked Next, I was expecting some instructions to appear before the exam. But, to my surpise, I got the first question. Just for the future test-takers benefit, I would like to mention that there is a Help button towards left hand side on the bottom of the screen, which can be referred for any help. In this test, you can mark a question for review and also go back and forth and change answers.

Now, about the exam, I would say that the exam was good and some of the questions have fairly close answers and if one has not prepared well/or is not thorough with the concepts, then it is not difficult to falter in this exam. In short, make sure your concepts are thorough, which can be done, either by having practical experience, or as in my case, studying thoroughly and learning concepts of Hadoop.

Once, I was done with the exam, I clicked finish and soon the score flashed. I was happy to see the score, though, I felt, I could have done better.

Some useful links

If you have any questions about preparing for certification or need any other help in Hadoop, you can post a comment or directly email me at techvikas79@gmail.com

My certificate

For those who want to know how the cloudera certificate looks like, this is the certificate CCDAH certificate.

 

Posted in Certification, Hadoop | Tagged: , , | 24 Comments »

Intalling VMware Player on Ubuntu 12.04

Posted by Vikas Gupta on August 18, 2012

In my pursuit to learn Hadoop, I decided to use one of the Hadoop virtual machine images provided by Cloudera before actually installing Hadoop on my Ubuntu 12.04. Cloudera provides virtual machines for VMWare, KVM and VirtualBox. For more information about Cloudera Hadoop virtual  machines, please visit Cloudera Demo VM . I decided to go with VMWare image. This required me to install VMWare player. In this blog, you will find step by step instructions to install VMWare player and solution to the problems faced during this process.

1. While looking for VMWare Player(vmplayer) installer, i found that vmplayer installer is not provided by the repositories, you have to instead download it from VMware website. While downloading please make sure that you download the latest and correct version for your architecture. That is, if you’re running the 32-bit version of Ubuntu, get the 32-bit version, and if you’re running the 64-bit version of Ubuntu, get the 64-bit version. To find out the version of Ubuntu running on your computer, type in uname -m  from the terminal and press enter. If the result is x86_64, you’re running the 64-bit version of Ubuntu. If it says i686, you’re running the 32-bit version of Ubuntu.

2. The downloaded file is VMware-Player-4.0.3-703057.x86_64.txt for the 64-bit version. The download will vary depending upon Ubuntu version(32-bit vs 64-bit) and version of the vmplayer. Before installing vmplayer, check the file permissions of the downloaded file. You need to have execute permissions on the file. If you do not have execute permissions on the file, please execute the following command to change the mode of the file to have execute permissions.

chmod +x VMware-Player-4.0.3-703057.x86_64.txt

3. The installer may need to build and install kernel modules, so it’s advisable to make sure you have the necessary packages to facilitate this. Run uname -r. The output should end in genericgeneric-pae,server, or virtual. If it doesn’t end in generic, replace generic below with whatever it does end in.

sudo apt-get update
sudo apt-get install build-essential linux-headers-generic

4. Now run the installer. You have to run it as root, so use sudo:

sudo ./VMware-Player-4.0.3-703057.x86_64.txt

The rest of the installation process is guided and graphical. While you had to run the installer as root, you do not generally need to (and should not generally) run vmplayer as root after you’ve installed it. Occasionally, after an update to your kernel or vmplayer, vmplayer may ask you to allow it to build and install new kernel modules as root.

5. While trying the run the vmplayer, i got an error that vmplayer is not able to start the network services. After googling on the net, I found that there is a patch for that. To install that patch, please execute the following commands

wget http://webupd8.googlecode.com/files/vmware802fixlinux320.tar.gz
tar -xvf vmware802fixlinux320.tar.gz
sudo ~/vmware802fixlinux320/patch-modules_3.2.0.sh

My vmplayer ran fine after the following steps, I hope after following the above steps, you also have a problem free installing of the vmplayer.

Posted in Uncategorized | Tagged: , , | Leave a Comment »

AspectJ PointCut Expressions

Posted by Vikas Gupta on April 15, 2010

The intention of this blog is to explain AspectJ Pointcut Expressions in Spring Applications. In Aspect Oriented Programming, a pointcut is a set of joinpoints. A joinpoint is a point in program execution where you can add additional behavior. Spring applications only support method based joinpoints. So, you can use AspectJ pointcut expressions to define method pointcuts in Spring 2.x applications. Lets discuss some AspectJ pointcut expressions patterns.

Method Signature Patterns

The most typical pointcut expressions are used to match a number of methods by their signatures. A common method based pointcut expression is something like

expression(<method scope> <return type> <fully qualified class name>.*(parametes))
  1. method scope: Advice will be applied to all the methods having this scope. For e.g., public, private, etc. Please note that Spring AOP only supports advising public methods.
  2. return type: Advice will be applied to all the methods having this return type.
  3. fully qualified class name: Advice will be applied to all the methods of this type. If the class and advice are in the same package then package name is not required
  4. parameters: You can also filter the method names based on the types. Two dots(..) means any number and type of parameters.

Read the rest of this entry »

Posted in AspectJ, Spring | Tagged: , | 17 Comments »

Domain Driven Design : Making Implicit concepts Explicit

Posted by Vikas Gupta on January 6, 2010

In the previous post, I discussed about how to query the domain objects. That post completes the issues with domain object lifecycle management. In this post, I will discuss how to make implicit concepts explicit in order to make domain model more expressive

Sometimes small details of the design get lost in the code. We tend to consider small business rules and constraints as implicit and these details are not given enough importance as they should be given.They are often coded as a bunch of if-else blocks in the code and this makes the identification of these business rules difficult and more importantly makes design less effective. Let see some of the concepts/design patterns which can be used to make these implicit concepts more explicit. Read the rest of this entry »

Posted in DDD, Design Patterns, EA Patterns | Tagged: , , , | Leave a Comment »

Domain Driven Design : Querying Domain Objects

Posted by Vikas Gupta on December 30, 2009

After having discussed about object creation in the previous post, I would like to discuss the challenges of querying objects in a domain model.

There is a well designed domain model with a rich set of associations between objects. How to get to a particular entity or value object inside the domain model? Since the domain model is well connected, if you have a reference to a root entity, you can get to the other members to which this root is associated. But, how to get to this root? Well, we can have a global root which contains the collections of all the root of the entities. For example, in a sales order application, you can have a global root containing the collection of order and customer objects.

One drawback of the above solution is that it will perform badly in a distributed system. For example, when a client asks the global root to return the list of customers, and there are millions of customers, we have to do a database query and it will return millions of records. This will cause a lot of data transfer on the network. With multiple clients, this solutions would not work at all.

In most situations, you would need only a subset of customers which meet a particular set of conditions. So, what are the options that we have to return a subset of customers to the client in a way which performs well and does swamp the domain model with the complexity of the query logic. The options that we have are Read the rest of this entry »

Posted in DDD, Design Patterns, EA Patterns | Tagged: , , , | 4 Comments »

Domain Driven Design : Creating Domain Objects

Posted by Vikas Gupta on December 21, 2009

In one of the previous posts, I discussed about Aggregates and their design considerations. In this part of the series, I will discuss the issues in object creation with domain objects.

The normal way of creating an object by it’s client is via it’s public constructor. This works well with a simple objects. But, when complex objects like Aggregates are concerned, their construction can be complex and the process of creation can become overwhelming for an object to handle. As part of it’s construction, an aggregate might have to

  • create local entities within an aggregate.
  • check the invariant logic before adding a local entity.

Object creation has nothing to do with the domain. Moreover, with complex object creation, an object tends break the Single Responsibility Principle(SRP). So, it is recommended to separate the object creation logic from the object. In Domain Driven Design(DDD), a program element whose primarily responsibility is to create complex domain objects and aggregates is called a Factory. Ideally, a factory should create the product atomically with all the safety checks, that is, with the all the invariants applied appropriately. Read the rest of this entry »

Posted in DDD, Design Patterns, EA Patterns | Tagged: , , , | 2 Comments »

Domain Driven Design : Aggregates

Posted by Vikas Gupta on December 14, 2009

Every object has a lifecycle. Some objects take birth and die in a single method while others have a more complex and longer life. Typically, Domain Objects falls into the second category. The lifecycle of a domain object is shown below

As the lifecycle of a domain model is complex, it is advisable to separate the domain logic from the lifecycle management logic. Furthermore, sometimes, grouping related objects to manage them as an entity is easier than managing each of them separately. Domain Driven Design(DDD) suggests to group related objects as Aggregates and to use Factories and Repositories to manage the lifecycle of the object. In this blog, I will discuss about the Aggregates and their design considerations. Read the rest of this entry »

Posted in DDD, Design Patterns, EA Patterns | Tagged: , , | 2 Comments »

Domain Driven Design : Expressing Concepts

Posted by Vikas Gupta on December 8, 2009

Object oriented analysis and design(OOAD) is essentially an activity to model a system as an interaction between objects. Each object, which is an instance of a class and has state and behaviour, represents an entity with in the system. These entities invoke other entities to perform certain activities and are often associated with each other to model a system. Domain driven design(DDD) recommends guidelines which tend to optimize/enhance the process of identifying entities and defining relationship between them. It advocates to classify objects as Entities and Value objects based on certain criteria, to model Services, which abstract behaviour which is not specific to a particular entity, to divide application into Modules, and to minimize and constrain Associations between objects. In this blog, I will discuss these recommendations.
Read the rest of this entry »

Posted in DDD, Design Patterns, EA Patterns | Tagged: , , , | 2 Comments »

Domain Driven Design : An Introduction

Posted by Vikas Gupta on December 1, 2009

Introduction

The primary purpose of most software projects is to add value to the customer’s business. This is done by abstracting the complex web of thoughts inside a human mind in the form of a software program. Most software projects cater to a particular domain, and in many software projects, the domain of a software project is not technical. Unfortunately, majority of the technical people do not give enough importance to the domain of the software, and hence, they lack the required domain knowledge. This lack of domain knowledge leads to a situation where What(to do?) takes over Why(to do?). Generally, in such a scenario, it might be possible to deliver a usable product, but the primary purpose of adding business value is often neglected.

The above mentioned situation is quite common and there is a continual influx of thoughts, which intend to provide a solution, by various experts. Eric Evans is one of such experts, who based on his experience, gained by working on complex projects, epitomized his findings in the form of tips, suggestions, patterns and guidelines, and coined a term Domain Driven Design, better known as DDD. In this blog, I intend to present various aspects of DDD and how it suggests to tackle the complexity of the domain.
Read the rest of this entry »

Posted in DDD, Design Patterns, EA Patterns | Tagged: , , , | 3 Comments »