use of com.commercetools.sync.services.TypeService in project commercetools-sync-java by commercetools.
the class PriceReferenceResolverTest method resolveCustomTypeReference_WithExceptionOnCustomTypeFetch_ShouldNotResolveReferences.
@Test
void resolveCustomTypeReference_WithExceptionOnCustomTypeFetch_ShouldNotResolveReferences() {
// Preparation
final SphereClient ctpClient = mock(SphereClient.class);
final ProductSyncOptions productSyncOptions = ProductSyncOptionsBuilder.of(ctpClient).build();
final TypeService typeService = new TypeServiceImpl(productSyncOptions);
final CompletableFuture<PagedQueryResult<Type>> futureThrowingSphereException = new CompletableFuture<>();
futureThrowingSphereException.completeExceptionally(new SphereException("CTP error on fetch"));
when(ctpClient.execute(any(TypeQuery.class))).thenReturn(futureThrowingSphereException);
final String customTypeKey = "customTypeKey";
final PriceDraftBuilder priceBuilder = PriceDraftBuilder.of(MoneyImpl.of(BigDecimal.TEN, DefaultCurrencyUnits.EUR)).country(CountryCode.DE).custom(CustomFieldsDraft.ofTypeKeyAndJson(customTypeKey, new HashMap<>()));
final PriceReferenceResolver priceReferenceResolver = new PriceReferenceResolver(productSyncOptions, typeService, channelService, customerGroupService);
// Test and assertion
assertThat(priceReferenceResolver.resolveCustomTypeReference(priceBuilder)).failsWithin(1, TimeUnit.SECONDS).withThrowableOfType(ExecutionException.class).withCauseExactlyInstanceOf(SphereException.class).withMessageContaining("CTP error on fetch");
}
use of com.commercetools.sync.services.TypeService in project commercetools-sync-java by commercetools.
the class VariantReferenceResolverTest method setup.
@BeforeEach
void setup() {
final TypeService typeService = getMockTypeService();
final ChannelService channelService = getMockChannelService(getMockSupplyChannel(CHANNEL_ID, CHANNEL_KEY));
final ProductSyncOptions syncOptions = ProductSyncOptionsBuilder.of(mock(SphereClient.class)).build();
referenceResolver = new VariantReferenceResolver(syncOptions, typeService, channelService, mock(CustomerGroupService.class), getMockProductService(PRODUCT_ID), getMockProductTypeService(PRODUCT_TYPE_ID), getMockCategoryService(CATEGORY_ID), getMockCustomObjectService(CUSTOM_OBJECT_ID), getMockStateService(STATE_ID), getMockCustomerService(CUSTOMER_ID));
}
use of com.commercetools.sync.services.TypeService in project commercetools-sync-java by commercetools.
the class TypeSyncTest method sync_WithOnlyDraftsToCreate_ShouldCallBeforeCreateCallback.
@Test
void sync_WithOnlyDraftsToCreate_ShouldCallBeforeCreateCallback() {
// preparation
final TypeDraft newTypeDraft = TypeDraftBuilder.of("newType", ofEnglish("typeName"), ResourceTypeIdsSetBuilder.of().addChannels()).build();
final TypeSyncOptions typeSyncOptions = TypeSyncOptionsBuilder.of(mock(SphereClient.class)).build();
final TypeService typeService = mock(TypeService.class);
when(typeService.fetchMatchingTypesByKeys(anySet())).thenReturn(completedFuture(emptySet()));
when(typeService.createType(any())).thenReturn(completedFuture(Optional.empty()));
final TypeSyncOptions spyTypeSyncOptions = spy(typeSyncOptions);
// test
new TypeSync(spyTypeSyncOptions, typeService).sync(singletonList(newTypeDraft)).toCompletableFuture().join();
// assertion
verify(spyTypeSyncOptions).applyBeforeCreateCallback(newTypeDraft);
verify(spyTypeSyncOptions, never()).applyBeforeUpdateCallback(any(), any(), any());
}
use of com.commercetools.sync.services.TypeService in project commercetools-sync-java by commercetools.
the class TypeServiceImplIT method createType_WithValidType_ShouldCreateTypeAndCacheId.
@Test
void createType_WithValidType_ShouldCreateTypeAndCacheId() {
final TypeDraft newTypeDraft = TypeDraftBuilder.of(TYPE_KEY_1, TYPE_NAME_1, ResourceTypeIdsSetBuilder.of().addCategories().build()).description(TYPE_DESCRIPTION_1).fieldDefinitions(singletonList(FIELD_DEFINITION_1)).build();
final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
final TypeSyncOptions spyOptions = TypeSyncOptionsBuilder.of(spyClient).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).build();
final TypeService spyTypeService = new TypeServiceImpl(spyOptions);
// test
final Optional<Type> createdType = spyTypeService.createType(newTypeDraft).toCompletableFuture().join();
final Optional<Type> queriedOptional = CTP_TARGET_CLIENT.execute(TypeQuery.of().withPredicates(typeQueryModel -> typeQueryModel.key().is(TYPE_KEY_1))).toCompletableFuture().join().head();
assertThat(queriedOptional).hasValueSatisfying(queried -> assertThat(createdType).hasValueSatisfying(created -> {
assertThat(created.getKey()).isEqualTo(queried.getKey());
assertThat(created.getDescription()).isEqualTo(queried.getDescription());
assertThat(created.getName()).isEqualTo(queried.getName());
assertThat(created.getFieldDefinitions()).isEqualTo(queried.getFieldDefinitions());
}));
// Assert that the created type is cached
final Optional<String> typeId = spyTypeService.fetchCachedTypeId(TYPE_KEY_1).toCompletableFuture().join();
assertThat(typeId).isPresent();
verify(spyClient, times(0)).execute(any(TypeQuery.class));
}
use of com.commercetools.sync.services.TypeService in project commercetools-sync-java by commercetools.
the class InventorySyncTest method sync_WithFailOnCachingKeysToIds_ShouldTriggerErrorCallbackAndReturnProperStats.
@Test
void sync_WithFailOnCachingKeysToIds_ShouldTriggerErrorCallbackAndReturnProperStats() {
// preparation
final List<String> errorMessages = new ArrayList<>();
final List<Throwable> exceptions = new ArrayList<>();
final InventorySyncOptions inventorySyncOptions = InventorySyncOptionsBuilder.of(mock(SphereClient.class)).errorCallback((exception, oldResource, newResource, actions) -> {
errorMessages.add(exception.getMessage());
exceptions.add(exception.getCause());
}).build();
final TypeService typeService = spy(new TypeServiceImpl(inventorySyncOptions));
when(typeService.cacheKeysToIds(anySet())).thenReturn(supplyAsync(() -> {
throw new SphereException();
}));
final InventorySync inventorySync = new InventorySync(inventorySyncOptions, mock(InventoryService.class), mock(ChannelService.class), typeService);
final InventoryEntryDraft newInventoryDraftWithCustomType = mock(InventoryEntryDraft.class);
when(newInventoryDraftWithCustomType.getSku()).thenReturn("sku");
when(newInventoryDraftWithCustomType.getCustom()).thenReturn(CustomFieldsDraft.ofTypeKeyAndJson("typeKey", emptyMap()));
// test
final InventorySyncStatistics inventorySyncStatistics = inventorySync.sync(singletonList(newInventoryDraftWithCustomType)).toCompletableFuture().join();
// assertions
AssertionsForStatistics.assertThat(inventorySyncStatistics).hasValues(1, 0, 0, 1);
assertThat(errorMessages).hasSize(1).singleElement(as(STRING)).contains("Failed to build a cache of keys to ids.");
assertThat(exceptions).hasSize(1).singleElement(as(THROWABLE)).isExactlyInstanceOf(CompletionException.class).hasCauseExactlyInstanceOf(SphereException.class);
}
Aggregations