Wednesday 17 December 2014

Adding new user in Openshift Origin

Problem: Adding a new user account in OpenShift origin

Solution: Login to openshift machine as root user and go to command prompt. Run the command htpasswd

FYI- The same command can be used to update user password.
Example: Adding mario user
 
[root@broker ~]# htpasswd /etc/openshift/htpasswd mario
New password:
Re-type new password:
Adding password for user mario

In vi Up Arrow and Down Arrow is showing special charchters- Ubuntu

Problem: While using vi editor in ubuntu when i entered in to instert mode using i command. Using up and down arrows is showing special characters instead of navigating to up and down.

Solution:
 Create a file named ".vimrc" under your home directory and add the content "set nocompatible".

Example:



 
mario@ubuntu:~$ touch .vimrc
mario@ubuntu:~$ pwd
/home/mario
mario@ubuntu:~$ echo "set nocompatible" > .vimrc

Logging all the http request and response that is coming to Jetty Server

Problem: Logging all the http request and response that is coming to Jetty Server

Prerequisite:  Configure logback as your logging frame work

Solution:
Refer variables fullRequest and fullResponse in your  logback-access.xml

Snippet
 
 
        ${OPENSHIFT_SPRINGJETTYONODE_DIR}/logs/access.log
        
            logs/access-%i.log.gz
            1
            3
        

        
            20MB
        
        
            %fullRequest%n%n%fullResponse
        
    

Wednesday 17 September 2014

Mockito Tutorial -Part2 . Spring and Mockito Integration.

In the below example we use Calcuator Obect which computes the area of Rectangle using Rectangle Objects.

Frame works used

  • Spring -Dependency Injection
  • Mockito- Mocking java Obejcts
  • JUnit- Unit Test frame work.


CalculatorTestWithSpringAndMockito.java 
 
package test.com.lac.tut.mockito;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.lac.tut.mockito.Calculator;
import com.lac.tut.mockito.Rectangle;

