use of com.commercetools.sync.services.ShoppingListService in project commercetools-sync-java by commercetools.
the class ShoppingListServiceImplTest method updateShoppingList_WithMockSuccessfulCtpResponse_ShouldCallShoppingListUpdateCommand.
@Test
void updateShoppingList_WithMockSuccessfulCtpResponse_ShouldCallShoppingListUpdateCommand() {
// preparation
final ShoppingList shoppingList = mock(ShoppingList.class);
final ShoppingListSyncOptions options = ShoppingListSyncOptionsBuilder.of(mock(SphereClient.class)).build();
when(options.getCtpClient().execute(any())).thenReturn(completedFuture(shoppingList));
final ShoppingListService shoppingListService = new ShoppingListServiceImpl(options);
final List<UpdateAction<ShoppingList>> updateActions = singletonList(ChangeName.of(LocalizedString.ofEnglish("new_name")));
// test
final CompletionStage<ShoppingList> result = shoppingListService.updateShoppingList(shoppingList, updateActions);
// assertions
assertThat(result).isCompletedWithValue(shoppingList);
verify(options.getCtpClient()).execute(eq(ShoppingListUpdateCommand.of(shoppingList, updateActions)));
}
use of com.commercetools.sync.services.ShoppingListService in project commercetools-sync-java by commercetools.
the class ShoppingListServiceImplIT method createShoppingList_WithValidShoppingList_ShouldCreateShoppingList.
@Test
void createShoppingList_WithValidShoppingList_ShouldCreateShoppingList() {
// preparation
ProductType productType = createProductType(PRODUCT_TYPE_RESOURCE_PATH, CTP_TARGET_CLIENT);
final ProductDraft productDraft = ProductDraftBuilder.of(ResourceIdentifier.ofKey(productType.getKey()), LocalizedString.ofEnglish("newProduct"), LocalizedString.ofEnglish("foo"), ProductVariantDraftBuilder.of().key("foo-new").sku("sku-new").build()).key("newProduct").build();
executeBlocking(CTP_TARGET_CLIENT.execute(ProductCreateCommand.of(productDraft)));
LineItemDraft lineItemDraft = LineItemDraftBuilder.ofSku("sku-new", Long.valueOf(1)).build();
TextLineItemDraft textLineItemDraft = TextLineItemDraftBuilder.of(LocalizedString.ofEnglish("text"), 1L).build();
final ShoppingListDraftDsl newShoppingListDraft = ShoppingListDraftBuilder.of(LocalizedString.ofEnglish("new_name")).key("new_key").plusLineItems(lineItemDraft).plusTextLineItems(textLineItemDraft).build();
final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
final ShoppingListSyncOptions options = ShoppingListSyncOptionsBuilder.of(spyClient).errorCallback((exception, oldResource, newResource, actions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception);
}).build();
ShoppingListService spyShoppingListService = new ShoppingListServiceImpl(options);
// test
final Optional<ShoppingList> createdShoppingList = spyShoppingListService.createShoppingList(newShoppingListDraft).toCompletableFuture().join();
final Optional<ShoppingList> queriedOptional = CTP_TARGET_CLIENT.execute(ShoppingListQuery.of().withPredicates(shoppingListQueryModel -> shoppingListQueryModel.key().is("new_key"))).toCompletableFuture().join().head();
assertThat(queriedOptional).hasValueSatisfying(queried -> assertThat(createdShoppingList).hasValueSatisfying(created -> {
assertThat(created.getKey()).isEqualTo(queried.getKey());
assertThat(created.getName()).isEqualTo(queried.getName());
assertThat(created.getLineItems()).hasSize(1);
assertThat(created.getTextLineItems()).hasSize(1);
}));
}
use of com.commercetools.sync.services.ShoppingListService in project commercetools-sync-java by commercetools.
the class ShoppingListServiceImplIT method cacheKeysToIds_WithCachedKeys_ShouldReturnCachedKeysWithoutRequest.
@Test
void cacheKeysToIds_WithCachedKeys_ShouldReturnCachedKeysWithoutRequest() {
final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
final ShoppingListSyncOptions shoppingListSyncOptions = ShoppingListSyncOptionsBuilder.of(spyClient).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).build();
final ShoppingListService shoppingListService = new ShoppingListServiceImpl(shoppingListSyncOptions);
Map<String, String> cache = shoppingListService.cacheKeysToIds(singleton(shoppingList.getKey())).toCompletableFuture().join();
assertThat(cache).hasSize(1);
cache = shoppingListService.cacheKeysToIds(singleton(shoppingList.getKey())).toCompletableFuture().join();
assertThat(cache).hasSize(1);
verify(spyClient, times(1)).execute(any());
assertThat(errorCallBackExceptions).isEmpty();
assertThat(errorCallBackMessages).isEmpty();
}
use of com.commercetools.sync.services.ShoppingListService in project commercetools-sync-java by commercetools.
the class ShoppingListServiceImplTest method updateShoppingList_WithMockUnsuccessfulCtpResponse_ShouldCompleteExceptionally.
@Test
void updateShoppingList_WithMockUnsuccessfulCtpResponse_ShouldCompleteExceptionally() {
// preparation
final ShoppingList shoppingList = mock(ShoppingList.class);
final ShoppingListSyncOptions shoppingListSyncOptions = ShoppingListSyncOptionsBuilder.of(mock(SphereClient.class)).build();
when(shoppingListSyncOptions.getCtpClient().execute(any())).thenReturn(CompletableFutureUtils.exceptionallyCompletedFuture(new InternalServerErrorException()));
final ShoppingListService shoppingListService = new ShoppingListServiceImpl(shoppingListSyncOptions);
final List<UpdateAction<ShoppingList>> updateActions = singletonList(ChangeName.of(LocalizedString.ofEnglish("new_name")));
// test
final CompletionStage<ShoppingList> result = shoppingListService.updateShoppingList(shoppingList, updateActions);
// assertions
assertThat(result).failsWithin(1, TimeUnit.SECONDS).withThrowableOfType(ExecutionException.class).withCauseExactlyInstanceOf(InternalServerErrorException.class);
}
use of com.commercetools.sync.services.ShoppingListService in project commercetools-sync-java by commercetools.
the class ShoppingListServiceImplTest method createShoppingList_WithNullShoppingListKey_ShouldNotCreateShoppingList.
@Test
void createShoppingList_WithNullShoppingListKey_ShouldNotCreateShoppingList() {
// preparation
final ShoppingListDraft mockShoppingListDraft = mock(ShoppingListDraft.class);
final Map<String, Throwable> errors = new HashMap<>();
when(mockShoppingListDraft.getKey()).thenReturn(null);
final ShoppingListSyncOptions shoppingListSyncOptions = ShoppingListSyncOptionsBuilder.of(mock(SphereClient.class)).errorCallback((exception, oldResource, newResource, actions) -> errors.put(exception.getMessage(), exception)).build();
final ShoppingListService shoppingListService = new ShoppingListServiceImpl(shoppingListSyncOptions);
// test
final CompletionStage<Optional<ShoppingList>> result = shoppingListService.createShoppingList(mockShoppingListDraft);
// assertions
assertThat(result).isCompletedWithValue(Optional.empty());
assertThat(errors.keySet()).containsExactly("Failed to create draft with key: 'null'. Reason: Draft key is blank!");
verify(shoppingListSyncOptions.getCtpClient(), times(0)).execute(any());
}
Aggregations