Last active
March 27, 2019 22:26
-
-
Save dschinkel/4f7cd509d21b71f3ed660ac3db3a8145 to your computer and use it in GitHub Desktop.
Pillar Bootcamp
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
package subscriptions; | |
import dependencies.User; | |
import dependencies.UserMailer; | |
import dependencies.UserRepository; | |
public class EmailSweeper { | |
UserMailer mailer; | |
UserRepository repository; | |
String unpaidMessage = "Please renew your subscription to Ferret Fancy!"; | |
public EmailSweeper(UserRepository repository, UserMailer mailer) { | |
this.mailer = mailer; | |
this.repository = repository; | |
} | |
public void sweep() { | |
User firstUser = repository.getUsers().stream().findFirst().get(); | |
mailer.sendBillingProblemEmailTo(firstUser, unpaidMessage); | |
} | |
} |
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
package subscriptions; | |
import dependencies.User; | |
import dependencies.UserMailer; | |
import dependencies.UserRepository; | |
import java.time.LocalDate; | |
import java.util.List; | |
public class EmailSweeper { | |
UserMailer mailer; | |
UserRepository repository; | |
public EmailSweeper(UserRepository repository, UserMailer mailer) { | |
this.mailer = mailer; | |
this.repository = repository; | |
} | |
public void sweep() { | |
// get user | |
// checkUnpaidDate | |
// Check if range > 3 mo | |
User user = repository.getUsers().get(0); | |
mailer.sendBillingProblemEmailTo(user, "Please renew your subscription to Ferret Fancy!"); | |
} | |
} |
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
import dependencies.User; | |
import org.junit.Ignore; | |
import org.junit.Test; | |
import subscriptions.EmailSweeper; | |
import static org.junit.Assert.*; | |
public class EmailSweeperTest { | |
UserMailerSpy userMailerSpy = new UserMailerSpy(); | |
UserRepositoryStub userRepositoryStub = new UserRepositoryStub(); | |
EmailSweeper sweeper = new EmailSweeper(userRepositoryStub, userMailerSpy); | |
@Test | |
public void sendsEmail() { | |
sweeper.sweep(); | |
assertTrue(userMailerSpy.emailSent); | |
} | |
@Test | |
public void sendsEmailToAUser() { | |
User user = new UserRepositoryStub().getUsers().stream().findFirst().get(); | |
sweeper.sweep(); | |
assertEquals(user, userMailerSpy.userSentTo); | |
} | |
@Test | |
public void sendsEmailWithUnpaidMessage() { | |
String message = "Please renew your subscription to Ferret Fancy!"; | |
sweeper.sweep(); | |
assertEquals(message, userMailerSpy.emailMessage); | |
} | |
@Ignore | |
@Test | |
public void sendsEmailToUnpaidUser() { | |
User user = new UserRepositoryStub().getUsers().stream().findFirst().get(); | |
user.setLastPaidDate(null); | |
sweeper.sweep(); | |
assertEquals(userMailerSpy.userSentTo.getLastPaidDate(), null); | |
assertEquals(userMailerSpy.emailSent, true); | |
} | |
} |
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
import dependencies.*; | |
import org.junit.Ignore; | |
import org.junit.Test; | |
import subscriptions.EmailSweeper; | |
import java.time.LocalDate; | |
import java.util.Date; | |
import static org.mockito.Mockito.*; | |
public class EmailSweeperTest { | |
UserMailer mailerMock = mock(RealUserMailer.class); | |
UserRepository repositoryMock = mock(RealUserRepository.class); | |
EmailSweeper sweeper = new EmailSweeper(repositoryMock, mailerMock); | |
@Test | |
public void sendsEmail() { | |
sweeper.sweep(); | |
verify(mailerMock, times(1)).sendBillingProblemEmailTo(any(), anyString()); | |
} | |
@Test | |
public void sendsUnpaidEmailMessage() { | |
String unpaidMessage = "Please renew your subscription to Ferret Fancy!"; | |
User testUser = new User(false, null, null ); | |
sweeper.sweep(); | |
verify(mailerMock, times(1)).sendBillingProblemEmailTo(eq(testUser), eq(unpaidMessage)); | |
} | |
@Test | |
@Ignore | |
public void sendsEmailToUnpaidUser() { | |
LocalDate localDate = LocalDate.of(2018, 12, 24); | |
User testUser = new User(false, localDate, null ); | |
String unpaidMessage = "Please renew your subscription to Ferret Fancy!"; | |
sweeper.sweep(); | |
verify(mailerMock,times(1) ).sendBillingProblemEmailTo(eq(testUser), eq(unpaidMessage)); | |
} | |
// sends email to unpaid user | |
// sends email to multiple users | |
} |
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
import dependencies.User; | |
import dependencies.UserMailer; | |
public class UserMailerSpy implements UserMailer { | |
public boolean emailSent = false; | |
public User userSentTo; | |
public String emailMessage; | |
@Override | |
public void sendBillingProblemEmailTo(User user, String message) { | |
emailMessage = message; | |
userSentTo = user; | |
emailSent = true; | |
} | |
} |
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
import dependencies.User; | |
import dependencies.UserRepository; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class UserRepositoryStub implements UserRepository { | |
@Override | |
public List<User> getUsers() { | |
return new ArrayList<User>(){{ | |
add(new User(false, null, null)); | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment