Search in sources :

Example 1 with WaitingToBeResolvedProducts

use of com.commercetools.sync.commons.models.WaitingToBeResolvedProducts in project commercetools-sync-java by commercetools.

the class UnresolvedReferencesServiceImplIT method saveFetchAndDelete_WithKeyWithSpecialCharacter_shouldWorkCorrectly.

@Test
void saveFetchAndDelete_WithKeyWithSpecialCharacter_shouldWorkCorrectly() {
    // preparation
    final ProductDraft productDraft = SphereJsonUtils.readObjectFromResource(PRODUCT_KEY_SPECIAL_CHARS_RESOURCE_PATH, ProductDraft.class);
    final WaitingToBeResolvedProducts productDraftWithUnresolvedRefs = new WaitingToBeResolvedProducts(productDraft, asSet("foo", "bar"));
    // test
    final Optional<WaitingToBeResolvedProducts> result = unresolvedReferencesService.save(productDraftWithUnresolvedRefs, CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY, WaitingToBeResolvedProducts.class).toCompletableFuture().join();
    // assertions
    assertThat(result).hasValueSatisfying(waitingToBeResolved -> assertThat(waitingToBeResolved.getProductDraft()).isEqualTo(productDraft));
    // test
    final CustomObjectByKeyGet<WaitingToBeResolvedProducts> customObjectByKeyGet = CustomObjectByKeyGet.of(CUSTOM_OBJECT_CONTAINER_KEY, sha1Hex(productDraft.getKey()), WaitingToBeResolvedProducts.class);
    final CustomObject<WaitingToBeResolvedProducts> createdCustomObject = CTP_TARGET_CLIENT.execute(customObjectByKeyGet).toCompletableFuture().join();
    // assertions
    assertThat(createdCustomObject.getKey()).isEqualTo(sha1Hex(productDraft.getKey()));
    // test
    final Set<WaitingToBeResolvedProducts> waitingDrafts = unresolvedReferencesService.fetch(singleton(productDraft.getKey()), CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY, WaitingToBeResolvedProducts.class).toCompletableFuture().join();
    // assertions
    assertThat(waitingDrafts).containsExactly(productDraftWithUnresolvedRefs);
    // test
    final Optional<WaitingToBeResolvedProducts> deletionResult = unresolvedReferencesService.delete(productDraft.getKey(), CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY, WaitingToBeResolvedProducts.class).toCompletableFuture().join();
    // assertions
    assertThat(deletionResult).hasValueSatisfying(waitingToBeResolved -> assertThat(waitingToBeResolved.getProductDraft()).isEqualTo(productDraft));
    assertThat(errorCallBackMessages).isEmpty();
    assertThat(warningCallBackMessages).isEmpty();
    assertThat(errorCallBackExceptions).isEmpty();
}
Also used : ProductDraft(io.sphere.sdk.products.ProductDraft) WaitingToBeResolvedProducts(com.commercetools.sync.commons.models.WaitingToBeResolvedProducts) Test(org.junit.jupiter.api.Test)

Example 2 with WaitingToBeResolvedProducts

use of com.commercetools.sync.commons.models.WaitingToBeResolvedProducts in project commercetools-sync-java by commercetools.

the class UnresolvedReferencesServiceImplTest method delete_OnSuccess_ShouldMakeRequestWithSha1HashedKey.

@SuppressWarnings("unchecked")
@Test
void delete_OnSuccess_ShouldMakeRequestWithSha1HashedKey() {
    // preparation
    final CustomObject customObjectMock = mock(CustomObject.class);
    final ProductDraft productDraftMock = mock(ProductDraft.class);
    when(productDraftMock.getKey()).thenReturn("product-draft-key");
    final WaitingToBeResolvedProducts waitingDraft = new WaitingToBeResolvedProducts(productDraftMock, singleton("test-ref"));
    when(customObjectMock.getValue()).thenReturn(waitingDraft);
    when(productSyncOptions.getCtpClient().execute(any(CustomObjectDeleteCommand.class))).thenReturn(completedFuture(customObjectMock));
    final ArgumentCaptor<CustomObjectDeleteCommand<WaitingToBeResolved>> requestArgumentCaptor = ArgumentCaptor.forClass(CustomObjectDeleteCommand.class);
    // test
    final Optional<WaitingToBeResolvedProducts> toBeResolvedOptional = service.delete("product-draft-key", CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY, WaitingToBeResolvedProducts.class).toCompletableFuture().join();
    // assertions
    verify(productSyncOptions.getCtpClient()).execute(requestArgumentCaptor.capture());
    assertThat(toBeResolvedOptional).contains(waitingDraft);
    final CustomObjectDeleteCommand<WaitingToBeResolved> value = requestArgumentCaptor.getValue();
    assertThat(value.httpRequestIntent().getPath()).contains(sha1Hex(productDraftMock.getKey()));
}
Also used : CustomObject(io.sphere.sdk.customobjects.CustomObject) ProductDraft(io.sphere.sdk.products.ProductDraft) WaitingToBeResolvedProducts(com.commercetools.sync.commons.models.WaitingToBeResolvedProducts) WaitingToBeResolved(com.commercetools.sync.commons.models.WaitingToBeResolved) CustomObjectDeleteCommand(io.sphere.sdk.customobjects.commands.CustomObjectDeleteCommand) Test(org.junit.jupiter.api.Test)

