Sandro Mancuso, tripservicekata.
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 20 Nov 2016 21:42:43 +0000 (22:42 +0100)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 20 Nov 2016 21:42:43 +0000 (22:42 +0100)
TDD/src/main/java/org/craftedsw/tripservicekata/TripService_Original.java [new file with mode: 0644]
TDD/src/main/java/org/craftedsw/tripservicekata/exception/CollaboratorCallException.java [new file with mode: 0644]
TDD/src/main/java/org/craftedsw/tripservicekata/exception/UserNotLoggedInException.java [new file with mode: 0644]
TDD/src/main/java/org/craftedsw/tripservicekata/trip/Trip.java [new file with mode: 0644]
TDD/src/main/java/org/craftedsw/tripservicekata/trip/TripDAO.java [new file with mode: 0644]
TDD/src/main/java/org/craftedsw/tripservicekata/trip/TripService.java [new file with mode: 0644]
TDD/src/main/java/org/craftedsw/tripservicekata/user/User.java [new file with mode: 0644]
TDD/src/main/java/org/craftedsw/tripservicekata/user/UserSession.java [new file with mode: 0644]
TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripDAOTest.java [new file with mode: 0644]
TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripServiceTest.java [new file with mode: 0644]
TDD/src/test/java/org/craftedsw/tripservicekata/user/UserTest.java [new file with mode: 0644]

