Skip to content

Commit f1a1316

Browse files
Fixed tests
1 parent 3416a0f commit f1a1316

File tree

4 files changed

+81
-61
lines changed

4 files changed

+81
-61
lines changed

src/test/java/de/tum/cit/aet/artemis/hyperion/service/codegeneration/HyperionCodeGenerationStrategyTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ void generateCode_withValidInput_orchestratesAllStepsAndReturnsFiles() throws Ex
8585

8686
// Verify all 4 steps were called in correct order
8787
verify(chatModel, times(4)).call(any(Prompt.class));
88-
verify(templates).render(eq("test-plan-template"), any(Map.class));
89-
verify(templates).render(eq("test-structure-template"), any(Map.class));
90-
verify(templates).render(eq("test-headers-template"), any(Map.class));
91-
verify(templates).render(eq("test-logic-template"), any(Map.class));
88+
verify(templates).renderObject(eq("test-plan-template"), any(Map.class));
89+
verify(templates).renderObject(eq("test-structure-template"), any(Map.class));
90+
verify(templates).renderObject(eq("test-headers-template"), any(Map.class));
91+
verify(templates).renderObject(eq("test-logic-template"), any(Map.class));
9292
}
9393

9494
@Test
@@ -155,10 +155,10 @@ private void setupMockTemplateAndChatResponses(String finalResponse) throws Exce
155155
String structureResponse = "{\"solutionPlan\":\"plan\",\"files\":[{\"path\":\"stub\",\"content\":\"stub\"}]}";
156156
String headersResponse = "{\"solutionPlan\":\"plan\",\"files\":[{\"path\":\"headers\",\"content\":\"headers\"}]}";
157157

158-
when(templates.render(eq("test-plan-template"), any(Map.class))).thenReturn("rendered plan");
159-
when(templates.render(eq("test-structure-template"), any(Map.class))).thenReturn("rendered structure");
160-
when(templates.render(eq("test-headers-template"), any(Map.class))).thenReturn("rendered headers");
161-
when(templates.render(eq("test-logic-template"), any(Map.class))).thenReturn("rendered logic");
158+
when(templates.renderObject(eq("test-plan-template"), any(Map.class))).thenReturn("rendered plan");
159+
when(templates.renderObject(eq("test-structure-template"), any(Map.class))).thenReturn("rendered structure");
160+
when(templates.renderObject(eq("test-headers-template"), any(Map.class))).thenReturn("rendered headers");
161+
when(templates.renderObject(eq("test-logic-template"), any(Map.class))).thenReturn("rendered logic");
162162

163163
when(chatModel.call(any(Prompt.class))).thenReturn(createChatResponse(planResponse)).thenReturn(createChatResponse(structureResponse))
164164
.thenReturn(createChatResponse(headersResponse)).thenReturn(createChatResponse(finalResponse));

src/test/java/de/tum/cit/aet/artemis/hyperion/service/codegeneration/HyperionSolutionRepsitoryTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,14 @@ void generateSolutionPlan_withValidInput_returnsCodeGenerationResponse() throws
7272
String renderedPrompt = "Rendered prompt for solution plan";
7373
String jsonResponse = "{\"solutionPlan\":\"" + expectedPlan + "\",\"files\":[]}";
7474

75-
when(templates.render(eq("/prompts/hyperion/solution/1_plan.st"), any(Map.class))).thenReturn(renderedPrompt);
75+
when(templates.renderObject(eq("/prompts/hyperion/solution/1_plan.st"), any(Map.class))).thenReturn(renderedPrompt);
7676
when(chatModel.call(any(Prompt.class))).thenReturn(createChatResponse(jsonResponse));
7777

7878
CodeGenerationResponseDTO result = solutionRepository.generateSolutionPlan(user, exercise, "build logs", "repo structure");
7979

8080
assertThat(result).isNotNull();
8181
assertThat(result.getSolutionPlan()).isEqualTo(expectedPlan);
82-
verify(templates).render(eq("/prompts/hyperion/solution/1_plan.st"), any(Map.class));
82+
verify(templates).renderObject(eq("/prompts/hyperion/solution/1_plan.st"), any(Map.class));
8383
verify(chatModel).call(any(Prompt.class));
8484
}
8585

@@ -88,7 +88,7 @@ void defineFileStructure_withValidInput_returnsFileStructure() throws Exception
8888
List<GeneratedFileDTO> expectedFiles = List.of(new GeneratedFileDTO("Sort.java", "class Sort {}"));
8989
String jsonResponse = "{\"solutionPlan\":\"plan\",\"files\":[{\"path\":\"Sort.java\",\"content\":\"class Sort {}\"}]}";
9090

