use of com.commercetools.sync.services.ProductTypeService in project commercetools-sync-java by commercetools.
the class ProductTypeServiceImplIT method createProductType_WithDuplicateKey_ShouldHaveEmptyOptionalAsAResult.
@Test
void createProductType_WithDuplicateKey_ShouldHaveEmptyOptionalAsAResult() {
// preparation
final ProductTypeDraft newProductTypeDraft = ProductTypeDraft.ofAttributeDefinitionDrafts(OLD_PRODUCT_TYPE_KEY, PRODUCT_TYPE_NAME_1, PRODUCT_TYPE_DESCRIPTION_1, singletonList(ATTRIBUTE_DEFINITION_DRAFT_1));
final ProductTypeSyncOptions options = ProductTypeSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).build();
final ProductTypeService service = new ProductTypeServiceImpl(options);
// test
final Optional<ProductType> result = service.createProductType(newProductTypeDraft).toCompletableFuture().join();
// assertion
assertThat(result).isEmpty();
assertThat(errorCallBackMessages).hasSize(1).singleElement(as(STRING)).contains("A duplicate value");
assertThat(errorCallBackExceptions).hasSize(1).singleElement().matches(exception -> {
assertThat(exception).isExactlyInstanceOf(ErrorResponseException.class);
final ErrorResponseException errorResponseException = (ErrorResponseException) exception;
final List<DuplicateFieldError> fieldErrors = errorResponseException.getErrors().stream().map(sphereError -> {
assertThat(sphereError.getCode()).isEqualTo(DuplicateFieldError.CODE);
return sphereError.as(DuplicateFieldError.class);
}).collect(toList());
return fieldErrors.size() == 1;
});
}
use of com.commercetools.sync.services.ProductTypeService in project commercetools-sync-java by commercetools.
the class ProductTypeServiceImplIT method fetchMatchingProductTypesByKeys_WithBadGateWayExceptionAlways_ShouldFail.
@Test
void fetchMatchingProductTypesByKeys_WithBadGateWayExceptionAlways_ShouldFail() {
// Mock sphere client to return BadGatewayException on any request.
final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
when(spyClient.execute(any(ProductTypeQuery.class))).thenReturn(CompletableFutureUtils.exceptionallyCompletedFuture(new BadGatewayException())).thenCallRealMethod();
final ProductTypeSyncOptions spyOptions = ProductTypeSyncOptionsBuilder.of(spyClient).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).build();
final ProductTypeService spyProductTypeService = new ProductTypeServiceImpl(spyOptions);
final Set<String> keys = new HashSet<>();
keys.add(OLD_PRODUCT_TYPE_KEY);
// test and assert
assertThat(errorCallBackExceptions).isEmpty();
assertThat(errorCallBackMessages).isEmpty();
assertThat(spyProductTypeService.fetchMatchingProductTypesByKeys(keys)).failsWithin(10, TimeUnit.SECONDS).withThrowableOfType(ExecutionException.class).withCauseExactlyInstanceOf(BadGatewayException.class);
}
use of com.commercetools.sync.services.ProductTypeService in project commercetools-sync-java by commercetools.
the class ProductTypeServiceImplIT method createProductType_WithValidProductType_ShouldCreateProductTypeAndCacheId.
@Test
void createProductType_WithValidProductType_ShouldCreateProductTypeAndCacheId() {
// preparation
final ProductTypeDraft newProductTypeDraft = ProductTypeDraft.ofAttributeDefinitionDrafts(PRODUCT_TYPE_KEY_1, PRODUCT_TYPE_NAME_1, PRODUCT_TYPE_DESCRIPTION_1, singletonList(ATTRIBUTE_DEFINITION_DRAFT_1));
final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
final ProductTypeSyncOptions spyOptions = ProductTypeSyncOptionsBuilder.of(spyClient).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).build();
final ProductTypeService spyProductTypeService = new ProductTypeServiceImpl(spyOptions);
// test
final Optional<ProductType> createdOptional = spyProductTypeService.createProductType(newProductTypeDraft).toCompletableFuture().join();
// assertion
final Optional<ProductType> queriedOptional = CTP_TARGET_CLIENT.execute(ProductTypeQuery.of().withPredicates(productTypeQueryModel -> productTypeQueryModel.key().is(PRODUCT_TYPE_KEY_1))).toCompletableFuture().join().head();
assertThat(queriedOptional).hasValueSatisfying(queried -> assertThat(createdOptional).hasValueSatisfying(created -> {
assertThat(created.getKey()).isEqualTo(queried.getKey());
assertThat(created.getDescription()).isEqualTo(queried.getDescription());
assertThat(created.getName()).isEqualTo(queried.getName());
assertThat(created.getAttributes()).isEqualTo(queried.getAttributes());
}));
// Assert that the created product type is cached
final Optional<String> productTypeId = spyProductTypeService.fetchCachedProductTypeId(PRODUCT_TYPE_KEY_1).toCompletableFuture().join();
assertThat(productTypeId).isPresent();
verify(spyClient, times(0)).execute(any(ProductTypeQuery.class));
}
use of com.commercetools.sync.services.ProductTypeService in project commercetools-sync-java by commercetools.
the class ProductTypeServiceImplIT method createProductType_WithInvalidProductType_ShouldHaveEmptyOptionalAsAResult.
@Test
void createProductType_WithInvalidProductType_ShouldHaveEmptyOptionalAsAResult() {
// preparation
final ProductTypeDraft newProductTypeDraft = ProductTypeDraft.ofAttributeDefinitionDrafts("", PRODUCT_TYPE_NAME_1, PRODUCT_TYPE_DESCRIPTION_1, singletonList(ATTRIBUTE_DEFINITION_DRAFT_1));
final ProductTypeSyncOptions options = ProductTypeSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).build();
final ProductTypeService service = new ProductTypeServiceImpl(options);
// test
final Optional<ProductType> result = service.createProductType(newProductTypeDraft).toCompletableFuture().join();
// assertion
assertThat(result).isEmpty();
assertThat(errorCallBackMessages).containsExactly("Failed to create draft with key: ''. Reason: Draft key is blank!");
}
use of com.commercetools.sync.services.ProductTypeService in project commercetools-sync-java by commercetools.
the class ProductTypeSyncTest method sync_WithOnlyDraftsToCreate_ShouldCallBeforeCreateCallback.
@Test
void sync_WithOnlyDraftsToCreate_ShouldCallBeforeCreateCallback() {
// 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 ProductTypeService productTypeService = new ProductTypeServiceImpl(productTypeSyncOptions);
when(sphereClient.execute(any(ProductTypeQuery.class))).thenReturn(completedFuture(PagedQueryResult.empty()));
final ProductType createdProductType = mock(ProductType.class);
when(createdProductType.getKey()).thenReturn(newProductTypeDraft.getKey());
when(createdProductType.getId()).thenReturn(UUID.randomUUID().toString());
when(sphereClient.execute(any(ProductTypeCreateCommand.class))).thenReturn(completedFuture(createdProductType));
final ProductTypeSyncOptions spyProductTypeSyncOptions = spy(productTypeSyncOptions);
// test
new ProductTypeSync(spyProductTypeSyncOptions, productTypeService).sync(singletonList(newProductTypeDraft)).toCompletableFuture().join();
// assertion
verify(spyProductTypeSyncOptions).applyBeforeCreateCallback(newProductTypeDraft);
verify(spyProductTypeSyncOptions, never()).applyBeforeUpdateCallback(any(), any(), any());
}
Aggregations