McpIntegrationTest.java
8.65 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
/*
* 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 org.junit.jupiter.api.*;
import org.moqui.context.ExecutionContext;
import org.moqui.context.ExecutionContextFactory;
import org.moqui.entity.EntityValue;
import org.moqui.Moqui;
import static org.junit.jupiter.api.Assertions.*;
/**
* MCP Integration Tests - Tests MCP services with running Moqui instance
*/
public class McpIntegrationTest {
private static ExecutionContextFactory ecf;
private ExecutionContext ec;
@BeforeAll
static void initMoqui() {
System.out.println("🚀 Initializing Moqui for MCP tests...");
try {
ecf = Moqui.getExecutionContextFactory();
assertNotNull(ecf, "ExecutionContextFactory should not be null");
System.out.println("✅ Moqui initialized successfully");
} catch (Exception e) {
fail("Failed to initialize Moqui: " + e.getMessage());
}
}
@AfterAll
static void destroyMoqui() {
if (ecf != null) {
System.out.println("🔒 Destroying Moqui...");
ecf.destroy();
}
}
@BeforeEach
void setUp() {
ec = ecf.getExecutionContext();
assertNotNull(ec, "ExecutionContext should not be null");
}
@AfterEach
void tearDown() {
if (ec != null) {
ec.destroy();
}
}
@Test
@DisplayName("Test MCP Services Initialization")
void testMcpServicesInitialization() {
System.out.println("🔍 Testing MCP Services Initialization...");
try {
// Check if MCP services are available
boolean mcpServiceAvailable = ec.getService().isServiceDefined("org.moqui.mcp.McpServices.initialize#McpSession");
assertTrue(mcpServiceAvailable, "MCP initialize service should be available");
// Check if MCP entities exist
long mcpSessionCount = ec.getEntity().findCount("org.moqui.mcp.entity.McpSession");
System.out.println("📊 Found " + mcpSessionCount + " MCP sessions");
// Check if MCP tools service is available
boolean toolsServiceAvailable = ec.getService().isServiceDefined("org.moqui.mcp.McpServices.list#McpTools");
assertTrue(toolsServiceAvailable, "MCP tools service should be available");
// Check if MCP resources service is available
boolean resourcesServiceAvailable = ec.getService().isServiceDefined("org.moqui.mcp.McpServices.list#McpResources");
assertTrue(resourcesServiceAvailable, "MCP resources service should be available");
System.out.println("✅ MCP services are properly initialized");
} catch (Exception e) {
fail("MCP services initialization failed: " + e.getMessage());
}
}
@Test
@DisplayName("Test MCP Session Creation")
void testMcpSessionCreation() {
System.out.println("🔍 Testing MCP Session Creation...");
try {
// Create a new MCP session
EntityValue session = ec.getService().sync().name("org.moqui.mcp.McpServices.initialize#McpSession")
.parameters("protocolVersion", "2025-06-18")
.parameters("clientInfo", [name: "Test Client", version: "1.0.0"])
.call();
assertNotNull(session, "MCP session should be created");
assertNotNull(session.get("sessionId"), "Session ID should not be null");
String sessionId = session.getString("sessionId");
System.out.println("✅ Created MCP session: " + sessionId);
// Verify session exists in database
EntityValue foundSession = ec.getEntity().find("org.moqui.mcp.entity.McpSession")
.condition("sessionId", sessionId)
.one();
assertNotNull(foundSession, "Session should be found in database");
assertEquals(sessionId, foundSession.getString("sessionId"));
System.out.println("✅ Session verified in database");
} catch (Exception e) {
fail("MCP session creation failed: " + e.getMessage());
}
}
@Test
@DisplayName("Test MCP Tools List")
void testMcpToolsList() {
System.out.println("🔍 Testing MCP Tools List...");
try {
// First create a session
EntityValue session = ec.getService().sync().name("org.moqui.mcp.McpServices.initialize#McpSession")
.parameters("protocolVersion", "2025-06-18")
.parameters("clientInfo", [name: "Test Client", version: "1.0.0"])
.call();
String sessionId = session.getString("sessionId");
// List tools
EntityValue toolsResult = ec.getService().sync().name("org.moqui.mcp.McpServices.list#McpTools")
.parameters("sessionId", sessionId)
.call();
assertNotNull(toolsResult, "Tools result should not be null");
Object tools = toolsResult.get("tools");
assertNotNull(tools, "Tools list should not be null");
System.out.println("✅ Retrieved MCP tools successfully");
} catch (Exception e) {
fail("MCP tools list failed: " + e.getMessage());
}
}
@Test
@DisplayName("Test MCP Resources List")
void testMcpResourcesList() {
System.out.println("🔍 Testing MCP Resources List...");
try {
// First create a session
EntityValue session = ec.getService().sync().name("org.moqui.mcp.McpServices.initialize#McpSession")
.parameters("protocolVersion", "2025-06-18")
.parameters("clientInfo", [name: "Test Client", version: "1.0.0"])
.call();
String sessionId = session.getString("sessionId");
// List resources
EntityValue resourcesResult = ec.getService().sync().name("org.moqui.mcp.McpServices.list#McpResources")
.parameters("sessionId", sessionId)
.call();
assertNotNull(resourcesResult, "Resources result should not be null");
Object resources = resourcesResult.get("resources");
assertNotNull(resources, "Resources list should not be null");
System.out.println("✅ Retrieved MCP resources successfully");
} catch (Exception e) {
fail("MCP resources list failed: " + e.getMessage());
}
}
@Test
@DisplayName("Test MCP Ping")
void testMcpPing() {
System.out.println("🔍 Testing MCP Ping...");
try {
// Ping the MCP service
EntityValue pingResult = ec.getService().sync().name("org.moqui.mcp.McpServices.ping#Mcp")
.call();
assertNotNull(pingResult, "Ping result should not be null");
Object pong = pingResult.get("pong");
assertNotNull(pong, "Pong should not be null");
System.out.println("✅ MCP ping successful: " + pong);
} catch (Exception e) {
fail("MCP ping failed: " + e.getMessage());
}
}
@Test
@DisplayName("Test MCP Health Check")
void testMcpHealthCheck() {
System.out.println("🔍 Testing MCP Health Check...");
try {
// Check MCP health
EntityValue healthResult = ec.getService().sync().name("org.moqui.mcp.McpServices.health#Mcp")
.call();
assertNotNull(healthResult, "Health result should not be null");
Object status = healthResult.get("status");
assertNotNull(status, "Health status should not be null");
System.out.println("✅ MCP health check successful: " + status);
} catch (Exception e) {
fail("MCP health check failed: " + e.getMessage());
}
}
}