From a50618b6f5e11ac4a27e07a50baf7b3d21a43fce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Cholewi=C5=84ski?= Date: Mon, 23 Dec 2024 22:23:49 +0100 Subject: [PATCH] mockito.homework package with tests --- kodilla-advanced-tests/build.gradle | 1 + .../java/com/kodilla/mockito/MobilePhone.java | 8 ++ .../kodilla/mockito/NotificationService.java | 26 +++++++ .../com/kodilla/mockito/homework/Alert.java | 4 + .../kodilla/mockito/homework/Subscriber.java | 5 ++ .../mockito/homework/WeatherAlertService.java | 44 +++++++++++ .../java/com/kodilla/notification/Client.java | 5 ++ .../kodilla/notification/Notification.java | 4 + .../kodilla/mockito/MobilePhoneTestSuite.java | 28 +++++++ .../mockito/NotificationServiceTestSuite.java | 69 ++++++++++++++++++ .../WeatherAlertServiceTestSuite.java | 73 +++++++++++++++++++ 11 files changed, 267 insertions(+) create mode 100644 kodilla-advanced-tests/src/main/java/com/kodilla/mockito/MobilePhone.java create mode 100644 kodilla-advanced-tests/src/main/java/com/kodilla/mockito/NotificationService.java create mode 100644 kodilla-advanced-tests/src/main/java/com/kodilla/mockito/homework/Alert.java create mode 100644 kodilla-advanced-tests/src/main/java/com/kodilla/mockito/homework/Subscriber.java create mode 100644 kodilla-advanced-tests/src/main/java/com/kodilla/mockito/homework/WeatherAlertService.java create mode 100644 kodilla-advanced-tests/src/main/java/com/kodilla/notification/Client.java create mode 100644 kodilla-advanced-tests/src/main/java/com/kodilla/notification/Notification.java create mode 100644 kodilla-advanced-tests/src/test/java/com/kodilla/mockito/MobilePhoneTestSuite.java create mode 100644 kodilla-advanced-tests/src/test/java/com/kodilla/mockito/NotificationServiceTestSuite.java create mode 100644 kodilla-advanced-tests/src/test/java/com/kodilla/mockito/homework/WeatherAlertServiceTestSuite.java diff --git a/kodilla-advanced-tests/build.gradle b/kodilla-advanced-tests/build.gradle index b19a9c7..d1a447c 100644 --- a/kodilla-advanced-tests/build.gradle +++ b/kodilla-advanced-tests/build.gradle @@ -12,6 +12,7 @@ repositories { dependencies { testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.3' testImplementation 'org.junit.jupiter:junit-jupiter-params:5.9.3' + testImplementation 'org.mockito:mockito-core:5.3.1' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.3' } diff --git a/kodilla-advanced-tests/src/main/java/com/kodilla/mockito/MobilePhone.java b/kodilla-advanced-tests/src/main/java/com/kodilla/mockito/MobilePhone.java new file mode 100644 index 0000000..7556786 --- /dev/null +++ b/kodilla-advanced-tests/src/main/java/com/kodilla/mockito/MobilePhone.java @@ -0,0 +1,8 @@ +package com.kodilla.mockito; + +public interface MobilePhone { + + boolean needsCharging(); + double getFreeStorage(); + void launchApplication(String applicationName); +} \ No newline at end of file diff --git a/kodilla-advanced-tests/src/main/java/com/kodilla/mockito/NotificationService.java b/kodilla-advanced-tests/src/main/java/com/kodilla/mockito/NotificationService.java new file mode 100644 index 0000000..4b3d3c7 --- /dev/null +++ b/kodilla-advanced-tests/src/main/java/com/kodilla/mockito/NotificationService.java @@ -0,0 +1,26 @@ +package com.kodilla.mockito; + +import com.kodilla.notification.Client; +import com.kodilla.notification.Notification; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class NotificationService { + + private Set clients = new HashSet<>(); + + public void addSubscriber(Client client) { + clients.add(client); + } + + public void sendNotification(Notification notification) { + clients.forEach(client -> client.receive(notification)); + } + + public void removeSubscriber(Client client) { + clients.remove(client); + } +} \ No newline at end of file diff --git a/kodilla-advanced-tests/src/main/java/com/kodilla/mockito/homework/Alert.java b/kodilla-advanced-tests/src/main/java/com/kodilla/mockito/homework/Alert.java new file mode 100644 index 0000000..973eee4 --- /dev/null +++ b/kodilla-advanced-tests/src/main/java/com/kodilla/mockito/homework/Alert.java @@ -0,0 +1,4 @@ +package com.kodilla.mockito.homework; + +public interface Alert { +} diff --git a/kodilla-advanced-tests/src/main/java/com/kodilla/mockito/homework/Subscriber.java b/kodilla-advanced-tests/src/main/java/com/kodilla/mockito/homework/Subscriber.java new file mode 100644 index 0000000..cf8debc --- /dev/null +++ b/kodilla-advanced-tests/src/main/java/com/kodilla/mockito/homework/Subscriber.java @@ -0,0 +1,5 @@ +package com.kodilla.mockito.homework; + +public interface Subscriber { + void receive(Alert alert); +} diff --git a/kodilla-advanced-tests/src/main/java/com/kodilla/mockito/homework/WeatherAlertService.java b/kodilla-advanced-tests/src/main/java/com/kodilla/mockito/homework/WeatherAlertService.java new file mode 100644 index 0000000..29ef66b --- /dev/null +++ b/kodilla-advanced-tests/src/main/java/com/kodilla/mockito/homework/WeatherAlertService.java @@ -0,0 +1,44 @@ +package com.kodilla.mockito.homework; + +import com.kodilla.notification.Client; +import com.kodilla.notification.Notification; + +import java.util.*; +import java.util.stream.Collectors; + +public class WeatherAlertService { + Map> subscriptions = new ArrayList<>(Arrays.asList("Warszawa", "Kraków", "Katowice")) + .stream() + .collect(Collectors.toMap(localization -> localization, localization -> new HashSet<>())); + + public void addSubscriberToLocalization(Subscriber subscriber, String localization) { + if (subscriptions.containsKey(localization)) { + subscriptions.get(localization).add(subscriber); + } + } + + public void removeSubscriberFromLocalization(Subscriber subscriber, String localization) { + if (subscriptions.containsKey(localization)) { + subscriptions.get(localization).remove(subscriber); + } + } + + public void removeSubscriberFromAllLocalizations(Subscriber subscriber) { + subscriptions.values().forEach(subscribers -> subscribers.remove(subscriber)); + } + + public void sendNotificationToAllSubscribers(Alert alert) { + Set allSubscribers = subscriptions.values().stream().flatMap(subscribers -> subscribers.stream()).collect(Collectors.toSet()); + allSubscribers.forEach(subscriber -> subscriber.receive(alert)); + } + + public void sendNotificationToLocalSubscribers(Alert alert, String localization) { + if (subscriptions.containsKey(localization)) { + subscriptions.get(localization).forEach(subscriber -> subscriber.receive(alert)); + } + } + + public void removeLocalization(String localization) { + subscriptions.remove(localization); + } +} diff --git a/kodilla-advanced-tests/src/main/java/com/kodilla/notification/Client.java b/kodilla-advanced-tests/src/main/java/com/kodilla/notification/Client.java new file mode 100644 index 0000000..130246a --- /dev/null +++ b/kodilla-advanced-tests/src/main/java/com/kodilla/notification/Client.java @@ -0,0 +1,5 @@ +package com.kodilla.notification; + +public interface Client { + void receive(Notification notification); +} diff --git a/kodilla-advanced-tests/src/main/java/com/kodilla/notification/Notification.java b/kodilla-advanced-tests/src/main/java/com/kodilla/notification/Notification.java new file mode 100644 index 0000000..eee4461 --- /dev/null +++ b/kodilla-advanced-tests/src/main/java/com/kodilla/notification/Notification.java @@ -0,0 +1,4 @@ +package com.kodilla.notification; + +public interface Notification { +} diff --git a/kodilla-advanced-tests/src/test/java/com/kodilla/mockito/MobilePhoneTestSuite.java b/kodilla-advanced-tests/src/test/java/com/kodilla/mockito/MobilePhoneTestSuite.java new file mode 100644 index 0000000..080997e --- /dev/null +++ b/kodilla-advanced-tests/src/test/java/com/kodilla/mockito/MobilePhoneTestSuite.java @@ -0,0 +1,28 @@ +package com.kodilla.mockito; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +public class MobilePhoneTestSuite { + MobilePhone myPhone = Mockito.mock(MobilePhone.class); + + @Test + public void testDefaultBehaviourOfTestDouble() { + Assertions.assertFalse(myPhone.needsCharging()); + Assertions.assertEquals(0.0, myPhone.getFreeStorage()); + } + + @Test + public void testExpectation() { + Assertions.assertFalse(myPhone.needsCharging()); + Mockito.when(myPhone.needsCharging()).thenReturn(true); + Assertions.assertTrue(myPhone.needsCharging()); + } + + @Test + public void shouldCallLaunchApplication() { + myPhone.launchApplication("Tetris4D"); + Mockito.verify(myPhone).launchApplication("Tetris4D"); + } +} diff --git a/kodilla-advanced-tests/src/test/java/com/kodilla/mockito/NotificationServiceTestSuite.java b/kodilla-advanced-tests/src/test/java/com/kodilla/mockito/NotificationServiceTestSuite.java new file mode 100644 index 0000000..8bc1b1a --- /dev/null +++ b/kodilla-advanced-tests/src/test/java/com/kodilla/mockito/NotificationServiceTestSuite.java @@ -0,0 +1,69 @@ +package com.kodilla.mockito; + +import com.kodilla.notification.Client; +import com.kodilla.notification.Notification; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +public class NotificationServiceTestSuite { + + NotificationService notificationService = new NotificationService(); + Client client = Mockito.mock(Client.class); + Client secondClient = Mockito.mock(Client.class); + Client thirdClient = Mockito.mock(Client.class); + Notification notification = Mockito.mock(Notification.class); + + @Test + public void notSubscribedClientShouldNotReceiveNotification() { + notificationService.sendNotification(notification); + + Mockito.verify(client, Mockito.never()).receive(notification); + } + + @Test + public void subscribedClientShouldReceiveNotification() { + notificationService.addSubscriber(client); + + notificationService.sendNotification(notification); + + Mockito.verify(client).receive(notification); + } + + @Test + public void notificationShouldBeSentToAllSubscribedClients() { + addSubscribers(client, secondClient, thirdClient); + + notificationService.sendNotification(notification); + + Mockito.verify(client).receive(notification); + Mockito.verify(secondClient).receive(notification); + Mockito.verify(thirdClient).receive(notification); + } + + + @Test + public void shouldSendOnlyOneNotificationToMultiTimeSubscriber() { + addSubscribers(client, client, client); + + notificationService.sendNotification(notification); + + Mockito.verify(client).receive(notification); + } + + @Test + public void unsubscribedClientShouldNotReceiveNotification() { + notificationService.addSubscriber(client); + notificationService.removeSubscriber(client); + + notificationService.sendNotification(notification); + + Mockito.verify(client, Mockito.never()).receive(notification); + } + + private void addSubscribers(Client... clients) { + for(Client currentClient : clients) { + notificationService.addSubscriber(currentClient); + } + } + +} \ No newline at end of file diff --git a/kodilla-advanced-tests/src/test/java/com/kodilla/mockito/homework/WeatherAlertServiceTestSuite.java b/kodilla-advanced-tests/src/test/java/com/kodilla/mockito/homework/WeatherAlertServiceTestSuite.java new file mode 100644 index 0000000..6d4baa7 --- /dev/null +++ b/kodilla-advanced-tests/src/test/java/com/kodilla/mockito/homework/WeatherAlertServiceTestSuite.java @@ -0,0 +1,73 @@ +package com.kodilla.mockito.homework; + +import com.kodilla.notification.Client; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +public class WeatherAlertServiceTestSuite { + WeatherAlertService service = new WeatherAlertService(); + Subscriber firstSubscriber = Mockito.mock(Subscriber.class); + Subscriber secondSubscriber = Mockito.mock(Subscriber.class); + Subscriber thirdSubscriber = Mockito.mock(Subscriber.class); + Alert alert = Mockito.mock(Alert.class); + + @Test + public void activeSubscriberShouldReceiveAlerts() { + service.addSubscriberToLocalization(firstSubscriber, "Warszawa"); + service.addSubscriberToLocalization(firstSubscriber, "Kraków"); + service.addSubscriberToLocalization(secondSubscriber, "Kraków"); + service.sendNotificationToLocalSubscribers(alert, "Warszawa"); + service.sendNotificationToLocalSubscribers(alert, "Kraków"); + + Mockito.verify(firstSubscriber, Mockito.times(2)).receive(alert); + Mockito.verify(secondSubscriber, Mockito.times(1)).receive(alert); + } + + @Test + public void locallyRemovedSubscriberShouldNotReceiveAlerts() { + service.addSubscriberToLocalization(firstSubscriber, "Warszawa"); + service.removeSubscriberFromLocalization(firstSubscriber, "Warszawa"); + service.sendNotificationToLocalSubscribers(alert, "Warszawa"); + + Mockito.verify(firstSubscriber, Mockito.never()).receive(alert); + } + + @Test + public void completelyRemovedSubscriberShouldNotReceiveAlerts() { + service.addSubscriberToLocalization(firstSubscriber, "Warszawa"); + service.addSubscriberToLocalization(firstSubscriber, "Kraków"); + service.removeSubscriberFromAllLocalizations(firstSubscriber); + service.sendNotificationToLocalSubscribers(alert, "Warszawa"); + service.sendNotificationToLocalSubscribers(alert, "Kraków"); + service.sendNotificationToAllSubscribers(alert); + + Mockito.verify(firstSubscriber, Mockito.never()).receive(alert); + } + + @Test + public void locallyInactiveSubscriberShouldNotReceiveAlerts() { + service.addSubscriberToLocalization(firstSubscriber, "Kraków"); + service.addSubscriberToLocalization(secondSubscriber, "Warszawa"); + service.sendNotificationToLocalSubscribers(alert, "Warszawa"); + service.sendNotificationToLocalSubscribers(alert, "Kraków"); + Mockito.verify(firstSubscriber, Mockito.times(1)).receive(alert); + Mockito.verify(secondSubscriber, Mockito.times(1)).receive(alert); + } + + @Test + public void allSubscribersShouldReceiveGeneralAlerts() { + service.addSubscriberToLocalization(firstSubscriber, "Kraków"); + service.addSubscriberToLocalization(secondSubscriber, "Warszawa"); + service.sendNotificationToAllSubscribers(alert); + Mockito.verify(firstSubscriber, Mockito.times(1)).receive(alert); + Mockito.verify(secondSubscriber, Mockito.times(1)).receive(alert); + } + + @Test + public void subscriberShouldNotReceiveAlertsForRemovedLocalization() { + service.addSubscriberToLocalization(secondSubscriber, "Warszawa"); + service.removeLocalization("Warszawa"); + service.sendNotificationToLocalSubscribers(alert, "Warszawa"); + Mockito.verify(firstSubscriber, Mockito.never()).receive(alert); + } +}