Skip to content

Commit 93d945d

Browse files
test: store in shipping-lists
1 parent 4ea9d67 commit 93d945d

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

src/test/java/com/commercetools/sync/shoppinglists/utils/ShoppingListUpdateActionUtilsTest.java

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@
1717
import com.commercetools.api.models.shopping_list.ShoppingListSetDeleteDaysAfterLastModificationAction;
1818
import com.commercetools.api.models.shopping_list.ShoppingListSetDescriptionAction;
1919
import com.commercetools.api.models.shopping_list.ShoppingListSetSlugAction;
20+
import com.commercetools.api.models.shopping_list.ShoppingListSetStoreAction;
2021
import com.commercetools.api.models.shopping_list.ShoppingListUpdateAction;
22+
import com.commercetools.api.models.store.StoreKeyReference;
23+
import com.commercetools.api.models.store.StoreKeyReferenceBuilder;
24+
import com.commercetools.api.models.store.StoreResourceIdentifier;
25+
import com.commercetools.api.models.store.StoreResourceIdentifierBuilder;
2126
import java.util.Locale;
2227
import java.util.Optional;
2328
import java.util.UUID;
@@ -351,4 +356,94 @@ void buildSetAnonymousId_WithSameValues_ShouldNotBuildUpdateActions() {
351356
assertThat(setDeleteDaysUpdateAction)
352357
.containsInstanceOf(ShoppingListSetDeleteDaysAfterLastModificationAction.class);
353358
}
359+
360+
@Test
361+
void buildSetStoreUpdateAction_WithDifferentStoreKeys_ShouldBuildUpdateAction() {
362+
final StoreKeyReference oldStoreReference =
363+
StoreKeyReferenceBuilder.of().key("old-store-key").build();
364+
final ShoppingList oldShoppingList = mock(ShoppingList.class);
365+
when(oldShoppingList.getStore()).thenReturn(oldStoreReference);
366+
367+
final StoreResourceIdentifier newStoreIdentifier =
368+
StoreResourceIdentifierBuilder.of().key("new-store-key").build();
369+
final ShoppingListDraft newShoppingList = mock(ShoppingListDraft.class);
370+
when(newShoppingList.getStore()).thenReturn(newStoreIdentifier);
371+
372+
final ShoppingListUpdateAction setStoreUpdateAction =
373+
ShoppingListUpdateActionUtils.buildSetStoreUpdateAction(oldShoppingList, newShoppingList)
374+
.orElse(null);
375+
376+
assertThat(setStoreUpdateAction).isNotNull();
377+
assertThat(setStoreUpdateAction.getAction()).isEqualTo("setStore");
378+
assertThat(((ShoppingListSetStoreAction) setStoreUpdateAction).getStore())
379+
.isEqualTo(newStoreIdentifier);
380+
}
381+
382+
@Test
383+
void buildSetStoreUpdateAction_WithSameStoreKeys_ShouldNotBuildUpdateAction() {
384+
final StoreKeyReference oldStoreReference =
385+
StoreKeyReferenceBuilder.of().key("same-store-key").build();
386+
final ShoppingList oldShoppingList = mock(ShoppingList.class);
387+
when(oldShoppingList.getStore()).thenReturn(oldStoreReference);
388+
389+
final StoreResourceIdentifier newStoreIdentifier =
390+
StoreResourceIdentifierBuilder.of().key("same-store-key").build();
391+
final ShoppingListDraft newShoppingList = mock(ShoppingListDraft.class);
392+
when(newShoppingList.getStore()).thenReturn(newStoreIdentifier);
393+
394+
final Optional<ShoppingListUpdateAction> setStoreUpdateAction =
395+
ShoppingListUpdateActionUtils.buildSetStoreUpdateAction(oldShoppingList, newShoppingList);
396+
397+
assertThat(setStoreUpdateAction).isNotPresent();
398+
}
399+
400+
@Test
401+
void buildSetStoreUpdateAction_WithNullOldStore_ShouldBuildUpdateAction() {
402+
final ShoppingList oldShoppingList = mock(ShoppingList.class);
403+
when(oldShoppingList.getStore()).thenReturn(null);
404+
405+
final StoreResourceIdentifier newStoreIdentifier =
406+
StoreResourceIdentifierBuilder.of().key("new-store-key").build();
407+
final ShoppingListDraft newShoppingList = mock(ShoppingListDraft.class);
408+
when(newShoppingList.getStore()).thenReturn(newStoreIdentifier);
409+
410+
final ShoppingListUpdateAction setStoreUpdateAction =
411+
ShoppingListUpdateActionUtils.buildSetStoreUpdateAction(oldShoppingList, newShoppingList)
412+
.orElse(null);
413+
414+
assertThat(setStoreUpdateAction).isNotNull();
415+
assertThat(setStoreUpdateAction.getAction()).isEqualTo("setStore");
416+
}
417+
418+
@Test
419+
void buildSetStoreUpdateAction_WithNullNewStore_ShouldBuildUpdateAction() {
420+
final StoreKeyReference oldStoreReference =
421+
StoreKeyReferenceBuilder.of().key("old-store-key").build();
422+
final ShoppingList oldShoppingList = mock(ShoppingList.class);
423+
when(oldShoppingList.getStore()).thenReturn(oldStoreReference);
424+
425+
final ShoppingListDraft newShoppingList = mock(ShoppingListDraft.class);
426+
when(newShoppingList.getStore()).thenReturn(null);
427+
428+
final ShoppingListUpdateAction setStoreUpdateAction =
429+
ShoppingListUpdateActionUtils.buildSetStoreUpdateAction(oldShoppingList, newShoppingList)
430+
.orElse(null);
431+
432+
assertThat(setStoreUpdateAction).isNotNull();
433+
assertThat(setStoreUpdateAction.getAction()).isEqualTo("setStore");
434+
}
435+
436+
@Test
437+
void buildSetStoreUpdateAction_WithBothNull_ShouldNotBuildUpdateAction() {
438+
final ShoppingList oldShoppingList = mock(ShoppingList.class);
439+
when(oldShoppingList.getStore()).thenReturn(null);
440+
441+
final ShoppingListDraft newShoppingList = mock(ShoppingListDraft.class);
442+
when(newShoppingList.getStore()).thenReturn(null);
443+
444+
final Optional<ShoppingListUpdateAction> setStoreUpdateAction =
445+
ShoppingListUpdateActionUtils.buildSetStoreUpdateAction(oldShoppingList, newShoppingList);
446+
447+
assertThat(setStoreUpdateAction).isNotPresent();
448+
}
354449
}

