use of com.commercetools.sync.customobjects.CustomObjectSyncOptions 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.customobjects.CustomObjectSyncOptions 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.customobjects.CustomObjectSyncOptions in project commercetools-sync-java by commercetools.
the class BaseServiceImplTest method cacheKeysToIds_WithEmptyCache_ShouldMakeRequestAndReturnCacheEntries.
@Test
void cacheKeysToIds_WithEmptyCache_ShouldMakeRequestAndReturnCacheEntries() {
// preparation
final CustomObjectSyncOptions customObjectSyncOptions = CustomObjectSyncOptionsBuilder.of(client).build();
final CustomObjectServiceImpl serviceImpl = new CustomObjectServiceImpl(customObjectSyncOptions);
final PagedQueryResult pagedQueryResult = mock(PagedQueryResult.class);
final CustomObject customObject = mock(CustomObject.class);
final String customObjectId = "customObjectId";
final String customObjectContainer = "customObjectContainer";
final String customObjectKey = "customObjectKey";
when(customObject.getId()).thenReturn(customObjectId);
when(customObject.getKey()).thenReturn(customObjectKey);
when(customObject.getContainer()).thenReturn(customObjectContainer);
when(pagedQueryResult.getResults()).thenReturn(singletonList(customObject));
when(client.execute(any())).thenReturn(completedFuture(pagedQueryResult));
final Map<String, String> result = serviceImpl.cacheKeysToIds(singleton(CustomObjectCompositeIdentifier.of(customObjectKey, customObjectContainer))).toCompletableFuture().join();
assertAll(() -> assertThat(result).hasSize(1), () -> assertThat(result.get(CustomObjectCompositeIdentifier.of(customObjectKey, customObjectContainer).toString())).isEqualTo(customObjectId));
verify(client).execute(any(CustomObjectQuery.class));
}
use of com.commercetools.sync.customobjects.CustomObjectSyncOptions in project commercetools-sync-java by commercetools.
the class BaseServiceImplTest method cacheKeysToIds_With500CustomObjectIdentifiers_ShouldChunkAndMakeRequestAndReturnCacheEntries.
@Test
void cacheKeysToIds_With500CustomObjectIdentifiers_ShouldChunkAndMakeRequestAndReturnCacheEntries() {
// preparation
Set<CustomObjectCompositeIdentifier> randomIdentifiers = new HashSet<>();
IntStream.range(0, 500).forEach(i -> randomIdentifiers.add(CustomObjectCompositeIdentifier.of("customObjectId" + i, "customObjectContainer" + i)));
final CustomObjectSyncOptions customObjectSyncOptions = CustomObjectSyncOptionsBuilder.of(client).build();
final CustomObjectServiceImpl serviceImpl = new CustomObjectServiceImpl(customObjectSyncOptions);
final PagedQueryResult pagedQueryResult = mock(PagedQueryResult.class);
final CustomObject customObject = mock(CustomObject.class);
final String customObjectId = randomIdentifiers.stream().findFirst().get().getKey();
final String customObjectContainer = randomIdentifiers.stream().findFirst().get().getContainer();
final String customObjectKey = "customObjectKey";
when(customObject.getId()).thenReturn(customObjectId);
when(customObject.getKey()).thenReturn(customObjectKey);
when(customObject.getContainer()).thenReturn(customObjectContainer);
when(pagedQueryResult.getResults()).thenReturn(singletonList(customObject));
when(client.execute(any())).thenReturn(completedFuture(pagedQueryResult));
// test
serviceImpl.cacheKeysToIds(randomIdentifiers).toCompletableFuture().join();
// assertion
verify(client, times(2)).execute(any(CustomObjectQuery.class));
}
use of com.commercetools.sync.customobjects.CustomObjectSyncOptions in project commercetools-sync-java by commercetools.
the class CustomObjectServiceImplTest method setup.
@BeforeEach
void setup() {
customObjectId = RandomStringUtils.random(15, true, true);
customObjectContainer = RandomStringUtils.random(15, true, true);
customObjectKey = RandomStringUtils.random(15, true, true);
CustomObjectSyncOptions customObjectSyncOptions = CustomObjectSyncOptionsBuilder.of(client).build();
service = new CustomObjectServiceImpl(customObjectSyncOptions);
}
Aggregations