@ContextConfiguration(locations = { "classpath:/beans.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
public class CalculatorTestWithSpringAndMockito {

 @Mock
 Rectangle rectangle;

 @InjectMocks
 @Autowired
 Calculator calculator;

 @Before
 public void create() {
  initMocks(this);// Initialize this mock objects
  when(rectangle.getLength()).thenReturn(10);
  when(rectangle.getBreadth()).thenReturn(40);
 }

 @Test
 public void test() {
  assertEquals(calculator.getArea(), 400);
 }

}

Explanation: In the above example the mocked rectangle object is injected into calculator object. @InjectMocks annotation of Mockito inject mocked rectangle object into calculator bean. The annotation @RunWith makes the class is runned with SpringJUnit4ClassRunner. The annotation ContextConfiguration specifies the path of spring bean definition.


Calculator.java
 
package com.lac.tut.mockito;

public class Calculator {
 private Rectangle rectangle;

 public Rectangle getRectangle() {
  return rectangle;
 }

 public void setRectangle(Rectangle rectangle) {
  this.rectangle = rectangle;
 }

 public int getArea() {
  return rectangle.getLength() * rectangle.getBreadth();
 }

}


Rectangle.java
 
package com.lac.tut.mockito;

public class Rectangle {
 public int length;
 public int breadth;

 public int getLength() {
  return length;
 }

 public void setLength(int length) {
  this.length = length;
 }

 public int getBreadth() {
  return breadth;
 }

 public void setBreadth(int breadth) {
  this.breadth = breadth;
 }

}


beans.xml
 


 
 
 
  
 
 
 
  
  
 
 



Download the source code of above tutorial at : http://www.luckyacademy.com/downloads.html




Tuesday 16 September 2014

Mockito Tutorial -Part1


  • What is Mockito?
    • Mockito is mocking frame work for Java Objects.
  • Why do we need to Mock Objects?
    • When an unit of code depended upon object that will not be available during test or development. We can create a mock object of it.
  • What is Mock Object?
    • A mock object is a dummy implementation for an interface or a class in which you define the output of certain method calls
Lets try to Mock a object of  Math class that was not avilable during development.

Math.java
package com.lac.tut.mockito;

/**
 * Performs various Math Operations like addition,multiplication and division ..etc 
 *
 */
public class Math {
 // addition of two numbers
 public int add(int a, int b) {
  return a + b;
 }

 // Multiply two numbers
 public int mul(int a, int b) {
  return a * b;
 }

 // Division of two numbers
 public int div(int a, int b) {
  return a / b;
 }
 
 //Return a prime number
 public int primeNumber(){
  return 5;
 }
}
Explanation: The above Math class has addition,multiplication,division as functions.

MathAddTestWithOutMock.java is a unit test cases that test add method on Math Object
 
package test.com.lac.tut.mockito;

import static org.junit.Assert.assertSame;

import org.junit.Before;
import org.junit.Test;

import com.lac.tut.mockito.Math;

/**
 * Test the add method of Math object
 */
public class MathAddTestWithOutMock {
 Math mathObj; 

 @Before
 /**
  * Create Math object before you use them
  */
 public void create() {
  mathObj = new Math();//create a math object
 }

 @Test
 public void test() {
  assertSame(3, mathObj.add(1, 2)); // Assert that math object return 3
 }

}

Explanation: In the above example we try to test add method. Often In the Test Driven Development we will not have Math class before we write tests around it. The approach we have to take is Mock the Math object and write the tests using Mocked object.


MathMockAddTest.java Tests the add method of Math class by mocking Math Object
 
package test.com.lac.tut.mockito;

import static org.junit.Assert.assertSame;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.junit.Before;
import org.junit.Test;

import com.lac.tut.mockito.Math;

/**
 * Test the add method of Math object
 */
public class MathMockAddTest {
 Math mathObj; //The object that needs to mocked
 
 @Before
 /**
  * Create mock object before you use them
  */
 public void create(){
  mathObj= mock(Math.class); //Create Math mock Object
  when(mathObj.add(1, 2)).thenReturn(3); // Configure it to return 3 when arguments passed are 1,2
 }
 
 @Test
 public void test() {
  assertSame(3, mathObj.add(1,2)); //Assert that math object return 3
 }

}

Explanation: In the above code the static method mock is going to create mock object of Math class. The static method when defines the behavior of add method. (i.e) When some one called add method with arguments 1 and 2 return 3 as output. Note: Please bear in mind. MathMockAddTest is never creating a Math concrete object it is only creating Math mock object.


MathMockAddTestWithAnnotation.java Creates a mock object with Mockito Annotations
 
package test.com.lac.tut.mockito;

import static org.junit.Assert.assertSame;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;

import com.lac.tut.mockito.Math;

/**
* Test the add method of Math object with  Mockito annotation
*/
public class MathMockAddTestWithAnnotation {
 @Mock
 Math mathObj;
 
 @Before
 /**
  * Create mock object before you use them
  */
 public void create(){
  initMocks(this);// Initialize this mock objects
  when(mathObj.add(1, 2)).thenReturn(3); // Configure it to return 3 when arguments passed are 1,2
 }
 
 @Test
 public void test() {
  assertSame(3, mathObj.add(1,2));//Assert that math object return 3
 }

}

Explanation:In the above example @Mock annotation is used to mock Math object. The static method initMocks initializes the mock objects in current class. Rest the behavior defined is same.


MathMockMulTest.java Test the multiplication method in Math class
 
package test.com.lac.tut.mockito;

import static org.junit.Assert.assertSame;
import static org.mockito.Mockito.*;

import org.junit.Before;
import org.junit.Test;

import com.lac.tut.mockito.Math;

/**
 * Test multiply function
 */
public class MathMockMulTest {
 Math mathObj;
 
 @Before
 public void create(){
  mathObj= mock(Math.class);
  when(mathObj.mul(anyInt(), eq(0))).thenReturn(0); //Multiply any number with zero. The function should return zero
 }
 
 @Test
 /**
  * Test the Multiply function in math object return zero if one of the argument is zero
  */
 public void test() {
  assertSame(mathObj.mul(1,0),0);
  assertSame(mathObj.mul(3,0),0);
 }

}

Explanation:In the above example the static method anyInt accepts any argument as input and static method eq checks that the argument is zero.


MathMockDivTestWithException test the exception returned by div method in Math class.
 
package test.com.lac.tut.mockito;

import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.junit.Before;
import org.junit.Test;

import com.lac.tut.mockito.Math;

/**
 * Test division method of Math class
 */
public class MathMockDivTestWithException {
 Math mathObj;
 
 @Before
 public void create(){
  mathObj= mock(Math.class); //Create Math Object
  when(mathObj.div(anyInt(), eq(0))).thenThrow(new ArithmeticException()); // Configure it to return exception when denominator is zero
 }
 
 @Test(expected=ArithmeticException.class) //expect the method throws ArithmeticException
 public void test() {
  mathObj.div(1,0); //call the div and expect to return ArithmeticException
 }

}

Explanation: The static method thenThrow should throw exception when denominator is zero.


MathMockPrimeNumTestWhichIsAMethodWithOutParameters Test a primeNumber method that doesn't take any argument
 
package test.com.lac.tut.mockito;

import static org.junit.Assert.assertSame;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.junit.Before;
import org.junit.Test;

import com.lac.tut.mockito.Math;

//Test a method that doesn't take any argument
public class MathMockPrimeNumTestWhichIsAMethodWithOutParameters{
 Math mathObj;
 
 @Before
 public void create(){
  mathObj= mock(Math.class); //Create Math Object
  when(mathObj.primeNumber()).thenReturn(5); // Configure it to return 5 as prime number
 }
 
 @Test
 public void test() {
  assertSame(5, mathObj.primeNumber());
 }

}
Explanation: In the above example when method primeNumber() is called the mock object will return 5


MathMockVerfiyTest Some times it is important to verify whether certain method on the object is called or called how many times. In such case we can use static method verify
 
package test.com.lac.tut.mockito;

import static org.junit.Assert.assertSame;
import static org.mockito.Mockito.*;

import org.junit.Before;
import org.junit.Test;

import com.lac.tut.mockito.Math;


public class MathMockVerfiyTest {
 Math mathObj;
 
 @Before
 public void create(){
  mathObj= mock(Math.class);
  when(mathObj.add(1,2)).thenReturn(3); 
 }
 
 @Test
 public void test() {
  //call the add function with 1,2 as arguments
  assertSame(mathObj.add(1,2),3);
  
  //Verify whether add method is tested with arguments 1,2 
  verify(mathObj).add(eq(1), eq(2));
  
  //Verify whether add method is called only once
  verify(mathObj,times(1)).add(1,2);
 }

}
Explanation: In the above example we try to assert that add method is called with arguments 1,2 and it is called only once. That can be achieved using verify method.


Download the source code of the above tutorial at http://www.luckyacademy.com/downloads.html



Monday 23 June 2014

Find inside a file or directory

Problem: Find a word inside a file or directory

Solution: grep command can be used to find a word inside a file.


[root@iravath search]# grep -rnw '.' -e "Apple"
./names.txt:3:Apple
 
The above command search for word "Apple" inside a file. The word "Apple" was found in names.txt at line number 3.

Friday 8 November 2013