Search in sources :

Example 6 with CustomObjectCompositeIdentifier

use of com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier in project commercetools-sync-java by commercetools.

the class BaseServiceImplTest method cacheKeysToIds_With500CustomObjectIdentifiers_ShouldChunkAndMakeRequestAndReturnCacheEntries.

@Test
void cacheKeysToIds_With500CustomObjectIdentifiers_ShouldChunkAndMakeRequestAndReturnCacheEntries() {
    // preparation
    Set<CustomObjectCompositeIdentifier> randomIdentifiers = new HashSet<>();
    IntStream.range(0, 500).forEach(i -> randomIdentifiers.add(CustomObjectCompositeIdentifier.of("customObjectId" + i, "customObjectContainer" + i)));
    final CustomObjectSyncOptions customObjectSyncOptions = CustomObjectSyncOptionsBuilder.of(client).build();
    final CustomObjectServiceImpl serviceImpl = new CustomObjectServiceImpl(customObjectSyncOptions);
    final PagedQueryResult pagedQueryResult = mock(PagedQueryResult.class);
    final CustomObject customObject = mock(CustomObject.class);
    final String customObjectId = randomIdentifiers.stream().findFirst().get().getKey();
    final String customObjectContainer = randomIdentifiers.stream().findFirst().get().getContainer();
    final String customObjectKey = "customObjectKey";
    when(customObject.getId()).thenReturn(customObjectId);
    when(customObject.getKey()).thenReturn(customObjectKey);
    when(customObject.getContainer()).thenReturn(customObjectContainer);
    when(pagedQueryResult.getResults()).thenReturn(singletonList(customObject));
    when(client.execute(any())).thenReturn(completedFuture(pagedQueryResult));
    // test
    serviceImpl.cacheKeysToIds(randomIdentifiers).toCompletableFuture().join();
    // assertion
    verify(client, times(2)).execute(any(CustomObjectQuery.class));
}
Also used : CustomObjectSyncOptions(com.commercetools.sync.customobjects.CustomObjectSyncOptions) PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) CustomObject(io.sphere.sdk.customobjects.CustomObject) CustomObjectCompositeIdentifier(com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier) HashSet(java.util.HashSet) CustomObjectQuery(io.sphere.sdk.customobjects.queries.CustomObjectQuery) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 7 with CustomObjectCompositeIdentifier

use of com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier in project commercetools-sync-java by commercetools.

the class CustomObjectServiceImplTest method fetchMatchingCustomObjects_WithKeySet_ShouldFetchCustomObjects.

@Test
void fetchMatchingCustomObjects_WithKeySet_ShouldFetchCustomObjects() {
    final String key1 = RandomStringUtils.random(15, true, true);
    final String key2 = RandomStringUtils.random(15, true, true);
    final String container1 = RandomStringUtils.random(15, true, true);
    final String container2 = RandomStringUtils.random(15, true, true);
    final Set<CustomObjectCompositeIdentifier> customObjectCompositeIdentifiers = new HashSet<>();
    customObjectCompositeIdentifiers.add(CustomObjectCompositeIdentifier.of(key1, container1));
    customObjectCompositeIdentifiers.add(CustomObjectCompositeIdentifier.of(key2, container2));
    final CustomObject mock1 = mock(CustomObject.class);
    when(mock1.getId()).thenReturn(RandomStringUtils.random(15));
    when(mock1.getKey()).thenReturn(key1);
    when(mock1.getContainer()).thenReturn(container1);
    final CustomObject mock2 = mock(CustomObject.class);
    when(mock2.getId()).thenReturn(RandomStringUtils.random(15));
    when(mock2.getKey()).thenReturn(key2);
    when(mock2.getContainer()).thenReturn(container2);
    final CustomObjectPagedQueryResult result = mock(CustomObjectPagedQueryResult.class);
    when(result.getResults()).thenReturn(Arrays.asList(mock1, mock2));
    when(client.execute(any())).thenReturn(CompletableFuture.completedFuture(result));
    final Set<CustomObject<JsonNode>> customObjects = service.fetchMatchingCustomObjects(customObjectCompositeIdentifiers).toCompletableFuture().join();
    List<CustomObjectCompositeIdentifier> customObjectCompositeIdlist = new ArrayList<CustomObjectCompositeIdentifier>(customObjectCompositeIdentifiers);
    assertAll(() -> assertThat(customObjects).contains(mock1, mock2), () -> assertThat(service.keyToIdCache.asMap()).containsKeys(String.valueOf(customObjectCompositeIdlist.get(0)), String.valueOf(customObjectCompositeIdlist.get(1))));
    verify(client).execute(any(CustomObjectQuery.class));
}
Also used : CustomObject(io.sphere.sdk.customobjects.CustomObject) ArrayList(java.util.ArrayList) CustomObjectCompositeIdentifier(com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier) HashSet(java.util.HashSet) CustomObjectQuery(io.sphere.sdk.customobjects.queries.CustomObjectQuery) Test(org.junit.jupiter.api.Test)