91-
when(templates.render(eq("/prompts/hyperion/solution/2_file_structure.st"), any(Map.class))).thenReturn("rendered");
91+
when(templates.renderObject(eq("/prompts/hyperion/solution/2_file_structure.st"), any(Map.class))).thenReturn("rendered");
9292
when(chatModel.call(any(Prompt.class))).thenReturn(createChatResponse(jsonResponse));
9393

9494
CodeGenerationResponseDTO result = solutionRepository.defineFileStructure(user, exercise, "solution plan", "repo structure");
@@ -104,8 +104,8 @@ void generateClassAndMethodHeaders_callsDefineFileStructureAndUsesResult() throw
104104
String fileStructureJson = "{\"solutionPlan\":\"plan\",\"files\":[{\"path\":\"Sort.java\",\"content\":\"stub\"}]}";
105105
String headersJson = "{\"solutionPlan\":\"plan\",\"files\":[{\"path\":\"Sort.java\",\"content\":\"class Sort { void sort(); }\"}]}";
106106

107-
when(templates.render(eq("/prompts/hyperion/solution/2_file_structure.st"), any(Map.class))).thenReturn("rendered");
108-
when(templates.render(eq("/prompts/hyperion/solution/3_headers.st"), any(Map.class))).thenReturn("rendered");
107+
when(templates.renderObject(eq("/prompts/hyperion/solution/2_file_structure.st"), any(Map.class))).thenReturn("rendered");
108+
when(templates.renderObject(eq("/prompts/hyperion/solution/3_headers.st"), any(Map.class))).thenReturn("rendered");
109109
when(chatModel.call(any(Prompt.class))).thenReturn(createChatResponse(fileStructureJson)).thenReturn(createChatResponse(headersJson));
110110

111111
CodeGenerationResponseDTO result = solutionRepository.generateClassAndMethodHeaders(user, exercise, "solution plan", "repo structure");
@@ -121,7 +121,7 @@ void generateCoreLogic_callsHeadersAndUsesResult() throws Exception {
121121
String headersJson = "{\"solutionPlan\":\"plan\",\"files\":[{\"path\":\"Sort.java\",\"content\":\"class Sort { void sort(); }\"}]}";
122122
String coreLogicJson = "{\"solutionPlan\":\"plan\",\"files\":[{\"path\":\"Sort.java\",\"content\":\"class Sort { void sort() { /* implementation */ } }\"}]}";
123123

124-
when(templates.render(any(String.class), any(Map.class))).thenReturn("rendered");
124+
when(templates.renderObject(any(String.class), any(Map.class))).thenReturn("rendered");
125125
when(chatModel.call(any(Prompt.class))).thenReturn(createChatResponse(fileStructureJson)).thenReturn(createChatResponse(headersJson))
126126
.thenReturn(createChatResponse(coreLogicJson));
127127

@@ -141,7 +141,7 @@ void getRepositoryType_returnsSolution() {
141141

142142
@Test
143143
void generateSolutionPlan_withNonTransientAiException_throwsNetworkingException() {
144-
when(templates.render(any(String.class), any(Map.class))).thenReturn("rendered");
144+
when(templates.renderObject(any(String.class), any(Map.class))).thenReturn("rendered");
145145
when(chatModel.call(any(Prompt.class))).thenThrow(new NonTransientAiException("AI service error"));
146146

147147
assertThatThrownBy(() -> solutionRepository.generateSolutionPlan(user, exercise, "logs", "structure")).isInstanceOf(NetworkingException.class)

src/test/java/de/tum/cit/aet/artemis/hyperion/service/codegeneration/HyperionTemplateRepositoryTest.java

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -93,71 +93,77 @@ void generateSolutionPlan_withValidInput_returnsCodeGenerationResponse() throws
9393
String renderedPrompt = "Rendered prompt for template plan";
9494
String jsonResponse = "{\"solutionPlan\":\"" + expectedPlan + "\",\"files\":[]}";
9595

96-
when(templates.render(eq("/prompts/hyperion/template/1_plan.st"), any(Map.class))).thenReturn(renderedPrompt);
96+
when(contextRenderer.getExistingSolutionCode(any(ProgrammingExercise.class), any(GitService.class))).thenReturn("public class Solution {}");
97+
when(templates.renderObject(eq("/prompts/hyperion/template/1_plan.st"), any(Map.class))).thenReturn(renderedPrompt);
9798
when(chatModel.call(any(Prompt.class))).thenReturn(createChatResponse(jsonResponse));
9899

99100
CodeGenerationResponseDTO result = templateRepository.generateSolutionPlan(user, exercise, "build logs", "repo structure");
100101

101102
assertThat(result).isNotNull();
102103
assertThat(result.getSolutionPlan()).isEqualTo(expectedPlan);
103-
verify(templates).render(eq("/prompts/hyperion/template/1_plan.st"), any(Map.class));
104+
verify(templates).renderObject(eq("/prompts/hyperion/template/1_plan.st"), any(Map.class));
104105
verify(chatModel).call(any(Prompt.class));
105106
}
106107

107108
@Test
108109
void generateSolutionPlan_includesSolutionCodeInTemplateVariables() throws Exception {
109110
String jsonResponse = "{\"solutionPlan\":\"test plan\",\"files\":[]}";
110111

111-
when(templates.render(any(String.class), any(Map.class))).thenReturn("rendered");
112+
when(contextRenderer.getExistingSolutionCode(any(ProgrammingExercise.class), any(GitService.class))).thenReturn("public class Solution {}");
113+
when(templates.renderObject(any(String.class), any(Map.class))).thenReturn("rendered");
112114
when(chatModel.call(any(Prompt.class))).thenReturn(createChatResponse(jsonResponse));
113115

114116
templateRepository.generateSolutionPlan(user, exercise, "logs", "structure");
115117

116-
verify(templates).render(eq("/prompts/hyperion/template/1_plan.st"), any(Map.class));
118+
verify(templates).renderObject(eq("/prompts/hyperion/template/1_plan.st"), any(Map.class));
117119
verify(chatModel).call(any(Prompt.class));
118120
}
119121

120122
@Test
121123
void generateSolutionPlan_withFailedRepositoryCheckout_usesWarningMessage() throws Exception {
122124
String jsonResponse = "{\"solutionPlan\":\"test plan\",\"files\":[]}";
123-
when(templates.render(any(String.class), any(Map.class))).thenReturn("rendered");
125+
when(contextRenderer.getExistingSolutionCode(any(ProgrammingExercise.class), any(GitService.class))).thenReturn("public class Solution {}");
126+
when(templates.renderObject(any(String.class), any(Map.class))).thenReturn("rendered");
124127
when(chatModel.call(any(Prompt.class))).thenReturn(createChatResponse(jsonResponse));
125128

126129
templateRepository.generateSolutionPlan(user, exercise, "logs", "structure");
127130

128-
verify(templates).render(eq("/prompts/hyperion/template/1_plan.st"), any(Map.class));
131+
verify(templates).renderObject(eq("/prompts/hyperion/template/1_plan.st"), any(Map.class));
129132
verify(chatModel).call(any(Prompt.class));
130133
}
131134

132135
@Test
133136
void generateSolutionPlan_withMultipleJavaFiles_concatenatesContent() throws Exception {
134137
String jsonResponse = "{\"solutionPlan\":\"test plan\",\"files\":[]}";
135138

136-
when(templates.render(any(String.class), any(Map.class))).thenReturn("rendered");
139+
when(contextRenderer.getExistingSolutionCode(any(ProgrammingExercise.class), any(GitService.class))).thenReturn("public class Solution {}");
140+
when(templates.renderObject(any(String.class), any(Map.class))).thenReturn("rendered");
137141
when(chatModel.call(any(Prompt.class))).thenReturn(createChatResponse(jsonResponse));
138142

139143
templateRepository.generateSolutionPlan(user, exercise, "logs", "structure");
140144

141-
verify(templates).render(eq("/prompts/hyperion/template/1_plan.st"), any(Map.class));
145+
verify(templates).renderObject(eq("/prompts/hyperion/template/1_plan.st"), any(Map.class));
142146
verify(chatModel).call(any(Prompt.class));
143147
}
144148

145149
@Test
146150
void generateSolutionPlan_withIOExceptionDuringWalk_returnsErrorMessage() throws Exception {
147151
String jsonResponse = "{\"solutionPlan\":\"test plan\",\"files\":[]}";
148152

149-
when(templates.render(any(String.class), any(Map.class))).thenReturn("rendered");
153+
when(contextRenderer.getExistingSolutionCode(any(ProgrammingExercise.class), any(GitService.class))).thenReturn("public class Solution {}");
154+
when(templates.renderObject(any(String.class), any(Map.class))).thenReturn("rendered");
150155
when(chatModel.call(any(Prompt.class))).thenReturn(createChatResponse(jsonResponse));
151156

152157
templateRepository.generateSolutionPlan(user, exercise, "logs", "structure");
153158

154-
verify(templates).render(eq("/prompts/hyperion/template/1_plan.st"), any(Map.class));
159+
verify(templates).renderObject(eq("/prompts/hyperion/template/1_plan.st"), any(Map.class));
155160
verify(chatModel).call(any(Prompt.class));
156161
}
157162

158163
@Test
159164
void generateSolutionPlan_withRepositoryAccessException_throwsNetworkingException() throws Exception {
160-
when(templates.render(any(String.class), any(Map.class))).thenReturn("rendered");
165+
when(contextRenderer.getExistingSolutionCode(any(ProgrammingExercise.class), any(GitService.class))).thenReturn("public class Solution {}");
166+
when(templates.renderObject(any(String.class), any(Map.class))).thenReturn("rendered");
161167
when(chatModel.call(any(Prompt.class))).thenThrow(new RuntimeException("Repository access failed"));
162168

163169
assertThatThrownBy(() -> templateRepository.generateSolutionPlan(user, exercise, "logs", "structure")).isInstanceOf(RuntimeException.class)
@@ -168,12 +174,13 @@ void generateSolutionPlan_withRepositoryAccessException_throwsNetworkingExceptio
168174
void generateSolutionPlan_withNoJavaFiles_returnsNoFilesMessage() throws Exception {
169175
String jsonResponse = "{\"solutionPlan\":\"test plan\",\"files\":[]}";
170176

171-
when(templates.render(any(String.class), any(Map.class))).thenReturn("rendered");
177+
when(contextRenderer.getExistingSolutionCode(any(ProgrammingExercise.class), any(GitService.class))).thenReturn("public class Solution {}");
178+
when(templates.renderObject(any(String.class), any(Map.class))).thenReturn("rendered");
172179
when(chatModel.call(any(Prompt.class))).thenReturn(createChatResponse(jsonResponse));
173180

174181
templateRepository.generateSolutionPlan(user, exercise, "logs", "structure");
175182

176-
verify(templates).render(eq("/prompts/hyperion/template/1_plan.st"), any(Map.class));
183+
verify(templates).renderObject(eq("/prompts/hyperion/template/1_plan.st"), any(Map.class));
177184
verify(chatModel).call(any(Prompt.class));
178185
}
179186

@@ -182,7 +189,7 @@ void defineFileStructure_withValidInput_returnsFileStructure() throws Exception
182189
List<GeneratedFileDTO> expectedFiles = List.of(new GeneratedFileDTO("Template.java", "class Template {}"));
183190
String jsonResponse = "{\"solutionPlan\":\"plan\",\"files\":[{\"path\":\"Template.java\",\"content\":\"class Template {}\"}]}";
184191

185-
when(templates.render(eq("/prompts/hyperion/template/2_file_structure.st"), any(Map.class))).thenReturn("rendered");
192+
when(templates.renderObject(eq("/prompts/hyperion/template/2_file_structure.st"), any(Map.class))).thenReturn("rendered");
186193
when(chatModel.call(any(Prompt.class))).thenReturn(createChatResponse(jsonResponse));
187194

188195
CodeGenerationResponseDTO result = templateRepository.defineFileStructure(user, exercise, "solution plan", "repo structure");
@@ -198,8 +205,8 @@ void generateClassAndMethodHeaders_callsDefineFileStructureAndUsesResult() throw
198205
String fileStructureJson = "{\"solutionPlan\":\"plan\",\"files\":[{\"path\":\"Template.java\",\"content\":\"stub\"}]}";
199206
String headersJson = "{\"solutionPlan\":\"plan\",\"files\":[{\"path\":\"Template.java\",\"content\":\"class Template { void method(); }\"}]}";
200207

201-
when(templates.render(eq("/prompts/hyperion/template/2_file_structure.st"), any(Map.class))).thenReturn("rendered");
202-
when(templates.render(eq("/prompts/hyperion/template/3_headers.st"), any(Map.class))).thenReturn("rendered");
208+
when(templates.renderObject(eq("/prompts/hyperion/template/2_file_structure.st"), any(Map.class))).thenReturn("rendered");
209+
when(templates.renderObject(eq("/prompts/hyperion/template/3_headers.st"), any(Map.class))).thenReturn("rendered");
203210
when(chatModel.call(any(Prompt.class))).thenReturn(createChatResponse(fileStructureJson)).thenReturn(createChatResponse(headersJson));
204211

205212
CodeGenerationResponseDTO result = templateRepository.generateClassAndMethodHeaders(user, exercise, "solution plan", "repo structure");
@@ -215,7 +222,7 @@ void generateCoreLogic_callsHeadersAndUsesResult() throws Exception {
215222
String headersJson = "{\"solutionPlan\":\"plan\",\"files\":[{\"path\":\"Template.java\",\"content\":\"class Template { void method(); }\"}]}";
216223
String coreLogicJson = "{\"solutionPlan\":\"plan\",\"files\":[{\"path\":\"Template.java\",\"content\":\"class Template { void method() { /* TODO */ } }\"}]}";
217224

218-
when(templates.render(any(String.class), any(Map.class))).thenReturn("rendered");
225+
when(templates.renderObject(any(String.class), any(Map.class))).thenReturn("rendered");
219226
when(chatModel.call(any(Prompt.class))).thenReturn(createChatResponse(fileStructureJson)).thenReturn(createChatResponse(headersJson))
220227
.thenReturn(createChatResponse(coreLogicJson));
221228

@@ -224,7 +231,7 @@ void generateCoreLogic_callsHeadersAndUsesResult() throws Exception {
224231
assertThat(result).isNotNull();
225232
assertThat(result.getFiles().get(0).content()).contains("TODO");
226233
verify(chatModel, org.mockito.Mockito.times(3)).call(any(Prompt.class));
227-
verify(templates).render(eq("/prompts/hyperion/template/4_logic.st"), any(Map.class));
234+
verify(templates).renderObject(eq("/prompts/hyperion/template/4_logic.st"), any(Map.class));
228235
}
229236

230237
@Test
@@ -236,7 +243,8 @@ void getRepositoryType_returnsTemplate() {
236243

237244
@Test
238245
void generateSolutionPlan_withNonTransientAiException_throwsNetworkingException() throws Exception {
239-
when(templates.render(any(String.class), any(Map.class))).thenReturn("rendered");
246+
when(contextRenderer.getExistingSolutionCode(any(ProgrammingExercise.class), any(GitService.class))).thenReturn("public class Solution {}");
247+
when(templates.renderObject(any(String.class), any(Map.class))).thenReturn("rendered");
240248
when(chatModel.call(any(Prompt.class))).thenThrow(new NonTransientAiException("AI service error"));
241249

242250
assertThatThrownBy(() -> templateRepository.generateSolutionPlan(user, exercise, "logs", "structure")).isInstanceOf(NetworkingException.class)
@@ -247,25 +255,27 @@ void generateSolutionPlan_withNonTransientAiException_throwsNetworkingException(
247255
void generateSolutionPlan_withNullBuildLogs_usesEmptyString() throws Exception {
248256
String jsonResponse = "{\"solutionPlan\":\"test plan\",\"files\":[]}";
249257

250-
when(templates.render(any(String.class), any(Map.class))).thenReturn("rendered");
258+
when(contextRenderer.getExistingSolutionCode(any(ProgrammingExercise.class), any(GitService.class))).thenReturn("public class Solution {}");
259+
when(templates.renderObject(any(String.class), any(Map.class))).thenReturn("rendered");
251260
when(chatModel.call(any(Prompt.class))).thenReturn(createChatResponse(jsonResponse));
252261

253262
templateRepository.generateSolutionPlan(user, exercise, null, "repo structure");
254263

255-
verify(templates).render(eq("/prompts/hyperion/template/1_plan.st"), any(Map.class));
264+
verify(templates).renderObject(eq("/prompts/hyperion/template/1_plan.st"), any(Map.class));
256265
verify(chatModel).call(any(Prompt.class));
257266
}
258267

259268
@Test
260269
void generateSolutionPlan_withNullRepositoryStructure_usesEmptyString() throws Exception {
261270
String jsonResponse = "{\"solutionPlan\":\"test plan\",\"files\":[]}";
262271

263-
when(templates.render(any(String.class), any(Map.class))).thenReturn("rendered");
272+
when(contextRenderer.getExistingSolutionCode(any(ProgrammingExercise.class), any(GitService.class))).thenReturn("public class Solution {}");
273+
when(templates.renderObject(any(String.class), any(Map.class))).thenReturn("rendered");
264274
when(chatModel.call(any(Prompt.class))).thenReturn(createChatResponse(jsonResponse));
265275

266276
templateRepository.generateSolutionPlan(user, exercise, "logs", null);
267277

268-
verify(templates).render(eq("/prompts/hyperion/template/1_plan.st"), any(Map.class));
278+
verify(templates).renderObject(eq("/prompts/hyperion/template/1_plan.st"), any(Map.class));
269279
verify(chatModel).call(any(Prompt.class));
270280
}
271281

0 commit comments

Comments
 (0)