Skip to content

Commit 5d6ae00

Browse files
refactor: minimize flaky test
1 parent 8663a46 commit 5d6ae00

File tree

2 files changed

+18
-21
lines changed

2 files changed

+18
-21
lines changed

src/integration-test/java/com/commercetools/sync/integration/commons/utils/CategoryITUtils.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,10 @@ public static void deleteCategoriesBySlug(
331331
@Nonnull final ProjectApiRoot ctpClient,
332332
@Nonnull final Locale locale,
333333
@Nonnull final List<String> slugs) {
334+
if (slugs == null || slugs.isEmpty()) {
335+
return;
336+
}
337+
334338
for (String slug : slugs) {
335339
try {
336340
ctpClient
@@ -354,18 +358,21 @@ public static void deleteCategoriesBySlug(
354358
.execute()
355359
.toCompletableFuture()
356360
.join();
361+
} catch (NotFoundException e) {
362+
// Already deleted
363+
logger.debug(
364+
"Category with slug '{}' already deleted", slug);
365+
357366
} catch (RuntimeException e) {
358-
// Expected: already deleted, cascade deleted, or version conflict
367+
// May have been deleted by parent cascade or version conflict
359368
logger.debug(
360-
"Could not delete category with slug '{}', ID: {}. Reason: {}",
361-
slug,
362-
cat.getId(),
363-
e.getMessage());
369+
"Could not delete category with slug '{}', ID: {}", slug, cat.getId());
364370
}
365371
});
366372
} catch (RuntimeException e) {
367-
// Expected: slug doesn't exist or query failed
368-
logger.debug("Could not query categories with slug '{}'. Reason: {}", slug, e.getMessage());
373+
// Slug doesn't exist or query failed - this is expected
374+
logger.debug(
375+
"Category with slug '{}' does not exist", slug);
369376
}
370377
}
371378
}

src/integration-test/java/com/commercetools/sync/integration/ctpprojectsource/categories/CategorySyncIT.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,6 @@ void setupTest() {
6868
CategoryITUtils.deleteAllCategories(TestClientUtils.CTP_TARGET_CLIENT);
6969
CategoryITUtils.deleteAllCategories(TestClientUtils.CTP_SOURCE_CLIENT);
7070

71-
// Force delete any orphaned categories with specific slugs that deleteAllCategories might miss
72-
CategoryITUtils.deleteCategoriesBySlug(
73-
TestClientUtils.CTP_TARGET_CLIENT,
74-
Locale.ENGLISH,
75-
List.of("furniture1-project-source", "furniture2-project-source"));
76-
CategoryITUtils.deleteCategoriesBySlug(
77-
TestClientUtils.CTP_SOURCE_CLIENT,
78-
Locale.ENGLISH,
79-
List.of("furniture1-project-source", "furniture2-project-source"));
80-
8171
CategoryITUtils.ensureCategories(
8272
TestClientUtils.CTP_TARGET_CLIENT, CategoryITUtils.getCategoryDrafts(null, 2, true));
8373

@@ -108,7 +98,7 @@ private CategorySyncOptions buildCategorySyncOptions(final int batchSize) {
10898
*/
10999
@AfterEach
110100
void cleanupAfterTest() {
111-
// Clean up, including any categories without keys
101+
// Clean up specific problematic slugs first (categories without keys from failed tests)
112102
CategoryITUtils.deleteCategoriesBySlug(
113103
TestClientUtils.CTP_TARGET_CLIENT,
114104
Locale.ENGLISH,
@@ -117,7 +107,8 @@ void cleanupAfterTest() {
117107
TestClientUtils.CTP_SOURCE_CLIENT,
118108
Locale.ENGLISH,
119109
List.of("furniture1-project-source", "furniture2-project-source"));
120-
110+
111+
// Then clean up everything else
121112
CategoryITUtils.deleteAllCategories(TestClientUtils.CTP_TARGET_CLIENT);
122113
CategoryITUtils.deleteAllCategories(TestClientUtils.CTP_SOURCE_CLIENT);
123114
}
@@ -516,14 +507,13 @@ void syncDrafts_fromCategoriesWithoutKeys_ShouldNotUpdateCategories() {
516507
CompletableFuture.allOf(futureCreations.toArray(new CompletableFuture[futureCreations.size()]))
517508
.join();
518509

519-
// Create two categories in the target without Keys (sequentially to avoid race conditions).
510+
// Create two categories in the target without Keys.
520511
final CategoryDraft newCategoryDraft1 =
521512
CategoryDraftBuilder.of(oldCategoryDraft1).key(null).build();
522513
final CategoryDraft newCategoryDraft2 =
523514
CategoryDraftBuilder.of(oldCategoryDraft2).key(null).build();
524515

525516
TestClientUtils.CTP_TARGET_CLIENT.categories().create(newCategoryDraft1).executeBlocking();
526-
527517
TestClientUtils.CTP_TARGET_CLIENT.categories().create(newCategoryDraft2).executeBlocking();
528518

529519
// ---------

0 commit comments

Comments
 (0)