Example 8 with CustomObjectCompositeIdentifier

use of com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier in project commercetools-sync-java by commercetools.

the class CustomObjectSync method fetchAndUpdate.

@Nonnull
private CompletionStage<Optional<CustomObject<JsonNode>>> fetchAndUpdate(@Nonnull final CustomObject<JsonNode> oldCustomObject, @Nonnull final CustomObjectDraft<JsonNode> customObjectDraft) {
    final CustomObjectCompositeIdentifier identifier = CustomObjectCompositeIdentifier.of(oldCustomObject);
    return customObjectService.fetchCustomObject(identifier).handle(ImmutablePair::new).thenCompose(fetchedResponseEntry -> {
        final Optional<CustomObject<JsonNode>> fetchedCustomObjectOptional = fetchedResponseEntry.getKey();
        final Throwable exception = fetchedResponseEntry.getValue();
        if (exception != null) {
            final String errorMessage = format(CTP_CUSTOM_OBJECT_UPDATE_FAILED, identifier.toString(), "Failed to fetch from CTP while retrying after concurrency modification.");
            handleError(errorMessage, exception, 1, oldCustomObject, customObjectDraft);
            return CompletableFuture.completedFuture(Optional.empty());
        }
        return fetchedCustomObjectOptional.map(fetchedCustomObject -> updateCustomObject(fetchedCustomObject, customObjectDraft)).orElseGet(() -> {
            final String errorMessage = format(CTP_CUSTOM_OBJECT_UPDATE_FAILED, identifier.toString(), "Not found when attempting to fetch while retrying " + "after concurrency modification.");
            handleError(errorMessage, null, 1, oldCustomObject, customObjectDraft);
            return CompletableFuture.completedFuture(null);
        });
    });
}
Also used : SyncException(com.commercetools.sync.commons.exceptions.SyncException) BaseSync(com.commercetools.sync.commons.BaseSync) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) CustomObject(io.sphere.sdk.customobjects.CustomObject) CompletableFuture(java.util.concurrent.CompletableFuture) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) SyncUtils.batchElements(com.commercetools.sync.commons.utils.SyncUtils.batchElements) CustomObjectCompositeIdentifier(com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) CustomObjectSyncUtils(com.commercetools.sync.customobjects.utils.CustomObjectSyncUtils) CustomObjectDraft(io.sphere.sdk.customobjects.CustomObjectDraft) Optional.ofNullable(java.util.Optional.ofNullable) CustomObjectSyncStatistics(com.commercetools.sync.customobjects.helpers.CustomObjectSyncStatistics) Set(java.util.Set) String.format(java.lang.String.format) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) Function.identity(java.util.function.Function.identity) CustomObjectBatchValidator(com.commercetools.sync.customobjects.helpers.CustomObjectBatchValidator) Optional(java.util.Optional) CustomObjectServiceImpl(com.commercetools.sync.services.impl.CustomObjectServiceImpl) CustomObjectService(com.commercetools.sync.services.CustomObjectService) CustomObject(io.sphere.sdk.customobjects.CustomObject) CustomObjectCompositeIdentifier(com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier) Nonnull(javax.annotation.Nonnull)

Example 9 with CustomObjectCompositeIdentifier

use of com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier in project commercetools-sync-java by commercetools.

the class WithCustomObjectReferencesTest method resolveReferences_WithNonExistingCustomObjectReferenceAttribute_ShouldNotResolveReferences.

