-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ba178ef
commit a50618b
Showing
11 changed files
with
267 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
kodilla-advanced-tests/src/main/java/com/kodilla/mockito/MobilePhone.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.kodilla.mockito; | ||
|
||
public interface MobilePhone { | ||
|
||
boolean needsCharging(); | ||
double getFreeStorage(); | ||
void launchApplication(String applicationName); | ||
} |
26 changes: 26 additions & 0 deletions
26
kodilla-advanced-tests/src/main/java/com/kodilla/mockito/NotificationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Client> 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); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
kodilla-advanced-tests/src/main/java/com/kodilla/mockito/homework/Alert.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.kodilla.mockito.homework; | ||
|
||
public interface Alert { | ||
} |
5 changes: 5 additions & 0 deletions
5
kodilla-advanced-tests/src/main/java/com/kodilla/mockito/homework/Subscriber.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.kodilla.mockito.homework; | ||
|
||
public interface Subscriber { | ||
void receive(Alert alert); | ||
} |
44 changes: 44 additions & 0 deletions
44
kodilla-advanced-tests/src/main/java/com/kodilla/mockito/homework/WeatherAlertService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, Set<Subscriber>> 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<Subscriber> 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); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
kodilla-advanced-tests/src/main/java/com/kodilla/notification/Client.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.kodilla.notification; | ||
|
||
public interface Client { | ||
void receive(Notification notification); | ||
} |
4 changes: 4 additions & 0 deletions
4
kodilla-advanced-tests/src/main/java/com/kodilla/notification/Notification.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.kodilla.notification; | ||
|
||
public interface Notification { | ||
} |
28 changes: 28 additions & 0 deletions
28
kodilla-advanced-tests/src/test/java/com/kodilla/mockito/MobilePhoneTestSuite.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
kodilla-advanced-tests/src/test/java/com/kodilla/mockito/NotificationServiceTestSuite.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
|
||
} |
73 changes: 73 additions & 0 deletions
73
...vanced-tests/src/test/java/com/kodilla/mockito/homework/WeatherAlertServiceTestSuite.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |