Skip to content

Commit e878eee

Browse files
Copilotphrocker
andcommitted
Add comprehensive tests for ATPL chat service and finalize implementation
Co-authored-by: phrocker <[email protected]>
1 parent 42fbb3e commit e878eee

File tree

1 file changed

+160
-0
lines changed

1 file changed

+160
-0
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
package io.sentrius.sso.services;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
import static org.mockito.Mockito.*;
5+
6+
import java.util.ArrayList;
7+
import java.util.HashMap;
8+
import java.util.List;
9+
import java.util.Map;
10+
11+
import org.junit.jupiter.api.BeforeEach;
12+
import org.junit.jupiter.api.Test;
13+
import org.junit.jupiter.api.extension.ExtendWith;
14+
import org.mockito.Mock;
15+
import org.mockito.junit.jupiter.MockitoExtension;
16+
17+
import com.fasterxml.jackson.databind.ObjectMapper;
18+
import com.fasterxml.jackson.databind.node.ObjectNode;
19+
20+
import io.sentrius.sso.core.services.ATPLPolicyService;
21+
import io.sentrius.sso.core.trust.ATPLPolicy;
22+
23+
@ExtendWith(MockitoExtension.class)
24+
class ATPLChatServiceTest {
25+
26+
@Mock
27+
private ATPLPolicyService atplPolicyService;
28+
29+
private ObjectMapper objectMapper;
30+
private ATPLChatService atplChatService;
31+
32+
@BeforeEach
33+
void setUp() {
34+
objectMapper = new ObjectMapper();
35+
atplChatService = new ATPLChatService(atplPolicyService, objectMapper);
36+
}
37+
38+
@Test
39+
void testProcessATPLChatMessage_StartMessage() {
40+
// Given
41+
String userMessage = "I want to start creating a new ATPL policy";
42+
Map<String, Object> context = new HashMap<>();
43+
44+
// When
45+
String response = atplChatService.processATPLChatMessage(userMessage, context);
46+
47+
// Then
48+
assertNotNull(response);
49+
assertTrue(response.contains("I'll help you create a new ATPL policy"));
50+
assertTrue(response.contains("File operations"));
51+
assertTrue(response.contains("System monitoring"));
52+
}
53+
54+
@Test
55+
void testProcessATPLChatMessage_EndpointMessage() {
56+
// Given
57+
String userMessage = "What endpoints should I define?";
58+
Map<String, Object> context = new HashMap<>();
59+
60+
// Mock existing policies
61+
List<ATPLPolicy> existingPolicies = new ArrayList<>();
62+
when(atplPolicyService.getAllPolicies()).thenReturn(existingPolicies);
63+
64+
// When
65+
String response = atplChatService.processATPLChatMessage(userMessage, context);
66+
67+
// Then
68+
assertNotNull(response);
69+
assertTrue(response.contains("endpoints"));
70+
assertTrue(response.contains("/api/v1/"));
71+
}
72+
73+
@Test
74+
void testProcessATPLChatMessage_CommandMessage() {
75+
// Given
76+
String userMessage = "What commands should I allow?";
77+
Map<String, Object> context = new HashMap<>();
78+
79+
// When
80+
String response = atplChatService.processATPLChatMessage(userMessage, context);
81+
82+
// Then
83+
assertNotNull(response);
84+
assertTrue(response.contains("commands"));
85+
assertTrue(response.contains("ls"));
86+
assertTrue(response.contains("ps"));
87+
}
88+
89+
@Test
90+
void testSuggestCapabilities() {
91+
// Given
92+
String description = "I need an agent that can read files and monitor the system";
93+
94+
// When
95+
List<String> suggestions = atplChatService.suggestCapabilities(description);
96+
97+
// Then
98+
assertNotNull(suggestions);
99+
assertTrue(suggestions.contains("read_access"));
100+
assertTrue(suggestions.contains("monitoring_access"));
101+
assertTrue(suggestions.contains("filesystem_access"));
102+
}
103+
104+
@Test
105+
void testGenerateATPLPolicy() {
106+
// Given
107+
Map<String, Object> configuration = new HashMap<>();
108+
configuration.put("policy_id", "test_policy");
109+
configuration.put("description", "Test policy description");
110+
111+
// When
112+
ObjectNode policy = atplChatService.generateATPLPolicy(configuration);
113+
114+
// Then
115+
assertNotNull(policy);
116+
assertEquals("v0", policy.get("version").asText());
117+
assertEquals("test_policy", policy.get("policy_id").asText());
118+
assertEquals("Test policy description", policy.get("description").asText());
119+
assertTrue(policy.has("capabilities"));
120+
assertTrue(policy.get("capabilities").has("primitives"));
121+
}
122+
123+
@Test
124+
void testGenerateATPLPolicy_WithCapabilities() {
125+
// Given
126+
Map<String, Object> configuration = new HashMap<>();
127+
configuration.put("policy_id", "test_policy_with_caps");
128+
129+
List<Map<String, Object>> capabilities = new ArrayList<>();
130+
Map<String, Object> capability = new HashMap<>();
131+
capability.put("id", "test_capability");
132+
capability.put("description", "Test capability");
133+
134+
List<String> endpoints = List.of("/api/v1/test");
135+
List<String> commands = List.of("ls", "ps");
136+
List<String> activities = List.of("monitoring");
137+
138+
capability.put("endpoints", endpoints);
139+
capability.put("commands", commands);
140+
capability.put("activities", activities);
141+
142+
capabilities.add(capability);
143+
configuration.put("capabilities", capabilities);
144+
145+
// When
146+
ObjectNode policy = atplChatService.generateATPLPolicy(configuration);
147+
148+
// Then
149+
assertNotNull(policy);
150+
assertTrue(policy.has("capabilities"));
151+
assertTrue(policy.get("capabilities").has("primitives"));
152+
153+
ObjectNode firstCapability = (ObjectNode) policy.get("capabilities").get("primitives").get(0);
154+
assertEquals("test_capability", firstCapability.get("id").asText());
155+
assertEquals("Test capability", firstCapability.get("description").asText());
156+
assertTrue(firstCapability.has("endpoints"));
157+
assertTrue(firstCapability.has("commands"));
158+
assertTrue(firstCapability.has("activities"));
159+
}
160+
}

0 commit comments

Comments
 (0)