Example 3 with WaitingToBeResolvedProducts

use of com.commercetools.sync.commons.models.WaitingToBeResolvedProducts in project commercetools-sync-java by commercetools.

the class UnresolvedReferencesServiceImplTest method save_OnSuccess_ShouldSaveMock.

@Test
void save_OnSuccess_ShouldSaveMock() {
    // preparation
    final CustomObject customObjectMock = mock(CustomObject.class);
    final ProductDraft productDraftMock = mock(ProductDraft.class);
    when(productDraftMock.getKey()).thenReturn("product-draft-key");
    final WaitingToBeResolvedProducts waitingToBeResolved = new WaitingToBeResolvedProducts(productDraftMock, singleton("test-ref"));
    when(customObjectMock.getValue()).thenReturn(waitingToBeResolved);
    when(productSyncOptions.getCtpClient().execute(any())).thenReturn(completedFuture(customObjectMock));
    // test
    final Optional<WaitingToBeResolvedProducts> result = service.save(waitingToBeResolved, CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY, WaitingToBeResolvedProducts.class).toCompletableFuture().join();
    // assertions
    assertThat(result).contains(waitingToBeResolved);
}
Also used : CustomObject(io.sphere.sdk.customobjects.CustomObject) ProductDraft(io.sphere.sdk.products.ProductDraft) WaitingToBeResolvedProducts(com.commercetools.sync.commons.models.WaitingToBeResolvedProducts) Test(org.junit.jupiter.api.Test)

Example 4 with WaitingToBeResolvedProducts

use of com.commercetools.sync.commons.models.WaitingToBeResolvedProducts in project commercetools-sync-java by commercetools.

the class UnresolvedReferencesServiceImplTest method save_OnSuccess_ShouldSaveMockWithSha1HashedKey.

@SuppressWarnings("unchecked")
@Test
void save_OnSuccess_ShouldSaveMockWithSha1HashedKey() {
    // preparation
    final CustomObject customObjectMock = mock(CustomObject.class);
    final ProductDraft productDraftMock = mock(ProductDraft.class);
    when(productDraftMock.getKey()).thenReturn("product-draft-key");
    final WaitingToBeResolvedProducts waitingToBeResolved = new WaitingToBeResolvedProducts(productDraftMock, singleton("test-ref"));
    when(customObjectMock.getValue()).thenReturn(waitingToBeResolved);
    when(productSyncOptions.getCtpClient().execute(any())).thenReturn(completedFuture(customObjectMock));
    final ArgumentCaptor<CustomObjectUpsertCommand<WaitingToBeResolved>> requestArgumentCaptor = ArgumentCaptor.forClass(CustomObjectUpsertCommand.class);
    // test
    final Optional<WaitingToBeResolvedProducts> result = service.save(waitingToBeResolved, CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY, WaitingToBeResolvedProducts.class).toCompletableFuture().join();
    // assertions
    verify(productSyncOptions.getCtpClient()).execute(requestArgumentCaptor.capture());
    assertThat(result).contains(waitingToBeResolved);
    assertThat(requestArgumentCaptor.getValue().getDraft().getKey()).isEqualTo(sha1Hex(productDraftMock.getKey()));
}
Also used : CustomObjectUpsertCommand(io.sphere.sdk.customobjects.commands.CustomObjectUpsertCommand) CustomObject(io.sphere.sdk.customobjects.CustomObject) ProductDraft(io.sphere.sdk.products.ProductDraft) WaitingToBeResolvedProducts(com.commercetools.sync.commons.models.WaitingToBeResolvedProducts) Test(org.junit.jupiter.api.Test)

