Skip to content

Commit

Permalink
Fix save car function
Browse files Browse the repository at this point in the history
Update CarControllerTest - add updateCar test
  • Loading branch information
elgeorsk committed Dec 14, 2020
1 parent 1fe20bc commit 1beb804
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 1beb804

Please sign in to comment.