EcommerceWorkflowTest.groovy 13.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
/*
 * This software is in the public domain under CC0 1.0 Universal plus a 
 * Grant of Patent License.
 * 
 * To the extent possible under law, author(s) have dedicated all
 * copyright and related and neighboring rights to this software to the
 * public domain worldwide. This software is distributed without any
 * warranty.
 * 
 * You should have received a copy of the CC0 Public Domain Dedication
 * along with this software (see the LICENSE.md file). If not, see
 * <http://creativecommons.org/publicdomain/zero/1.0/>.
 */
package org.moqui.mcp.test.workflows

import groovy.json.JsonSlurper
import groovy.json.JsonOutput
import java.util.concurrent.atomic.AtomicInteger

/**
 * E-commerce Workflow Test for MCP
 * Tests complete product discovery to order placement workflow
 */
class EcommerceWorkflowTest {
    private String baseUrl
    private String username
    private String password
    private JsonSlurper jsonSlurper = new JsonSlurper()
    private String sessionId = null
    private AtomicInteger requestId = new AtomicInteger(1)
    
    // Test data
    def testProductId = null
    def testCustomerId = null
    def testOrderId = null
    
    EcommerceWorkflowTest(String baseUrl = "http://localhost:8080/mcp", 
                          String username = "john.sales", 
                          String password = "opencode") {
        this.baseUrl = baseUrl
        this.username = username
        this.password = password
    }
    
    /**
     * Initialize MCP session
     */
    boolean initialize() {
        println "🚀 Initializing MCP session for workflow test..."
        
        def response = sendJsonRpc("initialize", [
            protocolVersion: "2025-06-18",
            capabilities: [
                tools: [:],
                resources: [:]
            ],
            clientInfo: [
                name: "E-commerce Workflow Test",
                version: "1.0.0"
            ]
        ])
        
        if (response && response.result) {
            this.sessionId = response.result.sessionId
            println "✅ Session initialized: ${sessionId}"
            return true
        } else {
            println "❌ Failed to initialize session"
            return false
        }
    }
    
    /**
     * Send JSON-RPC request
     */
    def sendJsonRpc(String method, Map params = null) {
        def request = [
            jsonrpc: "2.0",
            id: requestId.getAndIncrement().toString(),
            method: method,
            params: params ?: [:]
        ]
        
        if (sessionId) {
            request.params.sessionId = sessionId
        }
        
        def jsonRequest = JsonOutput.toJson(request)
        
        try {
            def process = ["curl", "-s", "-u", "${username}:${password}", 
                          "-H", "Content-Type: application/json",
                          "-H", "Mcp-Session-Id: ${sessionId ?: ''}",
                          "-d", jsonRequest, baseUrl].execute()
            
            def responseText = process.text
            def response = jsonSlurper.parseText(responseText)
            
            if (response.error) {
                println "❌ Error: ${response.error.message}"
                return null
            }
            
            return response
        } catch (Exception e) {
            println "❌ Request failed: ${e.message}"
            return null
        }
    }
    
    /**
     * Execute a tool
     */
    def executeTool(String toolName, Map arguments = [:]) {
        def response = sendJsonRpc("tools/call", [
            name: toolName,
            arguments: arguments
        ])
        return response?.result
    }
    
    /**
     * Step 1: Product Discovery
     */
    boolean testProductDiscovery() {
        println "\n🔍 Step 1: Product Discovery"
        println "==========================="
        
        try {
            // Get available tools
            def toolsResponse = sendJsonRpc("tools/list")
            def tools = toolsResponse?.result?.tools ?: []
            println "Found ${tools.size()} available tools"
            
            // Find product-related tools
            def productTools = tools.findAll { 
                it.name?.toLowerCase()?.contains("product") || 
                it.description?.toLowerCase()?.contains("product")
            }
            println "Found ${productTools.size()} product-related tools"
            
            // Try to create a test product using MCP test service
            def createProductResult = executeTool("org.moqui.mcp.McpTestServices.create#TestProduct", [
                productName: "MCP Test Product ${System.currentTimeMillis()}",
                description: "Product created via MCP workflow test",
                price: 29.99,
                category: "MCP Test"
            ])
            
            if (createProductResult && createProductResult.success) {
                testProductId = createProductResult.productId
                println "✅ Created test product: ${testProductId}"
                return true
            } else {
                println "❌ Failed to create test product"
                return false
            }
            
        } catch (Exception e) {
            println "❌ Product discovery failed: ${e.message}"
            return false
        }
    }
    
    /**
     * Step 2: Customer Management
     */
    boolean testCustomerManagement() {
        println "\n👥 Step 2: Customer Management"
        println "==============================="
        
        try {
            // Create a test customer using MCP test service
            def createCustomerResult = executeTool("org.moqui.mcp.McpTestServices.create#TestCustomer", [
                firstName: "MCP",
                lastName: "TestCustomer",
                email: "test-${System.currentTimeMillis()}@mcp.test",
                phoneNumber: "555-TEST-MCP"
            ])
            
            if (createCustomerResult && createCustomerResult.success) {
                testCustomerId = createCustomerResult.partyId
                println "✅ Created test customer: ${testCustomerId}"
                return true
            } else {
                println "❌ Failed to create test customer"
                return false
            }
            
        } catch (Exception e) {
            println "❌ Customer management failed: ${e.message}"
            return false
        }
    }
    
