use of com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier in project commercetools-sync-java by commercetools.
the class CustomObjectServiceImplIT method fetchCachedCustomObjectId_WithNonExistingCustomObject_ShouldReturnEmptyCustomObject.
@Test
void fetchCachedCustomObjectId_WithNonExistingCustomObject_ShouldReturnEmptyCustomObject() {
CustomObjectCompositeIdentifier compositeIdentifier = CustomObjectCompositeIdentifier.of("non-existing-key", "non-existing-container");
final Optional<String> customObject = customObjectService.fetchCachedCustomObjectId(compositeIdentifier).toCompletableFuture().join();
assertThat(customObject).isEmpty();
assertThat(errorCallBackExceptions).isEmpty();
assertThat(errorCallBackMessages).isEmpty();
}
use of com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier in project commercetools-sync-java by commercetools.
the class WithCustomObjectReferencesTest method resolveReferences_WithSomeNonExistingNestedCustomObjReferenceAttrs_ShouldOnlyResolveExistingReferences.
@Test
void resolveReferences_WithSomeNonExistingNestedCustomObjReferenceAttrs_ShouldOnlyResolveExistingReferences() {
// preparation
final CustomObjectCompositeIdentifier nonExistingCustomObject1Id = CustomObjectCompositeIdentifier.of("non-existing-key-1", "non-existing-container");
when(customObjectService.fetchCachedCustomObjectId(nonExistingCustomObject1Id)).thenReturn(CompletableFuture.completedFuture(Optional.empty()));
final CustomObjectCompositeIdentifier nonExistingCustomObject3Id = CustomObjectCompositeIdentifier.of("non-existing-key-3", "non-existing-container");
when(customObjectService.fetchCachedCustomObjectId(nonExistingCustomObject3Id)).thenReturn(CompletableFuture.completedFuture(Optional.empty()));
final ProductVariantDraft withSomeNonExistingNestedCustomObjReferenceAttributes = readObjectFromResource(NESTED_ATTRIBUTE_WITH_SOME_NOT_EXISTING_CUSTOM_OBJECT_REFERENCE_ATTRIBUTES, ProductVariantDraft.class);
// test
final ProductVariantDraft resolvedAttributeDraft = referenceResolver.resolveReferences(withSomeNonExistingNestedCustomObjReferenceAttributes).toCompletableFuture().join();
// assertions
assertThat(resolvedAttributeDraft.getAttributes()).isNotNull();
final JsonNode value = resolvedAttributeDraft.getAttributes().get(0).getValue();
assertThat(value).isInstanceOf(ArrayNode.class);
final ArrayNode resolvedNestedAttributes = (ArrayNode) value;
final Map<String, JsonNode> resolvedNestedAttributesMap = StreamSupport.stream(resolvedNestedAttributes.spliterator(), false).collect(Collectors.toMap(jsonNode -> jsonNode.get("name").asText(), jsonNode -> jsonNode));
assertReferenceSetAttributeValue(resolvedNestedAttributesMap, "nested-attribute-1-name", createReferenceObject("non-existing-container|non-existing-key-1", CustomObject.referenceTypeId()), createReferenceObject(CUSTOM_OBJECT_ID, CustomObject.referenceTypeId()));
assertReferenceAttributeValue(resolvedNestedAttributesMap, "nested-attribute-2-name", CUSTOM_OBJECT_ID, CustomObject.referenceTypeId());
assertReferenceAttributeValue(resolvedNestedAttributesMap, "nested-attribute-3-name", "non-existing-container|non-existing-key-3", CustomObject.referenceTypeId());
}
use of com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier in project commercetools-sync-java by commercetools.
the class WithCustomObjectReferencesTest method resolveReferences_WithSetOfNestedProdTypeReferenceSetOfSomeNonExisting_ShouldOnlyResolveExistingReferences.
@Test
void resolveReferences_WithSetOfNestedProdTypeReferenceSetOfSomeNonExisting_ShouldOnlyResolveExistingReferences() {
// preparation
final CustomObjectCompositeIdentifier nonExistingCustomObject1Id = CustomObjectCompositeIdentifier.of("non-existing-key-1", "non-existing-container");
when(customObjectService.fetchCachedCustomObjectId(nonExistingCustomObject1Id)).thenReturn(CompletableFuture.completedFuture(Optional.empty()));
final CustomObjectCompositeIdentifier nonExistingCustomObject3Id = CustomObjectCompositeIdentifier.of("non-existing-key-3", "non-existing-container");
when(customObjectService.fetchCachedCustomObjectId(nonExistingCustomObject3Id)).thenReturn(CompletableFuture.completedFuture(Optional.empty()));
final ProductVariantDraft withSetOfNestedCustomObjectReferenceSetWithSomeNonExisting = readObjectFromResource(SET_OF_NESTED_ATTRIBUTE_WITH_SOME_NOT_EXISTING_CUSTOM_OBJECT_REFERENCE_ATTRIBUTES, ProductVariantDraft.class);
// test
final ProductVariantDraft resolvedAttributeDraft = referenceResolver.resolveReferences(withSetOfNestedCustomObjectReferenceSetWithSomeNonExisting).toCompletableFuture().join();
// assertions
assertThat(resolvedAttributeDraft.getAttributes()).isNotNull();
final JsonNode value = resolvedAttributeDraft.getAttributes().get(0).getValue();
assertThat(value).isInstanceOf(ArrayNode.class);
final ArrayNode setOfResolvedNestedAttributes = (ArrayNode) value;
final JsonNode resolvedNestedAttribute = setOfResolvedNestedAttributes.get(0);
assertThat(resolvedNestedAttribute).isInstanceOf(ArrayNode.class);
final ArrayNode resolvedNestedAttributeAsArray = (ArrayNode) resolvedNestedAttribute;
final Map<String, JsonNode> resolvedNestedAttributesMap = StreamSupport.stream(resolvedNestedAttributeAsArray.spliterator(), false).collect(Collectors.toMap(jsonNode -> jsonNode.get("name").asText(), jsonNode -> jsonNode));
assertReferenceSetAttributeValue(resolvedNestedAttributesMap, "nested-attribute-1-name", createReferenceObject("non-existing-container|non-existing-key-1", CustomObject.referenceTypeId()), createReferenceObject(CUSTOM_OBJECT_ID, CustomObject.referenceTypeId()));
assertReferenceAttributeValue(resolvedNestedAttributesMap, "nested-attribute-2-name", CUSTOM_OBJECT_ID, CustomObject.referenceTypeId());
assertReferenceAttributeValue(resolvedNestedAttributesMap, "nested-attribute-3-name", "non-existing-container|non-existing-key-3", CustomObject.referenceTypeId());
}
use of com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier in project commercetools-sync-java by commercetools.
the class WithCustomObjectReferencesTest method resolveReferences_WithSomeExistingCustomObjectReferenceSetAttribute_ShouldResolveExistingReferences.
@Test
void resolveReferences_WithSomeExistingCustomObjectReferenceSetAttribute_ShouldResolveExistingReferences() {
// preparation
final CustomObjectCompositeIdentifier existingCustomObjectId = CustomObjectCompositeIdentifier.of("existing-key", "existing-container");
final CustomObjectCompositeIdentifier randomCustomObjectId = CustomObjectCompositeIdentifier.of("random-key", "random-container");
when(customObjectService.fetchCachedCustomObjectId(existingCustomObjectId)).thenReturn(CompletableFuture.completedFuture(Optional.of("existingId")));
when(customObjectService.fetchCachedCustomObjectId(randomCustomObjectId)).thenReturn(CompletableFuture.completedFuture(Optional.empty()));
final ObjectNode customObjectReference1 = createReferenceObject("existing-container|existing-key", CustomObject.referenceTypeId());
final ObjectNode customObjectReference2 = createReferenceObject("random-container|random-key", CustomObject.referenceTypeId());
final AttributeDraft attributeDraft = getReferenceSetAttributeDraft("attributeName", customObjectReference1, customObjectReference2);
final ProductVariantDraft productVariantDraft = ProductVariantDraftBuilder.of().attributes(attributeDraft).build();
// test
final ProductVariantDraft resolvedProductVariantDraft = referenceResolver.resolveReferences(productVariantDraft).toCompletableFuture().join();
// assertions
assertThat(resolvedProductVariantDraft).isNotNull();
assertThat(resolvedProductVariantDraft.getAttributes()).isNotNull();
final AttributeDraft resolvedAttributeDraft = resolvedProductVariantDraft.getAttributes().get(0);
assertThat(resolvedAttributeDraft).isNotNull();
assertThat(resolvedAttributeDraft.getValue()).isNotNull();
final Spliterator<JsonNode> attributeReferencesIterator = resolvedAttributeDraft.getValue().spliterator();
assertThat(attributeReferencesIterator).isNotNull();
final Set<JsonNode> resolvedSet = StreamSupport.stream(attributeReferencesIterator, false).collect(Collectors.toSet());
final ObjectNode resolvedReference1 = createReferenceObject("existingId", CustomObject.referenceTypeId());
final ObjectNode resolvedReference2 = createReferenceObject("random-container|random-key", CustomObject.referenceTypeId());
assertThat(resolvedSet).containsExactlyInAnyOrder(resolvedReference1, resolvedReference2);
}
use of com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier in project commercetools-sync-java by commercetools.
the class CustomObjectServiceImpl method createQuery.
@Nonnull
private QueryPredicate<CustomObject<JsonNode>> createQuery(@Nonnull final CustomObjectQueryModel<CustomObject<JsonNode>> queryModel, @Nonnull final Set<CustomObjectCompositeIdentifier> identifiers) {
QueryPredicate<CustomObject<JsonNode>> queryPredicate = QueryPredicate.of(null);
boolean firstAttempt = true;
for (CustomObjectCompositeIdentifier identifier : identifiers) {
String key = identifier.getKey();
String container = identifier.getContainer();
if (firstAttempt) {
queryPredicate = queryModel.container().is(container).and(queryModel.key().is(key));
firstAttempt = false;
} else {
queryPredicate = queryPredicate.or(queryModel.container().is(container).and(queryModel.key().is(key)));
}
}
return queryPredicate;
}
Aggregations