@Test
void resolveReferences_WithNonExistingCustomObjectReferenceAttribute_ShouldNotResolveReferences() {
    // preparation
    final CustomObjectCompositeIdentifier nonExistingCustomObjectId = CustomObjectCompositeIdentifier.of("non-existing-key", "non-existing-container");
    when(customObjectService.fetchCachedCustomObjectId(nonExistingCustomObjectId)).thenReturn(CompletableFuture.completedFuture(Optional.empty()));
    final ObjectNode attributeValue = createReferenceObject("non-existing-container|non-existing-key", CustomObject.referenceTypeId());
    final AttributeDraft attributeDraft = AttributeDraft.of("attributeName", attributeValue);
    final ProductVariantDraft productVariantDraft = ProductVariantDraftBuilder.of().attributes(attributeDraft).build();
    // test
    final ProductVariantDraft resolvedAttributeDraft = referenceResolver.resolveReferences(productVariantDraft).toCompletableFuture().join();
    // assertions
    assertThat(resolvedAttributeDraft).isEqualTo(productVariantDraft);
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ProductSyncMockUtils.getReferenceSetAttributeDraft(com.commercetools.sync.products.ProductSyncMockUtils.getReferenceSetAttributeDraft) AttributeDraft(io.sphere.sdk.products.attributes.AttributeDraft) ProductVariantDraft(io.sphere.sdk.products.ProductVariantDraft) CustomObjectCompositeIdentifier(com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier) Test(org.junit.jupiter.api.Test)

Example 10 with CustomObjectCompositeIdentifier

use of com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier in project commercetools-sync-java by commercetools.

the class ProductBatchValidatorTest method validateAndCollectReferencedKeys_WithKeyValueDocumentAttDrafts_ShouldNotHaveKeyValidationError.

@Test
void validateAndCollectReferencedKeys_WithKeyValueDocumentAttDrafts_ShouldNotHaveKeyValidationError() {
    final String uuid = UUID.randomUUID().toString();
    final String validIdentifier = "container|key";
    final String invalidIdentifier = "container-key";
    final AttributeDraft referenceSetAttributeDraft = getReferenceSetAttributeDraft("foo", createReferenceObject(validIdentifier, CustomObject.referenceTypeId()), createReferenceObject(uuid, CustomObject.referenceTypeId()), createReferenceObject(invalidIdentifier, CustomObject.referenceTypeId()));
    final List<AttributeDraft> attributes = asList(referenceSetAttributeDraft);
    final ProductVariantDraft validVariantDraft = ProductVariantDraftBuilder.of().key("variantKey").sku("variantSku").attributes(attributes).build();
    final ProductDraft validProductDraft = mock(ProductDraft.class);
    when(validProductDraft.getKey()).thenReturn("validProductDraft");
    when(validProductDraft.getMasterVariant()).thenReturn(validVariantDraft);
    final ProductBatchValidator productBatchValidator = new ProductBatchValidator(syncOptions, syncStatistics);
    final ImmutablePair<Set<ProductDraft>, ProductBatchValidator.ReferencedKeys> pair = productBatchValidator.validateAndCollectReferencedKeys(singletonList(validProductDraft));
    assertThat(pair.getLeft()).hasSize(1);
    assertThat(pair.getLeft()).containsExactly(validProductDraft);
    Set<CustomObjectCompositeIdentifier> identifiers = pair.getRight().getCustomObjectCompositeIdentifiers();
    assertThat(identifiers).containsExactlyInAnyOrder(CustomObjectCompositeIdentifier.of(validIdentifier));
    assertThat(errorCallBackMessages).hasSize(0);
}
Also used : Set(java.util.Set) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductSyncMockUtils.getReferenceSetAttributeDraft(com.commercetools.sync.products.ProductSyncMockUtils.getReferenceSetAttributeDraft) AttributeDraft(io.sphere.sdk.products.attributes.AttributeDraft) ProductVariantDraft(io.sphere.sdk.products.ProductVariantDraft) CustomObjectCompositeIdentifier(com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier) Test(org.junit.jupiter.api.Test)

Aggregations

CustomObjectCompositeIdentifier (com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier)11 Test (org.junit.jupiter.api.Test)9 CustomObject (io.sphere.sdk.customobjects.CustomObject)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 ProductVariantDraft (io.sphere.sdk.products.ProductVariantDraft)5 CustomObjectService (com.commercetools.sync.services.CustomObjectService)4 Optional (java.util.Optional)4 ProductSyncMockUtils.getReferenceSetAttributeDraft (com.commercetools.sync.products.ProductSyncMockUtils.getReferenceSetAttributeDraft)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 SphereClient (io.sphere.sdk.client.SphereClient)3 CustomObjectQuery (io.sphere.sdk.customobjects.queries.CustomObjectQuery)3 HashSet (java.util.HashSet)3 Map (java.util.Map)3 Set (java.util.Set)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 CustomObjectSyncOptions (com.commercetools.sync.customobjects.CustomObjectSyncOptions)2 ProductSyncMockUtils.createReferenceObject (com.commercetools.sync.products.ProductSyncMockUtils.createReferenceObject)2 ProductSyncMockUtils.getMockCustomObjectService (com.commercetools.sync.products.ProductSyncMockUtils.getMockCustomObjectService)2 ProductSyncOptions (com.commercetools.sync.products.ProductSyncOptions)2 ProductSyncOptionsBuilder (com.commercetools.sync.products.ProductSyncOptionsBuilder)2