Search in sources :

Example 1 with CustomObjectCompositeIdentifier

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();
}
Also used : CustomObjectCompositeIdentifier(com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier) Test(org.junit.jupiter.api.Test)

Example 2 with CustomObjectCompositeIdentifier

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());
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) ProductVariantDraft(io.sphere.sdk.products.ProductVariantDraft) SphereJsonUtils.readObjectFromResource(io.sphere.sdk.json.SphereJsonUtils.readObjectFromResource) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CustomObject(io.sphere.sdk.customobjects.CustomObject) CompletableFuture(java.util.concurrent.CompletableFuture) VariantReferenceResolver(com.commercetools.sync.products.helpers.VariantReferenceResolver) AssertionUtilsForVariantReferenceResolver.assertReferenceSetAttributeValue(com.commercetools.sync.products.helpers.variantreferenceresolver.AssertionUtilsForVariantReferenceResolver.assertReferenceSetAttributeValue) ProductTypeService(com.commercetools.sync.services.ProductTypeService) TypeService(com.commercetools.sync.services.TypeService) Map(java.util.Map) ProductSyncMockUtils.getMockCustomObjectService(com.commercetools.sync.products.ProductSyncMockUtils.getMockCustomObjectService) SphereClient(io.sphere.sdk.client.SphereClient) JsonNode(com.fasterxml.jackson.databind.JsonNode) StreamSupport(java.util.stream.StreamSupport) CustomObjectCompositeIdentifier(com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier) CustomerService(com.commercetools.sync.services.CustomerService) CategoryService(com.commercetools.sync.services.CategoryService) Mockito.when(org.mockito.Mockito.when) StateService(com.commercetools.sync.services.StateService) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Test(org.junit.jupiter.api.Test) ChannelService(com.commercetools.sync.services.ChannelService) ProductSyncOptionsBuilder(com.commercetools.sync.products.ProductSyncOptionsBuilder) RES_ROOT(com.commercetools.sync.products.helpers.variantreferenceresolver.withnestedattributes.WithNoReferencesTest.RES_ROOT) ProductSyncMockUtils.createReferenceObject(com.commercetools.sync.products.ProductSyncMockUtils.createReferenceObject) CustomerGroupService(com.commercetools.sync.services.CustomerGroupService) Optional(java.util.Optional) AssertionUtilsForVariantReferenceResolver.assertReferenceAttributeValue(com.commercetools.sync.products.helpers.variantreferenceresolver.AssertionUtilsForVariantReferenceResolver.assertReferenceAttributeValue) ProductService(com.commercetools.sync.services.ProductService) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) Mockito.mock(org.mockito.Mockito.mock) CustomObjectService(com.commercetools.sync.services.CustomObjectService) ProductVariantDraft(io.sphere.sdk.products.ProductVariantDraft) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) CustomObjectCompositeIdentifier(com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier) Test(org.junit.jupiter.api.Test)

Example 3 with CustomObjectCompositeIdentifier

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());
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) ProductVariantDraft(io.sphere.sdk.products.ProductVariantDraft) SphereJsonUtils.readObjectFromResource(io.sphere.sdk.json.SphereJsonUtils.readObjectFromResource) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CustomObject(io.sphere.sdk.customobjects.CustomObject) CompletableFuture(java.util.concurrent.CompletableFuture) VariantReferenceResolver(com.commercetools.sync.products.helpers.VariantReferenceResolver) AssertionUtilsForVariantReferenceResolver.assertReferenceSetAttributeValue(com.commercetools.sync.products.helpers.variantreferenceresolver.AssertionUtilsForVariantReferenceResolver.assertReferenceSetAttributeValue) ProductTypeService(com.commercetools.sync.services.ProductTypeService) TypeService(com.commercetools.sync.services.TypeService) Map(java.util.Map) ProductSyncMockUtils.getMockCustomObjectService(com.commercetools.sync.products.ProductSyncMockUtils.getMockCustomObjectService) SphereClient(io.sphere.sdk.client.SphereClient) JsonNode(com.fasterxml.jackson.databind.JsonNode) StreamSupport(java.util.stream.StreamSupport) CustomObjectCompositeIdentifier(com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier) CustomerService(com.commercetools.sync.services.CustomerService) CategoryService(com.commercetools.sync.services.CategoryService) Mockito.when(org.mockito.Mockito.when) StateService(com.commercetools.sync.services.StateService) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Test(org.junit.jupiter.api.Test) ChannelService(com.commercetools.sync.services.ChannelService) ProductSyncOptionsBuilder(com.commercetools.sync.products.ProductSyncOptionsBuilder) ProductSyncMockUtils.createReferenceObject(com.commercetools.sync.products.ProductSyncMockUtils.createReferenceObject) CustomerGroupService(com.commercetools.sync.services.CustomerGroupService) Optional(java.util.Optional) AssertionUtilsForVariantReferenceResolver.assertReferenceAttributeValue(com.commercetools.sync.products.helpers.variantreferenceresolver.AssertionUtilsForVariantReferenceResolver.assertReferenceAttributeValue) ProductService(com.commercetools.sync.services.ProductService) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) Mockito.mock(org.mockito.Mockito.mock) CustomObjectService(com.commercetools.sync.services.CustomObjectService) ProductVariantDraft(io.sphere.sdk.products.ProductVariantDraft) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) CustomObjectCompositeIdentifier(com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier) Test(org.junit.jupiter.api.Test)

Example 4 with CustomObjectCompositeIdentifier

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);
}
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) JsonNode(com.fasterxml.jackson.databind.JsonNode) CustomObjectCompositeIdentifier(com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier) Test(org.junit.jupiter.api.Test)

Example 5 with CustomObjectCompositeIdentifier

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;
}
Also used : CustomObject(io.sphere.sdk.customobjects.CustomObject) CustomObjectCompositeIdentifier(com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier) Nonnull(javax.annotation.Nonnull)

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