diff --git a/TDD/src/main/java/org/craftedsw/tripservicekata/TripService_Original.java b/TDD/src/main/java/org/craftedsw/tripservicekata/TripService_Original.java
new file mode 100644 (file)
index 0000000..a2ba5ee
--- /dev/null
@@ -0,0 +1,34 @@
+package org.craftedsw.tripservicekata;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.craftedsw.tripservicekata.exception.UserNotLoggedInException;
+import org.craftedsw.tripservicekata.trip.Trip;
+import org.craftedsw.tripservicekata.trip.TripDAO;
+import org.craftedsw.tripservicekata.user.User;
+import org.craftedsw.tripservicekata.user.UserSession;
+
+public class TripService_Original {
+
+       public List<Trip> getTripsByUser(User user) throws UserNotLoggedInException {
+               List<Trip> tripList = new ArrayList<Trip>();
+               User loggedUser = UserSession.getInstance().getLoggedUser();
+               boolean isFriend = false;
+               if (loggedUser != null) {
+                       for (User friend : user.getFriends()) {
+                               if (friend.equals(loggedUser)) {
+                                       isFriend = true;
+                                       break;
+                               }
+                       }
+                       if (isFriend) {
+                               tripList = TripDAO.findTripsByUser(user);
+                       }
+                       return tripList;
+               } else {
+                       throw new UserNotLoggedInException();
+               }
+       }
+       
+}
diff --git a/TDD/src/main/java/org/craftedsw/tripservicekata/exception/CollaboratorCallException.java b/TDD/src/main/java/org/craftedsw/tripservicekata/exception/CollaboratorCallException.java
new file mode 100644 (file)
index 0000000..828353e
--- /dev/null
@@ -0,0 +1,25 @@
+package org.craftedsw.tripservicekata.exception;
+
+public class CollaboratorCallException extends RuntimeException {
+
+       private static final long serialVersionUID = -4584041339906109902L;
+
+       public CollaboratorCallException() {
+               super();
+       }
+
+       public CollaboratorCallException(String message,
+                       Throwable cause) {
+               super(message, cause);
+       }
+
+       public CollaboratorCallException(String message) {
+               super(message);
+       }
+
+       public CollaboratorCallException(Throwable cause) {
+               super(cause);
+       }
+
+       
+}
diff --git a/TDD/src/main/java/org/craftedsw/tripservicekata/exception/UserNotLoggedInException.java b/TDD/src/main/java/org/craftedsw/tripservicekata/exception/UserNotLoggedInException.java
new file mode 100644 (file)
index 0000000..880bf20
--- /dev/null
@@ -0,0 +1,7 @@
+package org.craftedsw.tripservicekata.exception;
+
+public class UserNotLoggedInException extends RuntimeException {
+
+       private static final long serialVersionUID = 8959479918185637340L;
+
+}
diff --git a/TDD/src/main/java/org/craftedsw/tripservicekata/trip/Trip.java b/TDD/src/main/java/org/craftedsw/tripservicekata/trip/Trip.java
new file mode 100644 (file)
index 0000000..f12ff3c
--- /dev/null
@@ -0,0 +1,5 @@
+package org.craftedsw.tripservicekata.trip;
+
+public class Trip {
+
+}
diff --git a/TDD/src/main/java/org/craftedsw/tripservicekata/trip/TripDAO.java b/TDD/src/main/java/org/craftedsw/tripservicekata/trip/TripDAO.java
new file mode 100644 (file)
index 0000000..f054f60
--- /dev/null
@@ -0,0 +1,15 @@
+package org.craftedsw.tripservicekata.trip;
+
+import java.util.List;
+
+import org.craftedsw.tripservicekata.exception.CollaboratorCallException;
+import org.craftedsw.tripservicekata.user.User;
+
+public class TripDAO {
+
+       public static List<Trip> findTripsByUser(User user) {
+               throw new CollaboratorCallException(
+                               "TripDAO should not be invoked on an unit test.");
+       }
+       
+}
\ No newline at end of file
diff --git a/TDD/src/main/java/org/craftedsw/tripservicekata/trip/TripService.java b/TDD/src/main/java/org/craftedsw/tripservicekata/trip/TripService.java
new file mode 100644 (file)
index 0000000..7836c95
--- /dev/null
@@ -0,0 +1,32 @@
+package org.craftedsw.tripservicekata.trip;\r
+\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+\r
+import org.craftedsw.tripservicekata.exception.UserNotLoggedInException;\r
+import org.craftedsw.tripservicekata.user.User;\r
+import org.craftedsw.tripservicekata.user.UserSession;\r
+\r
+public class TripService {\r
+\r
+       public List<Trip> getTripsByUser(User user) throws UserNotLoggedInException {\r
+               List<Trip> tripList = new ArrayList<Trip>();\r
+               User loggedUser = UserSession.getInstance().getLoggedUser();\r
+               boolean isFriend = false;\r
+               if (loggedUser != null) {\r
+                       for (User friend : user.getFriends()) {\r
+                               if (friend.equals(loggedUser)) {\r
+                                       isFriend = true;\r
+                                       break;\r
+                               }\r
+                       }\r
+                       if (isFriend) {\r
+                               tripList = TripDAO.findTripsByUser(user);\r
+                       }\r
+                       return tripList;\r
+               } else {\r
+                       throw new UserNotLoggedInException();\r
+               }\r
+       }\r
+       \r
+}\r
diff --git a/TDD/src/main/java/org/craftedsw/tripservicekata/user/User.java b/TDD/src/main/java/org/craftedsw/tripservicekata/user/User.java
new file mode 100644 (file)
index 0000000..c0442ff
--- /dev/null
@@ -0,0 +1,29 @@
+package org.craftedsw.tripservicekata.user;\r
+\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+\r
+import org.craftedsw.tripservicekata.trip.Trip;\r
+\r
+public class User {\r
+\r
+       private List<Trip> trips = new ArrayList<Trip>();\r
+       private List<User> friends = new ArrayList<User>();\r
+       \r
+       public List<User> getFriends() {\r
+               return friends;\r
+       }\r
+       \r
+       public void addFriend(User user) {\r
+               friends.add(user);\r
+       }\r
+\r
+       public void addTrip(Trip trip) {\r
+               trips.add(trip);\r
+       }\r
+       \r
+       public List<Trip> trips() {\r
+               return trips;\r
+       }\r
+\r
+}\r
diff --git a/TDD/src/main/java/org/craftedsw/tripservicekata/user/UserSession.java b/TDD/src/main/java/org/craftedsw/tripservicekata/user/UserSession.java
new file mode 100644 (file)
index 0000000..6b91c0d
--- /dev/null
@@ -0,0 +1,21 @@
+package org.craftedsw.tripservicekata.user;
+
+import org.craftedsw.tripservicekata.exception.CollaboratorCallException;
+
+public class UserSession {
+
+       private static final UserSession userSession = new UserSession();
+       
+       private UserSession() {
+       }
+       
+       public static UserSession getInstance() {
+               return userSession;
+       }
+
+       public User getLoggedUser() {
+               throw new CollaboratorCallException(
+                               "UserSession.getLoggedUser() should not be called in an unit test");
+       }
+
+}
diff --git a/TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripDAOTest.java b/TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripDAOTest.java
new file mode 100644 (file)
index 0000000..6482bca
--- /dev/null
@@ -0,0 +1,5 @@
+package org.craftedsw.tripservicekata.trip;
+
+public class TripDAOTest {
+
+}
diff --git a/TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripServiceTest.java b/TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripServiceTest.java
new file mode 100644 (file)
index 0000000..2559aad
--- /dev/null
@@ -0,0 +1,11 @@
+package org.craftedsw.tripservicekata.trip;\r
+\r
+import org.junit.Test;\r
+\r
+public class TripServiceTest {\r
+       \r
+       @Test public void\r
+       should_throw_an_exception_when_no_logged_user() {\r
+               \r
+       }\r
+}\r
diff --git a/TDD/src/test/java/org/craftedsw/tripservicekata/user/UserTest.java b/TDD/src/test/java/org/craftedsw/tripservicekata/user/UserTest.java
new file mode 100644 (file)
index 0000000..28b2a37
--- /dev/null
@@ -0,0 +1,5 @@
+package org.craftedsw.tripservicekata.user;
+
+public class UserTest {
+
+}