    /**
     * Step 3: Order Placement
     */
    boolean testOrderPlacement() {
        println "\n🛒 Step 3: Order Placement"
        println "=========================="
        
        if (!testProductId || !testCustomerId) {
            println "❌ Missing test data: productId=${testProductId}, customerId=${testCustomerId}"
            return false
        }
        
        try {
            // Create a test order using MCP test service
            def createOrderResult = executeTool("org.moqui.mcp.McpTestServices.create#TestOrder", [
                customerId: testCustomerId,
                productId: testProductId,
                quantity: 2,
                price: 29.99
            ])
            
            if (createOrderResult && createOrderResult.success) {
                testOrderId = createOrderResult.orderId
                println "✅ Created test order: ${testOrderId}"
                return true
            } else {
                println "❌ Failed to create test order"
                return false
            }
            
        } catch (Exception e) {
            println "❌ Order placement failed: ${e.message}"
            return false
        }
    }
    
    /**
     * Step 4: Screen-based Workflow
     */
    boolean testScreenBasedWorkflow() {
        println "\n🖥️ Step 4: Screen-based Workflow"
        println "================================="
        
        try {
            // Get available tools
            def toolsResponse = sendJsonRpc("tools/list")
            def tools = toolsResponse?.result?.tools ?: []
            
            // Find catalog screens
            def catalogScreens = tools.findAll { 
                it.name?.toLowerCase()?.contains("catalog") 
            }
            
            if (catalogScreens) {
                println "Found ${catalogScreens.size()} catalog screens"
                
                // Try to execute the first catalog screen
                def catalogResult = executeTool(catalogScreens[0].name)
                if (catalogResult) {
                    println "✅ Successfully executed catalog screen: ${catalogScreens[0].name}"
                    return true
                } else {
                    println "❌ Failed to execute catalog screen"
                    return false
                }
            } else {
                println "⚠️ No catalog screens found, skipping screen test"
                return true // Not a failure, just not available
            }
            
        } catch (Exception e) {
            println "❌ Screen-based workflow failed: ${e.message}"
            return false
        }
    }
    
    /**
     * Step 5: Complete E-commerce Workflow
     */
    boolean testCompleteWorkflow() {
        println "\n🔄 Step 5: Complete E-commerce Workflow"
        println "========================================"
        
        try {
            // Run the complete e-commerce workflow service
            def workflowResult = executeTool("org.moqui.mcp.McpTestServices.run#EcommerceWorkflow", [
                productName: "Complete Workflow Product ${System.currentTimeMillis()}",
                customerFirstName: "Workflow",
                customerLastName: "Test",
                customerEmail: "workflow-${System.currentTimeMillis()}@mcp.test",
                quantity: 1,
                price: 49.99
            ])
            
            if (workflowResult && workflowResult.success) {
                println "✅ Complete workflow executed successfully"
                println "   Workflow ID: ${workflowResult.workflowId}"
                println "   Product ID: ${workflowResult.productId}"
                println "   Customer ID: ${workflowResult.customerId}"
                println "   Order ID: ${workflowResult.orderId}"
                
                // Print workflow steps
                workflowResult.steps?.each { step ->
                    println "   ${step.success ? '✅' : '❌'} ${step.step}: ${step.message}"
                }
                
                return true
            } else {
                println "❌ Complete workflow failed"
                return false
            }
            
        } catch (Exception e) {
            println "❌ Complete workflow failed: ${e.message}"
            return false
        }
    }
    
    /**
     * Step 6: Cleanup Test Data
     */
    boolean testCleanup() {
        println "\n🧹 Step 6: Cleanup Test Data"
        println "============================"
        
        try {
            // Cleanup test data using MCP test service
            def cleanupResult = executeTool("org.moqui.mcp.McpTestServices.cleanup#TestData", [
                olderThanHours: 0 // Cleanup immediately
            ])
            
            if (cleanupResult && cleanupResult.success) {
                println "✅ Test data cleanup completed"
                println "   Deleted orders: ${cleanupResult.deletedOrders}"
                println "   Deleted products: ${cleanupResult.deletedProducts}"
                println "   Deleted customers: ${cleanupResult.deletedCustomers}"
                return true
            } else {
                println "❌ Test data cleanup failed"
                return false
            }
            
        } catch (Exception e) {
            println "❌ Cleanup failed: ${e.message}"
            return false
        }
    }
    
    /**
     * Run complete e-commerce workflow test
     */
    void runCompleteTest() {
        println "🧪 E-commerce Workflow Test for MCP"
        println "=================================="
        
        def startTime = System.currentTimeMillis()
        def results = [:]
        
        // Initialize session
        if (!initialize()) {
            println "❌ Failed to initialize MCP session"
            return
        }
        
        // Run all test steps
        results.productDiscovery = testProductDiscovery()
        results.customerManagement = testCustomerManagement()
        results.orderPlacement = testOrderPlacement()
        results.screenBasedWorkflow = testScreenBasedWorkflow()
        results.completeWorkflow = testCompleteWorkflow()
        results.cleanup = testCleanup()
        
        // Generate report
        def endTime = System.currentTimeMillis()
        def duration = endTime - startTime
        
        println "\n" + "="*60
        println "📋 E-COMMERCE WORKFLOW TEST REPORT"
        println "="*60
        println "Duration: ${duration}ms"
        println ""
        
        def totalSteps = results.size()
        def successfulSteps = results.count { it.value }
        
        results.each { stepName, success ->
            println "${success ? '✅' : '❌'} ${stepName}"
        }
        
        println ""
        println "Overall Result: ${successfulSteps}/${totalSteps} steps passed"
        println "Success Rate: ${(successfulSteps/totalSteps * 100).round()}%"
        
        if (successfulSteps == totalSteps) {
            println "🎉 ALL TESTS PASSED! MCP e-commerce workflow is working correctly."
        } else {
            println "⚠️ Some tests failed. Check the output above for details."
        }
        
        println "="*60
    }
    
    /**
     * Main method for standalone execution
     */
    static void main(String[] args) {
        def test = new EcommerceWorkflowTest()
        test.runCompleteTest()
    }
}