Skip to content
Snippets Groups Projects
Commit 68dcc613 authored by Petr Kabourek's avatar Petr Kabourek
Browse files

flights id as UUID

parent f613c3c2
No related branches found
No related tags found
1 merge request!9Service tests
Showing
with 124 additions and 57 deletions
......@@ -24,7 +24,7 @@ public class FlightFacade {
return result.stream().map(p -> modelMapper.map(p, Flight.class)).toList();
}
public Flight getFlight(Long id){
public Flight getFlight(UUID id){
var result = flightService.getFlight(id);
return modelMapper.map(result, Flight.class);
}
......@@ -36,13 +36,13 @@ public class FlightFacade {
}
public Flight updateFlight(Long id, FlightRequest flightRequest){
public Flight updateFlight(UUID id, FlightRequest flightRequest){
var result = flightService.updateFlight(id, modelMapper.map(flightRequest, Flight.class));
return modelMapper.map(result, Flight.class);
}
public void deleteFlight(Long id){
public void deleteFlight(UUID id){
flightService.deleteFlight(id);
}
}
......@@ -24,7 +24,7 @@ public class FlightRealizationFacade {
return result.stream().map(p -> modelMapper.map(p, FlightRealization.class)).toList();
}
public FlightRealization getFlightRealization(Long id){
public FlightRealization getFlightRealization(UUID id){
var result = flightRealizationService.getFlightRealizationById(id);
return modelMapper.map(result, FlightRealization.class);
}
......@@ -35,13 +35,13 @@ public class FlightRealizationFacade {
}
public FlightRealization updateFlightRealization(Long id, FlightRealizationRequest flightRealizationRequest){
public FlightRealization updateFlightRealization(UUID id, FlightRealizationRequest flightRealizationRequest){
var result = flightRealizationService.updateFlightRealization(id, modelMapper.map(flightRealizationRequest, FlightRealization.class));
return modelMapper.map(result, FlightRealization.class);
}
public void deleteFlightRealization(Long id){
public void deleteFlightRealization(UUID id){
flightRealizationService.deleteFlightRealization(id);
}
......
......@@ -8,6 +8,7 @@ import org.springframework.stereotype.Service;
import org.modelmapper.ModelMapper;
import java.util.List;
import java.util.UUID;
@Service
@RequiredArgsConstructor
......@@ -20,7 +21,7 @@ public class IssueReportFacade {
return result.stream().map(p -> modelMapper.map(p, IssueReport.class)).toList();
// return issueReportService.getAllIssueReports();
}
public IssueReport getIssueReport(Long id) {
public IssueReport getIssueReport(UUID id) {
var result = issueReportService.getIssueReportById(id);
return modelMapper.map(result, IssueReport.class);
// return issueReportService.getIssueReportById(id);
......@@ -34,12 +35,12 @@ public class IssueReportFacade {
}
public IssueReport updateIssueReport(Long id, IssueReportRequest issueReportRequest){
public IssueReport updateIssueReport(UUID id, IssueReportRequest issueReportRequest){
return issueReportService.updateIssueReport(id, modelMapper.map( issueReportRequest, IssueReport.class ) );
}
public void deleteIssueReport(Long id) {
public void deleteIssueReport(UUID id) {
issueReportService.deleteIssueReport(id);
}
}
......@@ -2,10 +2,11 @@ package cz.muni.fi.pa165.repository;
import cz.muni.fi.pa165.dao.FlightRealization;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import java.util.UUID;
@Repository
public interface FlightRealizationRepository extends JpaRepository<FlightRealization, Long> {
public interface FlightRealizationRepository extends JpaRepository<FlightRealization, UUID> {
}
......@@ -2,10 +2,11 @@ package cz.muni.fi.pa165.repository;
import cz.muni.fi.pa165.dao.Flight;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import java.util.UUID;
@Repository
public interface FlightRepository extends JpaRepository<Flight, Long> {
public interface FlightRepository extends JpaRepository<Flight, UUID> {
}
......@@ -2,8 +2,11 @@ package cz.muni.fi.pa165.repository;
import cz.muni.fi.pa165.dao.IssueReport;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import java.util.UUID;
@Repository
public interface IssueReportRepository extends JpaRepository<IssueReport, Long> {
public interface IssueReportRepository extends JpaRepository<IssueReport, UUID> {
}
......@@ -32,7 +32,7 @@ public class FlightController {
}
@GetMapping("/{id}")
public Flight getFlight(@PathVariable Long id) {
public Flight getFlight(@PathVariable UUID id) {
return flightFacade.getFlight(id);
}
......@@ -44,13 +44,13 @@ public class FlightController {
}
@PutMapping("/{id}")
public ResponseEntity<Flight> update(@PathVariable Long id, @RequestBody FlightRequest flightRequest){
public ResponseEntity<Flight> update(@PathVariable UUID id, @RequestBody FlightRequest flightRequest){
Flight updatedFlight = flightFacade.updateFlight(id, flightRequest);
return new ResponseEntity<>(updatedFlight, HttpStatus.CREATED);
}
@DeleteMapping("/{id}")
public ResponseEntity<String> deleteFlight(@PathVariable(value = "id") Long id) {
public ResponseEntity<String> deleteFlight(@PathVariable(value = "id") UUID id) {
flightRepository.deleteById(id);
return new ResponseEntity<>("Flight deleted sucessfully", HttpStatus.OK);
}
......
......@@ -32,7 +32,7 @@ public class FlightRealizationController {
}
@GetMapping("/{id}")
public FlightRealization getFlightRealizationById(@PathVariable Long id) {
public FlightRealization getFlightRealizationById(@PathVariable UUID id) {
return flightRealizationFacade.getFlightRealization(id);
}
......@@ -44,13 +44,13 @@ public class FlightRealizationController {
}
@PutMapping("/{id}")
public ResponseEntity<FlightRealization> updateFlightRealization(@PathVariable Long id, @RequestBody FlightRealizationRequest flightRealizationRequest) {
public ResponseEntity<FlightRealization> updateFlightRealization(@PathVariable UUID id, @RequestBody FlightRealizationRequest flightRealizationRequest) {
FlightRealization updatedFlightRealization = flightRealizationFacade.updateFlightRealization(id, flightRealizationRequest);
return new ResponseEntity<>(updatedFlightRealization, HttpStatus.CREATED);
}
@DeleteMapping("/{id}")
public ResponseEntity<String> deleteFlightRealization(@PathVariable Long id) {
public ResponseEntity<String> deleteFlightRealization(@PathVariable UUID id) {
flightRealizationRepository.deleteById(id);
return new ResponseEntity<>("Flight Realization deleted sucessfully", HttpStatus.OK);
......
......@@ -33,7 +33,7 @@ public class IssueReportController {
}
@GetMapping("/{id}")
public ResponseEntity<IssueReport> getIssueReportById(@PathVariable("id") Long id) {
public ResponseEntity<IssueReport> getIssueReportById(@PathVariable("id") UUID id) {
// IssueReport issueReport = issueReportService.getIssueReportById(id);
IssueReport issueReport = issueReportFacade.getIssueReport(id);
return new ResponseEntity<>(issueReport, HttpStatus.OK);
......@@ -47,14 +47,14 @@ public class IssueReportController {
}
@PutMapping("/{id}")
public ResponseEntity<IssueReport> updateIssueReport(@PathVariable("id") Long id, @RequestBody IssueReportRequest issueReportRequest) {
public ResponseEntity<IssueReport> updateIssueReport(@PathVariable("id") UUID id, @RequestBody IssueReportRequest issueReportRequest) {
// IssueReport updatedIssueReport = issueReportService.updateIssueReport(id, issueReport);
IssueReport updatedIssueReport = issueReportFacade.updateIssueReport(id, issueReportRequest);
return new ResponseEntity<>(updatedIssueReport, HttpStatus.CREATED);
}
@DeleteMapping("/{id}")
public ResponseEntity<String> deleteIssueReport(@PathVariable(value = "id") Long id) {
public ResponseEntity<String> deleteIssueReport(@PathVariable(value = "id") UUID id) {
issueReportService.deleteIssueReport(id);
return new ResponseEntity<>("Issue deleted sucessfully", HttpStatus.OK);
}
......
......@@ -8,6 +8,7 @@ import cz.muni.fi.pa165.repository.FlightRealizationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
......@@ -22,7 +23,7 @@ public class FlightRealizationService {
return flightRealizationRepository.findAll();
}
public FlightRealization getFlightRealizationById(Long id) {
public FlightRealization getFlightRealizationById(UUID id) {
return flightRealizationRepository.findById(id).orElseThrow(FlightRealizationNotFoundException::new);
}
......@@ -32,10 +33,8 @@ public class FlightRealizationService {
}
// Update an existing flight realization
public FlightRealization updateFlightRealization(Long id, FlightRealization flightRealization) {
FlightRealization existingFlightRealization = flightRealizationRepository.findById(id).get();
System.out.println(existingFlightRealization);
public FlightRealization updateFlightRealization(UUID id, FlightRealization flightRealization) {
FlightRealization existingFlightRealization = flightRealizationRepository.findById(id).orElseThrow(FlightRealizationNotFoundException::new);
existingFlightRealization.setArrivalTime( flightRealization.getArrivalTime() );
existingFlightRealization.setDuration(flightRealization.getDuration());
existingFlightRealization.setDepartureTime(flightRealization.getDepartureTime());
......@@ -47,7 +46,7 @@ public class FlightRealizationService {
return updateFlightRealization;
}
public void deleteFlightRealization(Long id) {
public void deleteFlightRealization(UUID id) {
flightRealizationRepository.deleteById(id);
}
}
......
......@@ -26,7 +26,7 @@ public class FlightService {
return flightRepository.findAll();
}
public Flight getFlight(Long id) {
public Flight getFlight(UUID id) {
return flightRepository.findById(id).orElseThrow(FlightNotFoundException::new);
}
......@@ -34,7 +34,7 @@ public class FlightService {
return flightRepository.save(flight);
}
public Flight updateFlight(Long id,Flight flight){
public Flight updateFlight(UUID id,Flight flight){
Flight existingFlight = flightRepository.findById(id).get();
existingFlight.setDestination(flight.getDestination());
existingFlight.setOrigin(flight.getOrigin());
......@@ -45,7 +45,7 @@ public class FlightService {
return updateFlight;
}
public void deleteFlight(Long id) {
public void deleteFlight(UUID id) {
flightRepository.deleteById(id);
}
}
......@@ -23,11 +23,11 @@ public class IssueReportService {
return issueReportRepository.findAll();
}
public IssueReport get(Long id) {
public IssueReport get(UUID id) {
return issueReportRepository.findById(id).orElseThrow(IssueReportNotFoundException::new);
}
public IssueReport getIssueReportById(Long id) {
public IssueReport getIssueReportById(UUID id) {
Optional<IssueReport> issueReport = issueReportRepository.findById(id);
return issueReport.get();
}
......@@ -36,7 +36,7 @@ public class IssueReportService {
return issueReportRepository.save(issueReport);
}
public IssueReport updateIssueReport(Long id, IssueReport issueReport) {
public IssueReport updateIssueReport(UUID id, IssueReport issueReport) {
IssueReport existingIssueReport = issueReportRepository.findById(id).get();
System.out.println(existingIssueReport);
existingIssueReport.setDescription(issueReport.getDescription());
......@@ -46,7 +46,7 @@ public class IssueReportService {
return updateIssueReport;
}
public void deleteIssueReport(Long id) {
public void deleteIssueReport(UUID id) {
issueReportRepository.deleteById(id);
}
}
......@@ -23,12 +23,12 @@ public class FlightControllerTest {
private FlightFacade flightFacade;
@Test
public void testGetsFlight() throws Exception {
public void testGetsFlight() {
assertTrue( true );
}
@Test
public void testGetFlight() throws Exception {
public void testGetFlight() {
assertTrue( true );
}
......
package cz.muni.fi.pa165.service;
import cz.muni.fi.pa165.dao.Flight;
import cz.muni.fi.pa165.exception.FlightNotFoundException;
import cz.muni.fi.pa165.repository.FlightRepository;
import cz.muni.fi.pa165.utils.TestDaoFactory;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.assertEquals;
@ExtendWith(MockitoExtension.class)
public class FlightServiceTest {
......@@ -18,27 +28,77 @@ public class FlightServiceTest {
private FlightService flightService;
@Test
public void getAllFlights() {
public void getFlight_flightFound_returnsFlight() {
// Prepare a sample flight ID
UUID id = UUID.randomUUID();
}
// Create a sample flight object
Flight flight = TestDaoFactory.getFlightEntity();
@Test
public void getFlight() {
// Mocking the behavior of the flightRepository.findById() method
Mockito.when(flightRepository.findById(id)).thenReturn(Optional.of(flight));
// Calling the method to be tested
Flight foundFlight = flightService.getFlight(id);
// Assertions
Assertions.assertEquals(foundFlight, flight); // Check if the returned flight matches the one we created
}
@Test
public void createFlight() {
void getById_flightNotFound_throwsResourceNotFoundException() {
// Prepare a random non-existent flight ID
UUID id = UUID.randomUUID();
// Mock the behavior of the flightRepository.findById() method to return an empty Optional
Mockito.when(flightRepository.findById(id)).thenReturn(Optional.empty());
// Assert that calling flightService.getFlight(id) should throw FlightNotFoundException
Assertions.assertThrows(FlightNotFoundException.class, () -> flightService.getFlight(id));
}
@Test
public void updateFlight(){
void createFlight() {
// Create a sample flight object
Flight flight = TestDaoFactory.getFlightEntity();
// Mocking the behavior of the flightRepository.save() method
Mockito.when(flightRepository.save(flight)).thenReturn(flight);
// Calling the method to be tested
Flight createdFlight = flightService.createFlight(flight);
// Assertions
assertEquals(flight, createdFlight); // Check if returned flight matches the one we created
Mockito.verify(flightRepository, Mockito.times(1)).save(flight); // Verify that save method was called exactly once with the correct parameter
}
@Test
public void deleteFlight() {
void updateFlight_flightFound() {
UUID id = UUID.randomUUID(); // Generate a random flight ID
var destId = UUID.randomUUID();
// Prepare an existing flight and an update object
Flight existingFlight = TestDaoFactory.getFlightEntity();
Flight update = new Flight();
update.setDestination(destId.toString());
// Prepare the expected updated flight
Flight updatedFlight = TestDaoFactory.getFlightEntity();
updatedFlight.setId(id);
updatedFlight.setDestination("New Destination");
// Mocking the behavior of the flightRepository.findById() method to return the existing flight
Mockito.when(flightRepository.findById(id)).thenReturn(Optional.of(existingFlight));
// Mocking the behavior of the flightRepository.save() method to return the updated flight
Mockito.when(flightRepository.save(Mockito.any(Flight.class))).thenAnswer(invocation -> invocation.getArgument(0));
// Call the method to be tested
Flight returnedFlight = flightService.updateFlight(id, update);
// Assertions
assertEquals(updatedFlight, returnedFlight); // Check if the returned flight matches the expected updated flight
Mockito.verify(flightRepository, Mockito.times(1)).save(updatedFlight); // Verify that the save method was called once with the updated flight
}
}
......@@ -18,7 +18,9 @@ public class IssueReportTest {
@Test
public void getAllIssueReports() {}
public void getAllIssueReports() {
}
@Test
public void get() {}
......
......@@ -54,19 +54,19 @@ class EmployeeServiceTest {
Mockito.verify(pilotRepository, Mockito.times(1)).save(pilot);
}
@Test
void getById_employeeFound_returnsEmployee() {
UUID id = new UUID(0x1, 0xf);
var employee = TestDaoFactory.getEmployeeEntity();
// Arrange
Mockito.when(employeeRepository.findById(id)).thenReturn(Optional.of(employee));
// Act
Employee foundEntity = employeeService.get(id);
// Assert
assertThat(foundEntity).isEqualTo(employee);
}
@Test
void getById_employeeFound_returnsEmployee() {
UUID id = new UUID(0x1, 0xf);
var employee = TestDaoFactory.getEmployeeEntity();
// Arrange
Mockito.when(employeeRepository.findById(id)).thenReturn(Optional.of(employee));
// Act
Employee foundEntity = employeeService.get(id);
// Assert
assertThat(foundEntity).isEqualTo(employee);
}
@Test
void getById_employeeNotFound_throwsResourceNotFoundException() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment