From c9213c78ae0062ab89921179de2083105a3f1eb2 Mon Sep 17 00:00:00 2001 From: Steve Marion Date: Fri, 27 Jan 2023 10:41:51 +0100 Subject: [PATCH] org.mockito:mockito-inline 4.10.0 -> 5.0.0 org.mockito:mockito-core 4.10.0 -> 5.0.0 --- build.gradle | 4 +- ...ProjectDuplicationsRepositoryStepTest.java | 28 +-- .../charset/MssqlMetadataReaderTest.java | 9 +- .../charset/OracleCharsetHandlerTest.java | 6 +- .../charset/PostgresMetadataReaderTest.java | 9 +- .../notification/NotificationServiceTest.java | 220 ++++++++---------- .../notification/NotificationMediumTest.java | 18 +- .../QProfileFactoryImplTest.java | 9 +- 8 files changed, 127 insertions(+), 176 deletions(-) diff --git a/build.gradle b/build.gradle index 67b93714380..745590504d6 100644 --- a/build.gradle +++ b/build.gradle @@ -326,10 +326,10 @@ subprojects { dependency 'org.hamcrest:hamcrest-all:1.3' dependency 'org.jsoup:jsoup:1.15.3' dependency 'org.mindrot:jbcrypt:0.4' - dependency('org.mockito:mockito-core:4.10.0') { + dependency('org.mockito:mockito-core:5.0.0') { exclude 'org.hamcrest:hamcrest-core' } - dependency('org.mockito:mockito-inline:4.10.0') + dependency('org.mockito:mockito-inline:5.0.0') dependency 'org.mybatis:mybatis:3.5.11' dependency 'org.nanohttpd:nanohttpd:2.3.1' dependencySet(group: 'org.slf4j', version: '2.0.6') { diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/LoadCrossProjectDuplicationsRepositoryStepTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/LoadCrossProjectDuplicationsRepositoryStepTest.java index d58045c6bfc..339e122a292 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/LoadCrossProjectDuplicationsRepositoryStepTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/LoadCrossProjectDuplicationsRepositoryStepTest.java @@ -19,7 +19,6 @@ */ package org.sonar.ce.task.projectanalysis.step; -import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -211,45 +210,42 @@ public class LoadCrossProjectDuplicationsRepositoryStepTest { underTest.execute(new TestComputationStepContext()); - Class> listClass = (Class>) (Class) ArrayList.class; - ArgumentCaptor> originBlocks = ArgumentCaptor.forClass(listClass); - ArgumentCaptor> duplicationBlocks = ArgumentCaptor.forClass(listClass); + ArgumentCaptor> originBlocks = ArgumentCaptor.forClass(List.class); + ArgumentCaptor> duplicationBlocks = ArgumentCaptor.forClass(List.class); verify(integrateCrossProjectDuplications).computeCpd(eq(CURRENT_FILE), originBlocks.capture(), duplicationBlocks.capture()); Map originBlocksByIndex = blocksByIndexInFile(originBlocks.getValue()); - assertThat(originBlocksByIndex.get(0)).isEqualTo( - new Block.Builder() + assertThat(originBlocksByIndex).containsExactly( + Map.entry(0, new Block.Builder() .setResourceId(CURRENT_FILE_KEY) .setBlockHash(new ByteArray(originBlock1.getHash())) .setIndexInFile(0) .setLines(originBlock1.getStartLine(), originBlock1.getEndLine()) .setUnit(originBlock1.getStartTokenIndex(), originBlock1.getEndTokenIndex()) - .build()); - assertThat(originBlocksByIndex.get(1)).isEqualTo( - new Block.Builder() + .build()), + Map.entry(1, new Block.Builder() .setResourceId(CURRENT_FILE_KEY) .setBlockHash(new ByteArray(originBlock2.getHash())) .setIndexInFile(1) .setLines(originBlock2.getStartLine(), originBlock2.getEndLine()) .setUnit(originBlock2.getStartTokenIndex(), originBlock2.getEndTokenIndex()) - .build()); + .build())); Map duplicationBlocksByIndex = blocksByIndexInFile(duplicationBlocks.getValue()); - assertThat(duplicationBlocksByIndex.get(0)).isEqualTo( - new Block.Builder() + assertThat(duplicationBlocksByIndex).containsExactly( + Map.entry(0, new Block.Builder() .setResourceId(otherFile.getKey()) .setBlockHash(new ByteArray(originBlock1.getHash())) .setIndexInFile(duplicate1.getIndexInFile()) .setLines(duplicate1.getStartLine(), duplicate1.getEndLine()) - .build()); - assertThat(duplicationBlocksByIndex.get(1)).isEqualTo( - new Block.Builder() + .build()), + Map.entry(1, new Block.Builder() .setResourceId(otherFile.getKey()) .setBlockHash(new ByteArray(originBlock2.getHash())) .setIndexInFile(duplicate2.getIndexInFile()) .setLines(duplicate2.getStartLine(), duplicate2.getEndLine()) - .build()); + .build())); } @Test diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/charset/MssqlMetadataReaderTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/charset/MssqlMetadataReaderTest.java index 5a757e52214..d02f100f67c 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/charset/MssqlMetadataReaderTest.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/charset/MssqlMetadataReaderTest.java @@ -21,12 +21,9 @@ package org.sonar.server.platform.db.migration.charset; import java.sql.Connection; import java.sql.SQLException; -import java.util.Arrays; -import java.util.List; import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.same; import static org.mockito.Mockito.mock; @@ -40,12 +37,12 @@ public class MssqlMetadataReaderTest { @Test public void test_getDefaultCollation() throws SQLException { - answerSelect(Arrays.asList(new String[] {"Latin1_General_CS_AS"})); + answerSelect("Latin1_General_CS_AS"); assertThat(underTest.getDefaultCollation(connection)).isEqualTo("Latin1_General_CS_AS"); } - private void answerSelect(List firstRequest) throws SQLException { - when(sqlExecutor.select(same(connection), anyString(), any(SqlExecutor.StringsConverter.class))).thenReturn(firstRequest); + private void answerSelect(String charset) throws SQLException { + when(sqlExecutor.selectSingleString(same(connection), anyString())).thenReturn(charset); } } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/charset/OracleCharsetHandlerTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/charset/OracleCharsetHandlerTest.java index e5ccbf0f0fc..9979f35fd1e 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/charset/OracleCharsetHandlerTest.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/charset/OracleCharsetHandlerTest.java @@ -21,12 +21,10 @@ package org.sonar.server.platform.db.migration.charset; import java.sql.Connection; import java.sql.SQLException; -import java.util.Collections; import javax.annotation.Nullable; import org.junit.Test; import org.sonar.api.utils.MessageException; -import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; @@ -86,7 +84,7 @@ public class OracleCharsetHandlerTest { } private void answerCharset(@Nullable String charset) throws SQLException { - when(sqlExecutor.select(any(Connection.class), anyString(), any(SqlExecutor.StringsConverter.class))) - .thenReturn(charset == null ? Collections.emptyList() : singletonList(new String[] {charset})); + when(sqlExecutor.selectSingleString(any(Connection.class), anyString())) + .thenReturn(charset); } } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/charset/PostgresMetadataReaderTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/charset/PostgresMetadataReaderTest.java index 0b4f8cc2637..82fb958c56e 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/charset/PostgresMetadataReaderTest.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/charset/PostgresMetadataReaderTest.java @@ -21,12 +21,9 @@ package org.sonar.server.platform.db.migration.charset; import java.sql.Connection; import java.sql.SQLException; -import java.util.Arrays; -import java.util.List; import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.same; import static org.mockito.Mockito.mock; @@ -40,13 +37,13 @@ public class PostgresMetadataReaderTest { @Test public void test_getDefaultCharset() throws SQLException { - answerSelect(Arrays.asList(new String[] {"latin"})); + answerSelect("latin"); assertThat(underTest.getDefaultCharset(connection)).isEqualTo("latin"); } - private void answerSelect(List firstRequest) throws SQLException { - when(sqlExecutor.select(same(connection), anyString(), any(SqlExecutor.StringsConverter.class))).thenReturn(firstRequest); + private void answerSelect(String charset) throws SQLException { + when(sqlExecutor.selectSingleString(same(connection), anyString())).thenReturn(charset); } } diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/notification/NotificationServiceTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/notification/NotificationServiceTest.java index d843696b383..17bdbeeda24 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/notification/NotificationServiceTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/notification/NotificationServiceTest.java @@ -23,7 +23,6 @@ import com.google.common.collect.ImmutableSet; import java.util.Collections; import java.util.List; import java.util.Optional; -import java.util.Random; import java.util.stream.Collectors; import java.util.stream.IntStream; import org.junit.Before; @@ -37,7 +36,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.ArgumentMatchers.anyCollection; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.reset; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoInteractions; @@ -56,11 +54,11 @@ public class NotificationServiceTest { @Test public void deliverEmails_fails_with_IAE_if_type_of_collection_is_Notification() { - NotificationHandler handler = mock(NotificationHandler1.class); - List notifications = IntStream.range(0, 1 + new Random().nextInt(20)) + NotificationHandler handler = getMockOfNotificationHandlerForType(Notification1.class); + List notifications = IntStream.range(0, 10) .mapToObj(i -> new Notification("i")) .collect(Collectors.toList()); - NotificationService underTest = new NotificationService(dbClient, new NotificationHandler[] {handler}); + NotificationService underTest = new NotificationService(dbClient, new NotificationHandler[]{handler}); assertThatThrownBy(() -> underTest.deliverEmails(notifications)) .isInstanceOf(IllegalArgumentException.class) @@ -69,7 +67,7 @@ public class NotificationServiceTest { @Test public void deliverEmails_collection_has_no_effect_if_no_handler_nor_dispatcher() { - List notifications = IntStream.range(0, 1 + new Random().nextInt(20)) + List notifications = IntStream.range(0, 10) .mapToObj(i -> mock(Notification.class)) .collect(Collectors.toList()); NotificationService underTest = new NotificationService(dbClient); @@ -81,10 +79,10 @@ public class NotificationServiceTest { @Test public void deliverEmails_collection_has_no_effect_if_no_handler() { NotificationDispatcher dispatcher = mock(NotificationDispatcher.class); - List notifications = IntStream.range(0, new Random().nextInt(20)) + List notifications = IntStream.range(0, 10) .mapToObj(i -> mock(Notification.class)) .collect(Collectors.toList()); - NotificationService underTest = new NotificationService(dbClient, new NotificationDispatcher[] {dispatcher}); + NotificationService underTest = new NotificationService(dbClient, new NotificationDispatcher[]{dispatcher}); assertThat(underTest.deliverEmails(notifications)).isZero(); verifyNoInteractions(dispatcher); @@ -93,10 +91,10 @@ public class NotificationServiceTest { @Test public void deliverEmails_collection_returns_0_if_collection_is_empty() { - NotificationHandler1 handler1 = mock(NotificationHandler1.class); - NotificationHandler2 handler2 = mock(NotificationHandler2.class); + NotificationHandler handler1 = getMockOfNotificationHandlerForType(Notification1.class); + NotificationHandler handler2 = getMockOfNotificationHandlerForType(Notification2.class); NotificationService underTest = new NotificationService(dbClient, - new NotificationHandler[] {handler1, handler2}); + new NotificationHandler[]{handler1, handler2}); assertThat(underTest.deliverEmails(Collections.emptyList())).isZero(); verifyNoInteractions(handler1, handler2); @@ -104,17 +102,17 @@ public class NotificationServiceTest { @Test public void deliverEmails_collection_returns_0_if_no_handler_for_the_notification_class() { - NotificationHandler1 handler1 = mock(NotificationHandler1.class); - NotificationHandler2 handler2 = mock(NotificationHandler2.class); - List notification1s = IntStream.range(0, 1 + new Random().nextInt(20)) + NotificationHandler handler1 = getMockOfNotificationHandlerForType(Notification1.class); + NotificationHandler handler2 = getMockOfNotificationHandlerForType(Notification2.class); + List notification1s = IntStream.range(0, 10) .mapToObj(i -> new Notification1()) .collect(Collectors.toList()); - List notification2s = IntStream.range(0, 1 + new Random().nextInt(20)) + List notification2s = IntStream.range(0, 10) .mapToObj(i -> new Notification2()) .collect(Collectors.toList()); NotificationService noHandler = new NotificationService(dbClient); - NotificationService onlyHandler1 = new NotificationService(dbClient, new NotificationHandler[] {handler1}); - NotificationService onlyHandler2 = new NotificationService(dbClient, new NotificationHandler[] {handler2}); + NotificationService onlyHandler1 = new NotificationService(dbClient, new NotificationHandler[]{handler1}); + NotificationService onlyHandler2 = new NotificationService(dbClient, new NotificationHandler[]{handler2}); assertThat(noHandler.deliverEmails(notification1s)).isZero(); assertThat(noHandler.deliverEmails(notification2s)).isZero(); @@ -126,96 +124,89 @@ public class NotificationServiceTest { @Test public void deliverEmails_collection_calls_deliver_method_of_handler_for_notification_class_and_returns_its_output() { - Random random = new Random(); - NotificationHandler1 handler1 = mock(NotificationHandler1.class); - NotificationHandler2 handler2 = mock(NotificationHandler2.class); - List notification1s = IntStream.range(0, 1 + random.nextInt(20)) + NotificationHandler handler1 = getMockOfNotificationHandlerForType(Notification1.class); + NotificationHandler handler2 = getMockOfNotificationHandlerForType(Notification2.class); + List notification1s = IntStream.range(0, 10) .mapToObj(i -> new Notification1()) .collect(Collectors.toList()); - List notification2s = IntStream.range(0, 1 + random.nextInt(20)) + List notification2s = IntStream.range(0, 10) .mapToObj(i -> new Notification2()) .collect(Collectors.toList()); - NotificationService onlyHandler1 = new NotificationService(dbClient, new NotificationHandler[] {handler1}); - NotificationService onlyHandler2 = new NotificationService(dbClient, new NotificationHandler[] {handler2}); - NotificationService bothHandlers = new NotificationService(dbClient, new NotificationHandler[] {handler1, handler2}); + NotificationService onlyHandler1 = new NotificationService(dbClient, new NotificationHandler[]{handler1}); + NotificationService onlyHandler2 = new NotificationService(dbClient, new NotificationHandler[]{handler2}); + NotificationService bothHandlers = new NotificationService(dbClient, new NotificationHandler[]{handler1, handler2}); - int expected = randomDeliveredCount(notification1s); + int expected = notification1s.size() - 1; when(handler1.deliver(notification1s)).thenReturn(expected); assertThat(onlyHandler1.deliverEmails(notification1s)).isEqualTo(expected); - verify(handler1).deliver(notification1s); + verify(handler1, times(1)).deliver(notification1s); verify(handler2, times(0)).deliver(anyCollection()); - reset(handler1, handler2); - expected = randomDeliveredCount(notification2s); + expected = notification2s.size() - 2; when(handler2.deliver(notification2s)).thenReturn(expected); assertThat(onlyHandler2.deliverEmails(notification2s)).isEqualTo(expected); - verify(handler2).deliver(notification2s); - verify(handler1, times(0)).deliver(anyCollection()); + verify(handler2, times(1)).deliver(notification2s); + verify(handler1, times(1)).deliver(anyCollection()); - reset(handler1, handler2); - expected = randomDeliveredCount(notification1s); + expected = notification1s.size() - 1; when(handler1.deliver(notification1s)).thenReturn(expected); assertThat(bothHandlers.deliverEmails(notification1s)).isEqualTo(expected); - verify(handler1).deliver(notification1s); - verify(handler2, times(0)).deliver(anyCollection()); + verify(handler1, times(2)).deliver(notification1s); + verify(handler2, times(1)).deliver(anyCollection()); - reset(handler1, handler2); - expected = randomDeliveredCount(notification2s); + expected = notification2s.size() - 2; when(handler2.deliver(notification2s)).thenReturn(expected); assertThat(bothHandlers.deliverEmails(notification2s)).isEqualTo(expected); - verify(handler2).deliver(notification2s); - verify(handler1, times(0)).deliver(anyCollection()); + verify(handler2, times(2)).deliver(notification2s); + verify(handler1, times(2)).deliver(anyCollection()); } @Test public void deliver_calls_deliver_method_on_each_handler_for_notification_class_and_returns_sum_of_their_outputs() { - Random random = new Random(); - NotificationHandler1A handler1A = mock(NotificationHandler1A.class); - NotificationHandler1B handler1B = mock(NotificationHandler1B.class); - NotificationHandler2 handler2 = mock(NotificationHandler2.class); - List notification1s = IntStream.range(0, 1 + random.nextInt(20)) + NotificationHandler handler1A = getMockOfNotificationHandlerForType(Notification1.class); + NotificationHandler handler1B = getMockOfNotificationHandlerForType(Notification1.class); + NotificationHandler handler2 = getMockOfNotificationHandlerForType(Notification2.class); + + List notification1s = IntStream.range(0, 10) .mapToObj(i -> new Notification1()) .collect(Collectors.toList()); - List notification2s = IntStream.range(0, 1 + random.nextInt(20)) + List notification2s = IntStream.range(0, 10) .mapToObj(i -> new Notification2()) .collect(Collectors.toList()); - NotificationService onlyHandler1A = new NotificationService(dbClient, new NotificationHandler[] {handler1A}); - NotificationService onlyHandler1B = new NotificationService(dbClient, new NotificationHandler[] {handler1B}); - NotificationService bothHandlers = new NotificationService(dbClient, new NotificationHandler[] {handler1A, handler1B}); - NotificationService allHandlers = new NotificationService(dbClient, new NotificationHandler[] {handler1A, handler1B, handler2}); + NotificationService onlyHandler1A = new NotificationService(dbClient, new NotificationHandler[]{handler1A}); + NotificationService onlyHandler1B = new NotificationService(dbClient, new NotificationHandler[]{handler1B}); + NotificationService bothHandlers = new NotificationService(dbClient, new NotificationHandler[]{handler1A, handler1B}); + NotificationService allHandlers = new NotificationService(dbClient, new NotificationHandler[]{handler1A, handler1B, handler2}); - int expected = randomDeliveredCount(notification1s); + int expected = notification1s.size() - 1; when(onlyHandler1A.deliverEmails(notification1s)).thenReturn(expected); assertThat(onlyHandler1A.deliverEmails(notification1s)).isEqualTo(expected); - verify(handler1A).deliver(notification1s); + verify(handler1A, times(1)).deliver(notification1s); verify(handler1B, times(0)).deliver(anyCollection()); verify(handler2, times(0)).deliver(anyCollection()); - reset(handler1A, handler1B, handler2); - expected = randomDeliveredCount(notification1s); + expected = notification1s.size() - 1; when(handler1B.deliver(notification1s)).thenReturn(expected); assertThat(onlyHandler1B.deliverEmails(notification1s)).isEqualTo(expected); - verify(handler1B).deliver(notification1s); - verify(handler1A, times(0)).deliver(anyCollection()); + verify(handler1B, times(1)).deliver(notification1s); + verify(handler1A, times(1)).deliver(anyCollection()); verify(handler2, times(0)).deliver(anyCollection()); - reset(handler1A, handler1B, handler2); - expected = randomDeliveredCount(notification1s); - int expected2 = randomDeliveredCount(notification1s); + expected = notification1s.size() - 1; + int expected2 = notification1s.size() - 2; when(handler1A.deliver(notification1s)).thenReturn(expected); when(handler1B.deliver(notification1s)).thenReturn(expected2); assertThat(bothHandlers.deliverEmails(notification1s)).isEqualTo(expected + expected2); - verify(handler1A).deliver(notification1s); - verify(handler1B).deliver(notification1s); + verify(handler1A, times(2)).deliver(notification1s); + verify(handler1B, times(2)).deliver(notification1s); verify(handler2, times(0)).deliver(anyCollection()); - reset(handler1A, handler1B, handler2); - expected = randomDeliveredCount(notification2s); + expected = notification2s.size() - 2; when(handler2.deliver(notification2s)).thenReturn(expected); assertThat(allHandlers.deliverEmails(notification2s)).isEqualTo(expected); - verify(handler2).deliver(notification2s); - verify(handler1A, times(0)).deliver(anyCollection()); - verify(handler1B, times(0)).deliver(anyCollection()); + verify(handler2, times(1)).deliver(notification2s); + verify(handler1A, times(2)).deliver(anyCollection()); + verify(handler1B, times(2)).deliver(anyCollection()); } @Test @@ -232,16 +223,20 @@ public class NotificationServiceTest { String dispatcherKey1A = randomAlphabetic(5); String dispatcherKey1B = randomAlphabetic(6); String projectUuid = randomAlphabetic(7); - NotificationHandler1A handler1A = mock(NotificationHandler1A.class); + + NotificationHandler handler1A = getMockOfNotificationHandlerForType(Notification1.class); when(handler1A.getMetadata()).thenReturn(Optional.of(NotificationDispatcherMetadata.create(dispatcherKey1A))); - NotificationHandler1B handler1B = mock(NotificationHandler1B.class); + + NotificationHandler handler1B = getMockOfNotificationHandlerForType(Notification1.class); when(handler1B.getMetadata()).thenReturn(Optional.of(NotificationDispatcherMetadata.create(dispatcherKey1B))); - NotificationHandler2 handler2 = mock(NotificationHandler2.class); + + NotificationHandler handler2 = getMockOfNotificationHandlerForType(Notification2.class); when(handler2.getMetadata()).thenReturn(Optional.empty()); - boolean expected = new Random().nextBoolean(); + + boolean expected = true; when(propertiesDao.hasProjectNotificationSubscribersForDispatchers(projectUuid, ImmutableSet.of(dispatcherKey1A, dispatcherKey1B))) .thenReturn(expected); - NotificationService underTest = new NotificationService(dbClient, new NotificationHandler[] {handler1A, handler1B, handler2}); + NotificationService underTest = new NotificationService(dbClient, new NotificationHandler[]{handler1A, handler1B, handler2}); boolean flag = underTest.hasProjectSubscribersForTypes(projectUuid, ImmutableSet.of(Notification1.class)); @@ -262,19 +257,22 @@ public class NotificationServiceTest { String dispatcherKey1B = randomAlphabetic(6); String dispatcherKey2 = randomAlphabetic(7); String projectUuid = randomAlphabetic(8); - NotificationHandler1A handler1A = mock(NotificationHandler1A.class); + NotificationHandler handler1A = getMockOfNotificationHandlerForType(Notification1.class); when(handler1A.getMetadata()).thenReturn(Optional.of(NotificationDispatcherMetadata.create(dispatcherKey1A))); - NotificationHandler1B handler1B = mock(NotificationHandler1B.class); + + NotificationHandler handler1B = getMockOfNotificationHandlerForType(Notification1.class); when(handler1B.getMetadata()).thenReturn(Optional.of(NotificationDispatcherMetadata.create(dispatcherKey1B))); - NotificationHandler2 handler2 = mock(NotificationHandler2.class); + + NotificationHandler handler2 = getMockOfNotificationHandlerForType(Notification2.class); when(handler2.getMetadata()).thenReturn(Optional.of(NotificationDispatcherMetadata.create(dispatcherKey2))); - boolean expected1 = new Random().nextBoolean(); + + boolean expected1 = false; when(propertiesDao.hasProjectNotificationSubscribersForDispatchers(projectUuid, ImmutableSet.of(dispatcherKey1A, dispatcherKey1B, dispatcherKey2))) .thenReturn(expected1); - boolean expected2 = new Random().nextBoolean(); + boolean expected2 = true; when(propertiesDao.hasProjectNotificationSubscribersForDispatchers(projectUuid, ImmutableSet.of(dispatcherKey2))) .thenReturn(expected2); - NotificationService underTest = new NotificationService(dbClient, new NotificationHandler[] {handler1A, handler1B, handler2}); + NotificationService underTest = new NotificationService(dbClient, new NotificationHandler[]{handler1A, handler1B, handler2}); boolean flag = underTest.hasProjectSubscribersForTypes(projectUuid, ImmutableSet.of(Notification1.class, Notification2.class)); @@ -295,13 +293,17 @@ public class NotificationServiceTest { String dispatcherKey1A = randomAlphabetic(5); String dispatcherKey1B = randomAlphabetic(6); String projectUuid = randomAlphabetic(7); - NotificationHandler1A handler1A = mock(NotificationHandler1A.class); + + NotificationHandler handler1A = getMockOfNotificationHandlerForType(Notification1.class); when(handler1A.getMetadata()).thenReturn(Optional.of(NotificationDispatcherMetadata.create(dispatcherKey1A))); - NotificationHandler1B handler1B = mock(NotificationHandler1B.class); + + NotificationHandler handler1B = getMockOfNotificationHandlerForType(Notification1.class); when(handler1B.getMetadata()).thenReturn(Optional.of(NotificationDispatcherMetadata.create(dispatcherKey1B))); - NotificationHandler2 handler2 = mock(NotificationHandler2.class); + + NotificationHandler handler2 = getMockOfNotificationHandlerForType(Notification2.class); when(handler2.getMetadata()).thenReturn(Optional.empty()); - NotificationService underTest = new NotificationService(dbClient, new NotificationHandler[] {handler1A, handler1B, handler2}); + + NotificationService underTest = new NotificationService(dbClient, new NotificationHandler[]{handler1A, handler1B, handler2}); boolean flag = underTest.hasProjectSubscribersForTypes(projectUuid, ImmutableSet.of()); @@ -315,11 +317,14 @@ public class NotificationServiceTest { String dispatcherKey1A = randomAlphabetic(5); String dispatcherKey1B = randomAlphabetic(6); String projectUuid = randomAlphabetic(7); - NotificationHandler1A handler1A = mock(NotificationHandler1A.class); + + NotificationHandler handler1A = getMockOfNotificationHandlerForType(Notification1.class); when(handler1A.getMetadata()).thenReturn(Optional.of(NotificationDispatcherMetadata.create(dispatcherKey1A))); - NotificationHandler1B handler1B = mock(NotificationHandler1B.class); + + NotificationHandler handler1B = getMockOfNotificationHandlerForType(Notification1.class); when(handler1B.getMetadata()).thenReturn(Optional.of(NotificationDispatcherMetadata.create(dispatcherKey1B))); - NotificationService underTest = new NotificationService(dbClient, new NotificationHandler[] {handler1A, handler1B}); + + NotificationService underTest = new NotificationService(dbClient, new NotificationHandler[]{handler1A, handler1B}); boolean flag = underTest.hasProjectSubscribersForTypes(projectUuid, ImmutableSet.of(Notification2.class)); @@ -335,34 +340,10 @@ public class NotificationServiceTest { } } - private static abstract class NotificationHandler1 implements NotificationHandler { - - // final to prevent mock to override implementation - @Override - public final Class getNotificationClass() { - return Notification1.class; - } - - } - - private static abstract class NotificationHandler1A implements NotificationHandler { - - // final to prevent mock to override implementation - @Override - public final Class getNotificationClass() { - return Notification1.class; - } - - } - - private static abstract class NotificationHandler1B implements NotificationHandler { - - // final to prevent mock to override implementation - @Override - public final Class getNotificationClass() { - return Notification1.class; - } - + private NotificationHandler getMockOfNotificationHandlerForType(Class notificationClass) { + NotificationHandler mock = mock(NotificationHandler.class); + when(mock.getNotificationClass()).thenReturn(notificationClass); + return mock; } private static final class Notification2 extends Notification { @@ -371,21 +352,4 @@ public class NotificationServiceTest { super("2"); } } - - private static abstract class NotificationHandler2 implements NotificationHandler { - - // final to prevent mock to override implementation - @Override - public final Class getNotificationClass() { - return Notification2.class; - } - } - - private static int randomDeliveredCount(List notifications) { - int size = notifications.size(); - if (size == 1) { - return size; - } - return 1 + new Random().nextInt(size - 1); - } } diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/notification/NotificationMediumTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/notification/NotificationMediumTest.java index 46028c7b070..d12143eb3da 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/notification/NotificationMediumTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/notification/NotificationMediumTest.java @@ -87,8 +87,8 @@ public class NotificationMediumTest { @Test public void scenario1() { setUpMocks(); - doAnswer(addUser(ASSIGNEE_SIMON, emailChannel)).when(commentOnIssueAssignedToMe).dispatch(same(notification), any(NotificationDispatcher.Context.class)); - doAnswer(addUser(CREATOR_SIMON, emailChannel)).when(commentOnIssueCreatedByMe).dispatch(same(notification), any(NotificationDispatcher.Context.class)); + doAnswer(addUser(ASSIGNEE_SIMON, emailChannel)).when(commentOnIssueAssignedToMe).performDispatch(same(notification), any(NotificationDispatcher.Context.class)); + doAnswer(addUser(CREATOR_SIMON, emailChannel)).when(commentOnIssueCreatedByMe).performDispatch(same(notification), any(NotificationDispatcher.Context.class)); underTest.start(); verify(emailChannel, timeout(2000)).deliver(notification, ASSIGNEE_SIMON); @@ -111,8 +111,8 @@ public class NotificationMediumTest { @Test public void scenario2() { setUpMocks(); - doAnswer(addUser(ASSIGNEE_SIMON, emailChannel)).when(commentOnIssueAssignedToMe).dispatch(same(notification), any(NotificationDispatcher.Context.class)); - doAnswer(addUser(CREATOR_EVGENY, gtalkChannel)).when(commentOnIssueCreatedByMe).dispatch(same(notification), any(NotificationDispatcher.Context.class)); + doAnswer(addUser(ASSIGNEE_SIMON, emailChannel)).when(commentOnIssueAssignedToMe).performDispatch(same(notification), any(NotificationDispatcher.Context.class)); + doAnswer(addUser(CREATOR_EVGENY, gtalkChannel)).when(commentOnIssueCreatedByMe).performDispatch(same(notification), any(NotificationDispatcher.Context.class)); underTest.start(); verify(emailChannel, timeout(2000)).deliver(notification, ASSIGNEE_SIMON); @@ -136,8 +136,8 @@ public class NotificationMediumTest { @Test public void scenario3() { setUpMocks(); - doAnswer(addUser(ASSIGNEE_SIMON, new NotificationChannel[] {emailChannel, gtalkChannel})) - .when(commentOnIssueAssignedToMe).dispatch(same(notification), any(NotificationDispatcher.Context.class)); + doAnswer(addUser(ASSIGNEE_SIMON, new NotificationChannel[]{emailChannel, gtalkChannel})) + .when(commentOnIssueAssignedToMe).performDispatch(same(notification), any(NotificationDispatcher.Context.class)); underTest.start(); verify(emailChannel, timeout(2000)).deliver(notification, ASSIGNEE_SIMON); @@ -174,8 +174,8 @@ public class NotificationMediumTest { public void shouldNotStopWhenException() { setUpMocks(); when(manager.getFromQueue()).thenThrow(new RuntimeException("Unexpected exception")).thenReturn(notification).thenReturn(null); - doAnswer(addUser(ASSIGNEE_SIMON, emailChannel)).when(commentOnIssueAssignedToMe).dispatch(same(notification), any(NotificationDispatcher.Context.class)); - doAnswer(addUser(CREATOR_SIMON, emailChannel)).when(commentOnIssueCreatedByMe).dispatch(same(notification), any(NotificationDispatcher.Context.class)); + doAnswer(addUser(ASSIGNEE_SIMON, emailChannel)).when(commentOnIssueAssignedToMe).performDispatch(same(notification), any(NotificationDispatcher.Context.class)); + doAnswer(addUser(CREATOR_SIMON, emailChannel)).when(commentOnIssueCreatedByMe).performDispatch(same(notification), any(NotificationDispatcher.Context.class)); underTest.start(); verify(emailChannel, timeout(2000)).deliver(notification, ASSIGNEE_SIMON); @@ -187,7 +187,7 @@ public class NotificationMediumTest { @Test public void shouldNotAddNullAsUser() { setUpMocks(); - doAnswer(addUser(null, gtalkChannel)).when(commentOnIssueCreatedByMe).dispatch(same(notification), any(NotificationDispatcher.Context.class)); + doAnswer(addUser(null, gtalkChannel)).when(commentOnIssueCreatedByMe).performDispatch(same(notification), any(NotificationDispatcher.Context.class)); underTest.start(); underTest.stop(); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualityprofile/QProfileFactoryImplTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualityprofile/QProfileFactoryImplTest.java index 66f490d2f5a..88f2b3b28c5 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualityprofile/QProfileFactoryImplTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualityprofile/QProfileFactoryImplTest.java @@ -19,8 +19,8 @@ */ package org.sonar.server.qualityprofile; +import java.util.Collection; import java.util.Collections; -import java.util.Set; import org.assertj.core.api.AbstractObjectAssert; import org.assertj.core.api.ThrowableAssert.ThrowingCallable; import org.junit.Before; @@ -288,11 +288,10 @@ public class QProfileFactoryImplTest { } private void verifyCallActiveRuleIndexerDelete(String... expectedRuleProfileUuids) { - Class> setClass = (Class>) (Class) Set.class; - ArgumentCaptor> setCaptor = ArgumentCaptor.forClass(setClass); - verify(activeRuleIndexer).commitDeletionOfProfiles(any(DbSession.class), setCaptor.capture()); + ArgumentCaptor> collectionCaptor = ArgumentCaptor.forClass(Collection.class); + verify(activeRuleIndexer).commitDeletionOfProfiles(any(DbSession.class), collectionCaptor.capture()); - assertThat(setCaptor.getValue()) + assertThat(collectionCaptor.getValue()) .extracting(QProfileDto::getKee) .containsExactlyInAnyOrder(expectedRuleProfileUuids); } -- 2.39.5