Getting rid of getLoggedInUser() method
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Fri, 25 Nov 2016 00:10:22 +0000 (01:10 +0100)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Fri, 25 Nov 2016 00:10:22 +0000 (01:10 +0100)
TDD/src/main/java/org/craftedsw/tripservicekata/trip/TripService.java
TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripServiceShould.java

index 6a195b8..139ae0a 100644 (file)
@@ -9,12 +9,12 @@ import org.craftedsw.tripservicekata.user.UserSession;
 \r
 public class TripService {\r
 \r
-       public List<Trip> getTripsByUser(User user) throws UserNotLoggedInException {\r
-               if (getLoggedInUser() == null) {\r
+       public List<Trip> getTripsByUser(User user, User loggedInUser) throws UserNotLoggedInException {\r
+               if (loggedInUser == null) {\r
                        throw new UserNotLoggedInException();\r
                }\r
                \r
-               return user.isFriendsWith(getLoggedInUser())\r
+               return user.isFriendsWith(loggedInUser)\r
                                ? tripsBy(user)\r
                            : noTrips();\r
        }\r
@@ -28,12 +28,4 @@ public class TripService {
                tripList = TripDAO.findTripsByUser(user);\r
                return tripList;\r
        }\r
-\r
-       // In MVC the Model layer should know nothing about the view.\r
-       // Service is in Model layer, so we have to get rid of this method.\r
-       protected User getLoggedInUser() {\r
-               User loggedUser = UserSession.getInstance().getLoggedUser();\r
-               return loggedUser;\r
-       }\r
-       \r
 }\r
index a41d1cd..73f2c24 100644 (file)
@@ -32,7 +32,7 @@ public class TripServiceShould {
        throw_an_exception_when_user_is_not_logged_in() {\r
                loggedInUser = GUEST;\r
                \r
-               tripService.getTripsByUser(UNUSED_USER);\r
+               tripService.getTripsByUser(UNUSED_USER, GUEST);\r
        }\r
        \r
        @Test public void\r
@@ -42,7 +42,7 @@ public class TripServiceShould {
                                                .withTrips(TO_BRAZIL)\r
                                                .build();\r
                \r
-               List<Trip> friendTrips = tripService.getTripsByUser(friend); \r
+               List<Trip> friendTrips = tripService.getTripsByUser(friend, loggedInUser); \r
                // You must always begin writing the assert.\r
                // Remember: the assert must match the unit test method's name!!\r
                // In this case, no trips must be returned.\r
@@ -56,7 +56,7 @@ public class TripServiceShould {
                                                .withTrips(TO_BRAZIL, TO_BERLIN)\r
                                                .build();\r
                \r
-               List<Trip> friendTrips = tripService.getTripsByUser(friend); \r
+               List<Trip> friendTrips = tripService.getTripsByUser(friend, loggedInUser); \r
                // You must always begin writing the assert.\r
                // Remember: the assert must match the unit test method's name!!\r
                // In this case, no trips must be returned.\r
@@ -66,11 +66,6 @@ public class TripServiceShould {
        private class TesteableTripService extends TripService {\r
 \r
                @Override\r
-               protected User getLoggedInUser() {\r
-                       return loggedInUser;\r
-               }\r
-\r
-               @Override\r
                protected List<Trip> tripsBy(User user) {\r
                        return user.trips();\r
                }\r