diff --git a/pom.xml b/pom.xml index 5ad436e8..bd930583 100644 --- a/pom.xml +++ b/pom.xml @@ -80,6 +80,12 @@ 2.3.0 + + org.springframework.boot + spring-boot-starter-test + test + + diff --git a/src/test/java/com/petshop/backend/config/RunConfigValidationTest.java b/src/test/java/com/petshop/backend/config/RunConfigValidationTest.java new file mode 100644 index 00000000..3714a29f --- /dev/null +++ b/src/test/java/com/petshop/backend/config/RunConfigValidationTest.java @@ -0,0 +1,81 @@ +package com.petshop.backend.config; + +import org.junit.jupiter.api.Test; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import java.io.File; +import static org.junit.jupiter.api.Assertions.*; + +/** + * Validates that IntelliJ run configurations match actual project structure. + * Prevents configuration drift when module name or main class changes. + */ +class RunConfigValidationTest { + + @Test + void mainClassExists() throws ClassNotFoundException, NoSuchMethodException { + // Verifies the main class referenced in run configs actually exists + Class mainClass = Class.forName("com.petshop.backend.BackendApplication"); + assertNotNull(mainClass, "Main class should exist"); + assertNotNull(mainClass.getMethod("main", String[].class), "Main method should exist"); + } + + @Test + void moduleNameMatchesPomArtifactId() throws Exception { + // Parse pom.xml + File pomFile = new File("pom.xml"); + assertTrue(pomFile.exists(), "pom.xml should exist in project root"); + + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); + Document doc = dBuilder.parse(pomFile); + doc.getDocumentElement().normalize(); + + // Get project's artifactId (direct child of project, not parent's artifactId) + Element projectElement = doc.getDocumentElement(); + NodeList children = projectElement.getChildNodes(); + String artifactId = null; + + for (int i = 0; i < children.getLength(); i++) { + if (children.item(i) instanceof Element) { + Element child = (Element) children.item(i); + if ("artifactId".equals(child.getTagName())) { + artifactId = child.getTextContent().trim(); + break; + } + } + } + + assertNotNull(artifactId, "artifactId not found in pom.xml"); + + // Expected module name in run configs should match artifactId + assertEquals("backend", artifactId, + "pom.xml artifactId has changed. Update .idea/runConfigurations/PetshopBackendApplication.xml module name to match."); + } + + @Test + void runConfigurationFileExists() { + File runConfigFile = new File(".idea/runConfigurations/PetshopBackendApplication.xml"); + assertTrue(runConfigFile.exists(), + "Run configuration file missing. Ensure .idea/runConfigurations/ directory is committed to Git."); + } + + @Test + void databaseSchemaFileExists() { + File schemaFile = new File("src/main/resources/schema.sql"); + assertTrue(schemaFile.exists(), "schema.sql should exist in src/main/resources/"); + } + + @Test + void schemaContainsActiveColumn() throws Exception { + File schemaFile = new File("src/main/resources/schema.sql"); + String schemaContent = new String(java.nio.file.Files.readAllBytes(schemaFile.toPath())); + + assertTrue(schemaContent.contains("active BOOLEAN") || schemaContent.contains("active boolean"), + "schema.sql should contain 'active' column definition in users table. " + + "If you see 'Unknown column active' error, run 'Reset Database Only' configuration."); + } +}