Commit 9342677d authored by mlaminebendib's avatar mlaminebendib
Browse files

Add tests for the refactoring branch

parent bbb4412e
Pipeline #895 canceled with stages
......@@ -10,6 +10,7 @@
<hamcrest.version>1.1</hamcrest.version>
<junit.version>4.8.2</junit.version>
<mockito.version>1.8.5</mockito.version>
<assertJ.version>3.11.1</assertJ.version>
<spring.version>3.1.1.RELEASE</spring.version>
</properties>
......@@ -43,5 +44,12 @@
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertJ.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
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();
}
}
}
package org.craftedsw.tripservicekata.trip;
import java.util.List;
import org.craftedsw.tripservicekata.exception.CollaboratorCallException;
import org.craftedsw.tripservicekata.user.User;
import java.util.List;
public class TripDAO {
public static List<Trip> findTripsByUser(User user) {
throw new CollaboratorCallException(
"TripDAO should not be invoked on an unit test.");
public List<Trip> tripsBy(User user) {
return user.trips();
}
}
\ No newline at end of file
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;
import java.util.ArrayList;
import java.util.List;
public class TripService {
private TripDAO tripDAO = new TripDAO();
private UserSession userSession = UserSession.getInstance();
public List<Trip> getTripsByUser(User user) throws UserNotLoggedInException {
List<Trip> tripList = new ArrayList<Trip>();
User loggedUser = UserSession.getInstance().getLoggedUser();
User loggedUser = userSession.getLoggedUser();
boolean isFriend = false;
if (loggedUser != null) {
for (User friend : user.getFriends()) {
......@@ -21,7 +25,7 @@ public class TripService {
}
}
if (isFriend) {
tripList = TripDAO.findTripsByUser(user);
tripList = tripDAO.tripsBy(user);
}
return tripList;
} else {
......
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;
}
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");
}
public User getLoggedUser() {
return new User();
}
}
package org.craftedsw.tripservicekata.trip;
import org.craftedsw.tripservicekata.exception.UserNotLoggedInException;
import org.craftedsw.tripservicekata.user.User;
import org.craftedsw.tripservicekata.user.UserSession;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.craftedsw.tripservicekata.user.UserBuilder.aUser;
import static org.mockito.BDDMockito.given;
@RunWith(MockitoJUnitRunner.class)
public class TripServiceTest {
private static final User UNUSED_USER = null;
private static final User ANOTHER_USER = new User();
private static final Trip TO_BRAZIL = new Trip();
private static final User GUEST = null;
private static final User LOGGED_IN_USER = new User();
private static final Trip TO_LONDON = new Trip();
@Mock
private TripDAO tripDAO;
@Mock
private UserSession userSession;
@InjectMocks
private TripService realTripService = new TripService();
@Test(expected = UserNotLoggedInException.class)
public void should_throw_an_exception_when_user_is_not_logged_in() {
realTripService.getTripsByUser(UNUSED_USER);
}
@Test
public void should_not_return_any_trips_when_users_are_not_friends() {
User friend = aUser()
.friendsWith(ANOTHER_USER)
.withTrips(TO_BRAZIL)
.build();
given(userSession.getLoggedUser()).willReturn(LOGGED_IN_USER);
given(tripDAO.tripsBy(friend)).willReturn(friend.trips());
List<Trip> friendTrips = realTripService.getTripsByUser(friend);
assertThat(friendTrips).hasSize(0);
}
@Test
public void should_return_trips_when_users_are_friends() {
User friend = aUser()
.friendsWith(ANOTHER_USER, LOGGED_IN_USER)
.withTrips(TO_BRAZIL, TO_LONDON)
.build();
given(userSession.getLoggedUser()).willReturn(LOGGED_IN_USER);
given(tripDAO.tripsBy(friend)).willReturn(friend.trips());
List<Trip> friendTrips = realTripService.getTripsByUser(friend);
assertThat(friendTrips).hasSize(2);
assertThat(friendTrips).containsExactly(TO_BRAZIL, TO_LONDON);
}
}
package org.craftedsw.tripservicekata.user;
import org.craftedsw.tripservicekata.trip.Trip;
public class UserBuilder {
private User[] friends = new User[] {};
private Trip[] trips = new Trip[] {};
public static UserBuilder aUser() {
return new UserBuilder();
}
public UserBuilder withTrips(Trip... trips) {
this.trips = trips;
return this;
}
public UserBuilder friendsWith(User... friends) {
this.friends = friends;
return this;
}
public User build() {
User user = new User();
addTripsTo(user);
addFriendsTo(user);
return user;
}
private void addFriendsTo(User user) {
for (User friend : friends) {
user.addFriend(friend);
}
}
private void addTripsTo(User user) {
for (Trip trip : trips) {
user.addTrip(trip);
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment