Inicio gobierno
Some checks are pending
Primer flujo de trabajo CI/CD (Build, Test and Push Docker Image) para Ampliación de Entornos de Desarrollo / build-and-push (push) Waiting to run
Some checks are pending
Primer flujo de trabajo CI/CD (Build, Test and Push Docker Image) para Ampliación de Entornos de Desarrollo / build-and-push (push) Waiting to run
This commit is contained in:
parent
4ca703a0c7
commit
6b38a6dc62
6
.env.example
Normal file
6
.env.example
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
# Información de configuración de la base de datos
|
||||||
|
DB_HOST=localhost
|
||||||
|
DB_PORT=3306
|
||||||
|
DB_NAME=mapa_mundi
|
||||||
|
DB_USER=
|
||||||
|
DB_PASSWORD=
|
||||||
49
.github/workflows/ci-cd.yml
vendored
Normal file
49
.github/workflows/ci-cd.yml
vendored
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
name: Primer flujo de trabajo CI/CD (Build, Test and Push Docker Image) para Ampliación de Entornos de Desarrollo
|
||||||
|
|
||||||
|
# ===== Permisos del token automático de GitHub para que pueda "subir"(push del paso 4.) la imagen docker generada en el paso 3.
|
||||||
|
# ===== La imagen se subirá al área de registro de GitHub ghcr.io/profies/aaee_mapamundi_sb =====
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
packages: write
|
||||||
|
|
||||||
|
# ===== Disparadores del workflow =====
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- '**' # Cualquier rama
|
||||||
|
|
||||||
|
# ===== Tareas del workflow =====
|
||||||
|
jobs:
|
||||||
|
build-and-push:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
|
||||||
|
# 1. Descarga código
|
||||||
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
|
||||||
|
# 2. JDK/Maven para tests previos
|
||||||
|
- name: Configución pasar TEST
|
||||||
|
uses: actions/setup-java@v3
|
||||||
|
with:
|
||||||
|
distribution: temurin
|
||||||
|
java-version: 23
|
||||||
|
|
||||||
|
- name: Construir proyecto con Maven
|
||||||
|
run: mvn clean package
|
||||||
|
|
||||||
|
# 3. Construir la imagen Docker usando el Dockerfile
|
||||||
|
- name: Build Docker image
|
||||||
|
run: docker build -t ghcr.io/${{ github.repository }}:latest .
|
||||||
|
|
||||||
|
# 4. Push a GHCR (GitHub Container Registry)
|
||||||
|
- name: Push Docker image
|
||||||
|
uses: docker/login-action@v2
|
||||||
|
with:
|
||||||
|
registry: ghcr.io
|
||||||
|
username: ${{ github.actor }}
|
||||||
|
password: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
|
- name: Push image
|
||||||
|
run: docker push ghcr.io/${{ github.repository }}:latest
|
||||||
27
Dockerfile
Normal file
27
Dockerfile
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
# ====== Etapa 1: Fase de construcción (es temporal)======
|
||||||
|
# Usamos una imagen de Maven con JDK 23 para construir la aplicación
|
||||||
|
FROM maven:3.9-eclipse-temurin-23 AS imagen_construccion
|
||||||
|
|
||||||
|
#
|
||||||
|
WORKDIR /app
|
||||||
|
COPY pom.xml .
|
||||||
|
|
||||||
|
COPY src ./src
|
||||||
|
RUN mvn clean package
|
||||||
|
|
||||||
|
# ====== Etapa 2: Fase de ejecución (con la que generará la imagen) ======
|
||||||
|
# En teoría sólo necesitamos la JRE para ejecutar la aplicación (ojo, a veces puede dar problemas y se usa la JDK )
|
||||||
|
# No se necesita el entorno de construcción completo (Maven + JDK + src), sólo el resultado de la construcción (el .jar) y un entorno de ejecución (JRE)
|
||||||
|
FROM eclipse-temurin:23-jre AS imagen_ejecucion
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Copia el jar generado: lo que se obtendría (aaee_mapamundi-0.0.1.jar según el pom.xml) se copia como app.jar
|
||||||
|
# LA idea es simplificar el comando de arranque
|
||||||
|
COPY --from=imagen_construccion /app/target/*.jar app.jar
|
||||||
|
|
||||||
|
|
||||||
|
# Comando de arranque
|
||||||
|
# En las actividades anteriores se ejectuaba el comando "java -jar target/aaee_mapamundi-0.0.1.jar" (info en el pom.xml) para iniciar la aplicación
|
||||||
|
# Aquí se hace lo mismo pero apuntando al jar copiado en esta etapa
|
||||||
|
ENTRYPOINT ["java", "-jar", "app.jar"]
|
||||||
16
pom.xml
16
pom.xml
@ -1,7 +1,7 @@
|
|||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>org.lapaloma.mapamundi</groupId>
|
<groupId>org.lapaloma.gobierno</groupId>
|
||||||
<artifactId>aaee_mapamundi</artifactId>
|
<artifactId>aaee_gobierno</artifactId>
|
||||||
<version>0.0.2</version>
|
<version>0.0.2</version>
|
||||||
|
|
||||||
<name>Prueba de Springboot</name>
|
<name>Prueba de Springboot</name>
|
||||||
@ -40,11 +40,13 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
<dependency>
|
<!-- Source: https://mvnrepository.com/artifact/org.mongodb/mongodb-driver-sync -->
|
||||||
<groupId>com.mysql</groupId>
|
<dependency>
|
||||||
<artifactId>mysql-connector-j</artifactId>
|
<groupId>org.mongodb</groupId>
|
||||||
<scope>runtime</scope>
|
<artifactId>mongodb-driver-sync</artifactId>
|
||||||
</dependency>
|
<version>5.6.5</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
package org.lapaloma.hogwarts;
|
package org.lapaloma.gobierno;
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
@ -0,0 +1,35 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.lapaloma.gobierno.controller;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.lapaloma.gobierno.service.MinisterioService;
|
||||||
|
import org.lapaloma.gobierno.vo.Ministerio;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Isidoro Nevares Martín - Virgen de la Paloma Fecha creación: 13 mar 2026
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/gobierno/ministerios")
|
||||||
|
public class MinisterioController {
|
||||||
|
private final MinisterioService ministerioService;
|
||||||
|
|
||||||
|
// Spring inyecta automáticamente el service con su DAO
|
||||||
|
public MinisterioController(MinisterioService ministerioService) {
|
||||||
|
this.ministerioService = ministerioService;
|
||||||
|
}
|
||||||
|
|
||||||
|
// GET /gobierno/ministerios - listar todos los ministerio
|
||||||
|
@GetMapping
|
||||||
|
public List<Ministerio> getAll() {
|
||||||
|
List<Ministerio> listaCasas = ministerioService.obtenerListaMinisterios();
|
||||||
|
return listaCasas;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
10
src/main/java/org/lapaloma/gobierno/dao/IMinisterioDAO.java
Normal file
10
src/main/java/org/lapaloma/gobierno/dao/IMinisterioDAO.java
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package org.lapaloma.gobierno.dao;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.lapaloma.gobierno.vo.Ministerio;
|
||||||
|
|
||||||
|
public interface IMinisterioDAO {
|
||||||
|
|
||||||
|
public List<Ministerio> obtenerListaMinisterios();
|
||||||
|
}
|
||||||
@ -0,0 +1,66 @@
|
|||||||
|
package org.lapaloma.gobierno.dao.impl;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import org.lapaloma.gobierno.dao.IMinisterioDAO;
|
||||||
|
import org.lapaloma.gobierno.vo.Ministerio;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public class MinisterioDaoJDBC implements IMinisterioDAO {
|
||||||
|
private final DataSource dataSource;
|
||||||
|
|
||||||
|
// Spring inyecta el DataSource configurado automáticamente
|
||||||
|
public MinisterioDaoJDBC(DataSource dataSource) {
|
||||||
|
this.dataSource = dataSource;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Ministerio> obtenerListaMinisterios() {
|
||||||
|
|
||||||
|
List<Ministerio> lista = new ArrayList<>();
|
||||||
|
|
||||||
|
String sentenciaSQL = """
|
||||||
|
SELECT * FROM ministerio
|
||||||
|
""";
|
||||||
|
|
||||||
|
try (Connection conexion = dataSource.getConnection();
|
||||||
|
PreparedStatement sentenciaJDBCPreparada = conexion.prepareStatement(sentenciaSQL);) {
|
||||||
|
|
||||||
|
System.out.println(sentenciaJDBCPreparada);
|
||||||
|
|
||||||
|
ResultSet resultadoSentencia = sentenciaJDBCPreparada.executeQuery();
|
||||||
|
|
||||||
|
while (resultadoSentencia.next()) {
|
||||||
|
lista.add(getLineaFromResultSet(resultadoSentencia));
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return lista;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private Ministerio getLineaFromResultSet(ResultSet resultadoSentencia) throws SQLException {
|
||||||
|
|
||||||
|
Ministerio ministerio = new Ministerio();
|
||||||
|
|
||||||
|
ministerio.setIdentificador(resultadoSentencia.getInt("codMinisterio"));
|
||||||
|
ministerio.setNombre(resultadoSentencia.getString("nombre"));
|
||||||
|
ministerio.setPresupuesto(resultadoSentencia.getDouble("presupuesto"));
|
||||||
|
ministerio.setGastos(resultadoSentencia.getDouble("gastos"));
|
||||||
|
|
||||||
|
return ministerio;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,12 @@
|
|||||||
|
package org.lapaloma.gobierno.excepcion;
|
||||||
|
|
||||||
|
public class MinisterioNoEncontradoException extends RuntimeException {
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = -3344627619585104664L;
|
||||||
|
|
||||||
|
public MinisterioNoEncontradoException(String mensaje) {
|
||||||
|
super(mensaje);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,37 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.lapaloma.gobierno.service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.lapaloma.gobierno.dao.IMinisterioDAO;
|
||||||
|
import org.lapaloma.gobierno.vo.Ministerio;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class MinisterioService {
|
||||||
|
|
||||||
|
private final IMinisterioDAO ministerioDAO;
|
||||||
|
|
||||||
|
// Spring inyecta el DAO automáticamente
|
||||||
|
public MinisterioService(IMinisterioDAO ministerioDAO) {
|
||||||
|
this.ministerioDAO = ministerioDAO;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Ministerio> obtenerListaMinisterios() {
|
||||||
|
|
||||||
|
List<Ministerio> lista = ministerioDAO.obtenerListaMinisterios();
|
||||||
|
|
||||||
|
// Esto provoca error
|
||||||
|
lista = null;
|
||||||
|
|
||||||
|
if (lista == null || lista.isEmpty()) {
|
||||||
|
throw new RuntimeException("No hay miinisterios disponibles");
|
||||||
|
}
|
||||||
|
|
||||||
|
return lista;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,29 +1,49 @@
|
|||||||
package org.lapaloma.hogwarts.vo;
|
package org.lapaloma.gobierno.vo;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Casa: Clase de persistencia que representa una Casa de Hogwarts.
|
* Casa: Clase de persistencia que representa una Ministerio de Gobierno.
|
||||||
*
|
*
|
||||||
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||||
* @date 03 marzo 2026
|
* @date 03 marzo 2026
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class Casa {
|
public class Ministerio {
|
||||||
private int identificador;
|
private int identificador;
|
||||||
private String nombre;
|
private String nombre;
|
||||||
|
private double presupuesto;
|
||||||
|
private double gastos;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param identificador
|
* @param identificador
|
||||||
* @param nombre
|
* @param nombre
|
||||||
*/
|
*/
|
||||||
public Casa(int identificador, String nombre) {
|
public Ministerio(int identificador, String nombre, double presupuesto, double gastos) {
|
||||||
super();
|
super();
|
||||||
this.identificador = identificador;
|
this.identificador = identificador;
|
||||||
this.nombre = nombre;
|
this.nombre = nombre;
|
||||||
|
this.presupuesto = presupuesto;
|
||||||
|
this.gastos = gastos;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Casa() {
|
public double getPresupuesto() {
|
||||||
|
return presupuesto;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPresupuesto(double presupuesto) {
|
||||||
|
this.presupuesto = presupuesto;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getGastos() {
|
||||||
|
return gastos;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setGastos(double gastos) {
|
||||||
|
this.gastos = gastos;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Ministerio() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getIdentificador() {
|
public int getIdentificador() {
|
||||||
@ -1,53 +0,0 @@
|
|||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
package org.lapaloma.hogwarts.controller;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.lapaloma.hogwarts.service.CasaService;
|
|
||||||
import org.lapaloma.hogwarts.vo.Casa;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Isidoro Nevares Martín - Virgen de la Paloma Fecha creación: 13 mar 2026
|
|
||||||
*/
|
|
||||||
@RestController
|
|
||||||
@RequestMapping("/hogwarts/casas")
|
|
||||||
public class CasaController {
|
|
||||||
|
|
||||||
private final CasaService casaService;
|
|
||||||
|
|
||||||
// Spring inyecta automáticamente el service con su DAO
|
|
||||||
public CasaController(CasaService casaService) {
|
|
||||||
this.casaService = casaService;
|
|
||||||
}
|
|
||||||
|
|
||||||
// GET /hogwarts/casas - listar todos las casas
|
|
||||||
@GetMapping
|
|
||||||
public List<Casa> getAll() {
|
|
||||||
List<Casa> listaCasas = casaService.obtenerListaCasas();
|
|
||||||
return listaCasas;
|
|
||||||
}
|
|
||||||
|
|
||||||
// GET /hogwarts/casas/id/{id} - Obtener un Casa por su código
|
|
||||||
@GetMapping("/id/{id}")
|
|
||||||
public ResponseEntity<Casa> getByCodigo(@PathVariable("id") Integer identificador) {
|
|
||||||
Casa Casa = casaService.obtenerCasaPorClave(identificador);
|
|
||||||
|
|
||||||
return Casa != null ? ResponseEntity.ok(Casa) : ResponseEntity.notFound().build();
|
|
||||||
}
|
|
||||||
|
|
||||||
// GET /hogwarts/casas/nombre/{nombre} - Obtener un Casa por su nombre
|
|
||||||
@GetMapping("/nombre/{nombre}")
|
|
||||||
public List<Casa> getByNombre(@PathVariable("nombre") String nombre) {
|
|
||||||
List<Casa> listaCasas = casaService.obtenerCasaPorNombre(nombre);
|
|
||||||
|
|
||||||
return listaCasas;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,19 +0,0 @@
|
|||||||
package org.lapaloma.hogwarts.dao;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.lapaloma.hogwarts.vo.Casa;
|
|
||||||
|
|
||||||
public interface ICasaDAO {
|
|
||||||
public Casa obtenerCasaPorClave(int identificador);
|
|
||||||
|
|
||||||
public void actualizarCasa(Casa Casa);
|
|
||||||
|
|
||||||
public void crearCasa(Casa Casa);
|
|
||||||
|
|
||||||
public void borrarCasa(Casa Casa);
|
|
||||||
|
|
||||||
public List<Casa> obtenerListaCasas();
|
|
||||||
|
|
||||||
public List<Casa> obtenerCasaPorNombre(String nombre);
|
|
||||||
}
|
|
||||||
@ -1,186 +0,0 @@
|
|||||||
package org.lapaloma.hogwarts.dao.impl;
|
|
||||||
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.PreparedStatement;
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
|
||||||
|
|
||||||
import org.lapaloma.hogwarts.dao.ICasaDAO;
|
|
||||||
import org.lapaloma.hogwarts.vo.Casa;
|
|
||||||
import org.springframework.stereotype.Repository;
|
|
||||||
|
|
||||||
@Repository
|
|
||||||
public class CasaDaoJDBC implements ICasaDAO {
|
|
||||||
private final DataSource dataSource;
|
|
||||||
|
|
||||||
// Spring inyecta el DataSource configurado automáticamente
|
|
||||||
public CasaDaoJDBC(DataSource dataSource) {
|
|
||||||
this.dataSource = dataSource;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Casa obtenerCasaPorClave(int identificador) {
|
|
||||||
Casa Casa = null;
|
|
||||||
|
|
||||||
String sentenciaSQL = """
|
|
||||||
SELECT * FROM casa
|
|
||||||
WHERE idCasa =?
|
|
||||||
""";
|
|
||||||
|
|
||||||
try (Connection conexion = dataSource.getConnection();
|
|
||||||
PreparedStatement sentenciaJDBCPreparada = conexion.prepareStatement(sentenciaSQL);) {
|
|
||||||
|
|
||||||
sentenciaJDBCPreparada.setInt(1, identificador);
|
|
||||||
System.out.println(sentenciaJDBCPreparada);
|
|
||||||
|
|
||||||
ResultSet resultadoSentencia = sentenciaJDBCPreparada.executeQuery();
|
|
||||||
|
|
||||||
if (resultadoSentencia.next()) {
|
|
||||||
Casa = getLineaFromResultSet(resultadoSentencia);
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
return Casa;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void actualizarCasa(Casa Casa) {
|
|
||||||
|
|
||||||
String sentenciaSQL = """
|
|
||||||
UPDATE casa
|
|
||||||
SET Nombre=?
|
|
||||||
WHERE idCasa=?
|
|
||||||
""";
|
|
||||||
|
|
||||||
try (Connection conexion = dataSource.getConnection();
|
|
||||||
PreparedStatement sentenciaJDBCPreparada = conexion.prepareStatement(sentenciaSQL);) {
|
|
||||||
|
|
||||||
sentenciaJDBCPreparada.setString(1, Casa.getNombre());
|
|
||||||
sentenciaJDBCPreparada.setInt(2, Casa.getIdentificador());
|
|
||||||
|
|
||||||
System.out.println(sentenciaJDBCPreparada);
|
|
||||||
|
|
||||||
sentenciaJDBCPreparada.executeUpdate();
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void crearCasa(Casa Casa) {
|
|
||||||
|
|
||||||
String sentenciaSQL = """
|
|
||||||
INSERT INTO casa (idCasa, Nombre)
|
|
||||||
VALUES (?, ?)
|
|
||||||
""";
|
|
||||||
|
|
||||||
try (Connection conexion = dataSource.getConnection();
|
|
||||||
PreparedStatement sentenciaJDBCPreparada = conexion.prepareStatement(sentenciaSQL);) {
|
|
||||||
|
|
||||||
sentenciaJDBCPreparada.setInt(1, Casa.getIdentificador());
|
|
||||||
sentenciaJDBCPreparada.setString(2, Casa.getNombre());
|
|
||||||
|
|
||||||
System.out.println(sentenciaJDBCPreparada);
|
|
||||||
|
|
||||||
sentenciaJDBCPreparada.executeUpdate();
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void borrarCasa(Casa Casa) {
|
|
||||||
|
|
||||||
String sentenciaSQL = """
|
|
||||||
DELETE FROM casa
|
|
||||||
WHERE idCasa=?
|
|
||||||
""";
|
|
||||||
|
|
||||||
try (Connection conexion = dataSource.getConnection();
|
|
||||||
PreparedStatement sentenciaJDBCPreparada = conexion.prepareStatement(sentenciaSQL);) {
|
|
||||||
|
|
||||||
sentenciaJDBCPreparada.setInt(1, Casa.getIdentificador());
|
|
||||||
sentenciaJDBCPreparada.executeUpdate();
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<Casa> obtenerListaCasas() {
|
|
||||||
|
|
||||||
List<Casa> lista = new ArrayList<>();
|
|
||||||
|
|
||||||
String sentenciaSQL = """
|
|
||||||
SELECT * FROM casa
|
|
||||||
""";
|
|
||||||
|
|
||||||
try (Connection conexion = dataSource.getConnection();
|
|
||||||
PreparedStatement sentenciaJDBCPreparada = conexion.prepareStatement(sentenciaSQL);) {
|
|
||||||
|
|
||||||
System.out.println(sentenciaJDBCPreparada);
|
|
||||||
|
|
||||||
ResultSet resultadoSentencia = sentenciaJDBCPreparada.executeQuery();
|
|
||||||
|
|
||||||
while (resultadoSentencia.next()) {
|
|
||||||
lista.add(getLineaFromResultSet(resultadoSentencia));
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
return lista;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<Casa> obtenerCasaPorNombre(String nombre) {
|
|
||||||
|
|
||||||
List<Casa> lista = new ArrayList<>();
|
|
||||||
|
|
||||||
String sentenciaSQL = """
|
|
||||||
SELECT * FROM casa
|
|
||||||
WHERE Nombre LIKE ?
|
|
||||||
""";
|
|
||||||
|
|
||||||
try (Connection conexion = dataSource.getConnection();
|
|
||||||
PreparedStatement sentenciaJDBCPreparada = conexion.prepareStatement(sentenciaSQL);) {
|
|
||||||
|
|
||||||
sentenciaJDBCPreparada.setString(1, "%" + nombre + "%");
|
|
||||||
|
|
||||||
System.out.println(sentenciaJDBCPreparada);
|
|
||||||
|
|
||||||
ResultSet resultadoSentencia = sentenciaJDBCPreparada.executeQuery();
|
|
||||||
|
|
||||||
while (resultadoSentencia.next()) {
|
|
||||||
lista.add(getLineaFromResultSet(resultadoSentencia));
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
return lista;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Casa getLineaFromResultSet(ResultSet resultadoSentencia) throws SQLException {
|
|
||||||
|
|
||||||
Casa Casa = new Casa();
|
|
||||||
|
|
||||||
Casa.setIdentificador(resultadoSentencia.getInt("idCasa"));
|
|
||||||
Casa.setNombre(resultadoSentencia.getString("Nombre"));
|
|
||||||
|
|
||||||
return Casa;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,12 +0,0 @@
|
|||||||
package org.lapaloma.hogwarts.excepcion;
|
|
||||||
|
|
||||||
public class CasaNoEncontradaException extends RuntimeException {
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
private static final long serialVersionUID = -3344627619585104664L;
|
|
||||||
|
|
||||||
public CasaNoEncontradaException(String mensaje) {
|
|
||||||
super(mensaje);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,76 +0,0 @@
|
|||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
package org.lapaloma.hogwarts.service;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.lapaloma.hogwarts.dao.ICasaDAO;
|
|
||||||
import org.lapaloma.hogwarts.excepcion.CasaNoEncontradaException;
|
|
||||||
import org.lapaloma.hogwarts.vo.Casa;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
public class CasaService {
|
|
||||||
|
|
||||||
private final ICasaDAO casaDAO;
|
|
||||||
|
|
||||||
// Spring inyecta el DAO automáticamente
|
|
||||||
public CasaService(ICasaDAO casaDAO) {
|
|
||||||
this.casaDAO = casaDAO;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public Casa obtenerCasaPorClave(Integer identificador) {
|
|
||||||
|
|
||||||
|
|
||||||
if (identificador == null || identificador <= 0) {
|
|
||||||
throw new IllegalArgumentException("Código inválido");
|
|
||||||
}
|
|
||||||
|
|
||||||
Casa Casa = casaDAO.obtenerCasaPorClave(identificador);
|
|
||||||
|
|
||||||
|
|
||||||
if (Casa == null) {
|
|
||||||
throw new CasaNoEncontradaException(
|
|
||||||
"No existe una casa con idenficador: " + identificador
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Casa;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Casa> obtenerListaCasas() {
|
|
||||||
|
|
||||||
List<Casa> lista = casaDAO.obtenerListaCasas();
|
|
||||||
|
|
||||||
// Esto provoca error
|
|
||||||
lista = null;
|
|
||||||
|
|
||||||
if (lista == null || lista.isEmpty()) {
|
|
||||||
throw new RuntimeException("No hay casas disponibles");
|
|
||||||
}
|
|
||||||
|
|
||||||
return lista;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Casa> obtenerCasaPorNombre(String nombre) {
|
|
||||||
|
|
||||||
if (nombre == null || nombre.isBlank()) {
|
|
||||||
throw new IllegalArgumentException("Nombre inválido");
|
|
||||||
}
|
|
||||||
|
|
||||||
List<Casa> lista = casaDAO.obtenerCasaPorNombre(nombre);
|
|
||||||
|
|
||||||
// Esto provoca error s
|
|
||||||
lista =null;
|
|
||||||
|
|
||||||
if (lista == null || lista.isEmpty()) {
|
|
||||||
throw new CasaNoEncontradaException(
|
|
||||||
"No existen casas con nombre: " + nombre
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return lista;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,12 +1,12 @@
|
|||||||
# Puerto en que escucha Spring Boot
|
# Puerto en que escucha Spring Boot
|
||||||
server.port=8080
|
server.port=8090
|
||||||
|
|
||||||
# Servicio de la base de datos.
|
# Servicio de la base de datos.
|
||||||
# Ejemplo url base dator:
|
# Ejemplo url base dator:
|
||||||
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
spring.datasource.driver-class-name=org.postgresql.Driver
|
||||||
spring.datasource.url=jdbc:mysql://${DB_HOST:192.168.1.36}:${DB_PORT:3306}/${DB_NAME:Hogwarts}
|
spring.datasource.url=jdbc:postgresql://${DB_HOST:192.168.1.36}:${DB_PORT:3306}/${DB_NAME:ministerio}
|
||||||
spring.datasource.username=${DB_USER:root}
|
spring.datasource.username=${DB_USER:root}
|
||||||
spring.datasource.password=${DB_PASSWORD:mysql_123}
|
spring.datasource.password=${DB_PASSWORD:postgresql_123}
|
||||||
|
|
||||||
# Parámetros del Pool de conexiones HikariCP
|
# Parámetros del Pool de conexiones HikariCP
|
||||||
spring.datasource.hikari.maximum-pool-size=10
|
spring.datasource.hikari.maximum-pool-size=10
|
||||||
|
|||||||
@ -9,54 +9,19 @@ import java.util.List;
|
|||||||
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.lapaloma.hogwarts.dao.ICasaDAO;
|
import org.lapaloma.gobierno.dao.IMinisterioDAO;
|
||||||
import org.lapaloma.hogwarts.excepcion.CasaNoEncontradaException;
|
import org.lapaloma.gobierno.service.MinisterioService;
|
||||||
import org.lapaloma.hogwarts.vo.Casa;
|
import org.lapaloma.gobierno.vo.Ministerio;
|
||||||
|
|
||||||
class CasaServiceTest {
|
class CasaServiceTest {
|
||||||
|
|
||||||
private CasaService casaService;
|
private MinisterioService ministerioService;
|
||||||
private FakeCasaDAO fakeDAO;
|
private FakeMinisterioDAO fakeDAO;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setUp() {
|
void setUp() {
|
||||||
fakeDAO = new FakeCasaDAO();
|
fakeDAO = new FakeMinisterioDAO();
|
||||||
casaService = new CasaService(fakeDAO);
|
ministerioService = new MinisterioService(fakeDAO);
|
||||||
}
|
|
||||||
|
|
||||||
// =========================
|
|
||||||
// obtenerCasaPorClave
|
|
||||||
// =========================
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void obtenerCasaPorClave_cuandoCodigoEsNull_lanzaExcepcion() {
|
|
||||||
assertThrows(IllegalArgumentException.class, () -> {
|
|
||||||
casaService.obtenerCasaPorClave(null);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void obtenerCasaPorClave_cuandoCodigoEstaVacio_lanzaExcepcion() {
|
|
||||||
assertThrows(IllegalArgumentException.class, () -> {
|
|
||||||
casaService.obtenerCasaPorClave(0);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void obtenerCasaPorClave_cuandoNoExiste_lanzaExcepcion() {
|
|
||||||
assertThrows(CasaNoEncontradaException.class, () -> {
|
|
||||||
casaService.obtenerCasaPorClave(99);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void obtenerCasaPorClave_cuandoExiste_retornaCasa() {
|
|
||||||
fakeDAO.crearCasa(new Casa(2, "Slytherin"));
|
|
||||||
|
|
||||||
Casa resultado = casaService.obtenerCasaPorClave(2);
|
|
||||||
|
|
||||||
assertNotNull(resultado);
|
|
||||||
assertEquals("Slytherin", resultado.getNombre());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// =========================
|
// =========================
|
||||||
@ -64,102 +29,32 @@ class CasaServiceTest {
|
|||||||
// =========================
|
// =========================
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void obtenerListaCasas_cuandoListaEstaVacia_lanzaExcepcion() {
|
void obtenerListaMinisterios_cuandoListaEstaVacio_lanzaExcepcion() {
|
||||||
assertThrows(RuntimeException.class, () -> {
|
assertThrows(RuntimeException.class, () -> {
|
||||||
casaService.obtenerListaCasas();
|
ministerioService.obtenerListaMinisterios();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void obtenerListaCasas_cuandoHayDatos_retornaLista() {
|
void obtenerListaMinistertio_cuandoHayDatos_retornaLista() {
|
||||||
fakeDAO.crearCasa(new Casa(2, "Slytherin"));
|
fakeDAO.data.add(new Ministerio(1, "Ministerio de Defensa", 120000, 100000));
|
||||||
|
|
||||||
List<Casa> resultado = casaService.obtenerListaCasas();
|
List<Ministerio> resultado = ministerioService.obtenerListaMinisterios();
|
||||||
|
|
||||||
assertNotNull(resultado);
|
assertNotNull(resultado);
|
||||||
assertEquals(1, resultado.size());
|
assertEquals(1, resultado.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
// =========================
|
static class FakeMinisterioDAO implements IMinisterioDAO {
|
||||||
// obtenerCasaPorNombre
|
|
||||||
// =========================
|
|
||||||
|
|
||||||
@Test
|
private List<Ministerio> data = new ArrayList<>();
|
||||||
void obtenerCasaPorNombre_cuandoNombreEsNull_lanzaExcepcion() {
|
|
||||||
assertThrows(IllegalArgumentException.class, () -> {
|
|
||||||
casaService.obtenerCasaPorNombre(null);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void obtenerCasaPorNombre_cuandoNombreEstaVacio_lanzaExcepcion() {
|
|
||||||
assertThrows(IllegalArgumentException.class, () -> {
|
|
||||||
casaService.obtenerCasaPorNombre("");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void obtenerCasaPorNombre_cuandoNoExiste_lanzaExcepcion() {
|
|
||||||
assertThrows(CasaNoEncontradaException.class, () -> {
|
|
||||||
casaService.obtenerCasaPorNombre("Inexistente");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void obtenerCasaPorNombre_cuandoExiste_retornaLista() {
|
|
||||||
fakeDAO.crearCasa(new Casa(2, "Slytherin"));
|
|
||||||
|
|
||||||
List<Casa> resultado = casaService.obtenerCasaPorNombre("Slytherin");
|
|
||||||
|
|
||||||
assertEquals(1, resultado.size());
|
|
||||||
}
|
|
||||||
|
|
||||||
// =========================
|
|
||||||
// Fake DAO. Se crea el DAO dentro del test para no depender de la conexión a la base de datos, de si hay red, de si accede a un fichero...
|
|
||||||
// En caso de usar el DOA real (CasaDaoJDBC) estaríamos hablando de prubeas de integración.
|
|
||||||
// =========================
|
|
||||||
|
|
||||||
static class FakeCasaDAO implements ICasaDAO {
|
|
||||||
|
|
||||||
private List<Casa> data = new ArrayList<>();
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Casa obtenerCasaPorClave(int identificador) {
|
public List<Ministerio> obtenerListaMinisterios() {
|
||||||
return data.stream()
|
|
||||||
.filter(c -> c.getIdentificador()==identificador)
|
|
||||||
.findFirst()
|
|
||||||
.orElse(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<Casa> obtenerListaCasas() {
|
|
||||||
return new ArrayList<>(data);
|
return new ArrayList<>(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<Casa> obtenerCasaPorNombre(String nombre) {
|
|
||||||
List<Casa> resultado = new ArrayList<>();
|
|
||||||
|
|
||||||
for (Casa c : data) {
|
|
||||||
if (c.getNombre().equals(nombre)) {
|
|
||||||
resultado.add(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return resultado;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void actualizarCasa(Casa Casa) {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void crearCasa(Casa Casa) {
|
|
||||||
data.add(Casa);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void borrarCasa(Casa Casa) {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user