use of com.commercetools.sync.services.ProductTypeService in project commercetools-sync-java by commercetools.
the class ProductTypeServiceImplTest method fetchProductType_WithBadGateWayException_ShouldCompleteExceptionally.
@Test
void fetchProductType_WithBadGateWayException_ShouldCompleteExceptionally() {
// preparation
final SphereClient sphereClient = mock(SphereClient.class);
when(sphereClient.execute(any(ProductTypeQuery.class))).thenReturn(CompletableFutureUtils.exceptionallyCompletedFuture(new BadGatewayException()));
final ProductTypeSyncOptions syncOptions = ProductTypeSyncOptionsBuilder.of(sphereClient).build();
final ProductTypeService productTypeService = new ProductTypeServiceImpl(syncOptions);
// test
final CompletionStage<Optional<ProductType>> result = productTypeService.fetchProductType("foo");
// assertions
assertThat(result).failsWithin(1, TimeUnit.SECONDS).withThrowableOfType(ExecutionException.class).withCauseExactlyInstanceOf(BadGatewayException.class);
}
use of com.commercetools.sync.services.ProductTypeService in project commercetools-sync-java by commercetools.
the class ProductSyncMockUtils method getMockProductTypeService.
/**
* Creates a mock {@link ProductTypeService} that returns a completed {@link CompletableFuture}
* containing an {@link Optional} containing the id of the supplied value whenever the following
* method is called on the service:
*
* <ul>
* <li>{@link ProductTypeService#fetchCachedProductTypeId(String)}
* </ul>
*
* @return the created mock of the {@link ProductTypeService}.
*/
public static ProductTypeService getMockProductTypeService(@Nonnull final String id) {
final ProductTypeService productTypeService = mock(ProductTypeService.class);
when(productTypeService.fetchCachedProductTypeId(anyString())).thenReturn(CompletableFuture.completedFuture(Optional.of(id)));
return productTypeService;
}
use of com.commercetools.sync.services.ProductTypeService in project commercetools-sync-java by commercetools.
the class ProductTypeSyncTest method sync_WithErrorCachingKeysButNoKeysToCache_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter.
@Test
void sync_WithErrorCachingKeysButNoKeysToCache_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter() {
// preparation
final ProductTypeDraft newProductTypeDraft = ProductTypeDraft.ofAttributeDefinitionDrafts("foo", "name", "desc", emptyList());
final List<String> errorMessages = new ArrayList<>();
final List<Throwable> exceptions = new ArrayList<>();
final SphereClient sphereClient = mock(SphereClient.class);
final ProductTypeSyncOptions syncOptions = ProductTypeSyncOptionsBuilder.of(sphereClient).errorCallback((exception, oldResource, newResource, actions) -> {
errorMessages.add(exception.getMessage());
exceptions.add(exception);
}).build();
final ProductTypeService mockProductTypeService = new ProductTypeServiceImpl(syncOptions);
when(sphereClient.execute(any(ProductTypeQuery.class))).thenReturn(supplyAsync(() -> {
throw new SphereException();
}));
final ProductTypeSync productTypeSync = new ProductTypeSync(syncOptions, mockProductTypeService);
// test
final ProductTypeSyncStatistics productTypeSyncStatistics = productTypeSync.sync(singletonList(newProductTypeDraft)).toCompletableFuture().join();
// assertions
assertThat(errorMessages).hasSize(1).singleElement(as(STRING)).isEqualTo("Failed to fetch existing product types with keys: '[foo]'.");
assertThat(exceptions).hasSize(1).singleElement(as(THROWABLE)).isExactlyInstanceOf(SyncException.class).hasCauseExactlyInstanceOf(CompletionException.class).hasRootCauseExactlyInstanceOf(SphereException.class);
assertThat(productTypeSyncStatistics).hasValues(1, 0, 0, 1, 0);
}
use of com.commercetools.sync.services.ProductTypeService in project commercetools-sync-java by commercetools.
the class ProductTypeSyncTest method sync_WithInvalidAttributeDefinitions_ShouldThrowError.
@Test
@SuppressWarnings("unchecked")
void sync_WithInvalidAttributeDefinitions_ShouldThrowError() {
// preparation
String nestedAttributeTypeId = "attributeId";
NestedAttributeType nestedAttributeType = spy(NestedAttributeType.of(referenceOfId(nestedAttributeTypeId)));
Reference reference = spy(Reference.class);
when(reference.getId()).thenReturn(nestedAttributeTypeId).thenReturn(null);
when(nestedAttributeType.getTypeReference()).thenReturn(reference);
final AttributeDefinitionDraft nestedTypeAttrDefDraft = AttributeDefinitionDraftBuilder.of(nestedAttributeType, "validNested", ofEnglish("koko"), true).build();
// preparation
final ProductTypeDraft newProductTypeDraft = ProductTypeDraft.ofAttributeDefinitionDrafts("foo", "name", "desc", singletonList(nestedTypeAttrDefDraft));
final List<String> errorMessages = new ArrayList<>();
final List<Throwable> exceptions = new ArrayList<>();
final ProductTypeSyncOptions syncOptions = ProductTypeSyncOptionsBuilder.of(mock(SphereClient.class)).errorCallback((exception, oldResource, newResource, actions) -> {
errorMessages.add(exception.getMessage());
exceptions.add(exception);
}).build();
final ProductTypeService mockProductTypeService = mock(ProductTypeServiceImpl.class);
final ProductType existingProductType = mock(ProductType.class);
when(existingProductType.getKey()).thenReturn(newProductTypeDraft.getKey());
when(mockProductTypeService.fetchMatchingProductTypesByKeys(singleton(newProductTypeDraft.getKey()))).thenReturn(CompletableFuture.completedFuture(singleton(existingProductType)));
when(mockProductTypeService.fetchMatchingProductTypesByKeys(emptySet())).thenReturn(CompletableFuture.completedFuture(Collections.emptySet()));
when(mockProductTypeService.updateProductType(any(), any())).thenReturn(CompletableFuture.completedFuture(existingProductType));
when(mockProductTypeService.cacheKeysToIds(anySet())).thenReturn(CompletableFuture.completedFuture(emptyMap()));
final ProductTypeSync productTypeSync = new ProductTypeSync(syncOptions, mockProductTypeService);
// test
final ProductTypeSyncStatistics productTypeSyncStatistics = productTypeSync.sync(singletonList(newProductTypeDraft)).toCompletableFuture().join();
// assertions
assertThat(errorMessages.get(0)).contains("This exception is unexpectedly thrown since the draft batch has been" + "already validated for blank keys");
assertThat(errorMessages.get(1)).contains("Failed to process the productTypeDraft with key:'foo'");
assertThat(exceptions.size()).isEqualTo(2);
assertThat(productTypeSyncStatistics).hasValues(1, 0, 0, 2, 0);
}
use of com.commercetools.sync.services.ProductTypeService in project commercetools-sync-java by commercetools.
the class ProductTypeSyncTest method sync_WithErrorFetchingExistingKeys_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter.
@Test
void sync_WithErrorFetchingExistingKeys_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter() {
// preparation
final ProductTypeDraft newProductTypeDraft = ProductTypeDraft.ofAttributeDefinitionDrafts("foo", "name", "desc", emptyList());
final List<String> errorMessages = new ArrayList<>();
final List<Throwable> exceptions = new ArrayList<>();
final ProductTypeSyncOptions syncOptions = ProductTypeSyncOptionsBuilder.of(mock(SphereClient.class)).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorMessages.add(exception.getMessage());
exceptions.add(exception);
}).build();
final ProductTypeService mockProductTypeService = mock(ProductTypeService.class);
when(mockProductTypeService.fetchMatchingProductTypesByKeys(singleton(newProductTypeDraft.getKey()))).thenReturn(supplyAsync(() -> {
throw new SphereException();
}));
when(mockProductTypeService.cacheKeysToIds(anySet())).thenReturn(CompletableFuture.completedFuture(emptyMap()));
final ProductTypeSync productTypeSync = new ProductTypeSync(syncOptions, mockProductTypeService);
// test
final ProductTypeSyncStatistics productTypeSyncStatistics = productTypeSync.sync(singletonList(newProductTypeDraft)).toCompletableFuture().join();
// assertions
assertThat(errorMessages).hasSize(1).singleElement(as(STRING)).isEqualTo("Failed to fetch existing product types with keys: '[foo]'.");
assertThat(exceptions).hasSize(1).singleElement(as(THROWABLE)).isExactlyInstanceOf(SyncException.class).hasCauseExactlyInstanceOf(CompletionException.class).hasRootCauseExactlyInstanceOf(SphereException.class);
assertThat(productTypeSyncStatistics).hasValues(1, 0, 0, 1, 0);
}
Aggregations