Search in sources :

Example 26 with SyncException

use of com.commercetools.sync.commons.exceptions.SyncException in project commercetools-sync-java by commercetools.

the class StateBatchValidator method getTransitionKeys.

@Nonnull
private static Set<String> getTransitionKeys(@Nonnull final StateDraft stateDraft) throws SyncException {
    final Set<Reference<State>> transitions = stateDraft.getTransitions();
    if (transitions == null || transitions.isEmpty()) {
        return emptySet();
    }
    final Set<String> referencedStateKeys = new HashSet<>();
    final List<Reference<State>> invalidStates = new ArrayList<>();
    for (Reference<State> transition : transitions) {
        if (transition != null) {
            try {
                referencedStateKeys.add(getStateKey(transition));
            } catch (InvalidReferenceException invalidReferenceException) {
                invalidStates.add(transition);
            }
        }
    }
    if (!invalidStates.isEmpty()) {
        final String errorMessage = format(STATE_HAS_INVALID_REFERENCES, stateDraft.getKey());
        throw new SyncException(errorMessage, new InvalidReferenceException(BLANK_ID_VALUE_ON_REFERENCE));
    }
    return referencedStateKeys;
}
Also used : Reference(io.sphere.sdk.models.Reference) State(io.sphere.sdk.states.State) ArrayList(java.util.ArrayList) SyncException(com.commercetools.sync.commons.exceptions.SyncException) InvalidReferenceException(com.commercetools.sync.commons.exceptions.InvalidReferenceException) HashSet(java.util.HashSet) Nonnull(javax.annotation.Nonnull)

Example 27 with SyncException

use of com.commercetools.sync.commons.exceptions.SyncException in project commercetools-sync-java by commercetools.

the class CustomerServiceImpl method createCustomer.

@Nonnull
@Override
public CompletionStage<Optional<Customer>> createCustomer(@Nonnull final CustomerDraft customerDraft) {
    // Uses a different implementation than in the base service because CustomerCreateCommand uses a
    // different library as CTP responds with a CustomerSignInResult which is not extending resource
    // but a
    // different model, containing the customer resource.
    final String draftKey = customerDraft.getKey();
    final CustomerCreateCommand createCommand = CustomerCreateCommand.of(customerDraft);
    if (isBlank(draftKey)) {
        syncOptions.applyErrorCallback(new SyncException(format(CREATE_FAILED, draftKey, "Draft key is blank!")), null, customerDraft, null);
        return CompletableFuture.completedFuture(Optional.empty());
    } else {
        return syncOptions.getCtpClient().execute(createCommand).handle(((resource, exception) -> {
            if (exception == null && resource.getCustomer() != null) {
                keyToIdCache.put(draftKey, resource.getCustomer().getId());
                return Optional.of(resource.getCustomer());
            } else if (exception != null) {
                syncOptions.applyErrorCallback(new SyncException(format(CREATE_FAILED, draftKey, exception.getMessage()), exception), null, customerDraft, null);
                return Optional.empty();
            } else {
                return Optional.empty();
            }
        }));
    }
}
Also used : CustomerService(com.commercetools.sync.services.CustomerService) CustomerCreateCommand(io.sphere.sdk.customers.commands.CustomerCreateCommand) SyncException(com.commercetools.sync.commons.exceptions.SyncException) ResourceKeyIdGraphQlRequest(com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest) CustomerQueryModel(io.sphere.sdk.customers.queries.CustomerQueryModel) CustomerUpdateCommand(io.sphere.sdk.customers.commands.CustomerUpdateCommand) UpdateAction(io.sphere.sdk.commands.UpdateAction) Set(java.util.Set) CompletableFuture(java.util.concurrent.CompletableFuture) Customer(io.sphere.sdk.customers.Customer) String.format(java.lang.String.format) CustomerExpansionModel(io.sphere.sdk.customers.expansion.CustomerExpansionModel) CustomerQuery(io.sphere.sdk.customers.queries.CustomerQuery) CustomerQueryBuilder(io.sphere.sdk.customers.queries.CustomerQueryBuilder) List(java.util.List) CustomerSyncOptions(com.commercetools.sync.customers.CustomerSyncOptions) CompletionStage(java.util.concurrent.CompletionStage) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) Map(java.util.Map) Optional(java.util.Optional) GraphQlQueryResources(com.commercetools.sync.commons.models.GraphQlQueryResources) CustomerDraft(io.sphere.sdk.customers.CustomerDraft) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) CustomerCreateCommand(io.sphere.sdk.customers.commands.CustomerCreateCommand) SyncException(com.commercetools.sync.commons.exceptions.SyncException) Nonnull(javax.annotation.Nonnull)

Example 28 with SyncException

use of com.commercetools.sync.commons.exceptions.SyncException in project commercetools-sync-java by commercetools.

