Skip to content

Commit 480ec1c

Browse files
committed
Re-optimise unit tests according to Code review recommendations .
1 parent 4e0d60a commit 480ec1c

File tree

1 file changed

+31
-40
lines changed

1 file changed

+31
-40
lines changed

tests/server-common/src/test/java/io/a2a/server/apps/common/AbstractA2AServerTest.java

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ private void testGetTask() throws Exception {
136136
}
137137

138138
private void testGetTask(String mediaType) throws Exception {
139-
getTaskStore().save(MINIMAL_TASK);
139+
saveTaskInTaskStore(MINIMAL_TASK);
140140
try {
141141
GetTaskResponse response = client.getTask("1", new TaskQueryParams(MINIMAL_TASK.getId()));
142142
assertEquals("1", response.getId());
@@ -147,13 +147,13 @@ private void testGetTask(String mediaType) throws Exception {
147147
} catch (A2AServerException e) {
148148
fail("Unexpected exception during getTask: " + e.getMessage(), e);
149149
} finally {
150-
getTaskStore().delete(MINIMAL_TASK.getId());
150+
deleteTaskInTaskStore(MINIMAL_TASK.getId());
151151
}
152152
}
153153

154154
@Test
155155
public void testGetTaskNotFound() throws Exception {
156-
assertTrue(getTaskStore().get("non-existent-task") == null);
156+
assertTrue(getTaskFromTaskStore("non-existent-task") == null);
157157
try {
158158
GetTaskResponse response = client.getTask("1", new TaskQueryParams("non-existent-task"));
159159
assertEquals("1", response.getId());
@@ -167,7 +167,7 @@ public void testGetTaskNotFound() throws Exception {
167167

168168
@Test
169169
public void testCancelTaskSuccess() throws Exception {
170-
getTaskStore().save(CANCEL_TASK);
170+
saveTaskInTaskStore(CANCEL_TASK);
171171
try {
172172
CancelTaskResponse response = client.cancelTask("1", new TaskIdParams(CANCEL_TASK.getId()));
173173
assertEquals("1", response.getId());
@@ -178,13 +178,13 @@ public void testCancelTaskSuccess() throws Exception {
178178
} catch (A2AServerException e) {
179179
fail("Unexpected exception during cancel task success test: " + e.getMessage(), e);
180180
} finally {
181-
getTaskStore().delete(CANCEL_TASK.getId());
181+
deleteTaskInTaskStore(CANCEL_TASK.getId());
182182
}
183183
}
184184

185185
@Test
186186
public void testCancelTaskNotSupported() throws Exception {
187-
getTaskStore().save(CANCEL_TASK_NOT_SUPPORTED);
187+
saveTaskInTaskStore(CANCEL_TASK_NOT_SUPPORTED);
188188
try {
189189
CancelTaskResponse response = client.cancelTask("1", new TaskIdParams(CANCEL_TASK_NOT_SUPPORTED.getId()));
190190
assertNull(response.getResult());
@@ -194,7 +194,7 @@ public void testCancelTaskNotSupported() throws Exception {
194194
} catch (A2AServerException e) {
195195
fail("Unexpected exception during cancel task not supported test: " + e.getMessage(), e);
196196
} finally {
197-
getTaskStore().delete(CANCEL_TASK_NOT_SUPPORTED.getId());
197+
deleteTaskInTaskStore(CANCEL_TASK_NOT_SUPPORTED.getId());
198198
}
199199
}
200200

@@ -213,7 +213,7 @@ public void testCancelTaskNotFound() {
213213

214214
@Test
215215
public void testSendMessageNewMessageSuccess() throws Exception {
216-
assertTrue(getTaskStore().get(MINIMAL_TASK.getId()) == null);
216+
assertTrue(getTaskFromTaskStore(MINIMAL_TASK.getId()) == null);
217217
Message message = new Message.Builder(MESSAGE)
218218
.taskId(MINIMAL_TASK.getId())
219219
.contextId(MINIMAL_TASK.getContextId())
@@ -237,7 +237,7 @@ public void testSendMessageNewMessageSuccess() throws Exception {
237237

238238
@Test
239239
public void testSendMessageExistingTaskSuccess() throws Exception {
240-
getTaskStore().save(MINIMAL_TASK);
240+
saveTaskInTaskStore(MINIMAL_TASK);
241241
try {
242242
Message message = new Message.Builder(MESSAGE)
243243
.taskId(MINIMAL_TASK.getId())
@@ -257,13 +257,13 @@ public void testSendMessageExistingTaskSuccess() throws Exception {
257257
} catch (A2AServerException e) {
258258
fail("Unexpected exception during send message to existing task test: " + e.getMessage(), e);
259259
} finally {
260-
getTaskStore().delete(MINIMAL_TASK.getId());
260+
deleteTaskInTaskStore(MINIMAL_TASK.getId());
261261
}
262262
}
263263

264264
@Test
265265
public void testSetPushNotificationSuccess() throws Exception {
266-
getTaskStore().save(MINIMAL_TASK);
266+
saveTaskInTaskStore(MINIMAL_TASK);
267267
try {
268268
PushNotificationConfig pushNotificationConfig = new PushNotificationConfig.Builder()
269269
.url("http://example.com")
@@ -278,13 +278,14 @@ public void testSetPushNotificationSuccess() throws Exception {
278278
} catch (A2AServerException e) {
279279
fail("Unexpected exception during set push notification test: " + e.getMessage(), e);
280280
} finally {
281-
getTaskStore().delete(MINIMAL_TASK.getId());
281+
deletePushNotificationConfigInStore(MINIMAL_TASK.getId(), MINIMAL_TASK.getId());
282+
deleteTaskInTaskStore(MINIMAL_TASK.getId());
282283
}
283284
}
284285

285286
@Test
286287
public void testGetPushNotificationSuccess() throws Exception {
287-
getTaskStore().save(MINIMAL_TASK);
288+
saveTaskInTaskStore(MINIMAL_TASK);
288289
try {
289290
PushNotificationConfig pushNotificationConfig = new PushNotificationConfig.Builder()
290291
.url("http://example.com")
@@ -305,7 +306,7 @@ public void testGetPushNotificationSuccess() throws Exception {
305306
} catch (A2AServerException e) {
306307
fail("Unexpected exception during get push notification test: " + e.getMessage(), e);
307308
} finally {
308-
getTaskStore().delete(MINIMAL_TASK.getId());
309+
deleteTaskInTaskStore(MINIMAL_TASK.getId());
309310
}
310311
}
311312

@@ -490,7 +491,7 @@ public void testNonStreamingMethodWithAcceptHeader() throws Exception {
490491

491492
@Test
492493
public void testSendMessageStreamExistingTaskSuccess() throws Exception {
493-
getTaskStore().save(MINIMAL_TASK);
494+
saveTaskInTaskStore(MINIMAL_TASK);
494495
try {
495496
Message message = new Message.Builder(MESSAGE)
496497
.taskId(MINIMAL_TASK.getId())
@@ -546,15 +547,15 @@ public void testSendMessageStreamExistingTaskSuccess() throws Exception {
546547
} catch (Exception e) {
547548
fail("Unexpected exception during error handling test: " + e.getMessage(), e);
548549
} finally {
549-
getTaskStore().delete(MINIMAL_TASK.getId());
550+
deleteTaskInTaskStore(MINIMAL_TASK.getId());
550551
}
551552
}
552553

553554
@Test
554555
@Timeout(value = 3, unit = TimeUnit.MINUTES)
555556
public void testResubscribeExistingTaskSuccess() throws Exception {
556557
ExecutorService executorService = Executors.newSingleThreadExecutor();
557-
getTaskStore().save(MINIMAL_TASK);
558+
saveTaskInTaskStore(MINIMAL_TASK);
558559

559560
try {
560561
// attempting to send a streaming message instead of explicitly calling queueManager#createOrTap
@@ -657,7 +658,7 @@ public void testResubscribeExistingTaskSuccess() throws Exception {
657658
assertEquals(TaskState.COMPLETED, taskStatusUpdateEvent.getStatus().state());
658659
assertNotNull(taskStatusUpdateEvent.getStatus().timestamp());
659660
} finally {
660-
getTaskStore().delete(MINIMAL_TASK.getId());
661+
deleteTaskInTaskStore(MINIMAL_TASK.getId());
661662
executorService.shutdown();
662663
if (!executorService.awaitTermination(10, TimeUnit.SECONDS)) {
663664
executorService.shutdownNow();
@@ -774,7 +775,7 @@ private void testSendStreamingMessage(String mediaType) throws Exception {
774775

775776
@Test
776777
public void testListPushNotificationConfigWithConfigId() throws Exception {
777-
getTaskStore().save(MINIMAL_TASK);
778+
saveTaskInTaskStore(MINIMAL_TASK);
778779
PushNotificationConfig notificationConfig1 =
779780
new PushNotificationConfig.Builder()
780781
.url("http://example.com")
@@ -799,13 +800,13 @@ public void testListPushNotificationConfigWithConfigId() throws Exception {
799800
} finally {
800801
deletePushNotificationConfigInStore(MINIMAL_TASK.getId(), "config1");
801802
deletePushNotificationConfigInStore(MINIMAL_TASK.getId(), "config2");
802-
getTaskStore().delete(MINIMAL_TASK.getId());
803+
deleteTaskInTaskStore(MINIMAL_TASK.getId());
803804
}
804805
}
805806

806807
@Test
807808
public void testListPushNotificationConfigWithoutConfigId() throws Exception {
808-
getTaskStore().save(MINIMAL_TASK);
809+
saveTaskInTaskStore(MINIMAL_TASK);
809810
PushNotificationConfig notificationConfig1 =
810811
new PushNotificationConfig.Builder()
811812
.url("http://1.example.com")
@@ -833,7 +834,7 @@ public void testListPushNotificationConfigWithoutConfigId() throws Exception {
833834
fail();
834835
} finally {
835836
deletePushNotificationConfigInStore(MINIMAL_TASK.getId(), MINIMAL_TASK.getId());
836-
getTaskStore().delete(MINIMAL_TASK.getId());
837+
deleteTaskInTaskStore(MINIMAL_TASK.getId());
837838
}
838839
}
839840

@@ -849,21 +850,21 @@ public void testListPushNotificationConfigTaskNotFound() {
849850

850851
@Test
851852
public void testListPushNotificationConfigEmptyList() throws Exception {
852-
getTaskStore().save(MINIMAL_TASK);
853+
saveTaskInTaskStore(MINIMAL_TASK);
853854
try {
854855
ListTaskPushNotificationConfigResponse listResponse = client.listTaskPushNotificationConfig("111", MINIMAL_TASK.getId());
855856
assertEquals("111", listResponse.getId());
856857
assertEquals(0, listResponse.getResult().size());
857858
} catch (Exception e) {
858859
fail();
859860
} finally {
860-
getTaskStore().delete(MINIMAL_TASK.getId());
861+
deleteTaskInTaskStore(MINIMAL_TASK.getId());
861862
}
862863
}
863864

864865
@Test
865866
public void testDeletePushNotificationConfigWithValidConfigId() throws Exception {
866-
getTaskStore().save(MINIMAL_TASK);
867+
saveTaskInTaskStore(MINIMAL_TASK);
867868
saveTaskInTaskStore(new Task.Builder()
868869
.id("task-456")
869870
.contextId("session-xyz")
@@ -904,14 +905,14 @@ public void testDeletePushNotificationConfigWithValidConfigId() throws Exception
904905
deletePushNotificationConfigInStore(MINIMAL_TASK.getId(), "config1");
905906
deletePushNotificationConfigInStore(MINIMAL_TASK.getId(), "config2");
906907
deletePushNotificationConfigInStore("task-456", "config1");
907-
getTaskStore().delete(MINIMAL_TASK.getId());
908+
deleteTaskInTaskStore(MINIMAL_TASK.getId());
908909
deleteTaskInTaskStore("task-456");
909910
}
910911
}
911912

912913
@Test
913914
public void testDeletePushNotificationConfigWithNonExistingConfigId() throws Exception {
914-
getTaskStore().save(MINIMAL_TASK);
915+
saveTaskInTaskStore(MINIMAL_TASK);
915916
PushNotificationConfig notificationConfig1 =
916917
new PushNotificationConfig.Builder()
917918
.url("http://example.com")
@@ -939,7 +940,7 @@ public void testDeletePushNotificationConfigWithNonExistingConfigId() throws Exc
939940
} finally {
940941
deletePushNotificationConfigInStore(MINIMAL_TASK.getId(), "config1");
941942
deletePushNotificationConfigInStore(MINIMAL_TASK.getId(), "config2");
942-
getTaskStore().delete(MINIMAL_TASK.getId());
943+
deleteTaskInTaskStore(MINIMAL_TASK.getId());
943944
}
944945
}
945946

@@ -955,7 +956,7 @@ public void testDeletePushNotificationConfigTaskNotFound() {
955956

956957
@Test
957958
public void testDeletePushNotificationConfigSetWithoutConfigId() throws Exception {
958-
getTaskStore().save(MINIMAL_TASK);
959+
saveTaskInTaskStore(MINIMAL_TASK);
959960
PushNotificationConfig notificationConfig1 =
960961
new PushNotificationConfig.Builder()
961962
.url("http://1.example.com")
@@ -982,7 +983,7 @@ public void testDeletePushNotificationConfigSetWithoutConfigId() throws Exceptio
982983
fail();
983984
} finally {
984985
deletePushNotificationConfigInStore(MINIMAL_TASK.getId(), MINIMAL_TASK.getId());
985-
getTaskStore().delete(MINIMAL_TASK.getId());
986+
deleteTaskInTaskStore(MINIMAL_TASK.getId());
986987
}
987988
}
988989

@@ -1146,14 +1147,4 @@ protected void savePushNotificationConfigInStore(String taskId, PushNotification
11461147
throw new RuntimeException(response.statusCode() + ": Creating task push notification config failed! " + response.body());
11471148
}
11481149
}
1149-
1150-
protected abstract TaskStore getTaskStore();
1151-
1152-
protected abstract InMemoryQueueManager getQueueManager();
1153-
1154-
protected abstract void setStreamingSubscribedRunnable(Runnable runnable);
1155-
1156-
private static class BreakException extends RuntimeException {
1157-
1158-
}
11591150
}

0 commit comments

Comments
 (0)