Skip to content

Commit 078a1e5

Browse files
refacotr: using uuid on tests
1 parent 066f03f commit 078a1e5

File tree

3 files changed

+42
-142
lines changed

3 files changed

+42
-142
lines changed

src/benchmark/java/com/commercetools/sync/benchmark/ProductTypeSyncBenchmark.java

Lines changed: 20 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.ArrayList;
3030
import java.util.List;
3131
import java.util.Optional;
32+
import java.util.UUID;
3233
import java.util.concurrent.CompletableFuture;
3334
import java.util.concurrent.CompletionStage;
3435
import java.util.stream.Collectors;
@@ -95,26 +96,12 @@ private void clearSyncTestCollections() {
9596

9697
@Test
9798
void sync_NewProductTypes_ShouldCreateProductTypes() throws IOException {
98-
// Verify the project is clean before starting
99-
final Integer initialProductTypeCount =
100-
CTP_TARGET_CLIENT
101-
.productTypes()
102-
.get()
103-
.execute()
104-
.thenApply(ApiHttpResponse::getBody)
105-
.thenApply(ProductTypePagedQueryResponse::getTotal)
106-
.thenApply(Long::intValue)
107-
.toCompletableFuture()
108-
.join();
109-
assertThat(initialProductTypeCount)
110-
.withFailMessage(
111-
"Project should be clean before benchmark, but found %d product types",
112-
initialProductTypeCount)
113-
.isZero();
99+
// Generate unique prefix for this test run to avoid collisions
100+
final String testRunPrefix = "create_" + UUID.randomUUID().toString().substring(0, 8);
114101

115102
// preparation
116103
final List<ProductTypeDraft> productTypeDrafts =
117-
buildProductTypeDrafts(NUMBER_OF_RESOURCE_UNDER_TEST);
104+
buildProductTypeDrafts(NUMBER_OF_RESOURCE_UNDER_TEST, testRunPrefix);
118105
final ProductTypeSync productTypeSync = new ProductTypeSync(productTypeSyncOptions);
119106

120107
// benchmark
@@ -132,20 +119,7 @@ void sync_NewProductTypes_ShouldCreateProductTypes() throws IOException {
132119
PRODUCT_TYPE_BENCHMARKS_CREATE_ACTION_THRESHOLD))
133120
.isLessThan(PRODUCT_TYPE_BENCHMARKS_CREATE_ACTION_THRESHOLD);
134121

135-
// Assert actual state of CTP project (total number of existing product types)
136-
final Integer totalNumberOfProductTypes =
137-
CTP_TARGET_CLIENT
138-
.productTypes()
139-
.get()
140-
.execute()
141-
.thenApply(ApiHttpResponse::getBody)
142-
.thenApply(ProductTypePagedQueryResponse::getTotal)
143-
.thenApply(Long::intValue)
144-
.toCompletableFuture()
145-
.join();
146-
147-
assertThat(totalNumberOfProductTypes).isEqualTo(NUMBER_OF_RESOURCE_UNDER_TEST);
148-
122+
// Assert sync statistics - all should be created since we use unique keys
149123
assertThat(syncStatistics)
150124
.hasValues(NUMBER_OF_RESOURCE_UNDER_TEST, NUMBER_OF_RESOURCE_UNDER_TEST, 0, 0);
151125
assertThat(errorCallBackExceptions).isEmpty();
@@ -158,9 +132,12 @@ void sync_NewProductTypes_ShouldCreateProductTypes() throws IOException {
158132

159133
@Test
160134
void sync_ExistingProductTypes_ShouldUpdateProductTypes() throws IOException {
135+
// Generate unique prefix for this test run to avoid collisions
136+
final String testRunPrefix = "update_" + UUID.randomUUID().toString().substring(0, 8);
137+
161138
// preparation
162139
final List<ProductTypeDraft> productTypeDrafts =
163-
buildProductTypeDrafts(NUMBER_OF_RESOURCE_UNDER_TEST);
140+
buildProductTypeDrafts(NUMBER_OF_RESOURCE_UNDER_TEST, testRunPrefix);
164141
// Create drafts to target project with different attribute definition name
165142
CompletableFuture.allOf(
166143
productTypeDrafts.stream()
@@ -188,35 +165,7 @@ void sync_ExistingProductTypes_ShouldUpdateProductTypes() throws IOException {
188165
PRODUCT_TYPE_BENCHMARKS_UPDATE_ACTION_THRESHOLD))
189166
.isLessThan(PRODUCT_TYPE_BENCHMARKS_UPDATE_ACTION_THRESHOLD);
190167

191-
// Assert actual state of CTP project (number of updated product types)
192-
final Long totalNumberOfUpdatedProductTypes =
193-
CTP_TARGET_CLIENT
194-
.productTypes()
195-
.get()
196-
.withWhere("attributes(name=:name)")
197-
.withPredicateVar("name", "attr_name_1")
198-
.execute()
199-
.thenApply(ApiHttpResponse::getBody)
200-
.thenApply(ProductTypePagedQueryResponse::getTotal)
201-
.toCompletableFuture()
202-
.join();
203-
204-
assertThat(totalNumberOfUpdatedProductTypes).isEqualTo(NUMBER_OF_RESOURCE_UNDER_TEST);
205-
206-
// Assert actual state of CTP project (total number of existing product types)
207-
final Long totalNumberOfProductTypes =
208-
CTP_TARGET_CLIENT
209-
.productTypes()
210-
.get()
211-
.execute()
212-
.thenApply(ApiHttpResponse::getBody)
213-
.thenApply(ProductTypePagedQueryResponse::getTotal)
214-
.toCompletableFuture()
215-
.join();
216-
217-
assertThat(totalNumberOfProductTypes).isEqualTo(NUMBER_OF_RESOURCE_UNDER_TEST);
218-
219-
// Assert statistics
168+
// Assert statistics - all should be updated since we created them first with modified names
220169
assertThat(syncStatistics)
221170
.hasValues(NUMBER_OF_RESOURCE_UNDER_TEST, 0, NUMBER_OF_RESOURCE_UNDER_TEST, 0);
222171

@@ -230,9 +179,12 @@ void sync_ExistingProductTypes_ShouldUpdateProductTypes() throws IOException {
230179

231180
@Test
232181
void sync_WithSomeExistingProductTypes_ShouldSyncProductTypes() throws IOException {
182+
// Generate unique prefix for this test run to avoid collisions
183+
final String testRunPrefix = "mix_" + UUID.randomUUID().toString().substring(0, 8);
184+
233185
// preparation
234186
final List<ProductTypeDraft> productTypeDrafts =
235-
buildProductTypeDrafts(NUMBER_OF_RESOURCE_UNDER_TEST);
187+
buildProductTypeDrafts(NUMBER_OF_RESOURCE_UNDER_TEST, testRunPrefix);
236188
final int halfNumberOfDrafts = productTypeDrafts.size() / 2;
237189
final List<ProductTypeDraft> firstHalf = productTypeDrafts.subList(0, halfNumberOfDrafts);
238190

@@ -264,35 +216,7 @@ void sync_WithSomeExistingProductTypes_ShouldSyncProductTypes() throws IOExcepti
264216
PRODUCT_TYPE_BENCHMARKS_UPDATE_ACTION_THRESHOLD))
265217
.isLessThan(PRODUCT_TYPE_BENCHMARKS_UPDATE_ACTION_THRESHOLD);
266218

267-
// Assert actual state of CTP project (number of updated product types)
268-
final Long totalNumberOfProductTypesWithOldName =
269-
CTP_TARGET_CLIENT
270-
.productTypes()
271-
.get()
272-
.withWhere("attributes(name=:name)")
273-
.withPredicateVar("name", "attr_name_1_old")
274-
.execute()
275-
.thenApply(ApiHttpResponse::getBody)
276-
.thenApply(ProductTypePagedQueryResponse::getTotal)
277-
.toCompletableFuture()
278-
.join();
279-
280-
assertThat(totalNumberOfProductTypesWithOldName).isEqualTo(0);
281-
282-
// Assert actual state of CTP project (total number of existing product types)
283-
final Long totalNumberOfProductTypes =
284-
CTP_TARGET_CLIENT
285-
.productTypes()
286-
.get()
287-
.execute()
288-
.thenApply(ApiHttpResponse::getBody)
289-
.thenApply(ProductTypePagedQueryResponse::getTotal)
290-
.toCompletableFuture()
291-
.join();
292-
293-
assertThat(totalNumberOfProductTypes).isEqualTo(NUMBER_OF_RESOURCE_UNDER_TEST);
294-
295-
// Assert statistics
219+
// Assert statistics - first half should be updated, second half created
296220
assertThat(syncStatistics)
297221
.hasValues(NUMBER_OF_RESOURCE_UNDER_TEST, halfNumberOfDrafts, halfNumberOfDrafts, 0);
298222

@@ -305,14 +229,15 @@ void sync_WithSomeExistingProductTypes_ShouldSyncProductTypes() throws IOExcepti
305229
}
306230

307231
@Nonnull
308-
private static List<ProductTypeDraft> buildProductTypeDrafts(final int numberOfTypes) {
232+
private static List<ProductTypeDraft> buildProductTypeDrafts(
233+
final int numberOfTypes, @Nonnull final String prefix) {
309234
return IntStream.range(0, numberOfTypes)
310235
.mapToObj(
311236
i ->
312237
ProductTypeDraftBuilder.of()
313-
.key(format("key__%d", i))
314-
.name(format("name__%d", i))
315-
.description(format("description__%d", i))
238+
.key(format("%s_key_%d", prefix, i))
239+
.name(format("%s_name_%d", prefix, i))
240+
.description(format("%s_description_%d", prefix, i))
316241
.attributes(singletonList(ATTRIBUTE_DEFINITION_DRAFT_1))
317242
.build())
318243
.collect(Collectors.toList());

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

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.Collections;
3030
import java.util.List;
3131
import java.util.Locale;
32+
import java.util.UUID;
3233
import java.util.concurrent.CompletableFuture;
3334
import java.util.concurrent.CompletionException;
3435
import org.junit.jupiter.api.*;
@@ -68,16 +69,6 @@ void setupTest() {
6869
CategoryITUtils.deleteAllCategories(TestClientUtils.CTP_TARGET_CLIENT);
6970
CategoryITUtils.deleteAllCategories(TestClientUtils.CTP_SOURCE_CLIENT);
7071

71-
// Clean up any categories without keys that deleteAllCategories() might have missed
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-
8172
CategoryITUtils.ensureCategories(
8273
TestClientUtils.CTP_TARGET_CLIENT, CategoryITUtils.getCategoryDrafts(null, 2, true));
8374

@@ -463,52 +454,34 @@ void syncDrafts_withANonExistingNewParent_ShouldUpdateCategories() {
463454

464455
@Test
465456
void syncDrafts_fromCategoriesWithoutKeys_ShouldNotUpdateCategories() {
457+
// Generate unique identifiers for this test run to avoid collisions
458+
final String testRunId = UUID.randomUUID().toString();
459+
final String key1 = "cat-key-1-" + testRunId;
460+
final String key2 = "cat-key-2-" + testRunId;
461+
final String slug1 = "cat-slug-1-" + testRunId;
462+
final String slug2 = "cat-slug-2-" + testRunId;
463+
466464
final CategoryDraft oldCategoryDraft1 =
467465
CategoryDraftBuilder.of()
468466
.name(LocalizedString.of(Locale.ENGLISH, "cat1"))
469-
.slug(LocalizedString.of(Locale.ENGLISH, "furniture1-project-source"))
470-
.key("newKey1")
467+
.slug(LocalizedString.of(Locale.ENGLISH, slug1))
468+
.key(key1)
471469
.custom(CategoryITUtils.getCustomFieldsDraft())
472470
.build();
473471

474472
final CategoryDraft oldCategoryDraft2 =
475473
CategoryDraftBuilder.of()
476474
.name(LocalizedString.of(Locale.ENGLISH, "cat2"))
477-
.slug(LocalizedString.of(Locale.ENGLISH, "furniture2-project-source"))
478-
.key("newKey2")
475+
.slug(LocalizedString.of(Locale.ENGLISH, slug2))
476+
.key(key2)
479477
.custom(CategoryITUtils.getCustomFieldsDraft())
480478
.build();
481479

482-
// Ensure SOURCE is clean before creating categories (defensive cleanup)
483-
CategoryITUtils.deleteCategoriesBySlug(
484-
TestClientUtils.CTP_SOURCE_CLIENT,
485-
Locale.ENGLISH,
486-
List.of("furniture1-project-source", "furniture2-project-source"));
487-
488480
// Create two categories in the source with Keys.
489-
List<CompletableFuture<ApiHttpResponse<Category>>> futureCreations = new ArrayList<>();
490-
futureCreations.add(
491-
TestClientUtils.CTP_SOURCE_CLIENT
492-
.categories()
493-
.create(oldCategoryDraft1)
494-
.execute()
495-
.toCompletableFuture());
496-
futureCreations.add(
497-
TestClientUtils.CTP_SOURCE_CLIENT
498-
.categories()
499-
.create(oldCategoryDraft2)
500-
.execute()
501-
.toCompletableFuture());
502-
CompletableFuture.allOf(futureCreations.toArray(new CompletableFuture[futureCreations.size()]))
503-
.join();
504-
505-
// Ensure TARGET is clean before creating categories without keys (defensive cleanup)
506-
CategoryITUtils.deleteCategoriesBySlug(
507-
TestClientUtils.CTP_TARGET_CLIENT,
508-
Locale.ENGLISH,
509-
List.of("furniture1-project-source", "furniture2-project-source"));
481+
TestClientUtils.CTP_SOURCE_CLIENT.categories().create(oldCategoryDraft1).executeBlocking();
482+
TestClientUtils.CTP_SOURCE_CLIENT.categories().create(oldCategoryDraft2).executeBlocking();
510483

511-
// Create two categories in the target without Keys (sequentially to ensure both are created).
484+
// Create two categories in the target without Keys (same slugs but no keys).
512485
final CategoryDraft newCategoryDraft1 =
513486
CategoryDraftBuilder.of(oldCategoryDraft1).key(null).build();
514487
final CategoryDraft newCategoryDraft2 =
@@ -525,7 +498,7 @@ void syncDrafts_fromCategoriesWithoutKeys_ShouldNotUpdateCategories() {
525498
.categories()
526499
.get()
527500
.withWhere("key in :keys")
528-
.withPredicateVar("keys", List.of("newKey1", "newKey2"))
501+
.withPredicateVar("keys", List.of(key1, key2))
529502
.execute()
530503
.toCompletableFuture()
531504
.join()

src/integration-test/java/com/commercetools/sync/integration/externalsource/shoppinglists/ShoppingListSyncIT.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import java.util.Map;
5858
import java.util.Objects;
5959
import java.util.Set;
60+
import java.util.UUID;
6061
import java.util.stream.Collectors;
6162
import javax.annotation.Nonnull;
6263
import org.apache.commons.lang3.tuple.ImmutablePair;
@@ -526,15 +527,16 @@ private List<ShoppingListLineItemDraft> setNullToAddedAtValuesForLineItems(
526527

527528
@Test
528529
void sync_WithStoreChange_ShouldUpdateShoppingListStore() {
530+
// Generate unique store key to avoid collisions with other test runs
531+
final String storeKey = "test-store-" + UUID.randomUUID();
532+
529533
// Create the store that will be referenced
530-
ensureStore(CTP_TARGET_CLIENT, "different-store-key");
534+
ensureStore(CTP_TARGET_CLIENT, storeKey);
531535

532536
// Create a shopping list draft with a different store
533537
final ShoppingListDraft modifiedDraft =
534538
ShoppingListDraftBuilder.of(shoppingListDraftSampleCarrotCake)
535-
.store(
536-
storeResourceIdentifierBuilder ->
537-
storeResourceIdentifierBuilder.key("different-store-key"))
539+
.store(storeResourceIdentifierBuilder -> storeResourceIdentifierBuilder.key(storeKey))
538540
.build();
539541

540542
final ShoppingListSyncStatistics shoppingListSyncStatistics =

0 commit comments

Comments
 (0)