the class CustomUpdateActionUtilsTest method buildCustomUpdateActions_WithNullOldCustomFieldsAndBlankNewTypeId_ShouldCallErrorCallBack.

@Test
void buildCustomUpdateActions_WithNullOldCustomFieldsAndBlankNewTypeId_ShouldCallErrorCallBack() {
    final Asset oldAsset = mock(Asset.class);
    final String oldAssetId = "oldAssetId";
    when(oldAsset.getId()).thenReturn(oldAssetId);
    when(oldAsset.getCustom()).thenReturn(null);
    final AssetDraft newAssetDraft = AssetDraftBuilder.of(emptyList(), ofEnglish("assetName")).custom(ofTypeKeyAndJson("key", new HashMap<>())).build();
    // Mock custom options error callback
    final ArrayList<Object> callBackResponses = new ArrayList<>();
    final QuadConsumer<SyncException, Optional<ProductDraft>, Optional<ProductProjection>, List<UpdateAction<Product>>> errorCallback = (exception, newResource, oldResource, updateActions) -> {
        callBackResponses.add(exception.getMessage());
        callBackResponses.add(exception.getCause());
    };
    // Mock sync options
    final ProductSyncOptions productSyncOptions = ProductSyncOptionsBuilder.of(CTP_CLIENT).errorCallback(errorCallback).build();
    final List<UpdateAction<Product>> updateActions = buildCustomUpdateActions(maiNewResource, oldAsset, newAssetDraft, new AssetCustomActionBuilder(), 10, Asset::getId, asset -> Asset.resourceTypeId(), Asset::getKey, productSyncOptions);
    assertThat(updateActions).isNotNull();
    assertThat(updateActions).hasSize(0);
    assertThat(callBackResponses.get(0)).isEqualTo(format("Failed to build custom fields update actions on the " + "asset with id '%s'. Reason: New resource's custom type id is blank (empty/null).", oldAssetId));
    assertThat(callBackResponses.get(1)).isNull();
}
Also used : CustomUpdateActionUtils.buildCustomUpdateActions(com.commercetools.sync.commons.utils.CustomUpdateActionUtils.buildCustomUpdateActions) SetProductPriceCustomType(io.sphere.sdk.products.commands.updateactions.SetProductPriceCustomType) AssetCustomActionBuilder(com.commercetools.sync.products.helpers.AssetCustomActionBuilder) Reference(io.sphere.sdk.models.Reference) SyncException(com.commercetools.sync.commons.exceptions.SyncException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UpdateAction(io.sphere.sdk.commands.UpdateAction) Map(java.util.Map) CategoryCustomActionBuilder(com.commercetools.sync.categories.helpers.CategoryCustomActionBuilder) SphereClient(io.sphere.sdk.client.SphereClient) JsonNode(com.fasterxml.jackson.databind.JsonNode) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductProjection(io.sphere.sdk.products.ProductProjection) AssetDraft(io.sphere.sdk.models.AssetDraft) AssetDraftBuilder(io.sphere.sdk.models.AssetDraftBuilder) Collections.emptyList(java.util.Collections.emptyList) Category(io.sphere.sdk.categories.Category) Price(io.sphere.sdk.products.Price) CustomFieldsDraft.ofTypeKeyAndJson(io.sphere.sdk.types.CustomFieldsDraft.ofTypeKeyAndJson) Product(io.sphere.sdk.products.Product) UUID(java.util.UUID) String.format(java.lang.String.format) Test(org.junit.jupiter.api.Test) List(java.util.List) LocalizedString.ofEnglish(io.sphere.sdk.models.LocalizedString.ofEnglish) ProductSyncOptionsBuilder(com.commercetools.sync.products.ProductSyncOptionsBuilder) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) Optional(java.util.Optional) BuildUpdateActionException(com.commercetools.sync.commons.exceptions.BuildUpdateActionException) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) Mockito.mock(org.mockito.Mockito.mock) ResourceIdentifier(io.sphere.sdk.models.ResourceIdentifier) CustomFields(io.sphere.sdk.types.CustomFields) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CustomUpdateActionUtils.buildNonNullCustomFieldsUpdateActions(com.commercetools.sync.commons.utils.CustomUpdateActionUtils.buildNonNullCustomFieldsUpdateActions) SetAssetCustomField(io.sphere.sdk.products.commands.updateactions.SetAssetCustomField) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) SetAssetCustomType(io.sphere.sdk.products.commands.updateactions.SetAssetCustomType) CustomUpdateActionUtils.buildSetCustomFieldsUpdateActions(com.commercetools.sync.commons.utils.CustomUpdateActionUtils.buildSetCustomFieldsUpdateActions) Mockito.when(org.mockito.Mockito.when) CategorySyncOptionsBuilder(com.commercetools.sync.categories.CategorySyncOptionsBuilder) Resource(io.sphere.sdk.models.Resource) CategorySyncOptions(com.commercetools.sync.categories.CategorySyncOptions) CustomFieldsDraft(io.sphere.sdk.types.CustomFieldsDraft) PriceCustomActionBuilder(com.commercetools.sync.products.helpers.PriceCustomActionBuilder) SetProductPriceCustomField(io.sphere.sdk.products.commands.updateactions.SetProductPriceCustomField) Asset(io.sphere.sdk.models.Asset) Type(io.sphere.sdk.types.Type) Optional(java.util.Optional) UpdateAction(io.sphere.sdk.commands.UpdateAction) ArrayList(java.util.ArrayList) AssetCustomActionBuilder(com.commercetools.sync.products.helpers.AssetCustomActionBuilder) Product(io.sphere.sdk.products.Product) SyncException(com.commercetools.sync.commons.exceptions.SyncException) AssetDraft(io.sphere.sdk.models.AssetDraft) Asset(io.sphere.sdk.models.Asset) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) ArrayList(java.util.ArrayList) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) Test(org.junit.jupiter.api.Test)

