use of com.commercetools.sync.states.StateSyncOptions in project commercetools-project-sync by commercetools.
the class StateSyncer method of.
public static StateSyncer of(@Nonnull final SphereClient sourceClient, @Nonnull final SphereClient targetClient, @Nonnull final Clock clock) {
final QuadConsumer<SyncException, Optional<StateDraft>, Optional<State>, List<UpdateAction<State>>> logErrorCallback = (exception, newResourceDraft, oldResource, updateActions) -> logErrorCallback(LOGGER, "state", exception, oldResource, updateActions);
final TriConsumer<SyncException, Optional<StateDraft>, Optional<State>> logWarningCallback = (exception, newResourceDraft, oldResource) -> logWarningCallback(LOGGER, "state", exception, oldResource);
StateSyncOptions syncOptions = StateSyncOptionsBuilder.of(targetClient).errorCallback(logErrorCallback).warningCallback(logWarningCallback).build();
StateSync stateSync = new StateSync(syncOptions);
CustomObjectService customObjectService = new CustomObjectServiceImpl(targetClient);
return new StateSyncer(stateSync, sourceClient, targetClient, customObjectService, clock);
}
use of com.commercetools.sync.states.StateSyncOptions in project commercetools-sync-java by commercetools.
the class StateServiceImplIT method setup.
/**
* Deletes states from the target CTP projects, then it populates the project with test data.
*/
@BeforeEach
void setup() {
errorCallBackMessages = new ArrayList<>();
errorCallBackExceptions = new ArrayList<>();
deleteStates(CTP_TARGET_CLIENT, Optional.of(STATE_TYPE));
deleteStates(CTP_TARGET_CLIENT, Optional.of(TRANSITION_STATE_TYPE));
warnings = new ArrayList<>();
oldState = createState(CTP_TARGET_CLIENT, STATE_TYPE);
final StateSyncOptions StateSyncOptions = StateSyncOptionsBuilder.of(CTP_TARGET_CLIENT).warningCallback((exception, oldResource, newResource) -> warnings.add(exception.getMessage())).build();
stateService = new StateServiceImpl(StateSyncOptions);
}
use of com.commercetools.sync.states.StateSyncOptions in project commercetools-sync-java by commercetools.
the class StateServiceImplIT method createState_WithInvalidState_ShouldHaveEmptyOptionalAsAResult.
@Test
void createState_WithInvalidState_ShouldHaveEmptyOptionalAsAResult() {
// preparation
final StateDraft newStateDraft = StateDraftBuilder.of("", STATE_TYPE).name(STATE_NAME_1).description(STATE_DESCRIPTION_1).build();
final StateSyncOptions options = StateSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).build();
final StateService stateService = new StateServiceImpl(options);
// test
final Optional<State> result = stateService.createState(newStateDraft).toCompletableFuture().join();
// assertion
assertThat(result).isEmpty();
assertThat(errorCallBackMessages).containsExactly("Failed to create draft with key: ''. Reason: Draft key is blank!");
}
use of com.commercetools.sync.states.StateSyncOptions in project commercetools-sync-java by commercetools.
the class StateServiceImplIT method fetchMatchingStatesByKeys_WithBadGateWayExceptionAlways_ShouldFail.
@Test
void fetchMatchingStatesByKeys_WithBadGateWayExceptionAlways_ShouldFail() {
// Mock sphere client to return BadGatewayException on any request.
final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
when(spyClient.execute(any(StateQuery.class))).thenReturn(CompletableFutureUtils.exceptionallyCompletedFuture(new BadGatewayException())).thenCallRealMethod();
final StateSyncOptions spyOptions = StateSyncOptionsBuilder.of(spyClient).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).build();
final StateService spyStateService = new StateServiceImpl(spyOptions);
final Set<String> keys = new HashSet<>();
keys.add(OLD_STATE_KEY);
// test and assert
assertThat(errorCallBackExceptions).isEmpty();
assertThat(errorCallBackMessages).isEmpty();
assertThat(spyStateService.fetchMatchingStatesByKeys(keys)).failsWithin(10, TimeUnit.SECONDS).withThrowableOfType(ExecutionException.class).withCauseExactlyInstanceOf(BadGatewayException.class);
}
use of com.commercetools.sync.states.StateSyncOptions in project commercetools-sync-java by commercetools.
the class StateServiceImplIT method createState_WithValidState_ShouldCreateStateAndCacheId.
@Test
void createState_WithValidState_ShouldCreateStateAndCacheId() {
final StateDraft newStateDraft = StateDraftBuilder.of(STATE_KEY_1, STATE_TYPE).name(STATE_NAME_1).description(STATE_DESCRIPTION_1).build();
final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
final StateSyncOptions spyOptions = StateSyncOptionsBuilder.of(spyClient).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).build();
final StateService spyStateService = new StateServiceImpl(spyOptions);
// test
final Optional<State> createdState = spyStateService.createState(newStateDraft).toCompletableFuture().join();
final Optional<State> queriedOptional = CTP_TARGET_CLIENT.execute(StateQuery.of().withPredicates(stateQueryModel -> stateQueryModel.key().is(STATE_KEY_1))).toCompletableFuture().join().head();
assertThat(queriedOptional).hasValueSatisfying(queried -> assertThat(createdState).hasValueSatisfying(created -> {
assertThat(created.getKey()).isEqualTo(queried.getKey());
assertThat(created.getDescription()).isEqualTo(queried.getDescription());
assertThat(created.getName()).isEqualTo(queried.getName());
}));
// Assert that the created state is cached
final Optional<String> stateId = spyStateService.fetchCachedStateId(STATE_KEY_1).toCompletableFuture().join();
assertThat(stateId).isPresent();
verify(spyClient, times(0)).execute(any(StateQuery.class));
}
Aggregations