From 8220865251721d39efaabb1953dd599c75aa3328 Mon Sep 17 00:00:00 2001 From: Sergi Fernandez Date: Tue, 19 Dec 2023 18:03:20 +0100 Subject: [PATCH 1/2] Changed RouteOrigin to Follows and fixed some issues resulting from the change. --- .../softarch/myroutes/domain/RouteFollowed.java | 3 +-- .../handler/RouteFollowedEventHandler.java | 2 +- .../repository/RouteFollowedRepository.java | 7 +++---- .../steps/CreateRouteFollowedStepdefs.java | 5 ++--- .../steps/RetrieveRouteFollowedStepDefs.java | 14 -------------- .../softarch/myroutes/steps/RouteFollowedUtil.java | 7 ++----- .../features/RetrieveRouteFollowed.feature | 5 ----- 7 files changed, 9 insertions(+), 34 deletions(-) diff --git a/src/main/java/cat/udl/eps/softarch/myroutes/domain/RouteFollowed.java b/src/main/java/cat/udl/eps/softarch/myroutes/domain/RouteFollowed.java index 4b7e669..f73ae69 100644 --- a/src/main/java/cat/udl/eps/softarch/myroutes/domain/RouteFollowed.java +++ b/src/main/java/cat/udl/eps/softarch/myroutes/domain/RouteFollowed.java @@ -40,7 +40,6 @@ public class RouteFollowed extends UriEntity { @JsonProperty(access = JsonProperty.Access.READ_ONLY) @NotNull - @ManyToOne - public Route routeOrigin; + public String follows; } diff --git a/src/main/java/cat/udl/eps/softarch/myroutes/handler/RouteFollowedEventHandler.java b/src/main/java/cat/udl/eps/softarch/myroutes/handler/RouteFollowedEventHandler.java index f9233ad..9635c80 100644 --- a/src/main/java/cat/udl/eps/softarch/myroutes/handler/RouteFollowedEventHandler.java +++ b/src/main/java/cat/udl/eps/softarch/myroutes/handler/RouteFollowedEventHandler.java @@ -32,7 +32,7 @@ public void handleRouteFollowedPreCreate(RouteFollowed routeFollowed) { routeFollowed.setCreatedBy(user); List routeList = routeRepository.findByCreatedBy(user); if(!routeList.isEmpty()) - routeFollowed.setRouteOrigin(routeList.get(0)); + routeFollowed.setFollows(routeList.get(0).getUri()); routeFollowed.setDate(ZonedDateTime.now()); logger.info("Creation of new routeFollowed: {}", routeFollowed); } diff --git a/src/main/java/cat/udl/eps/softarch/myroutes/repository/RouteFollowedRepository.java b/src/main/java/cat/udl/eps/softarch/myroutes/repository/RouteFollowedRepository.java index 610ec1c..648ada2 100644 --- a/src/main/java/cat/udl/eps/softarch/myroutes/repository/RouteFollowedRepository.java +++ b/src/main/java/cat/udl/eps/softarch/myroutes/repository/RouteFollowedRepository.java @@ -1,6 +1,5 @@ package cat.udl.eps.softarch.myroutes.repository; -import cat.udl.eps.softarch.myroutes.domain.Route; import cat.udl.eps.softarch.myroutes.domain.RouteFollowed; import cat.udl.eps.softarch.myroutes.domain.User; import org.springframework.data.repository.CrudRepository; @@ -11,7 +10,7 @@ public interface RouteFollowedRepository extends CrudRepository, PagingAndSortingRepository { List findByIdContaining(@Param("long") Long id); - List findByCreatedBy(@Param("creator") User creator); - List findByRouteOrigin(@Param("origin") Route origin); - List findByCreatedByAndRouteOrigin(@Param("creator") User creator, @Param("origin") Route origin); + List findByCreatedBy(@Param("creator") User creator); + List findByFollows(@Param("follows") String follows); + List findByCreatedByAndFollows(@Param("creator") User creator, @Param("follows") String follows); } diff --git a/src/test/java/cat/udl/eps/softarch/myroutes/steps/CreateRouteFollowedStepdefs.java b/src/test/java/cat/udl/eps/softarch/myroutes/steps/CreateRouteFollowedStepdefs.java index 4810b10..1887047 100644 --- a/src/test/java/cat/udl/eps/softarch/myroutes/steps/CreateRouteFollowedStepdefs.java +++ b/src/test/java/cat/udl/eps/softarch/myroutes/steps/CreateRouteFollowedStepdefs.java @@ -6,7 +6,6 @@ import cat.udl.eps.softarch.myroutes.repository.RouteFollowedRepository; import cat.udl.eps.softarch.myroutes.repository.RouteRepository; import cat.udl.eps.softarch.myroutes.repository.UserRepository; -import com.fasterxml.jackson.core.JsonProcessingException; import io.cucumber.java.en.And; import io.cucumber.java.en.Given; import io.cucumber.java.en.When; @@ -39,7 +38,7 @@ public void iCreateARouteFollowedWithDateDurationLevelUpAndALevelDown(String dat Iterable usersList = userRepository.findAll(); Route route = routesList.iterator().next(); User user = usersList.iterator().next(); - routeFollowed.setRouteOrigin(route); + routeFollowed.setFollows(route.getUri()); routeFollowed.setCreatedBy(user); stepDefs.result = stepDefs.mockMvc.perform( post("/routeFolloweds") @@ -60,7 +59,7 @@ public void iDonTHaveAnyRouteFollowed() { public void thereIsARouteFollowedWithDateDurationLevelUpAndALevelDownByUserUsernameAndRouteTitle(String date, String duration, String levelUp, String levelDown, String user, String route) { RouteFollowed routeFollowed = RouteFollowedUtil.buildRoute(date,duration,levelUp,levelDown); routeFollowed.setCreatedBy(userRepository.findById(user).get()); - routeFollowed.setRouteOrigin(routeRepository.findByTitle(route).get(0)); + routeFollowed.setFollows(routeRepository.findByTitle(route).get(0).getUri()); routeFollowedRepository.save(routeFollowed); } } diff --git a/src/test/java/cat/udl/eps/softarch/myroutes/steps/RetrieveRouteFollowedStepDefs.java b/src/test/java/cat/udl/eps/softarch/myroutes/steps/RetrieveRouteFollowedStepDefs.java index 136135e..1cf7e7a 100644 --- a/src/test/java/cat/udl/eps/softarch/myroutes/steps/RetrieveRouteFollowedStepDefs.java +++ b/src/test/java/cat/udl/eps/softarch/myroutes/steps/RetrieveRouteFollowedStepDefs.java @@ -3,7 +3,6 @@ import cat.udl.eps.softarch.myroutes.domain.Route; import cat.udl.eps.softarch.myroutes.domain.RouteFollowed; import cat.udl.eps.softarch.myroutes.domain.User; -import cat.udl.eps.softarch.myroutes.domain.Waypoint; import cat.udl.eps.softarch.myroutes.repository.RouteFollowedRepository; import cat.udl.eps.softarch.myroutes.repository.RouteRepository; import cat.udl.eps.softarch.myroutes.repository.UserRepository; @@ -11,8 +10,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; -import java.util.Optional; - import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; public class RetrieveRouteFollowedStepDefs { @@ -37,15 +34,4 @@ public void iTryToRetrieveARouteFollowedWithUserAndRoute(String userId, String r .with(AuthenticationStepDefs.authenticate()) .accept(MediaType.APPLICATION_JSON)); } - - @When("I try to retrieve a routeFollowed with user {string} and route {string}") - public void iTryToRetrieveAWaypointWithUserAndRoute(String userId, String routeTitle) throws Throwable { - Route route = RouteUtil.getRouteByTitle(routeRepository,routeTitle); - User user = userRepository.findById(userId).get(); - Optional optionalRouteFollowed = Optional.ofNullable(RouteFollowedUtil.getRouteFollowed(routeFollowedRepository, route, user)); - stepDefs.result = stepDefs.mockMvc.perform( - get("/waypoints/{id}", optionalRouteFollowed.isPresent() ? optionalRouteFollowed.get().getId() : "999") - .with(AuthenticationStepDefs.authenticate()) - .accept(MediaType.APPLICATION_JSON)); - } } diff --git a/src/test/java/cat/udl/eps/softarch/myroutes/steps/RouteFollowedUtil.java b/src/test/java/cat/udl/eps/softarch/myroutes/steps/RouteFollowedUtil.java index 2ada9ce..fa4cc67 100644 --- a/src/test/java/cat/udl/eps/softarch/myroutes/steps/RouteFollowedUtil.java +++ b/src/test/java/cat/udl/eps/softarch/myroutes/steps/RouteFollowedUtil.java @@ -4,17 +4,14 @@ import cat.udl.eps.softarch.myroutes.domain.RouteFollowed; import cat.udl.eps.softarch.myroutes.domain.User; import cat.udl.eps.softarch.myroutes.repository.RouteFollowedRepository; -import cat.udl.eps.softarch.myroutes.repository.RouteRepository; -import cat.udl.eps.softarch.myroutes.repository.UserRepository; import java.time.Duration; import java.time.ZonedDateTime; -import java.util.Date; import java.util.List; public class RouteFollowedUtil { public static RouteFollowed buildRoute(String date, String duration, String levelUp, String levelDown){ - RouteFollowed route = new RouteFollowed(); + RouteFollowed route; route = setValuesToRoute(new RouteFollowed(),date, duration, levelUp, levelDown); return route; } @@ -43,7 +40,7 @@ public static RouteFollowed getRouteFollowed(RouteFollowedRepository rfRepo, Lon public static RouteFollowed getRouteFollowed(RouteFollowedRepository rfRepo, Route route, User user){ - List routeFollowedList = rfRepo.findByCreatedByAndRouteOrigin(user, route); + List routeFollowedList = rfRepo.findByCreatedByAndFollows(user, route.getUri()); if(routeFollowedList.isEmpty()) return new RouteFollowed(); return routeFollowedList.get(0); diff --git a/src/test/resources/features/RetrieveRouteFollowed.feature b/src/test/resources/features/RetrieveRouteFollowed.feature index 6f42e5a..8255ab4 100644 --- a/src/test/resources/features/RetrieveRouteFollowed.feature +++ b/src/test/resources/features/RetrieveRouteFollowed.feature @@ -13,8 +13,3 @@ Feature: Retrieve RouteFollowed When I try to retrieve a RouteFollowed with user "user" and route "testRoute" Then The response code is 200 - Scenario: Get a RouteFollowed which does not exist - Given I login as "user" with password "password" - When I try to retrieve a routeFollowed with user "user" and route "testRoute2" - Then The response code is 404 - From cd42bc5c4751418e75fc13066ea34d123440df88 Mon Sep 17 00:00:00 2001 From: Sergi Fernandez Date: Thu, 21 Dec 2023 11:20:50 +0100 Subject: [PATCH 2/2] Changed RouteOrigin to Follows and changed type back to Route instead of String --- .../cat/udl/eps/softarch/myroutes/domain/RouteFollowed.java | 3 ++- .../softarch/myroutes/handler/RouteFollowedEventHandler.java | 2 +- .../myroutes/repository/RouteFollowedRepository.java | 5 +++-- .../softarch/myroutes/steps/CreateRouteFollowedStepdefs.java | 4 ++-- .../udl/eps/softarch/myroutes/steps/RouteFollowedUtil.java | 2 +- 5 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/main/java/cat/udl/eps/softarch/myroutes/domain/RouteFollowed.java b/src/main/java/cat/udl/eps/softarch/myroutes/domain/RouteFollowed.java index f73ae69..a414cdc 100644 --- a/src/main/java/cat/udl/eps/softarch/myroutes/domain/RouteFollowed.java +++ b/src/main/java/cat/udl/eps/softarch/myroutes/domain/RouteFollowed.java @@ -40,6 +40,7 @@ public class RouteFollowed extends UriEntity { @JsonProperty(access = JsonProperty.Access.READ_ONLY) @NotNull - public String follows; + @ManyToOne + public Route follows; } diff --git a/src/main/java/cat/udl/eps/softarch/myroutes/handler/RouteFollowedEventHandler.java b/src/main/java/cat/udl/eps/softarch/myroutes/handler/RouteFollowedEventHandler.java index 9635c80..dee70ed 100644 --- a/src/main/java/cat/udl/eps/softarch/myroutes/handler/RouteFollowedEventHandler.java +++ b/src/main/java/cat/udl/eps/softarch/myroutes/handler/RouteFollowedEventHandler.java @@ -32,7 +32,7 @@ public void handleRouteFollowedPreCreate(RouteFollowed routeFollowed) { routeFollowed.setCreatedBy(user); List routeList = routeRepository.findByCreatedBy(user); if(!routeList.isEmpty()) - routeFollowed.setFollows(routeList.get(0).getUri()); + routeFollowed.setFollows(routeList.get(0)); routeFollowed.setDate(ZonedDateTime.now()); logger.info("Creation of new routeFollowed: {}", routeFollowed); } diff --git a/src/main/java/cat/udl/eps/softarch/myroutes/repository/RouteFollowedRepository.java b/src/main/java/cat/udl/eps/softarch/myroutes/repository/RouteFollowedRepository.java index 648ada2..bc008d0 100644 --- a/src/main/java/cat/udl/eps/softarch/myroutes/repository/RouteFollowedRepository.java +++ b/src/main/java/cat/udl/eps/softarch/myroutes/repository/RouteFollowedRepository.java @@ -1,5 +1,6 @@ package cat.udl.eps.softarch.myroutes.repository; +import cat.udl.eps.softarch.myroutes.domain.Route; import cat.udl.eps.softarch.myroutes.domain.RouteFollowed; import cat.udl.eps.softarch.myroutes.domain.User; import org.springframework.data.repository.CrudRepository; @@ -11,6 +12,6 @@ public interface RouteFollowedRepository extends CrudRepository, PagingAndSortingRepository { List findByIdContaining(@Param("long") Long id); List findByCreatedBy(@Param("creator") User creator); - List findByFollows(@Param("follows") String follows); - List findByCreatedByAndFollows(@Param("creator") User creator, @Param("follows") String follows); + List findByFollows(@Param("follows") Route follows); + List findByCreatedByAndFollows(@Param("creator") User creator, @Param("follows") Route follows); } diff --git a/src/test/java/cat/udl/eps/softarch/myroutes/steps/CreateRouteFollowedStepdefs.java b/src/test/java/cat/udl/eps/softarch/myroutes/steps/CreateRouteFollowedStepdefs.java index 1887047..93e12d1 100644 --- a/src/test/java/cat/udl/eps/softarch/myroutes/steps/CreateRouteFollowedStepdefs.java +++ b/src/test/java/cat/udl/eps/softarch/myroutes/steps/CreateRouteFollowedStepdefs.java @@ -38,7 +38,7 @@ public void iCreateARouteFollowedWithDateDurationLevelUpAndALevelDown(String dat Iterable usersList = userRepository.findAll(); Route route = routesList.iterator().next(); User user = usersList.iterator().next(); - routeFollowed.setFollows(route.getUri()); + routeFollowed.setFollows(route); routeFollowed.setCreatedBy(user); stepDefs.result = stepDefs.mockMvc.perform( post("/routeFolloweds") @@ -59,7 +59,7 @@ public void iDonTHaveAnyRouteFollowed() { public void thereIsARouteFollowedWithDateDurationLevelUpAndALevelDownByUserUsernameAndRouteTitle(String date, String duration, String levelUp, String levelDown, String user, String route) { RouteFollowed routeFollowed = RouteFollowedUtil.buildRoute(date,duration,levelUp,levelDown); routeFollowed.setCreatedBy(userRepository.findById(user).get()); - routeFollowed.setFollows(routeRepository.findByTitle(route).get(0).getUri()); + routeFollowed.setFollows(routeRepository.findByTitle(route).get(0)); routeFollowedRepository.save(routeFollowed); } } diff --git a/src/test/java/cat/udl/eps/softarch/myroutes/steps/RouteFollowedUtil.java b/src/test/java/cat/udl/eps/softarch/myroutes/steps/RouteFollowedUtil.java index fa4cc67..215e9dc 100644 --- a/src/test/java/cat/udl/eps/softarch/myroutes/steps/RouteFollowedUtil.java +++ b/src/test/java/cat/udl/eps/softarch/myroutes/steps/RouteFollowedUtil.java @@ -40,7 +40,7 @@ public static RouteFollowed getRouteFollowed(RouteFollowedRepository rfRepo, Lon public static RouteFollowed getRouteFollowed(RouteFollowedRepository rfRepo, Route route, User user){ - List routeFollowedList = rfRepo.findByCreatedByAndFollows(user, route.getUri()); + List routeFollowedList = rfRepo.findByCreatedByAndFollows(user, route); if(routeFollowedList.isEmpty()) return new RouteFollowed(); return routeFollowedList.get(0);