use of org.mockito.ArgumentCaptor in project sonarqube by SonarSource.
the class SendIssueNotificationsStepTest method send_new_issues_notification_to_user_only_for_those_assigned_to_her.
@Test
public void send_new_issues_notification_to_user_only_for_those_assigned_to_her() throws IOException {
UserDto perceval = db.users().insertUser(u -> u.setLogin("perceval"));
Integer[] assigned = IntStream.range(0, 5).mapToObj(i -> 10_000 * i).toArray(Integer[]::new);
Duration expectedEffort = Duration.create(stream(assigned).mapToInt(i -> i).sum());
UserDto arthur = db.users().insertUser(u -> u.setLogin("arthur"));
Integer[] assignedToOther = IntStream.range(0, 3).mapToObj(i -> 10).toArray(Integer[]::new);
List<DefaultIssue> issues = concat(stream(assigned).map(effort -> createIssue().setType(randomRuleType).setEffort(Duration.create(effort)).setAssigneeUuid(perceval.getUuid()).setNew(true).setCreationDate(new Date(ANALYSE_DATE))), stream(assignedToOther).map(effort -> createIssue().setType(randomRuleType).setEffort(Duration.create(effort)).setAssigneeUuid(arthur.getUuid()).setNew(true).setCreationDate(new Date(ANALYSE_DATE)))).collect(toList());
shuffle(issues);
ProtoIssueCache protoIssueCache = new ProtoIssueCache(temp.newFile(), System2.INSTANCE);
DiskCache.CacheAppender newIssueCache = protoIssueCache.newAppender();
issues.forEach(newIssueCache::append);
newIssueCache.close();
analysisMetadataHolder.setProject(new Project(PROJECT.getUuid(), PROJECT.getKey(), PROJECT.getName(), null, emptyList()));
when(notificationService.hasProjectSubscribersForTypes(PROJECT.getUuid(), NOTIF_TYPES)).thenReturn(true);
NotificationFactory notificationFactory = mock(NotificationFactory.class);
NewIssuesNotification newIssuesNotificationMock = createNewIssuesNotificationMock();
when(notificationFactory.newNewIssuesNotification(assigneeCacheCaptor.capture())).thenReturn(newIssuesNotificationMock);
MyNewIssuesNotification myNewIssuesNotificationMock1 = createMyNewIssuesNotificationMock();
MyNewIssuesNotification myNewIssuesNotificationMock2 = createMyNewIssuesNotificationMock();
doReturn(myNewIssuesNotificationMock1).doReturn(myNewIssuesNotificationMock2).when(notificationFactory).newMyNewIssuesNotification(any(assigneeCacheType));
TestComputationStepContext context = new TestComputationStepContext();
new SendIssueNotificationsStep(protoIssueCache, treeRootHolder, notificationService, analysisMetadataHolder, notificationFactory, db.getDbClient()).execute(context);
verify(notificationService).deliverEmails(ImmutableSet.of(myNewIssuesNotificationMock1, myNewIssuesNotificationMock2));
// old API compatibility
verify(notificationService).deliver(myNewIssuesNotificationMock1);
verify(notificationService).deliver(myNewIssuesNotificationMock2);
verify(notificationFactory).newNewIssuesNotification(assigneeCacheCaptor.capture());
verify(notificationFactory, times(2)).newMyNewIssuesNotification(assigneeCacheCaptor.capture());
verifyNoMoreInteractions(notificationFactory);
verifyAssigneeCache(assigneeCacheCaptor, perceval, arthur);
Map<String, MyNewIssuesNotification> myNewIssuesNotificationMocksByUsersName = new HashMap<>();
ArgumentCaptor<UserDto> userCaptor1 = forClass(UserDto.class);
verify(myNewIssuesNotificationMock1).setAssignee(userCaptor1.capture());
myNewIssuesNotificationMocksByUsersName.put(userCaptor1.getValue().getLogin(), myNewIssuesNotificationMock1);
ArgumentCaptor<UserDto> userCaptor2 = forClass(UserDto.class);
verify(myNewIssuesNotificationMock2).setAssignee(userCaptor2.capture());
myNewIssuesNotificationMocksByUsersName.put(userCaptor2.getValue().getLogin(), myNewIssuesNotificationMock2);
MyNewIssuesNotification myNewIssuesNotificationMock = myNewIssuesNotificationMocksByUsersName.get("perceval");
ArgumentCaptor<NewIssuesStatistics.Stats> statsCaptor = forClass(NewIssuesStatistics.Stats.class);
verify(myNewIssuesNotificationMock).setStatistics(eq(PROJECT.getName()), statsCaptor.capture());
verify(myNewIssuesNotificationMock).setDebt(expectedEffort);
NewIssuesStatistics.Stats stats = statsCaptor.getValue();
assertThat(stats.hasIssues()).isTrue();
// just checking all issues have been added to the stats
DistributedMetricStatsInt severity = stats.getDistributedMetricStats(NewIssuesStatistics.Metric.RULE_TYPE);
assertThat(severity.getOnCurrentAnalysis()).isEqualTo(assigned.length);
assertThat(severity.getTotal()).isEqualTo(assigned.length);
verifyStatistics(context, 1, 2, 0);
}
use of org.mockito.ArgumentCaptor in project sonarqube by SonarSource.
the class FPOrWontFixNotificationHandlerTest method deliver_does_not_send_email_request_for_notifications_a_subscriber_is_the_changeAuthor_of.
@Test
@UseDataProvider("FPorWontFixResolution")
public void deliver_does_not_send_email_request_for_notifications_a_subscriber_is_the_changeAuthor_of(String newResolution) {
Project project = newProject(randomAlphabetic(5));
User subscriber1 = newUser("subscriber1");
User subscriber2 = newUser("subscriber2");
User subscriber3 = newUser("subscriber3");
User otherChangeAuthor = newUser("otherChangeAuthor");
// subscriber1 is the changeAuthor of some notifications with issues assigned to subscriber1 only
Set<IssuesChangesNotificationBuilder> subscriber1Notifications = IntStream.range(0, 1 + new Random().nextInt(2)).mapToObj(j -> new IssuesChangesNotificationBuilder(randomIssues(t -> t.setProject(project).setNewResolution(newResolution).setAssignee(subscriber2)).collect(toSet()), newUserChange(subscriber1))).collect(toSet());
// subscriber1 is the changeAuthor of some notifications with issues assigned to subscriber1 and subscriber2
Set<IssuesChangesNotificationBuilder> subscriber1and2Notifications = IntStream.range(0, 1 + new Random().nextInt(2)).mapToObj(j -> new IssuesChangesNotificationBuilder(Stream.concat(randomIssues(t -> t.setProject(project).setNewResolution(newResolution).setAssignee(subscriber2)), randomIssues(t -> t.setProject(project).setNewResolution(newResolution).setAssignee(subscriber1))).collect(toSet()), newUserChange(subscriber1))).collect(toSet());
// subscriber2 is the changeAuthor of some notifications with issues assigned to subscriber2 only
Set<IssuesChangesNotificationBuilder> subscriber2Notifications = IntStream.range(0, 1 + new Random().nextInt(2)).mapToObj(j -> new IssuesChangesNotificationBuilder(randomIssues(t -> t.setProject(project).setNewResolution(newResolution).setAssignee(subscriber2)).collect(toSet()), newUserChange(subscriber2))).collect(toSet());
// subscriber2 is the changeAuthor of some notifications with issues assigned to subscriber2 and subscriber 3
Set<IssuesChangesNotificationBuilder> subscriber2And3Notifications = IntStream.range(0, 1 + new Random().nextInt(2)).mapToObj(j -> new IssuesChangesNotificationBuilder(Stream.concat(randomIssues(t -> t.setProject(project).setNewResolution(newResolution).setAssignee(subscriber2)), randomIssues(t -> t.setProject(project).setNewResolution(newResolution).setAssignee(subscriber3))).collect(toSet()), newUserChange(subscriber2))).collect(toSet());
// subscriber3 is the changeAuthor of no notification
// otherChangeAuthor has some notifications
Set<IssuesChangesNotificationBuilder> otherChangeAuthorNotifications = IntStream.range(0, 1 + new Random().nextInt(2)).mapToObj(j -> new IssuesChangesNotificationBuilder(randomIssues(t -> t.setProject(project).setNewResolution(newResolution)).collect(toSet()), newUserChange(otherChangeAuthor))).collect(toSet());
when(emailNotificationChannel.isActivated()).thenReturn(true);
Set<String> subscriberLogins = ImmutableSet.of(subscriber1.getLogin(), subscriber2.getLogin(), subscriber3.getLogin());
when(notificationManager.findSubscribedEmailRecipients(DO_NOT_FIX_ISSUE_CHANGE_DISPATCHER_KEY, project.getKey(), ALL_MUST_HAVE_ROLE_USER)).thenReturn(subscriberLogins.stream().map(FPOrWontFixNotificationHandlerTest::emailRecipientOf).collect(toSet()));
int deliveredCount = new Random().nextInt(200);
when(emailNotificationChannel.deliverAll(anySet())).thenReturn(deliveredCount).thenThrow(new IllegalStateException("deliver should be called only once"));
Set<IssuesChangesNotification> notifications = Stream.of(subscriber1Notifications.stream(), subscriber1and2Notifications.stream(), subscriber2Notifications.stream(), subscriber2And3Notifications.stream(), otherChangeAuthorNotifications.stream()).flatMap(t -> t).map(serializer::serialize).collect(toSet());
reset(serializer);
int deliver = underTest.deliver(notifications);
assertThat(deliver).isEqualTo(deliveredCount);
verify(notificationManager).findSubscribedEmailRecipients(DO_NOT_FIX_ISSUE_CHANGE_DISPATCHER_KEY, project.getKey(), ALL_MUST_HAVE_ROLE_USER);
verifyNoMoreInteractions(notificationManager);
verify(emailNotificationChannel).isActivated();
ArgumentCaptor<Set<EmailDeliveryRequest>> captor = ArgumentCaptor.forClass(requestSetType);
verify(emailNotificationChannel).deliverAll(captor.capture());
verifyNoMoreInteractions(emailNotificationChannel);
ListMultimap<String, EmailDeliveryRequest> requestsByRecipientEmail = captor.getValue().stream().collect(index(EmailDeliveryRequest::getRecipientEmail));
assertThat(requestsByRecipientEmail.get(emailOf(subscriber1.getLogin()))).containsOnly(Stream.of(subscriber2Notifications.stream().map(notif -> newEmailDeliveryRequest(notif, subscriber1, toFpOrWontFix(newResolution))), subscriber2And3Notifications.stream().map(notif -> newEmailDeliveryRequest(notif, subscriber1, toFpOrWontFix(newResolution))), otherChangeAuthorNotifications.stream().map(notif -> newEmailDeliveryRequest(notif, subscriber1, toFpOrWontFix(newResolution)))).flatMap(t -> t).toArray(EmailDeliveryRequest[]::new));
assertThat(requestsByRecipientEmail.get(emailOf(subscriber2.getLogin()))).containsOnly(Stream.of(subscriber1Notifications.stream().map(notif -> newEmailDeliveryRequest(notif, subscriber2, toFpOrWontFix(newResolution))), subscriber1and2Notifications.stream().map(notif -> newEmailDeliveryRequest(notif, subscriber2, toFpOrWontFix(newResolution))), otherChangeAuthorNotifications.stream().map(notif -> newEmailDeliveryRequest(notif, subscriber2, toFpOrWontFix(newResolution)))).flatMap(t -> t).toArray(EmailDeliveryRequest[]::new));
assertThat(requestsByRecipientEmail.get(emailOf(subscriber3.getLogin()))).containsOnly(Stream.of(subscriber1Notifications.stream().map(notif -> newEmailDeliveryRequest(notif, subscriber3, toFpOrWontFix(newResolution))), subscriber1and2Notifications.stream().map(notif -> newEmailDeliveryRequest(notif, subscriber3, toFpOrWontFix(newResolution))), subscriber2Notifications.stream().map(notif -> newEmailDeliveryRequest(notif, subscriber3, toFpOrWontFix(newResolution))), subscriber2And3Notifications.stream().map(notif -> newEmailDeliveryRequest(notif, subscriber3, toFpOrWontFix(newResolution))), otherChangeAuthorNotifications.stream().map(notif -> newEmailDeliveryRequest(notif, subscriber3, toFpOrWontFix(newResolution)))).flatMap(t -> t).toArray(EmailDeliveryRequest[]::new));
assertThat(requestsByRecipientEmail.get(emailOf(otherChangeAuthor.getLogin()))).isEmpty();
}
use of org.mockito.ArgumentCaptor in project sonarqube by SonarSource.
the class AsyncIssueIndexingImplTest method characteristics_are_defined.
@Test
public void characteristics_are_defined() {
BranchDto dto = new BranchDto().setBranchType(BRANCH).setKey("branch_1").setUuid("branch_uuid1").setProjectUuid("project_uuid1");
dbClient.branchDao().insert(dbTester.getSession(), dto);
dbTester.commit();
insertSnapshot("analysis_1", "project_uuid1", 1);
BranchDto dto2 = new BranchDto().setBranchType(PULL_REQUEST).setKey("pr_1").setUuid("pr_uuid_1").setProjectUuid("project_uuid2");
dbClient.branchDao().insert(dbTester.getSession(), dto2);
dbTester.commit();
insertSnapshot("analysis_2", "project_uuid2", 2);
underTest.triggerOnIndexCreation();
ArgumentCaptor<Collection<CeTaskSubmit>> captor = ArgumentCaptor.forClass(Collection.class);
verify(ceQueue, times(1)).massSubmit(captor.capture());
List<Collection<CeTaskSubmit>> captures = captor.getAllValues();
assertThat(captures).hasSize(1);
Collection<CeTaskSubmit> tasks = captures.get(0);
assertThat(tasks).hasSize(2);
assertThat(tasks).extracting(p -> p.getCharacteristics().get(BRANCH_TYPE_KEY), p -> p.getCharacteristics().get(CeTaskCharacteristicDto.BRANCH_KEY), p -> p.getCharacteristics().get(CeTaskCharacteristicDto.PULL_REQUEST)).containsExactlyInAnyOrder(tuple("BRANCH", "branch_1", null), tuple("PULL_REQUEST", null, "pr_1"));
}
use of org.mockito.ArgumentCaptor in project sonarqube by SonarSource.
the class ValidateActionTest method github_validation_checks.
@Test
public void github_validation_checks() {
AlmSettingDto almSetting = insertAlmSetting(db.almSettings().insertGitHubAlmSetting(settings -> settings.setClientId("clientId").setClientSecret("clientSecret")));
when(encryption.isEncrypted(any())).thenReturn(false);
ws.newRequest().setParam("key", almSetting.getKey()).execute();
ArgumentCaptor<AlmSettingDto> almSettingDtoArgumentCaptor = ArgumentCaptor.forClass(AlmSettingDto.class);
verify(githubGlobalSettingsValidator).validate(almSettingDtoArgumentCaptor.capture());
assertThat(almSettingDtoArgumentCaptor.getAllValues()).hasSize(1);
assertThat(almSettingDtoArgumentCaptor.getValue().getClientId()).isEqualTo(almSetting.getClientId());
assertThat(almSettingDtoArgumentCaptor.getValue().getDecryptedClientSecret(encryption)).isEqualTo(almSetting.getDecryptedClientSecret(encryption));
assertThat(almSettingDtoArgumentCaptor.getValue().getAlm()).isEqualTo(almSetting.getAlm());
assertThat(almSettingDtoArgumentCaptor.getValue().getAppId()).isEqualTo(almSetting.getAppId());
}
use of org.mockito.ArgumentCaptor in project sonarqube by SonarSource.
the class ValidateActionTest method github_validation_checks_with_encrypted_secret.
@Test
public void github_validation_checks_with_encrypted_secret() {
String secret = "encrypted-secret";
String decryptedSecret = "decrypted-secret";
AlmSettingDto almSetting = insertAlmSetting(db.almSettings().insertGitHubAlmSetting(settings -> settings.setClientId("clientId").setClientSecret(secret)));
when(encryption.isEncrypted(secret)).thenReturn(true);
when(encryption.decrypt(secret)).thenReturn(decryptedSecret);
ws.newRequest().setParam("key", almSetting.getKey()).execute();
ArgumentCaptor<AlmSettingDto> almSettingDtoArgumentCaptor = ArgumentCaptor.forClass(AlmSettingDto.class);
verify(githubGlobalSettingsValidator).validate(almSettingDtoArgumentCaptor.capture());
assertThat(almSettingDtoArgumentCaptor.getAllValues()).hasSize(1);
assertThat(almSettingDtoArgumentCaptor.getValue().getClientId()).isEqualTo(almSetting.getClientId());
assertThat(almSettingDtoArgumentCaptor.getValue().getDecryptedClientSecret(encryption)).isEqualTo(decryptedSecret);
assertThat(almSettingDtoArgumentCaptor.getValue().getAlm()).isEqualTo(almSetting.getAlm());
assertThat(almSettingDtoArgumentCaptor.getValue().getAppId()).isEqualTo(almSetting.getAppId());
}
Aggregations