From 85f7069b331b4276cacdbb2dc54145f2dd770f23 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Fri, 25 Nov 2016 01:10:22 +0100 Subject: [PATCH] Getting rid of getLoggedInUser() method --- .../org/craftedsw/tripservicekata/trip/TripService.java | 14 +++----------- .../craftedsw/tripservicekata/trip/TripServiceShould.java | 11 +++-------- 2 files changed, 6 insertions(+), 19 deletions(-) diff --git a/TDD/src/main/java/org/craftedsw/tripservicekata/trip/TripService.java b/TDD/src/main/java/org/craftedsw/tripservicekata/trip/TripService.java index 6a195b8..139ae0a 100644 --- a/TDD/src/main/java/org/craftedsw/tripservicekata/trip/TripService.java +++ b/TDD/src/main/java/org/craftedsw/tripservicekata/trip/TripService.java @@ -9,12 +9,12 @@ import org.craftedsw.tripservicekata.user.UserSession; public class TripService { - public List getTripsByUser(User user) throws UserNotLoggedInException { - if (getLoggedInUser() == null) { + public List getTripsByUser(User user, User loggedInUser) throws UserNotLoggedInException { + if (loggedInUser == null) { throw new UserNotLoggedInException(); } - return user.isFriendsWith(getLoggedInUser()) + return user.isFriendsWith(loggedInUser) ? tripsBy(user) : noTrips(); } @@ -28,12 +28,4 @@ public class TripService { tripList = TripDAO.findTripsByUser(user); return tripList; } - - // In MVC the Model layer should know nothing about the view. - // Service is in Model layer, so we have to get rid of this method. - protected User getLoggedInUser() { - User loggedUser = UserSession.getInstance().getLoggedUser(); - return loggedUser; - } - } diff --git a/TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripServiceShould.java b/TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripServiceShould.java index a41d1cd..73f2c24 100644 --- a/TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripServiceShould.java +++ b/TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripServiceShould.java @@ -32,7 +32,7 @@ public class TripServiceShould { throw_an_exception_when_user_is_not_logged_in() { loggedInUser = GUEST; - tripService.getTripsByUser(UNUSED_USER); + tripService.getTripsByUser(UNUSED_USER, GUEST); } @Test public void @@ -42,7 +42,7 @@ public class TripServiceShould { .withTrips(TO_BRAZIL) .build(); - List friendTrips = tripService.getTripsByUser(friend); + List friendTrips = tripService.getTripsByUser(friend, loggedInUser); // You must always begin writing the assert. // Remember: the assert must match the unit test method's name!! // In this case, no trips must be returned. @@ -56,7 +56,7 @@ public class TripServiceShould { .withTrips(TO_BRAZIL, TO_BERLIN) .build(); - List friendTrips = tripService.getTripsByUser(friend); + List friendTrips = tripService.getTripsByUser(friend, loggedInUser); // You must always begin writing the assert. // Remember: the assert must match the unit test method's name!! // In this case, no trips must be returned. @@ -66,11 +66,6 @@ public class TripServiceShould { private class TesteableTripService extends TripService { @Override - protected User getLoggedInUser() { - return loggedInUser; - } - - @Override protected List tripsBy(User user) { return user.trips(); } -- 2.1.4