use of com.synopsys.integration.alert.common.persistence.model.ProviderUserModel in project hub-alert by blackducksoftware.
the class JobEmailAddressValidatorTest method validateTest.
@Test
public void validateTest() {
UUID testJobId = UUID.randomUUID();
Long testBlackDuckGlobalConfigId = 33L;
String testValidEmailAddress1 = "valid1";
String testValidEmailAddress2 = "valid2";
String testValidEmailAddress3 = "valid3";
String testInvalidEmailAddress1 = "invalid1";
String testInvalidEmailAddress2 = "invalid2";
Set<String> testEmailAddresses = Set.of(testValidEmailAddress1, testInvalidEmailAddress1, testValidEmailAddress2, testInvalidEmailAddress2, testValidEmailAddress3);
DistributionJobModel distributionJobModel = Mockito.mock(DistributionJobModel.class);
Mockito.when(distributionJobModel.getBlackDuckGlobalConfigId()).thenReturn(testBlackDuckGlobalConfigId);
JobAccessor jobAccessor = Mockito.mock(JobAccessor.class);
Mockito.when(jobAccessor.getJobById(testJobId)).thenReturn(Optional.of(distributionJobModel));
ProviderUserModel providerUserModel = Mockito.mock(ProviderUserModel.class);
Optional<ProviderUserModel> optionalProviderUser = Optional.of(providerUserModel);
ProviderDataAccessor providerDataAccessor = Mockito.mock(ProviderDataAccessor.class);
Mockito.when(providerDataAccessor.findFirstUserByEmailAddress(testBlackDuckGlobalConfigId, testValidEmailAddress1)).thenReturn(optionalProviderUser);
Mockito.when(providerDataAccessor.findFirstUserByEmailAddress(testBlackDuckGlobalConfigId, testValidEmailAddress2)).thenReturn(optionalProviderUser);
Mockito.when(providerDataAccessor.findFirstUserByEmailAddress(testBlackDuckGlobalConfigId, testValidEmailAddress3)).thenReturn(optionalProviderUser);
Mockito.when(providerDataAccessor.findFirstUserByEmailAddress(testBlackDuckGlobalConfigId, testInvalidEmailAddress1)).thenReturn(Optional.empty());
Mockito.when(providerDataAccessor.findFirstUserByEmailAddress(testBlackDuckGlobalConfigId, testInvalidEmailAddress2)).thenReturn(Optional.empty());
JobEmailAddressValidator validator = new JobEmailAddressValidator(jobAccessor, providerDataAccessor);
ValidatedEmailAddresses validatedEmailAddresses = validator.validate(testJobId, testEmailAddresses);
Set<String> validEmailAddresses = validatedEmailAddresses.getValidEmailAddresses();
assertEquals(3, validEmailAddresses.size());
assertValid(validEmailAddresses, testValidEmailAddress1);
assertValid(validEmailAddresses, testValidEmailAddress2);
assertValid(validEmailAddresses, testValidEmailAddress3);
Set<String> invalidEmailAddresses = validatedEmailAddresses.getInvalidEmailAddresses();
assertEquals(2, invalidEmailAddresses.size());
assertInvalid(invalidEmailAddresses, testInvalidEmailAddress1);
assertInvalid(invalidEmailAddresses, testInvalidEmailAddress2);
}
use of com.synopsys.integration.alert.common.persistence.model.ProviderUserModel in project hub-alert by blackducksoftware.
the class BlackDuckProviderDataAccessor method findFirstUserByEmailAddress.
@Override
public Optional<ProviderUserModel> findFirstUserByEmailAddress(Long providerConfigId, String emailAddress) {
Optional<ConfigurationModel> providerConfigOptional = configurationModelConfigurationAccessor.getConfigurationById(providerConfigId);
if (providerConfigOptional.isPresent()) {
try {
BlackDuckServicesFactory blackDuckServicesFactory = createBlackDuckServicesFactory(providerConfigOptional.get());
UserService userService = blackDuckServicesFactory.createUserService();
return userService.findUsersByEmail(emailAddress, new BlackDuckPageDefinition(1, 0)).getItems().stream().map(userView -> new ProviderUserModel(userView.getEmail(), false)).findFirst();
} catch (IntegrationException e) {
logger.errorAndDebug(createProjectNotFoundString(providerConfigId, e.getMessage()), e);
}
}
return Optional.empty();
}
use of com.synopsys.integration.alert.common.persistence.model.ProviderUserModel in project hub-alert by blackducksoftware.
the class BlackDuckProviderDataAccessor method getEmailAddressesByProvider.
private List<ProviderUserModel> getEmailAddressesByProvider(ConfigurationModel blackDuckConfiguration) throws IntegrationException {
BlackDuckServicesFactory blackDuckServicesFactory = createBlackDuckServicesFactory(blackDuckConfiguration);
BlackDuckApiClient blackDuckService = blackDuckServicesFactory.getBlackDuckApiClient();
ApiDiscovery apiDiscovery = blackDuckServicesFactory.getApiDiscovery();
Set<String> allActiveBlackDuckEmailAddresses = getAllActiveBlackDuckEmailAddresses(blackDuckService, apiDiscovery);
return allActiveBlackDuckEmailAddresses.stream().map(email -> new ProviderUserModel(email, false)).collect(Collectors.toList());
}
use of com.synopsys.integration.alert.common.persistence.model.ProviderUserModel in project hub-alert by blackducksoftware.
the class BlackDuckProviderDataAccessor method getProviderConfigUserById.
@Override
public ProviderUserModel getProviderConfigUserById(Long providerConfigId) throws AlertConfigurationException {
try {
Optional<ConfigurationModel> providerConfigOptional = configurationModelConfigurationAccessor.getConfigurationById(providerConfigId);
if (providerConfigOptional.isPresent()) {
BlackDuckServicesFactory blackDuckServicesFactory = createBlackDuckServicesFactory(providerConfigOptional.get());
UserService userService = blackDuckServicesFactory.createUserService();
UserView providerConfigUser = userService.findCurrentUser();
return new ProviderUserModel(providerConfigUser.getEmail(), false);
}
} catch (IntegrationException e) {
throw new AlertConfigurationException(createUserNotFoundString(providerConfigId, e.getMessage()), e);
}
throw new AlertConfigurationException(String.format("The provider config with id '%s' is invalid", providerConfigId));
}
use of com.synopsys.integration.alert.common.persistence.model.ProviderUserModel in project hub-alert by blackducksoftware.
the class BlackDuckProviderDataAccessor method retrieveUsersForProvider.
private AlertPagedModel<ProviderUserModel> retrieveUsersForProvider(ConfigurationModel blackDuckConfigurationModel, int pageNumber, int pageSize, String searchTerm) throws IntegrationException {
BlackDuckServicesFactory blackDuckServicesFactory = createBlackDuckServicesFactory(blackDuckConfigurationModel);
Predicate<UserView> searchFilter = userView -> StringUtils.isNotBlank(userView.getEmail());
if (StringUtils.isNotBlank(searchTerm)) {
searchFilter = searchFilter.and(userView -> StringUtils.containsIgnoreCase(userView.getEmail(), searchTerm));
}
ApiDiscovery apiDiscovery = blackDuckServicesFactory.getApiDiscovery();
BlackDuckPageResponse<UserView> pageOfUsers = retrieveBlackDuckPageResponse(blackDuckServicesFactory, apiDiscovery.metaUsersLink(), pageNumber, pageSize, searchFilter);
List<ProviderUserModel> foundUsers = pageOfUsers.getItems().stream().map(UserView::getEmail).map(email -> new ProviderUserModel(email, false)).collect(Collectors.toList());
// Due to a limitation in the blackduck-common library, the totalCount in the BlackDuckPageResponse does not represent the count the matches the searchFilter. It is the totalCount from Black Duck
int totalPageCount = computeTotalCount(pageOfUsers, pageSize);
return new AlertPagedModel<>(totalPageCount, pageNumber, pageSize, foundUsers);
}
Aggregations