Tuesday, February 11, 2014

How to setup of WSO2 Identity Server on windows

What is WSO2 Identity Server ?

WSO2 Identity Server is an enterprise-ready, fully-open source, lean, component-based solution for facilitating security and provides secure identity management for enterprise web applications, services, and APIs by managing identity and entitlements of the user securely and efficiently.
It helps improve customer experience by reducing identity provisioning time, guaranteeing secure online interactions, and delivering a reduced single sign-on environment. WSO2 Identity Server decreases identity management, entitlement management, and administration burden by including role-based access control (RBAC) convention, fine-grained policy-based access control, and SSO bridging.

Downloading the product :
  • In your Web browser, go to http://wso2.com/products/identity-server.
  • If you are a new user downloading WSO2 products for the first time, register and log in.
  • Once you are logged in, click the Binary button in the upper right corner of the page.

The binary distribution contains the Carbon binary files for both Windows and Linux operating systems, compressed into a single ZIP file.

Installing the Identity Server :

Before installing, You must set your JAVA_HOME environment variable.
  • Download the latest version of the Identity Server as described above
  • Extract the archive file to a dedicated directory for the Identity Server, which will hereafter be referred to as <WSO2_HOME>.

Starting the Server :

To start the server, you need to run the script “wso2server.bat” which is under $WSO2_HOME/bin folder.
Once the server has started, you can see the Management Console by opening a Web browser and typing in the management console's URL. You can check the URL as the last line in the start script's console. Check the screenshot below :






Use “admin” as username and password to sign in to the Management Console.
By default, The session-timeout value is 15 minutes but you can change this in the $WSO2_HOME/repository/conf/tomcat/carbon/WEB-INF/web.xml file as follows:

<session-config>
<session-timeout>15</session-timeout>
</session-config>

Stopping the Server :

To stop the server, press Ctrl+C in the command window or click the Shutdown/Restart link in the navigation pane in the Management Console.


Resources :
http://wso2.com/products/identity-server/

http://docs.wso2.org/display/IS460/WSO2+Identity+Server+Documentation

Tuesday, October 29, 2013

How to substitute dynamic placeholder in properties file

This blog will explain you how can you use replace placeholder with original value in properties file. To achieve this task we will you Java API MessageFormat.

Suppose you have properties file named "welcome.properties" having message :
welcome=Hi {0} , Welcome to {1}
Sample : 

import java.io.File;
import java.io.FileInputStream;
import java.text.MessageFormat;
import java.util.Properties;

/**
 * @author abdul
 *
 */
public class DynamicPlaceholder {

/**
* @param args
* @throws Exception 
*/
public static void main(String[] args) throws Exception {
File propFile = new File("D:\\juno\\Practise\\src\\com\\waheed\\dynamic\\placeholder\\substitution\\welcome.properties");
Properties props = new Properties();
FileInputStream stream=new FileInputStream(propFile);
props.load(stream);
String message = props.getProperty("welcome");
               // Here the {0} and {1} will be substitute with "Waheed" and "India".
String welcome = MessageFormat.format(message, "Waheed","India");
System.out.println("Your message : " + welcome);
}
}

Output : 

Your message : Hi Waheed , Welcome to India

Monday, October 21, 2013

Junit Test with Mockito

Mockito is a testing framework for Java which allows the creation of Test Double objects called "Mock Objects" for use in automated unit testing in conjunction with framework like Junit. 
For more details, Check here.

This blog will show you step by step working of Mockito.

Step 1 : You need mockito-all and Junit jars into you project classpath which you can download it from here.
If you are using maven, add following dependency into your pom.xml file.

<dependency>
            <groupid>org.mockito</groupid>
            <artifactid>mockito-all</artifactid>
            <version>1.9.5</version>
</dependency>
<dependency>
            <groupid>junit</groupid>
            <artifactid>junit</artifactid>
            <version>4.11</version>
</dependency>

Step 2 : Below is the main service method which we are going to test using Mockito.

import java.util.Iterator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Provide methods to work with Employees.
 *
 * @author abdul
 *
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {

    private EmployeeDao employeeDao;

  
    @Override
    @Transactional
    public Employee getEmployee(long employeeId) throws Exception {
        Employee employee = employeeDao.getEmployee(employeeId);
        if(employee == null) throw new Exception("Employee not found");
        return employee;
    }
}

Step 3: Now create the test with Mockito


package com.waheed.spring.hibernate.test;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import com.waheed.spring.hibernate.Employee;
import com.waheed.spring.hibernate.EmployeeDao;
import com.waheed.spring.hibernate.EmployeeServiceImpl;

@RunWith(MockitoJUnitRunner.class)
public class EmployeeTest {
   
    @Mock EmployeeDao mockDao;

    @InjectMocks EmployeeServiceImpl employeeServiceImpl;
   
    private Employee employee;
   
    @Before
    public void setup() {
        employee = new Employee();
        setEmployee();
    }
   
    private void setEmployee() {
        employee.setId(1);
        employee.setGender("M");
        employee.setCountry("INDIA");
        employee.setAboutYou("CRICKEETER");
        employee.setName("Sachin");
    }
   
    @Test
    public void employeeTest() throws Exception {
        when(mockDao.getEmployee(1)).thenReturn(employee);
        Employee emp =employeeServiceImpl.getEmployee(1);
        System.out.println(emp.toString());
       
        //Verify if getEmployee method was invoked on employeeServiceImpl call
        verify(mockDao).getEmployee(1);
        assertEquals("Tesing",emp.getName(),"Sachin");
    }
   
    @Test(expected=Exception.class)
    public void employeeTestFailure() throws Exception {
        when(mockDao.getEmployee(10)).thenReturn(null);
        Employee emp = employeeServiceImpl.getEmployee(10);
       
    }
}
Step 4 : Let me explain in few words, what's going on in my test class. 
  • As I am testing the EmployeeDao so I have created the mocks for EmployeeDao using @Mock Annotation. 
  • @InjectMocks : Use this annotation on the class you are testing. The mock classes are injected into the @InjectMocks class.
  • To create mocks, we need to add @RunWith(MockitoJUnitRunner.class) annotation to the test class or you can use MockitoAnnotations.initMocks(this) in your class in @Before method. 
  • When(...)thenReturn(...) : It specify what should be returned after the particular method was called. 
  • verify() : It ensure that specified conditions are met.
You can download the above source code from here

References:


How TOPT Works: Generating OTPs Without Internet Connection

Introduction Have you ever wondered how authentication apps like RSA Authenticator generate One-Time Passwords (OTPs) without requiring an i...