From aa655fdb54443b843262fa34e1c7133bca24c7e2 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Sun, 20 Nov 2016 22:42:43 +0100 Subject: [PATCH] Sandro Mancuso, tripservicekata. --- .../tripservicekata/TripService_Original.java | 34 ++++++++++++++++++++++ .../exception/CollaboratorCallException.java | 25 ++++++++++++++++ .../exception/UserNotLoggedInException.java | 7 +++++ .../org/craftedsw/tripservicekata/trip/Trip.java | 5 ++++ .../craftedsw/tripservicekata/trip/TripDAO.java | 15 ++++++++++ .../tripservicekata/trip/TripService.java | 32 ++++++++++++++++++++ .../org/craftedsw/tripservicekata/user/User.java | 29 ++++++++++++++++++ .../tripservicekata/user/UserSession.java | 21 +++++++++++++ .../tripservicekata/trip/TripDAOTest.java | 5 ++++ .../tripservicekata/trip/TripServiceTest.java | 11 +++++++ .../craftedsw/tripservicekata/user/UserTest.java | 5 ++++ 11 files changed, 189 insertions(+) create mode 100644 TDD/src/main/java/org/craftedsw/tripservicekata/TripService_Original.java create mode 100644 TDD/src/main/java/org/craftedsw/tripservicekata/exception/CollaboratorCallException.java create mode 100644 TDD/src/main/java/org/craftedsw/tripservicekata/exception/UserNotLoggedInException.java create mode 100644 TDD/src/main/java/org/craftedsw/tripservicekata/trip/Trip.java create mode 100644 TDD/src/main/java/org/craftedsw/tripservicekata/trip/TripDAO.java create mode 100644 TDD/src/main/java/org/craftedsw/tripservicekata/trip/TripService.java create mode 100644 TDD/src/main/java/org/craftedsw/tripservicekata/user/User.java create mode 100644 TDD/src/main/java/org/craftedsw/tripservicekata/user/UserSession.java create mode 100644 TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripDAOTest.java create mode 100644 TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripServiceTest.java create mode 100644 TDD/src/test/java/org/craftedsw/tripservicekata/user/UserTest.java 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 index 0000000..a2ba5ee --- /dev/null +++ b/TDD/src/main/java/org/craftedsw/tripservicekata/TripService_Original.java @@ -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 getTripsByUser(User user) throws UserNotLoggedInException { + List tripList = new ArrayList(); + 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 index 0000000..828353e --- /dev/null +++ b/TDD/src/main/java/org/craftedsw/tripservicekata/exception/CollaboratorCallException.java @@ -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 index 0000000..880bf20 --- /dev/null +++ b/TDD/src/main/java/org/craftedsw/tripservicekata/exception/UserNotLoggedInException.java @@ -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 index 0000000..f12ff3c --- /dev/null +++ b/TDD/src/main/java/org/craftedsw/tripservicekata/trip/Trip.java @@ -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 index 0000000..f054f60 --- /dev/null +++ b/TDD/src/main/java/org/craftedsw/tripservicekata/trip/TripDAO.java @@ -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 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 index 0000000..7836c95 --- /dev/null +++ b/TDD/src/main/java/org/craftedsw/tripservicekata/trip/TripService.java @@ -0,0 +1,32 @@ +package org.craftedsw.tripservicekata.trip; + +import java.util.ArrayList; +import java.util.List; + +import org.craftedsw.tripservicekata.exception.UserNotLoggedInException; +import org.craftedsw.tripservicekata.user.User; +import org.craftedsw.tripservicekata.user.UserSession; + +public class TripService { + + public List getTripsByUser(User user) throws UserNotLoggedInException { + List tripList = new ArrayList(); + 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/user/User.java b/TDD/src/main/java/org/craftedsw/tripservicekata/user/User.java new file mode 100644 index 0000000..c0442ff --- /dev/null +++ b/TDD/src/main/java/org/craftedsw/tripservicekata/user/User.java @@ -0,0 +1,29 @@ +package org.craftedsw.tripservicekata.user; + +import java.util.ArrayList; +import java.util.List; + +import org.craftedsw.tripservicekata.trip.Trip; + +public class User { + + private List trips = new ArrayList(); + private List friends = new ArrayList(); + + public List getFriends() { + return friends; + } + + public void addFriend(User user) { + friends.add(user); + } + + public void addTrip(Trip trip) { + trips.add(trip); + } + + public List trips() { + return trips; + } + +} 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 index 0000000..6b91c0d --- /dev/null +++ b/TDD/src/main/java/org/craftedsw/tripservicekata/user/UserSession.java @@ -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 index 0000000..6482bca --- /dev/null +++ b/TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripDAOTest.java @@ -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 index 0000000..2559aad --- /dev/null +++ b/TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripServiceTest.java @@ -0,0 +1,11 @@ +package org.craftedsw.tripservicekata.trip; + +import org.junit.Test; + +public class TripServiceTest { + + @Test public void + should_throw_an_exception_when_no_logged_user() { + + } +} 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 index 0000000..28b2a37 --- /dev/null +++ b/TDD/src/test/java/org/craftedsw/tripservicekata/user/UserTest.java @@ -0,0 +1,5 @@ +package org.craftedsw.tripservicekata.user; + +public class UserTest { + +} -- 2.1.4