src/test/java/com/commercetools/sync/shoppinglists/utils/ShoppingListsReferenceResolutionUtilsTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import com.commercetools.api.models.shopping_list.ShoppingListLineItemDraftBuilder;
1717
import com.commercetools.api.models.shopping_list.TextLineItem;
1818
import com.commercetools.api.models.shopping_list.TextLineItemDraftBuilder;
19+
import com.commercetools.api.models.store.StoreKeyReference;
20+
import com.commercetools.api.models.store.StoreKeyReferenceBuilder;
1921
import com.commercetools.api.models.type.CustomFields;
2022
import com.commercetools.api.models.type.CustomFieldsDraftBuilder;
2123
import com.commercetools.api.models.type.TypeReference;
@@ -225,4 +227,49 @@ void mapToShoppingListDrafts_WithOtherFields_ShouldReturnDraftsCorrectly() {
225227
.anonymousId("anonymousId")
226228
.build());
227229
}
230+
231+
@Test
232+
void mapToShoppingListDrafts_WithStoreReference_ShouldReturnDraftsWithStoreKey() {
233+
final String storeKey = "store-key";
234+
235+
final ShoppingList mockShoppingList = mock(ShoppingList.class);
236+
when(mockShoppingList.getName()).thenReturn(ofEnglish("name"));
237+
when(mockShoppingList.getKey()).thenReturn("key");
238+
239+
final StoreKeyReference storeKeyReference = StoreKeyReferenceBuilder.of().key(storeKey).build();
240+
when(mockShoppingList.getStore()).thenReturn(storeKeyReference);
241+
242+
when(mockShoppingList.getCustomer()).thenReturn(null);
243+
when(mockShoppingList.getCustom()).thenReturn(null);
244+
when(mockShoppingList.getLineItems()).thenReturn(null);
245+
when(mockShoppingList.getTextLineItems()).thenReturn(null);
246+
247+
final List<ShoppingListDraft> shoppingListDrafts =
248+
ShoppingListReferenceResolutionUtils.mapToShoppingListDrafts(
249+
singletonList(mockShoppingList), referenceIdToKeyCache);
250+
251+
assertThat(shoppingListDrafts).hasSize(1);
252+
assertThat(shoppingListDrafts.get(0).getStore()).isNotNull();
253+
assertThat(shoppingListDrafts.get(0).getStore().getKey()).isEqualTo(storeKey);
254+
}
255+
256+
@Test
257+
void mapToShoppingListDrafts_WithNullStore_ShouldReturnDraftsWithNullStore() {
258+
final ShoppingList mockShoppingList = mock(ShoppingList.class);
259+
when(mockShoppingList.getName()).thenReturn(ofEnglish("name"));
260+
when(mockShoppingList.getKey()).thenReturn("key");
261+
when(mockShoppingList.getStore()).thenReturn(null);
262+
263+
when(mockShoppingList.getCustomer()).thenReturn(null);
264+
when(mockShoppingList.getCustom()).thenReturn(null);
265+
when(mockShoppingList.getLineItems()).thenReturn(null);
266+
when(mockShoppingList.getTextLineItems()).thenReturn(null);
267+
268+
final List<ShoppingListDraft> shoppingListDrafts =
269+
ShoppingListReferenceResolutionUtils.mapToShoppingListDrafts(
270+
singletonList(mockShoppingList), referenceIdToKeyCache);
271+
272+
assertThat(shoppingListDrafts).hasSize(1);
273+
assertThat(shoppingListDrafts.get(0).getStore()).isNull();
274+
}
228275
}

0 commit comments

Comments
 (0)