use of com.commercetools.sync.services.ProductTypeService in project commercetools-sync-java by commercetools.
the class ProductTypeSyncTest method sync_WithErrorCachingKeys_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter.
@Test
void sync_WithErrorCachingKeys_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter() {
// preparation
final AttributeDefinitionDraft nestedTypeAttrDefDraft = AttributeDefinitionDraftBuilder.of(NestedAttributeType.of(referenceOfId("x")), "validNested", ofEnglish("koko"), true).build();
final ProductTypeDraft newProductTypeDraft = ProductTypeDraft.ofAttributeDefinitionDrafts("foo", "name", "desc", singletonList(nestedTypeAttrDefDraft));
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(ResourceKeyIdGraphQlRequest.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 build a cache of keys to ids.");
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_WithNullInAttributeDefinitions_ShouldSyncCorrectly.
@Test
void sync_WithNullInAttributeDefinitions_ShouldSyncCorrectly() {
// preparation
final ProductTypeDraft newProductTypeDraft = ProductTypeDraft.ofAttributeDefinitionDrafts("foo", "name", "desc", singletonList(null));
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).isEmpty();
assertThat(exceptions).isEmpty();
assertThat(productTypeSyncStatistics).hasValues(1, 0, 1, 0, 0);
}
use of com.commercetools.sync.services.ProductTypeService in project commercetools-sync-java by commercetools.
the class ProductTypeSyncTest method sync_WithOnlyDraftsToUpdate_ShouldOnlyCallBeforeUpdateCallback.
@Test
void sync_WithOnlyDraftsToUpdate_ShouldOnlyCallBeforeUpdateCallback() {
// preparation
final ProductTypeDraft newProductTypeDraft = ProductTypeDraftBuilder.of("newProductType", "productType", "a cool type", emptyList()).build();
final SphereClient sphereClient = mock(SphereClient.class);
final ProductTypeSyncOptions productTypeSyncOptions = ProductTypeSyncOptionsBuilder.of(sphereClient).build();
final ProductType mockedExistingProductType = mock(ProductType.class);
when(mockedExistingProductType.getKey()).thenReturn(newProductTypeDraft.getKey());
when(mockedExistingProductType.getId()).thenReturn(UUID.randomUUID().toString());
final ProductTypeService productTypeService = new ProductTypeServiceImpl(productTypeSyncOptions);
final PagedQueryResult<ProductType> productTypePagedQueryResult = spy(PagedQueryResult.empty());
when(productTypePagedQueryResult.getResults()).thenReturn(singletonList(mockedExistingProductType));
when(sphereClient.execute(any(ProductTypeQuery.class))).thenReturn(completedFuture(productTypePagedQueryResult));
when(sphereClient.execute(any(ProductTypeUpdateCommand.class))).thenReturn(completedFuture(mockedExistingProductType));
final ProductTypeSyncOptions spyProductTypeSyncOptions = spy(productTypeSyncOptions);
// test
new ProductTypeSync(spyProductTypeSyncOptions, productTypeService).sync(singletonList(newProductTypeDraft)).toCompletableFuture().join();
// assertion
verify(spyProductTypeSyncOptions).applyBeforeUpdateCallback(any(), any(), any());
verify(spyProductTypeSyncOptions, never()).applyBeforeCreateCallback(newProductTypeDraft);
}
use of com.commercetools.sync.services.ProductTypeService in project commercetools-sync-java by commercetools.
the class ProductTypeSyncTest method sync_WithErrorsOnSyncing_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter.
@Test
void sync_WithErrorsOnSyncing_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, 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(null);
when(mockProductTypeService.fetchMatchingProductTypesByKeys(singleton(newProductTypeDraft.getKey()))).thenReturn(CompletableFuture.completedFuture(singleton(existingProductType)));
when(mockProductTypeService.fetchMatchingProductTypesByKeys(emptySet())).thenReturn(CompletableFuture.completedFuture(Collections.emptySet()));
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)).contains("Failed to process the productTypeDraft with key:'foo'." + " Reason: java.lang.NullPointerException");
assertThat(exceptions).hasSize(1).singleElement(as(THROWABLE)).isExactlyInstanceOf(SyncException.class).hasCauseExactlyInstanceOf(CompletionException.class).hasRootCauseExactlyInstanceOf(NullPointerException.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 AttributeDefinitionReferenceResolverTest method resolveReferences_WithSetOfNestedTypeWithExistingProductTypeReference_ShouldResolveReferences.
@Test
void resolveReferences_WithSetOfNestedTypeWithExistingProductTypeReference_ShouldResolveReferences() {
// preparation
final NestedAttributeType nestedAttributeType = NestedAttributeType.of(ProductType.reference("x"));
final SetAttributeType setAttributeType = SetAttributeType.of(nestedAttributeType);
final AttributeDefinitionDraft attributeDefinitionDraft = AttributeDefinitionDraftBuilder.of(setAttributeType, "foo", ofEnglish("foo"), true).build();
final ProductTypeSyncOptions syncOptions = ProductTypeSyncOptionsBuilder.of(mock(SphereClient.class)).build();
final ProductTypeService productTypeService = mock(ProductTypeService.class);
when(productTypeService.fetchCachedProductTypeId(any())).thenReturn(CompletableFuture.completedFuture(Optional.of("foo")));
final AttributeDefinitionReferenceResolver attributeDefinitionReferenceResolver = new AttributeDefinitionReferenceResolver(syncOptions, productTypeService);
final NestedAttributeType expectedResolvedNestedAttributeType = NestedAttributeType.of(ProductType.referenceOfId("foo"));
final SetAttributeType expectedResolvedSetAttributeType = SetAttributeType.of(expectedResolvedNestedAttributeType);
final AttributeDefinitionDraft expectedResolvedAttrDef = AttributeDefinitionDraftBuilder.of(attributeDefinitionDraft).attributeType(expectedResolvedSetAttributeType).build();
// test and assertion
assertThat(attributeDefinitionReferenceResolver.resolveReferences(attributeDefinitionDraft)).isCompletedWithValue(expectedResolvedAttrDef);
}
Aggregations