import requests
import pytest

# Base URL for the Java API
JAVA_API_BASE_URL = "http://localhost:8080"
# Base URL for the Python API
PYTHON_API_BASE_URL = "http://localhost:5000"

def test_java_api_hello_endpoint():
    """
    Test to verify the /hello endpoint of the Java API.
    It checks if the status code is 200 and the response body contains the expected message.
    """
    print("Testing Java API /hello endpoint...")
    try:
        response = requests.get(f"{JAVA_API_BASE_URL}/hello")
        response.raise_for_status()  # Raise an exception for HTTP errors (4xx or 5xx)
        assert response.status_code == 200
        assert response.text == "Hello from Java API!"
        print("Java API /hello endpoint test passed.")
    except requests.exceptions.ConnectionError as e:
        pytest.fail(f"Java API is not running or accessible at {JAVA_API_BASE_URL}. Error: {e}")
    except Exception as e:
        pytest.fail(f"An unexpected error occurred during Java API test: {e}")

def test_python_api_hello_endpoint():
    """
    Test to verify the /hello endpoint of the Python API.
    It checks if the status code is 200 and the response body contains the expected JSON message.
    """
    print("Testing Python API /hello endpoint...")
    try:
        response = requests.get(f"{PYTHON_API_BASE_URL}/hello")
        response.raise_for_status()  # Raise an exception for HTTP errors (4xx or 5xx)
        assert response.status_code == 200
        assert response.json() == {"message": "Hello from Python API!"}
        print("Python API /hello endpoint test passed.")
    except requests.exceptions.ConnectionError as e:
        pytest.fail(f"Python API is not running or accessible at {PYTHON_API_BASE_URL}. Error: {e}")
    except Exception as e:
        pytest.fail(f"An unexpected error occurred during Python API test: {e}")
