Creación de ViajeRepository y ViajeTestRepository

parent d6100bd5
......@@ -37,8 +37,7 @@ public class Reserva {
this.conductor = conductor;
this.idReserva = ++incrementaReserva;
this.numPasajeros = nAsientos;
this.viaje = new Viaje(origen, destino, fechaSalida, nAsientos, precioAsiento);
this.viaje.setIdViaje(this.idReserva);
this.viaje = new Viaje(origen, destino, fechaSalida, nAsientos, precioAsiento, this.idReserva);
this.sistema = new SistemaCarPooling();
this.sistema.addViaje(this.viaje);
this.sistema.addReserva(this);
......
......@@ -12,16 +12,15 @@ public class Viaje {
private int nAsientos;
private int precioAsiento;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int idViaje;
public Viaje(){}
public Viaje(String origen, String destino, Date fechaSalida, int nAsientos, int precioAsiento) {
public Viaje(String origen, String destino, Date fechaSalida, int nAsientos, int precioAsiento, int idViaje) {
this.origen = origen;
this.destino = destino;
this.fechaSalida = fechaSalida;
this.nAsientos = nAsientos;
this.precioAsiento = precioAsiento;
this.idViaje = 0;
this.idViaje = idViaje;
}
public int getIdViaje() {
......
package com.carpooling.carpoolingaoraha.repositorios;
import com.carpooling.carpoolingaoraha.entidades.Viaje;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ViajeRepository extends JpaRepository<Viaje, Integer> {
Viaje findByIdViaje(Integer iD);
}
......@@ -21,7 +21,7 @@ public class SistemaCarPoolingTest {
public void testAddViaje() throws ParseException {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date fecha = dateFormat.parse("2023-10-07");
Viaje viaje = new Viaje("Origen", "Destino", fecha, 3, 10);
Viaje viaje = new Viaje("Origen", "Destino", fecha, 3, 10, 5);
sistema.addViaje(viaje);
assertTrue(sistema.getViajes().contains(viaje));
}
......
......@@ -48,9 +48,9 @@ public class UsuarioTest {
// Inicializar la lista de viajes con datos de ejemplo
List<Viaje> todosLosViajes = new ArrayList<>();
Viaje viaje1 = new Viaje("Origen1", "Destino1", new Date(), 3, 20);
Viaje viaje2 = new Viaje("Origen2", "Destino2", new Date(), 2, 15);
Viaje viaje3 = new Viaje("Origen3", "Destino3", new Date(), 1, 10);
Viaje viaje1 = new Viaje("Origen1", "Destino1", new Date(), 3, 20, 5);
Viaje viaje2 = new Viaje("Origen2", "Destino2", new Date(), 2, 15, 5);
Viaje viaje3 = new Viaje("Origen3", "Destino3", new Date(), 1, 10, 5);
todosLosViajes.add(viaje1);
todosLosViajes.add(viaje2);
......
......@@ -12,7 +12,7 @@ public class ViajeTest {
@Before
public void testConstructor() {
viaje = new Viaje("Origen", "Destino", new Date(), 3, 20);
viaje = new Viaje("Origen", "Destino", new Date(), 3, 20, 5);
}
@Test
......
package com.carpooling.carpoolingaoraha.repositorios;
import com.carpooling.carpoolingaoraha.entidades.Viaje;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class ViajeRepositoryTest {
@Autowired
private ViajeRepository repository;
@Test
public void testInsertViaje(){
Viaje viaje = new Viaje();
viaje.setDestino("Canarias");
int idViaje = 1;
viaje.setIdViaje(idViaje);
repository.save(viaje);
Viaje viajeGuardado = repository.findByIdViaje(idViaje);
Assert.assertNotNull(viajeGuardado);
Assert.assertEquals(idViaje, viajeGuardado.getIdViaje());
Assert.assertEquals("Canarias", viajeGuardado.getDestino());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment