/** * Gets a security function entity and ensure it exists. * * @param securityFunctionName the security function name (case insensitive) * * @return the security function entity */ public SecurityFunctionEntity getSecurityFunctionEntity(String securityFunctionName) { SecurityFunctionEntity securityFunctionEntity = securityFunctionDao.getSecurityFunctionByName(securityFunctionName); if (securityFunctionEntity == null) { throw new ObjectNotFoundException(String.format("Security function with name \"%s\" doesn't exist.", securityFunctionName)); } return securityFunctionEntity; } }
/** * Gets a security function entity and ensure it exists. * * @param securityFunctionName the security function name (case insensitive) * * @return the security function entity */ public SecurityFunctionEntity getSecurityFunctionEntity(String securityFunctionName) { SecurityFunctionEntity securityFunctionEntity = securityFunctionDao.getSecurityFunctionByName(securityFunctionName); if (securityFunctionEntity == null) { throw new ObjectNotFoundException(String.format("Security function with name \"%s\" doesn't exist.", securityFunctionName)); } return securityFunctionEntity; } }
@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); }
@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 testGetSecurityFunctionEntitySecurityFunctionNoExists() { // Mock the external calls. when(securityFunctionDao.getSecurityFunctionByName(SECURITY_FUNCTION)).thenReturn(null); // Specify the expected exception. expectedException.expect(ObjectNotFoundException.class); expectedException.expectMessage(String.format("Security function with name \"%s\" doesn't exist.", SECURITY_FUNCTION)); // Call the method under test. securityFunctionDaoHelper.getSecurityFunctionEntity(SECURITY_FUNCTION); // Verify the external calls. verify(securityFunctionDao).getSecurityFunctionByName(SECURITY_FUNCTION); verifyNoMoreInteractionsHelper(); }
@Test public void testGetSecurityFunctionEntity() { // Create a security function entity. SecurityFunctionEntity securityFunctionEntity = new SecurityFunctionEntity(); // Mock the external calls. when(securityFunctionDao.getSecurityFunctionByName(SECURITY_FUNCTION)).thenReturn(securityFunctionEntity); // Call the method under test. SecurityFunctionEntity result = securityFunctionDaoHelper.getSecurityFunctionEntity(SECURITY_FUNCTION); // Validate the results. assertEquals(securityFunctionEntity, result); // Verify the external calls. verify(securityFunctionDao).getSecurityFunctionByName(SECURITY_FUNCTION); verifyNoMoreInteractionsHelper(); }
@Override public SecurityFunction createSecurityFunction(SecurityFunctionCreateRequest request) { // Perform the validation. validateSecurityFunctionCreateRequest(request); // Ensure a security function with the specified security function name doesn't already exist. 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())); } // Create a security function entity from the request information. securityFunctionEntity = createSecurityFunctionEntity(request); // Persist the new entity. securityFunctionEntity = securityFunctionDao.saveAndRefresh(securityFunctionEntity); // Create and return the security function object from the persisted entity. return createSecurityFunctionFromEntity(securityFunctionEntity); }
@Test public void testGetSecurityFunctionByName() { SecurityFunctionEntity securityFunctionEntity = securityFunctionDaoTestHelper.createSecurityFunctionEntity(SECURITY_FUNCTION); SecurityFunctionEntity securityFunction2Entity = securityFunctionDaoTestHelper.createSecurityFunctionEntity(SECURITY_FUNCTION_2.toUpperCase()); // test the exact match SecurityFunctionEntity searchResult = securityFunctionDao.getSecurityFunctionByName(SECURITY_FUNCTION); assertEquals(securityFunctionEntity, searchResult); // test that the security function name is case insensitive searchResult = securityFunctionDao.getSecurityFunctionByName(SECURITY_FUNCTION_2.toLowerCase()); assertNotEquals(SECURITY_FUNCTION_2.toUpperCase(), SECURITY_FUNCTION_2.toLowerCase()); assertEquals(SECURITY_FUNCTION_2.toUpperCase(), searchResult.getCode()); assertEquals(securityFunction2Entity, searchResult); }
@Override public SecurityFunction createSecurityFunction(SecurityFunctionCreateRequest request) { // Perform the validation. validateSecurityFunctionCreateRequest(request); // Ensure a security function with the specified security function name doesn't already exist. 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())); } // Create a security function entity from the request information. securityFunctionEntity = createSecurityFunctionEntity(request); // Persist the new entity. securityFunctionEntity = securityFunctionDao.saveAndRefresh(securityFunctionEntity); // Create and return the security function object from the persisted entity. return createSecurityFunctionFromEntity(securityFunctionEntity); }
@Test public void testGetgetSecurityFunctionByNameNotExist() { securityFunctionDaoTestHelper.createSecurityFunctionEntity(SECURITY_FUNCTION); assertNull(securityFunctionDao.getSecurityFunctionByName(INVALID_VALUE)); }
/** * Creates and persists a security role to function mapping entity. * * @param securityRoleFunctionKey the security role to function mapping key * * @return the security role to function mapping entity */ public SecurityRoleFunctionEntity createSecurityRoleFunctionEntity(SecurityRoleFunctionKey securityRoleFunctionKey) { // Create a security role entity if needed. SecurityRoleEntity securityRoleEntity = securityRoleDao.getSecurityRoleByName(securityRoleFunctionKey.getSecurityRoleName()); if (securityRoleEntity == null) { securityRoleEntity = securityRoleDaoTestHelper.createSecurityRoleEntity(securityRoleFunctionKey.getSecurityRoleName()); } // Create a security role entity if needed. SecurityFunctionEntity securityFunctionEntity = securityFunctionDao.getSecurityFunctionByName(securityRoleFunctionKey.getSecurityFunctionName()); if (securityFunctionEntity == null) { securityFunctionEntity = securityFunctionDaoTestHelper.createSecurityFunctionEntity(securityRoleFunctionKey.getSecurityFunctionName()); } return createSecurityRoleFunctionEntity(securityRoleEntity, securityFunctionEntity); }