Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions gitgalaxy/tools/cobol_to_java/cobol_to_java_test_forge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#!/usr/bin/env python3
# ==============================================================================
# GitGalaxy Spoke: Java Spring Test Forge (Phase 3)
# Purpose: Auto-generates JUnit 5 and Spring Boot integration tests to
# mathematically prove the translated architecture compiles and runs.
# ==============================================================================
import argparse
import sys
import json
from pathlib import Path

def generate_context_test(package_name: str, class_name: str) -> str:
"""Forges a @SpringBootTest to ensure the entire dependency injection context loads."""
return f"""package {package_name};

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class {class_name}ApplicationTests {{

@Test
void contextLoads() {{
// If the application.yml is broken or a @Service is missing,
// this test will fail immediately, catching structural regressions.
}}
}}
"""

def generate_controller_test(package_name: str, class_name: str, endpoint_path: str) -> str:
"""Forges a @WebMvcTest to verify REST API mappings without booting the full server."""
service_var = class_name[0].lower() + class_name[1:]

return f"""package {package_name}.controller;

import {package_name}.service.{class_name}Service;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest({class_name}Controller.class)
class {class_name}ControllerTest {{

@Autowired
private MockMvc mockMvc;

@MockBean
private {class_name}Service {service_var}Service;

@Test
void testEndpointRouting() throws Exception {{
// Verifies the @RequestMapping and @PostMapping forged by the API Contract script
mockMvc.perform(post("/api/v1/{endpoint_path}/execute")
.contentType("application/json")
.content("{{}}")) // Empty JSON payload for structural routing test
.andExpect(status().isOk()); // Or isNoContent() based on outputs
}}
}}
"""

def main():
parser = argparse.ArgumentParser(description="GitGalaxy Java Test Forge")
parser.add_argument("ir_file", help="Path to the GitGalaxy _ir.json state dump")
parser.add_argument("--pkg", default="com.gitgalaxy.modernized", help="Base Java package name")
args = parser.parse_args()

ir_path = Path(args.ir_file).resolve()
if not ir_path.exists():
sys.exit(1)

try:
ir_state = json.loads(ir_path.read_text(encoding='utf-8'))

# Determine class names
prog_id = ir_state.get("metadata", {}).get("file_name", "Unknown").split('.')[0]
class_name = "".join(word.capitalize() for word in prog_id.split('-'))
endpoint_path = prog_id.lower()

# Target output directories (Assuming standard Maven layout)
test_dir = ir_path.parent / "src" / "test" / "java" / args.pkg.replace('.', '/')
test_dir.mkdir(parents=True, exist_ok=True)

# 1. Forge Context Test
context_code = generate_context_test(args.pkg, class_name)
(test_dir / f"{class_name}ApplicationTests.java").write_text(context_code, encoding='utf-8')

# 2. Forge Controller Test
controller_test_dir = test_dir / "controller"
controller_test_dir.mkdir(exist_ok=True)
controller_code = generate_controller_test(args.pkg, class_name, endpoint_path)
(controller_test_dir / f"{class_name}ControllerTest.java").write_text(controller_code, encoding='utf-8')

print(f"🧪 Spring Boot Test Suite Forged for {class_name}")

except Exception as e:
print(f"Error forging Java tests: {e}")

if __name__ == "__main__":
main()
Loading