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));
}
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));
}
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);
});
});
}
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);
}
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);
}
Aggregations