@RequestMapping( value = "/vehicles/{vehicleId}/accidents", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseStatus(HttpStatus.OK) public List<AccidentEntity> retrieveAllAccidentsByVehicle(@PathVariable String vehicleId) { List<AccidentEntity> accidentEntityList = accidentRepository.findAllByVehicleId(vehicleId); if (accidentEntityList == null) { throw new ResourcesNotFoundException("Accidents not found"); } return accidentEntityList; }
@RequestMapping( value = "/vehicles/{vehicleId}/accidents", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE) @ResponseStatus(HttpStatus.CREATED) public AccidentEntity addNewAccident( @PathVariable String vehicleId, @RequestBody AccidentEntity accidentEntity) { if (vehicleRepository.findOne(vehicleId) == null) { throw new ResourcesNotFoundException("Vehicle not found"); } accidentEntity.setVehicleId(vehicleId); accidentRepository.save(accidentEntity); return accidentEntity; }
@Test public void addAccident() throws Exception { VehicleResponse vehicleResponse = new VehicleResponse(); when(vehicleRepository.findOne("vId")).thenReturn(vehicleResponse); AccidentEntity expectedAccident = new AccidentEntity(); when(accidentRepository.save(any(AccidentEntity.class))).thenReturn(expectedAccident); mockMvc .perform( post("/vehicles/{vehicleId}/accidents", "vId") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON) .content("{}")) .andExpect(status().isCreated()); verify(vehicleRepository, times(1)).findOne("vId"); verify(accidentRepository, times(1)).save(Matchers.isA(AccidentEntity.class)); verifyNoMoreInteractions(accidentRepository, vehicleRepository); }