PopCommerceOrderTest.java
19 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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*
* 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;
import groovy.json.JsonSlurper
import java.util.regex.Pattern
/**
* PopCommerce Order Workflow Test
* Tests complete workflow: Product lookup → Order placement for John Doe
*/
class PopCommerceOrderTest {
private McpJavaClient client
private JsonSlurper jsonSlurper = new JsonSlurper()
// Test data
def testCustomerId = null
def testProductId = null
def testOrderId = null
PopCommerceOrderTest(McpJavaClient client) {
this.client = client
}
/**
* Step 1: Find and access PopCommerce catalog
*/
boolean testPopCommerceCatalogAccess() {
println "\n🛍️ Testing PopCommerce Catalog Access"
println "===================================="
try {
def tools = client.getTools()
// Find PopCommerce catalog screens
def catalogScreens = tools.findAll {
(it.name?.toLowerCase()?.contains("catalog") ||
it.name?.toLowerCase()?.contains("product")) &&
(it.description?.toLowerCase()?.contains("popcommerce") ||
it.name?.toLowerCase()?.contains("popcommerce"))
}
if (catalogScreens.size() == 0) {
// Try broader search for any catalog/product screens
catalogScreens = tools.findAll {
it.name?.toLowerCase()?.contains("catalog") ||
it.name?.toLowerCase()?.contains("product")
}
}
if (catalogScreens.size() == 0) {
client.recordStep("Find Catalog Screens", false, "No catalog screens found")
return false
}
client.recordStep("Find Catalog Screens", true, "Found ${catalogScreens.size()} catalog screens")
// Try to render catalog screen
def catalogScreen = catalogScreens[0]
println " 📱 Testing catalog screen: ${catalogScreen.name}"
def catalogResult = client.executeTool(catalogScreen.name, [:])
if (!catalogResult || !catalogResult.content) {
client.recordStep("Render Catalog Screen", false, "Failed to render catalog screen")
return false
}
def content = catalogResult.content[0]
if (!content.text || content.text.length() == 0) {
client.recordStep("Render Catalog Screen", false, "Catalog screen returned empty content")
return false
}
client.recordStep("Render Catalog Screen", true,
"Catalog rendered successfully (${content.text.length()} chars)")
// Look for product listings in content
def hasProducts = content.text.toLowerCase().contains("product") ||
content.text.toLowerCase().contains("item") ||
content.text.contains("<table") ||
content.text.contains("productList")
if (hasProducts) {
client.recordStep("Catalog Contains Products", true, "Catalog appears to contain product listings")
} else {
client.recordStep("Catalog Contains Products", false, "Catalog doesn't appear to have products")
}
return true
} catch (Exception e) {
client.recordStep("Catalog Access", false, e.message)
return false
}
}
/**
* Step 2: Search for blue products
*/
boolean testBlueProductSearch() {
println "\n🔵 Testing Blue Product Search"
println "================================"
try {
def tools = client.getTools()
// Find catalog or search screens
def searchScreens = tools.findAll {
it.name?.toLowerCase()?.contains("catalog") ||
it.name?.toLowerCase()?.contains("product") ||
it.name?.toLowerCase()?.contains("search")
}
if (searchScreens.size() == 0) {
client.recordStep("Find Search Screens", false, "No search screens found")
return false
}
client.recordStep("Find Search Screens", true, "Found ${searchScreens.size()} search screens")
// Try different search approaches
def foundBlueProduct = false
searchScreens.each { screen ->
try {
println " 🔍 Searching with screen: ${screen.name}"
// Try with search parameters
def searchParams = [:]
// Common search parameter names
def paramNames = ["search", "query", "productName", "name", "description", "color"]
paramNames.each { paramName ->
if (screen.inputSchema?.properties?.containsKey(paramName)) {
searchParams[paramName] = "blue"
}
}
def searchResult = client.executeTool(screen.name, searchParams)
if (searchResult && searchResult.content) {
def content = searchResult.content[0].text
// Check if we found blue products
if (content.toLowerCase().contains("blue") ||
Pattern.compile(?i)\bblue\b).matcher(content).find()) {
println " ✅ Found blue products!"
foundBlueProduct = true
// Try to extract a product ID
def productIdMatch = content =~ /product[_-]?id["\s]*[:=]["\s]*([A-Z0-9-_]+)/i
if (productIdMatch.find()) {
testProductId = productIdMatch[0][1]
println " 📋 Extracted product ID: ${testProductId}"
}
return true // Stop searching
}
}
} catch (Exception e) {
println " ❌ Search failed: ${e.message}"
}
}
if (!foundBlueProduct) {
// Try without search params - just get all products and look for blue ones
def catalogScreens = tools.findAll {
it.name?.toLowerCase()?.contains("catalog") ||
it.name?.toLowerCase()?.contains("product")
}
catalogScreens.each { screen ->
try {
def result = client.executeTool(screen.name, [:])
if (result && result.content) {
def content = result.content[0].text
if (content.toLowerCase().contains("blue")) {
foundBlueProduct = true
println " ✅ Found blue products in catalog"
return true
}
}
} catch (Exception e) {
println " ❌ Catalog check failed: ${e.message}"
}
}
}
if (foundBlueProduct) {
client.recordStep("Find Blue Products", true, "Successfully found blue products")
} else {
client.recordStep("Find Blue Products", false, "No blue products found")
}
return foundBlueProduct
} catch (Exception e) {
client.recordStep("Blue Product Search", false, e.message)
return false
}
}
/**
* Step 3: Find or create John Doe customer
*/
boolean testJohnDoeCustomer() {
println "\n👤 Testing John Doe Customer"
println "=============================="
try {
def tools = client.getTools()
// Look for customer screens
def customerScreens = tools.findAll {
it.name?.toLowerCase()?.contains("customer") ||
it.name?.toLowerCase()?.contains("party")
}
if (customerScreens.size() == 0) {
client.recordStep("Find Customer Screens", false, "No customer screens found")
return false
}
client.recordStep("Find Customer Screens", true, "Found ${customerScreens.size()} customer screens")
// Try to find John Doe
def foundJohnDoe = false
customerScreens.each { screen ->
try {
println " 👥 Searching for John Doe with screen: ${screen.name}"
// Try search parameters
def searchParams = [:]
def paramNames = ["search", "query", "firstName", "lastName", "name"]
paramNames.each { paramName ->
if (screen.inputSchema?.properties?.containsKey(paramName)) {
if (paramName.contains("first")) {
searchParams[paramName] = "John"
} else if (paramName.contains("last")) {
searchParams[paramName] = "Doe"
} else {
searchParams[paramName] = "John Doe"
}
}
}
def searchResult = client.executeTool(screen.name, searchParams)
if (searchResult && searchResult.content) {
def content = searchResult.content[0].text
// Check if we found John Doe
if (content.toLowerCase().contains("john") &&
content.toLowerCase().contains("doe")) {
println " ✅ Found John Doe!"
foundJohnDoe = true
// Try to extract customer ID
def customerIdMatch = content =~ /party[_-]?id["\s]*[:=]["\s]*([A-Z0-9-_]+)/i
if (customerIdMatch.find()) {
testCustomerId = customerIdMatch[0][1]
println " 📋 Extracted customer ID: ${testCustomerId}"
}
return true
}
}
} catch (Exception e) {
println " ❌ Customer search failed: ${e.message}"
}
}
if (foundJohnDoe) {
client.recordStep("Find John Doe", true, "Successfully found John Doe customer")
} else {
client.recordStep("Find John Doe", false, "John Doe customer not found")
}
return foundJohnDoe
} catch (Exception e) {
client.recordStep("John Doe Customer", false, e.message)
return false
}
}
/**
* Step 4: Create order for John Doe
*/
boolean testOrderCreation() {
println "\n🛒 Testing Order Creation"
println "=========================="
if (!testCustomerId) {
client.recordStep("Order Creation", false, "No customer ID available")
return false
}
try {
def tools = client.getTools()
// Find order screens
def orderScreens = tools.findAll {
it.name?.toLowerCase()?.contains("order") &&
!it.name?.toLowerCase()?.contains("find") &&
!it.name?.toLowerCase()?.contains("list")
}
if (orderScreens.size() == 0) {
client.recordStep("Find Order Screens", false, "No order creation screens found")
return false
}
client.recordStep("Find Order Screens", true, "Found ${orderScreens.size()} order screens")
// Try to create order
def orderCreated = false
orderScreens.each { screen ->
try {
println " 📝 Creating order with screen: ${screen.name}"
def orderParams = [:]
// Add customer ID if parameter exists
def paramNames = ["customerId", "partyId", "customer", "customerPartyId"]
paramNames.each { paramName ->
if (screen.inputSchema?.properties?.containsKey(paramName)) {
orderParams[paramName] = testCustomerId
}
}
// Add product ID if we have one
if (testProductId) {
def productParamNames = ["productId", "product", "itemId"]
productParamNames.each { paramName ->
if (screen.inputSchema?.properties?.containsKey(paramName)) {
orderParams[paramName] = testProductId
}
}
}
// Add quantity
if (screen.inputSchema?.properties?.containsKey("quantity")) {
orderParams.quantity = "1"
}
def orderResult = client.executeTool(screen.name, orderParams)
if (orderResult && orderResult.content) {
def content = orderResult.content[0].text
// Check if order was created
if (content.toLowerCase().contains("order") &&
(content.toLowerCase().contains("created") ||
content.toLowerCase().contains("success") ||
content.contains("orderId"))) {
println " ✅ Order created successfully!"
orderCreated = true
// Try to extract order ID
def orderIdMatch = content =~ /order[_-]?id["\s]*[:=]["\s]*([A-Z0-9-_]+)/i
if (orderIdMatch.find()) {
testOrderId = orderIdMatch[0][1]
println " 📋 Extracted order ID: ${testOrderId}"
}
return true
}
}
} catch (Exception e) {
println " ❌ Order creation failed: ${e.message}"
}
}
if (orderCreated) {
client.recordStep("Create Order", true, "Successfully created order for John Doe")
} else {
client.recordStep("Create Order", false, "Failed to create order")
}
return orderCreated
} catch (Exception e) {
client.recordStep("Order Creation", false, e.message)
return false
}
}
/**
* Step 5: Validate complete workflow
*/
boolean testWorkflowValidation() {
println "\n✅ Testing Workflow Validation"
println "==============================="
try {
def allStepsComplete = testCustomerId && testOrderId
if (allStepsComplete) {
client.recordStep("Workflow Validation", true,
"Complete workflow successful - Customer: ${testCustomerId}, Order: ${testOrderId}")
} else {
client.recordStep("Workflow Validation", false,
"Incomplete workflow - Customer: ${testCustomerId}, Order: ${testOrderId}")
}
return allStepsComplete
} catch (Exception e) {
client.recordStep("Workflow Validation", false, e.message)
return false
}
}
/**
* Run complete PopCommerce order workflow test
*/
boolean runCompleteTest() {
println "🛍️ Running PopCommerce Order Workflow Test"
println "========================================"
client.startWorkflow("PopCommerce Order Workflow")
def results = [
testPopCommerceCatalogAccess(),
testBlueProductSearch(),
testJohnDoeCustomer(),
testOrderCreation(),
testWorkflowValidation()
]
def workflowResult = client.completeWorkflow()
// Print summary
println "\n📊 Workflow Summary:"
println " Customer ID: ${testCustomerId ?: 'Not found'}"
println " Product ID: ${testProductId ?: 'Not found'}"
println " Order ID: ${testOrderId ?: 'Not created'}"
return workflowResult?.success ?: false
}
/**
* Main method for standalone execution
*/
static void main(String[] args) {
def client = new McpJavaClient()
def test = new PopCommerceOrderTest(client)
try {
if (!client.initialize()) {
println "❌ Failed to initialize MCP client"
return
}
def success = test.runCompleteTest()
println "\n" + "="*60
println "🏁 POPCOMMERCE ORDER WORKFLOW TEST COMPLETE"
println "="*60
println "Overall Result: ${success ? '✅ PASSED' : '❌ FAILED'}"
println "="*60
} finally {
client.close()
}
}
}