SubjectMatterExpertDaoImpl implements SubjectMatterExpertDao { @Override public SubjectMatterExpertContactDetails getSubjectMatterExpertByKey(SubjectMatterExpertKey subjectMatterExpertKey) { final String ldapUrl = configurationHelper.getProperty(ConfigurationValue.LDAP_URL); final String ldapBase = configurationHelper.getProperty(ConfigurationValue.LDAP_BASE); final String ldapUserDn = configurationHelper.getProperty(ConfigurationValue.LDAP_USER_DN); final String credStashEncryptionContext = configurationHelper.getProperty(ConfigurationValue.CREDSTASH_HERD_ENCRYPTION_CONTEXT); final String ldapUserCredentialName = configurationHelper.getProperty(ConfigurationValue.LDAP_USER_CREDENTIAL_NAME); LOGGER.info("Creating LDAP context source using the following parameters: {}=\"{}\" {}=\"{}\" {}=\"{}\" {}=\"{}\" {}=\"{}\"...", ConfigurationValue.LDAP_URL.getKey(), ldapUrl, ConfigurationValue.LDAP_BASE.getKey(), ldapBase, ConfigurationValue.LDAP_USER_DN.getKey(), ldapUserDn, ConfigurationValue.CREDSTASH_HERD_ENCRYPTION_CONTEXT.getKey(), credStashEncryptionContext, ConfigurationValue.LDAP_USER_CREDENTIAL_NAME.getKey(), ldapUserCredentialName); String ldapUserPassword; try { ldapUserPassword = credStashHelper.getCredentialFromCredStash(credStashEncryptionContext, ldapUserCredentialName); } catch (CredStashGetCredentialFailedException e) { throw new IllegalStateException(e); } LdapContextSource contextSource = new LdapContextSource(); contextSource.setUrl(ldapUrl); contextSource.setBase(ldapBase); contextSource.setUserDn(ldapUserDn); contextSource.setPassword(ldapUserPassword); contextSource.afterPropertiesSet(); LdapTemplate ldapTemplate = new LdapTemplate(contextSource); LdapQuery ldapQuery = query().where(configurationHelper.getProperty(ConfigurationValue.LDAP_ATTRIBUTE_USER_ID)).is(subjectMatterExpertKey.getUserId()); SubjectMatterExpertContactDetailsMapper subjectMatterExpertContactDetailsMapper = new SubjectMatterExpertContactDetailsMapper(configurationHelper.getProperty(ConfigurationValue.LDAP_ATTRIBUTE_USER_FULL_NAME), configurationHelper.getProperty(ConfigurationValue.LDAP_ATTRIBUTE_USER_JOB_TITLE), configurationHelper.getProperty(ConfigurationValue.LDAP_ATTRIBUTE_USER_EMAIL_ADDRESS), configurationHelper.getProperty(ConfigurationValue.LDAP_ATTRIBUTE_USER_TELEPHONE_NUMBER)); List<SubjectMatterExpertContactDetails> subjectMatterExpertContactDetailsList = ldapOperations.search(ldapTemplate, ldapQuery, subjectMatterExpertContactDetailsMapper); return CollectionUtils.isNotEmpty(subjectMatterExpertContactDetailsList) ? subjectMatterExpertContactDetailsList.get(0) : null; }  @Override SubjectMatterExpertContactDetails getSubjectMatterExpertByKey(SubjectMatterExpertKey subjectMatterExpertKey);  }
@Test public void testGetSubjectMatterExpertByKey() throws Exception { SubjectMatterExpertKey subjectMatterExpertKey = new SubjectMatterExpertKey(USER_ID); SubjectMatterExpertContactDetails subjectMatterExpertContactDetails = new SubjectMatterExpertContactDetails(USER_FULL_NAME, USER_JOB_TITLE, USER_EMAIL_ADDRESS, USER_TELEPHONE_NUMBER); when(configurationHelper.getProperty(ConfigurationValue.LDAP_URL)).thenReturn(LDAP_URL); when(configurationHelper.getProperty(ConfigurationValue.LDAP_BASE)).thenReturn(LDAP_BASE); when(configurationHelper.getProperty(ConfigurationValue.LDAP_USER_DN)).thenReturn(LDAP_USER_DN); when(configurationHelper.getProperty(ConfigurationValue.CREDSTASH_HERD_ENCRYPTION_CONTEXT)).thenReturn(CREDSTASH_ENCRYPTION_CONTEXT); when(configurationHelper.getProperty(ConfigurationValue.LDAP_USER_CREDENTIAL_NAME)).thenReturn(USER_CREDENTIAL_NAME); when(credStashHelper.getCredentialFromCredStash(CREDSTASH_ENCRYPTION_CONTEXT, USER_CREDENTIAL_NAME)).thenReturn(PASSWORD); when(configurationHelper.getProperty(ConfigurationValue.LDAP_ATTRIBUTE_USER_ID)).thenReturn(LDAP_ATTRIBUTE_USER_ID); when(configurationHelper.getProperty(ConfigurationValue.LDAP_ATTRIBUTE_USER_FULL_NAME)).thenReturn(LDAP_ATTRIBUTE_USER_FULL_NAME); when(configurationHelper.getProperty(ConfigurationValue.LDAP_ATTRIBUTE_USER_JOB_TITLE)).thenReturn(LDAP_ATTRIBUTE_USER_JOB_TITLE); when(configurationHelper.getProperty(ConfigurationValue.LDAP_ATTRIBUTE_USER_EMAIL_ADDRESS)).thenReturn(LDAP_ATTRIBUTE_USER_EMAIL_ADDRESS); when(configurationHelper.getProperty(ConfigurationValue.LDAP_ATTRIBUTE_USER_TELEPHONE_NUMBER)).thenReturn(LDAP_ATTRIBUTE_USER_TELEPHONE_NUMBER); when( ldapOperations.search(any(LdapTemplate.class), any(LdapQuery.class), any(SubjectMatterExpertDaoImpl.SubjectMatterExpertContactDetailsMapper.class))) .thenReturn(Collections.singletonList(subjectMatterExpertContactDetails)); SubjectMatterExpertContactDetails result = subjectMatterExpertDaoImpl.getSubjectMatterExpertByKey(subjectMatterExpertKey); assertEquals(subjectMatterExpertContactDetails, result); verify(configurationHelper).getProperty(ConfigurationValue.LDAP_URL); verify(configurationHelper).getProperty(ConfigurationValue.LDAP_BASE); verify(configurationHelper).getProperty(ConfigurationValue.LDAP_USER_DN); verify(configurationHelper).getProperty(ConfigurationValue.CREDSTASH_HERD_ENCRYPTION_CONTEXT); verify(configurationHelper).getProperty(ConfigurationValue.LDAP_USER_CREDENTIAL_NAME); verify(credStashHelper).getCredentialFromCredStash(CREDSTASH_ENCRYPTION_CONTEXT, USER_CREDENTIAL_NAME); verify(configurationHelper).getProperty(ConfigurationValue.LDAP_ATTRIBUTE_USER_ID); verify(configurationHelper).getProperty(ConfigurationValue.LDAP_ATTRIBUTE_USER_FULL_NAME); verify(configurationHelper).getProperty(ConfigurationValue.LDAP_ATTRIBUTE_USER_JOB_TITLE); verify(configurationHelper).getProperty(ConfigurationValue.LDAP_ATTRIBUTE_USER_EMAIL_ADDRESS); verify(configurationHelper).getProperty(ConfigurationValue.LDAP_ATTRIBUTE_USER_TELEPHONE_NUMBER); verify(ldapOperations) .search(any(LdapTemplate.class), any(LdapQuery.class), any(SubjectMatterExpertDaoImpl.SubjectMatterExpertContactDetailsMapper.class)); verifyNoMoreInteractionsHelper(); }
@Test public void testGetSubjectMatterExpertByKeyCredStashGetCredentialFailedException() throws Exception { SubjectMatterExpertKey subjectMatterExpertKey = new SubjectMatterExpertKey(USER_ID); SubjectMatterExpertContactDetails subjectMatterExpertContactDetails = new SubjectMatterExpertContactDetails(USER_FULL_NAME, USER_JOB_TITLE, USER_EMAIL_ADDRESS, USER_TELEPHONE_NUMBER); when(configurationHelper.getProperty(ConfigurationValue.LDAP_URL)).thenReturn(LDAP_URL); when(configurationHelper.getProperty(ConfigurationValue.LDAP_BASE)).thenReturn(LDAP_BASE); when(configurationHelper.getProperty(ConfigurationValue.LDAP_USER_DN)).thenReturn(LDAP_USER_DN); when(configurationHelper.getProperty(ConfigurationValue.CREDSTASH_HERD_ENCRYPTION_CONTEXT)).thenReturn(CREDSTASH_ENCRYPTION_CONTEXT); when(configurationHelper.getProperty(ConfigurationValue.LDAP_USER_CREDENTIAL_NAME)).thenReturn(USER_CREDENTIAL_NAME); when(credStashHelper.getCredentialFromCredStash(CREDSTASH_ENCRYPTION_CONTEXT, USER_CREDENTIAL_NAME)) .thenThrow(new CredStashGetCredentialFailedException(ERROR_MESSAGE)); expectedException.expect(IllegalStateException.class); expectedException.expectMessage(ERROR_MESSAGE); subjectMatterExpertDaoImpl.getSubjectMatterExpertByKey(subjectMatterExpertKey); verify(configurationHelper).getProperty(ConfigurationValue.LDAP_URL); verify(configurationHelper).getProperty(ConfigurationValue.LDAP_BASE); verify(configurationHelper).getProperty(ConfigurationValue.LDAP_USER_DN); verify(configurationHelper).getProperty(ConfigurationValue.CREDSTASH_HERD_ENCRYPTION_CONTEXT); verify(configurationHelper).getProperty(ConfigurationValue.LDAP_USER_CREDENTIAL_NAME); verify(credStashHelper).getCredentialFromCredStash(CREDSTASH_ENCRYPTION_CONTEXT, USER_CREDENTIAL_NAME); verifyNoMoreInteractionsHelper(); }
@Test public void testGetSubjectMatterExpertByKeyUserNoExists() throws Exception { SubjectMatterExpertKey subjectMatterExpertKey = new SubjectMatterExpertKey(USER_ID); SubjectMatterExpertContactDetails subjectMatterExpertContactDetails = new SubjectMatterExpertContactDetails(USER_FULL_NAME, USER_JOB_TITLE, USER_EMAIL_ADDRESS, USER_TELEPHONE_NUMBER); when(configurationHelper.getProperty(ConfigurationValue.LDAP_URL)).thenReturn(LDAP_URL); when(configurationHelper.getProperty(ConfigurationValue.LDAP_BASE)).thenReturn(LDAP_BASE); when(configurationHelper.getProperty(ConfigurationValue.LDAP_USER_DN)).thenReturn(LDAP_USER_DN); when(configurationHelper.getProperty(ConfigurationValue.CREDSTASH_HERD_ENCRYPTION_CONTEXT)).thenReturn(CREDSTASH_ENCRYPTION_CONTEXT); when(configurationHelper.getProperty(ConfigurationValue.LDAP_USER_CREDENTIAL_NAME)).thenReturn(USER_CREDENTIAL_NAME); when(credStashHelper.getCredentialFromCredStash(CREDSTASH_ENCRYPTION_CONTEXT, USER_CREDENTIAL_NAME)).thenReturn(PASSWORD); when(configurationHelper.getProperty(ConfigurationValue.LDAP_ATTRIBUTE_USER_ID)).thenReturn(LDAP_ATTRIBUTE_USER_ID); when(configurationHelper.getProperty(ConfigurationValue.LDAP_ATTRIBUTE_USER_FULL_NAME)).thenReturn(LDAP_ATTRIBUTE_USER_FULL_NAME); when(configurationHelper.getProperty(ConfigurationValue.LDAP_ATTRIBUTE_USER_JOB_TITLE)).thenReturn(LDAP_ATTRIBUTE_USER_JOB_TITLE); when(configurationHelper.getProperty(ConfigurationValue.LDAP_ATTRIBUTE_USER_EMAIL_ADDRESS)).thenReturn(LDAP_ATTRIBUTE_USER_EMAIL_ADDRESS); when(configurationHelper.getProperty(ConfigurationValue.LDAP_ATTRIBUTE_USER_TELEPHONE_NUMBER)).thenReturn(LDAP_ATTRIBUTE_USER_TELEPHONE_NUMBER); when( ldapOperations.search(any(LdapTemplate.class), any(LdapQuery.class), any(SubjectMatterExpertDaoImpl.SubjectMatterExpertContactDetailsMapper.class))) .thenReturn(Collections.emptyList()); SubjectMatterExpertContactDetails result = subjectMatterExpertDaoImpl.getSubjectMatterExpertByKey(subjectMatterExpertKey); assertNull(result); verify(configurationHelper).getProperty(ConfigurationValue.LDAP_URL); verify(configurationHelper).getProperty(ConfigurationValue.LDAP_BASE); verify(configurationHelper).getProperty(ConfigurationValue.LDAP_USER_DN); verify(configurationHelper).getProperty(ConfigurationValue.CREDSTASH_HERD_ENCRYPTION_CONTEXT); verify(configurationHelper).getProperty(ConfigurationValue.LDAP_USER_CREDENTIAL_NAME); verify(credStashHelper).getCredentialFromCredStash(CREDSTASH_ENCRYPTION_CONTEXT, USER_CREDENTIAL_NAME); verify(configurationHelper).getProperty(ConfigurationValue.LDAP_ATTRIBUTE_USER_ID); verify(configurationHelper).getProperty(ConfigurationValue.LDAP_ATTRIBUTE_USER_FULL_NAME); verify(configurationHelper).getProperty(ConfigurationValue.LDAP_ATTRIBUTE_USER_JOB_TITLE); verify(configurationHelper).getProperty(ConfigurationValue.LDAP_ATTRIBUTE_USER_EMAIL_ADDRESS); verify(configurationHelper).getProperty(ConfigurationValue.LDAP_ATTRIBUTE_USER_TELEPHONE_NUMBER); verify(ldapOperations) .search(any(LdapTemplate.class), any(LdapQuery.class), any(SubjectMatterExpertDaoImpl.SubjectMatterExpertContactDetailsMapper.class)); verifyNoMoreInteractionsHelper(); }