Example 29 with SyncException

use of com.commercetools.sync.commons.exceptions.SyncException in project commercetools-project-sync by commercetools.

the class SyncUtilsTest method logErrorCallbackWithStringResourceIdentifierAndNullUpdateActions_ShouldLogErrorWithCorrectMessage.

@Test
void logErrorCallbackWithStringResourceIdentifierAndNullUpdateActions_ShouldLogErrorWithCorrectMessage() {
    final TestLogger testLogger = TestLoggerFactory.getTestLogger(SyncUtilsTest.class);
    SyncException exception = new SyncException("test sync exception");
    logErrorCallback(testLogger, "test resource", exception, "test identifier", null);
    assertThat(testLogger.getAllLoggingEvents()).hasSize(1);
    final LoggingEvent loggingEvent = testLogger.getAllLoggingEvents().get(0);
    assertThat(loggingEvent.getMessage()).isEqualTo("Error when trying to sync test resource. Existing key: test identifier. Update actions: []");
    assertThat(loggingEvent.getThrowable().isPresent()).isTrue();
    assertThat(loggingEvent.getThrowable().get()).isInstanceOf(SyncException.class);
}
Also used : LoggingEvent(uk.org.lidalia.slf4jtest.LoggingEvent) SyncException(com.commercetools.sync.commons.exceptions.SyncException) TestLogger(uk.org.lidalia.slf4jtest.TestLogger) Test(org.junit.jupiter.api.Test)

Example 30 with SyncException

use of com.commercetools.sync.commons.exceptions.SyncException in project commercetools-project-sync by commercetools.

the class SyncUtilsTest method logWarningCallbackStringResourceIdentifier_ShouldLogWarningWithCorrectMessage.

@Test
void logWarningCallbackStringResourceIdentifier_ShouldLogWarningWithCorrectMessage() {
    final TestLogger testLogger = TestLoggerFactory.getTestLogger(SyncUtilsTest.class);
    SyncException exception = new SyncException("test sync exception");
    logWarningCallback(testLogger, "test resource", exception, "test identifier");
    assertThat(testLogger.getAllLoggingEvents()).hasSize(1);
    final LoggingEvent loggingEvent = testLogger.getAllLoggingEvents().get(0);
    assertThat(loggingEvent.getMessage()).isEqualTo("Warning when trying to sync test resource. Existing key: test identifier");
    assertThat(loggingEvent.getThrowable().isPresent()).isTrue();
    assertThat(loggingEvent.getThrowable().get()).isInstanceOf(SyncException.class);
}
Also used : LoggingEvent(uk.org.lidalia.slf4jtest.LoggingEvent) SyncException(com.commercetools.sync.commons.exceptions.SyncException) TestLogger(uk.org.lidalia.slf4jtest.TestLogger) Test(org.junit.jupiter.api.Test)

Aggregations

SyncException (com.commercetools.sync.commons.exceptions.SyncException)74 Nonnull (javax.annotation.Nonnull)47 UpdateAction (io.sphere.sdk.commands.UpdateAction)45 Optional (java.util.Optional)39 List (java.util.List)37 CompletionStage (java.util.concurrent.CompletionStage)25 SphereClient (io.sphere.sdk.client.SphereClient)24 String.format (java.lang.String.format)23 ArrayList (java.util.ArrayList)21 Map (java.util.Map)20 Test (org.junit.jupiter.api.Test)20 TriConsumer (com.commercetools.sync.commons.utils.TriConsumer)18 Set (java.util.Set)17 CompletableFuture (java.util.concurrent.CompletableFuture)17 Nullable (javax.annotation.Nullable)16 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)14 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)14 Category (io.sphere.sdk.categories.Category)13 BaseSync (com.commercetools.sync.commons.BaseSync)12 QuadConsumer (com.commercetools.sync.commons.utils.QuadConsumer)12