use of com.commercetools.sync.services.impl.CustomObjectServiceImpl in project commercetools-sync-java by commercetools.
the class CustomObjectServiceImplIT method setup.
/**
* Deletes customObjects from the target CTP project, then it populates the project with test
* data.
*/
@BeforeEach
void setup() {
errorCallBackMessages = new ArrayList<>();
errorCallBackExceptions = new ArrayList<>();
deleteCustomObject(CTP_TARGET_CLIENT, OLD_CUSTOM_OBJECT_KEY, OLD_CUSTOM_OBJECT_CONTAINER);
deleteCustomObject(CTP_TARGET_CLIENT, NEW_CUSTOM_OBJECT_KEY, NEW_CUSTOM_OBJECT_CONTAINER);
createCustomObject(CTP_TARGET_CLIENT, OLD_CUSTOM_OBJECT_KEY, OLD_CUSTOM_OBJECT_CONTAINER, OLD_CUSTOM_OBJECT_VALUE);
final CustomObjectSyncOptions customObjectSyncOptions = CustomObjectSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).build();
customObjectService = new CustomObjectServiceImpl(customObjectSyncOptions);
}
use of com.commercetools.sync.services.impl.CustomObjectServiceImpl in project commercetools-sync-java by commercetools.
the class CustomObjectServiceImplIT method upsertCustomObject_WithValidCustomObject_ShouldCreateCustomObjectAndCacheId.
@Test
void upsertCustomObject_WithValidCustomObject_ShouldCreateCustomObjectAndCacheId() {
final CustomObjectDraft<JsonNode> newCustomObjectDraft = CustomObjectDraft.ofUnversionedUpsert(NEW_CUSTOM_OBJECT_CONTAINER, NEW_CUSTOM_OBJECT_KEY, NEW_CUSTOM_OBJECT_VALUE);
final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
final CustomObjectSyncOptions spyOptions = CustomObjectSyncOptionsBuilder.of(spyClient).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).build();
final CustomObjectService spyCustomObjectService = new CustomObjectServiceImpl(spyOptions);
final Optional<CustomObject<JsonNode>> createdCustomObject = spyCustomObjectService.upsertCustomObject(newCustomObjectDraft).toCompletableFuture().join();
final Optional<CustomObject<JsonNode>> queriedOptional = CTP_TARGET_CLIENT.execute(CustomObjectQuery.ofJsonNode().withPredicates(customObjectQueryModel -> customObjectQueryModel.container().is(NEW_CUSTOM_OBJECT_CONTAINER).and(customObjectQueryModel.key().is(NEW_CUSTOM_OBJECT_KEY)))).toCompletableFuture().join().head();
assertThat(queriedOptional).hasValueSatisfying(queried -> assertThat(createdCustomObject).hasValueSatisfying(created -> {
assertThat(created.getKey()).isEqualTo(queried.getKey());
assertThat(created.getContainer()).isEqualTo(queried.getContainer());
assertThat(created.getId()).isEqualTo(queried.getId());
assertThat(created.getValue()).isEqualTo(queried.getValue());
}));
// Assert that the created customObject is cached
final Optional<String> customObjectId = spyCustomObjectService.fetchCachedCustomObjectId(CustomObjectCompositeIdentifier.of(NEW_CUSTOM_OBJECT_KEY, NEW_CUSTOM_OBJECT_CONTAINER)).toCompletableFuture().join();
assertThat(customObjectId).isPresent();
verify(spyClient, times(0)).execute(any(CustomObjectQuery.class));
}
use of com.commercetools.sync.services.impl.CustomObjectServiceImpl in project commercetools-sync-java by commercetools.
the class CustomObjectServiceImplIT method fetchMatchingCustomObjectsByCompositeIdentifiers_WithBadGateWayExceptionAlways_ShouldFail.
@Test
void fetchMatchingCustomObjectsByCompositeIdentifiers_WithBadGateWayExceptionAlways_ShouldFail() {
// Mock sphere client to return BadGatewayException on any request.
final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
when(spyClient.execute(any(CustomObjectQuery.class))).thenReturn(CompletableFutureUtils.exceptionallyCompletedFuture(new BadGatewayException())).thenCallRealMethod();
final CustomObjectSyncOptions spyOptions = CustomObjectSyncOptionsBuilder.of(spyClient).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).build();
final CustomObjectService spyCustomObjectService = new CustomObjectServiceImpl(spyOptions);
final Set<CustomObjectCompositeIdentifier> customObjectCompositeIdentifiers = new HashSet<>();
customObjectCompositeIdentifiers.add(CustomObjectCompositeIdentifier.of(OLD_CUSTOM_OBJECT_KEY, OLD_CUSTOM_OBJECT_CONTAINER));
// test and assert
assertThat(errorCallBackExceptions).isEmpty();
assertThat(errorCallBackMessages).isEmpty();
assertThat(spyCustomObjectService.fetchMatchingCustomObjects(customObjectCompositeIdentifiers)).failsWithin(10, TimeUnit.SECONDS).withThrowableOfType(ExecutionException.class).withCauseExactlyInstanceOf(BadGatewayException.class);
}
Aggregations