|
17 | 17 | import com.commercetools.api.models.shopping_list.ShoppingListSetDeleteDaysAfterLastModificationAction; |
18 | 18 | import com.commercetools.api.models.shopping_list.ShoppingListSetDescriptionAction; |
19 | 19 | import com.commercetools.api.models.shopping_list.ShoppingListSetSlugAction; |
| 20 | +import com.commercetools.api.models.shopping_list.ShoppingListSetStoreAction; |
20 | 21 | 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; |
21 | 26 | import java.util.Locale; |
22 | 27 | import java.util.Optional; |
23 | 28 | import java.util.UUID; |
@@ -351,4 +356,94 @@ void buildSetAnonymousId_WithSameValues_ShouldNotBuildUpdateActions() { |
351 | 356 | assertThat(setDeleteDaysUpdateAction) |
352 | 357 | .containsInstanceOf(ShoppingListSetDeleteDaysAfterLastModificationAction.class); |
353 | 358 | } |
| 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 | + } |
354 | 449 | } |
0 commit comments