From 1beb80423b5e7788e1ce67d068d581b2d7a9f2fd Mon Sep 17 00:00:00 2001 From: "ELGEOR-PC\\elgeor" Date: Mon, 14 Dec 2020 13:16:02 +0100 Subject: [PATCH] Fix save car function Update CarControllerTest - add updateCar test --- .../udacity/vehicles/service/CarService.java | 3 +++ .../vehicles/api/CarControllerTest.java | 19 ++++++++++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/vehicles-api/src/main/java/com/udacity/vehicles/service/CarService.java b/vehicles-api/src/main/java/com/udacity/vehicles/service/CarService.java index 85ea8f8..4cd68be 100644 --- a/vehicles-api/src/main/java/com/udacity/vehicles/service/CarService.java +++ b/vehicles-api/src/main/java/com/udacity/vehicles/service/CarService.java @@ -5,6 +5,7 @@ import com.udacity.vehicles.domain.car.Car; import com.udacity.vehicles.domain.car.CarRepository; +import java.time.LocalDateTime; import java.util.Collections; import java.util.List; @@ -109,6 +110,8 @@ public Car save(Car car) { .map(carToBeUpdated -> { carToBeUpdated.setDetails(car.getDetails()); carToBeUpdated.setLocation(car.getLocation()); + carToBeUpdated.setCondition(car.getCondition()); + carToBeUpdated.setModifiedAt(LocalDateTime.now()); return carRepository.save(carToBeUpdated); }).orElseThrow(CarNotFoundException::new); } diff --git a/vehicles-api/src/test/java/com/udacity/vehicles/api/CarControllerTest.java b/vehicles-api/src/test/java/com/udacity/vehicles/api/CarControllerTest.java index f305af5..0f4b904 100644 --- a/vehicles-api/src/test/java/com/udacity/vehicles/api/CarControllerTest.java +++ b/vehicles-api/src/test/java/com/udacity/vehicles/api/CarControllerTest.java @@ -28,7 +28,6 @@ import org.springframework.boot.test.json.JacksonTester; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.web.server.LocalServerPort; -import org.springframework.core.annotation.Order; import org.springframework.http.MediaType; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; @@ -77,7 +76,6 @@ public void setup() { * @throws Exception when car creation fails in the system */ @Test - @Order(1) public void createCar() throws Exception { Car car = getCar(); mvc.perform( @@ -93,7 +91,6 @@ public void createCar() throws Exception { * @throws Exception if the read operation of the vehicle list fails */ @Test - @Order(2) public void listCars() throws Exception { /** * Done: Add a test to check that the `get` method works by calling @@ -107,12 +104,25 @@ public void listCars() throws Exception { } + @Test + public void updateCar() throws Exception { + Car carToBeUpdate = carService.findById(1L); + carToBeUpdate.setId(1L); + Details details = carToBeUpdate.getDetails(); + details.setModel("Corvette"); + carToBeUpdate.setDetails(details); + carService.save(carToBeUpdate); + mvc.perform(get(new URI("/cars/1"))) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.id").value(1)) + .andExpect(jsonPath("$.details.model").value("Corvette")); + } + /** * Tests the read operation for a single car by ID. * @throws Exception if the read operation for a single car fails */ @Test - @Order(3) public void findCar() throws Exception { /** * Done: Add a test to check that the `get` method works by calling @@ -129,7 +139,6 @@ public void findCar() throws Exception { * @throws Exception if the delete operation of a vehicle fails */ @Test - @Order(4) public void deleteCar() throws Exception { /** * Done: Add a test to check whether a vehicle is appropriately deleted