@Test
public void testCreateSecurityRoleFunction()
{

    // Create a security role to function mapping key.
    SecurityRoleFunctionKey securityRoleFunctionKey = new SecurityRoleFunctionKey(SECURITY_ROLE, SECURITY_FUNCTION);
    // Create a security role to function mapping create request.
    SecurityRoleFunctionCreateRequest securityRoleFunctionCreateRequest = new SecurityRoleFunctionCreateRequest(securityRoleFunctionKey);
    // Create a security role entity.
    SecurityRoleEntity securityRoleEntity = new SecurityRoleEntity();
    securityRoleEntity.setCode(SECURITY_ROLE);
    // Create a security function entity.
    SecurityFunctionEntity securityFunctionEntity = new SecurityFunctionEntity();
    securityFunctionEntity.setCode(SECURITY_FUNCTION);
    // Mock the external calls.
    when(securityRoleFunctionDao.getSecurityRoleFunctionByKey(securityRoleFunctionKey)).thenReturn(null);
    when(securityRoleDaoHelper.getSecurityRoleEntity(SECURITY_ROLE)).thenReturn(securityRoleEntity);
    when(securityFunctionDaoHelper.getSecurityFunctionEntity(SECURITY_FUNCTION)).thenReturn(securityFunctionEntity);
    doAnswer(new Answer<Void>()
    {
        public Void answer(InvocationOnMock invocation)
        {
            // Get the security role to function mapping entity and set its primary key.
            SecurityRoleFunctionEntity securityRoleFunctionEntity = (SecurityRoleFunctionEntity) invocation.getArguments()[0];
            securityRoleFunctionEntity.setId(ID);
            return null;
        }
    }).when(securityRoleFunctionDao).saveAndRefresh(any(SecurityRoleFunctionEntity.class));
    // Call the method under test.
    SecurityRoleFunction result = securityRoleFunctionService.createSecurityRoleFunction(securityRoleFunctionCreateRequest);
    // Validate the results.
    assertEquals(new SecurityRoleFunction(result.getId(), securityRoleFunctionKey), result);
    // Verify the external calls.
    verify(securityRoleFunctionHelper).validateAndTrimSecurityRoleFunctionCreateRequest(securityRoleFunctionCreateRequest);
    verify(securityRoleFunctionDao).getSecurityRoleFunctionByKey(securityRoleFunctionKey);
    verify(securityRoleDaoHelper).getSecurityRoleEntity(SECURITY_ROLE);
    verify(securityFunctionDaoHelper).getSecurityFunctionEntity(SECURITY_FUNCTION);
    verify(securityRoleFunctionDao).saveAndRefresh(any(SecurityRoleFunctionEntity.class));
    verifyNoMoreInteractionsHelper();
}