Example 5 with WaitingToBeResolvedProducts

use of com.commercetools.sync.commons.models.WaitingToBeResolvedProducts in project commercetools-sync-java by commercetools.

the class ProductSyncWithReferencedProductsIT method sync_withProductReferenceSetContainingANonExistingReference_shouldFailCreatingTheProduct.

@Test
void sync_withProductReferenceSetContainingANonExistingReference_shouldFailCreatingTheProduct() {
    // preparation
    final AttributeDraft productReferenceAttribute = AttributeDraft.of("product-reference", Reference.of(Product.referenceTypeId(), product.getKey()));
    final HashSet<Reference<Product>> references = new HashSet<>();
    references.add(Reference.of(Product.referenceTypeId(), "nonExistingKey"));
    references.add(Reference.of(Product.referenceTypeId(), product2.getKey()));
    final AttributeDraft productReferenceSetAttribute = AttributeDraft.of("product-reference-set", references);
    final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of().sku("sku").key("new-product-master-variant").attributes(productReferenceAttribute, productReferenceSetAttribute).build();
    final ProductDraft productDraftWithProductReference = ProductDraftBuilder.of(productType, ofEnglish("productName"), ofEnglish("productSlug"), masterVariant).key("new-product").build();
    // test
    final ProductSync productSync = new ProductSync(syncOptions);
    final ProductSyncStatistics syncStatistics = productSync.sync(singletonList(productDraftWithProductReference)).toCompletableFuture().join();
    // assertion
    assertThat(syncStatistics).hasValues(1, 0, 0, 0, 1);
    assertThat(errorCallBackExceptions).isEmpty();
    assertThat(errorCallBackMessages).isEmpty();
    assertThat(warningCallBackMessages).isEmpty();
    assertThat(actions).isEmpty();
    final UnresolvedReferencesServiceImpl<WaitingToBeResolvedProducts> unresolvedReferencesService = new UnresolvedReferencesServiceImpl<>(syncOptions);
    final Set<WaitingToBeResolvedProducts> waitingToBeResolvedDrafts = unresolvedReferencesService.fetch(asSet(productDraftWithProductReference.getKey()), CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY, WaitingToBeResolvedProducts.class).toCompletableFuture().join();
    assertThat(waitingToBeResolvedDrafts).singleElement().matches(waitingToBeResolvedDraft -> {
        assertThat(waitingToBeResolvedDraft.getProductDraft().getKey()).isEqualTo(productDraftWithProductReference.getKey());
        assertThat(waitingToBeResolvedDraft.getMissingReferencedProductKeys()).containsExactly("nonExistingKey");
        return true;
    });
}
Also used : ProductSyncStatistics(com.commercetools.sync.products.helpers.ProductSyncStatistics) UnresolvedReferencesServiceImpl(com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl) ProductDraft(io.sphere.sdk.products.ProductDraft) AttributeDraft(io.sphere.sdk.products.attributes.AttributeDraft) Reference(io.sphere.sdk.models.Reference) WaitingToBeResolvedProducts(com.commercetools.sync.commons.models.WaitingToBeResolvedProducts) ProductVariantDraft(io.sphere.sdk.products.ProductVariantDraft) ProductSync(com.commercetools.sync.products.ProductSync) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

WaitingToBeResolvedProducts (com.commercetools.sync.commons.models.WaitingToBeResolvedProducts)19 ProductDraft (io.sphere.sdk.products.ProductDraft)19 Test (org.junit.jupiter.api.Test)18 UnresolvedReferencesServiceImpl (com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl)8 ProductSyncStatistics (com.commercetools.sync.products.helpers.ProductSyncStatistics)7 CustomObject (io.sphere.sdk.customobjects.CustomObject)7 ProductSync (com.commercetools.sync.products.ProductSync)6 AttributeDraft (io.sphere.sdk.products.attributes.AttributeDraft)6 SyncException (com.commercetools.sync.commons.exceptions.SyncException)4 ProductVariantDraft (io.sphere.sdk.products.ProductVariantDraft)4 UnresolvedReferencesService (com.commercetools.sync.services.UnresolvedReferencesService)3 CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY (com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl.CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 CustomObjectQuery (io.sphere.sdk.customobjects.queries.CustomObjectQuery)3 Reference (io.sphere.sdk.models.Reference)3 Product (io.sphere.sdk.products.Product)3 PagedQueryResult (io.sphere.sdk.queries.PagedQueryResult)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Set (java.util.Set)3