SecurityRoleFunctionServiceImpl implements SecurityRoleFunctionService { @Override public SecurityRoleFunction createSecurityRoleFunction(SecurityRoleFunctionCreateRequest securityRoleFunctionCreateRequest) { securityRoleFunctionHelper.validateAndTrimSecurityRoleFunctionCreateRequest(securityRoleFunctionCreateRequest); SecurityRoleFunctionEntity securityRoleFunctionEntity = securityRoleFunctionDao.getSecurityRoleFunctionByKey(securityRoleFunctionCreateRequest.getSecurityRoleFunctionKey()); if (securityRoleFunctionEntity != null) { throw new AlreadyExistsException(String.format( "Unable to create security role to function mapping for \"%s\" security role name and \"%s\" security function name because it already exists.", securityRoleFunctionCreateRequest.getSecurityRoleFunctionKey().getSecurityRoleName(), securityRoleFunctionCreateRequest.getSecurityRoleFunctionKey().getSecurityFunctionName())); } SecurityRoleEntity securityRoleEntity = securityRoleDaoHelper.getSecurityRoleEntity(securityRoleFunctionCreateRequest.getSecurityRoleFunctionKey().getSecurityRoleName()); SecurityFunctionEntity securityFunctionEntity = securityFunctionDaoHelper.getSecurityFunctionEntity(securityRoleFunctionCreateRequest.getSecurityRoleFunctionKey().getSecurityFunctionName()); securityRoleFunctionEntity = createSecurityRoleFunctionEntity(securityRoleEntity, securityFunctionEntity); securityRoleFunctionDao.saveAndRefresh(securityRoleFunctionEntity); return createSecurityRoleFunctionFromEntity(securityRoleFunctionEntity); }  @Override SecurityRoleFunction createSecurityRoleFunction(SecurityRoleFunctionCreateRequest securityRoleFunctionCreateRequest); @Override SecurityRoleFunction deleteSecurityRoleFunction(SecurityRoleFunctionKey securityRoleFunctionKey); @Override SecurityRoleFunction getSecurityRoleFunction(SecurityRoleFunctionKey securityRoleFunctionKey); @Override SecurityRoleFunctionKeys getSecurityRoleFunctions(); @Override SecurityRoleFunctionKeys getSecurityRoleFunctionsBySecurityFunction(SecurityFunctionKey securityFunctionKey); @Override SecurityRoleFunctionKeys getSecurityRoleFunctionsBySecurityRole(SecurityRoleKey securityRoleKey);  }
@Test public void testCreateSecurityRoleFunction() { SecurityRoleFunctionKey securityRoleFunctionKey = new SecurityRoleFunctionKey(SECURITY_ROLE, SECURITY_FUNCTION); SecurityRoleFunctionCreateRequest securityRoleFunctionCreateRequest = new SecurityRoleFunctionCreateRequest(securityRoleFunctionKey); SecurityRoleEntity securityRoleEntity = new SecurityRoleEntity(); securityRoleEntity.setCode(SECURITY_ROLE); SecurityFunctionEntity securityFunctionEntity = new SecurityFunctionEntity(); securityFunctionEntity.setCode(SECURITY_FUNCTION); 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) { SecurityRoleFunctionEntity securityRoleFunctionEntity = (SecurityRoleFunctionEntity) invocation.getArguments()[0]; securityRoleFunctionEntity.setId(ID); return null; } }).when(securityRoleFunctionDao).saveAndRefresh(any(SecurityRoleFunctionEntity.class)); SecurityRoleFunction result = securityRoleFunctionService.createSecurityRoleFunction(securityRoleFunctionCreateRequest); assertEquals(new SecurityRoleFunction(result.getId(), securityRoleFunctionKey), result); 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(); }
@Test public void testCreateSecurityRoleFunctionSecurityRoleFunctionAlreadyExists() { SecurityRoleFunctionKey securityRoleFunctionKey = new SecurityRoleFunctionKey(SECURITY_ROLE, SECURITY_FUNCTION); SecurityRoleFunctionCreateRequest securityRoleFunctionCreateRequest = new SecurityRoleFunctionCreateRequest(securityRoleFunctionKey); SecurityRoleFunctionEntity securityRoleFunctionEntity = new SecurityRoleFunctionEntity(); when(securityRoleFunctionDao.getSecurityRoleFunctionByKey(securityRoleFunctionKey)).thenReturn(securityRoleFunctionEntity); expectedException.expect(AlreadyExistsException.class); expectedException.expectMessage(String.format( "Unable to create security role to function mapping for \"%s\" security role name and \"%s\" security function name because it already exists.", SECURITY_ROLE, SECURITY_FUNCTION)); securityRoleFunctionService.createSecurityRoleFunction(securityRoleFunctionCreateRequest); verify(securityRoleFunctionHelper).validateAndTrimSecurityRoleFunctionCreateRequest(securityRoleFunctionCreateRequest); verify(securityRoleFunctionDao).getSecurityRoleFunctionByKey(securityRoleFunctionKey); verifyNoMoreInteractionsHelper(); }