SecurityFunctionServiceImpl implements SecurityFunctionService { @Override public SecurityFunction createSecurityFunction(SecurityFunctionCreateRequest request) { validateSecurityFunctionCreateRequest(request); SecurityFunctionEntity securityFunctionEntity = securityFunctionDao.getSecurityFunctionByName(request.getSecurityFunctionName()); if (securityFunctionEntity != null) { throw new AlreadyExistsException( String.format("Unable to create security function \"%s\" because it already exists.", request.getSecurityFunctionName())); } securityFunctionEntity = createSecurityFunctionEntity(request); securityFunctionEntity = securityFunctionDao.saveAndRefresh(securityFunctionEntity); return createSecurityFunctionFromEntity(securityFunctionEntity); }  @Override SecurityFunction createSecurityFunction(SecurityFunctionCreateRequest request); @Override SecurityFunction getSecurityFunction(SecurityFunctionKey securityFunctionKey); @Override SecurityFunction deleteSecurityFunction(SecurityFunctionKey securityFunctionKey); @Override SecurityFunctionKeys getSecurityFunctions();  }
@Test public void testCreateSecurityFunction() { when(securityFunctionDao.getSecurityFunctionByName(SECURITY_FUNCTION)).thenReturn(null); when(alternateKeyHelper.validateStringParameter(anyString(), anyString())).thenReturn(SECURITY_FUNCTION); when(securityFunctionDao.saveAndRefresh(any(SecurityFunctionEntity.class))).thenReturn(SECURITY_FUNCTION_ENTITY); SecurityFunction securityFunction = securityFunctionService.createSecurityFunction(SECURITY_FUNCTION_CREATE_REQUEST); assertEquals(SECURITY_FUNCTION, securityFunction.getSecurityFunctionName()); verify(alternateKeyHelper).validateStringParameter("security function name", SECURITY_FUNCTION); verify(securityFunctionDao).getSecurityFunctionByName(SECURITY_FUNCTION); verify(securityFunctionDao).saveAndRefresh(any(SecurityFunctionEntity.class)); verifyNoMoreInteractionsHelper(); }
@Test public void testCreateSecurityFunctionAlreadyExists() { expectedException.expect(AlreadyExistsException.class); expectedException.expectMessage(String.format("Unable to create security function \"%s\" because it already exists.", SECURITY_FUNCTION)); when(securityFunctionDao.getSecurityFunctionByName(SECURITY_FUNCTION)).thenReturn(SECURITY_FUNCTION_ENTITY); when(alternateKeyHelper.validateStringParameter(anyString(), anyString())).thenReturn(SECURITY_FUNCTION); securityFunctionService.createSecurityFunction(SECURITY_FUNCTION_CREATE_REQUEST); }