use of com.commercetools.sync.commons.models.ResourceKeyId in project commercetools-project-sync by commercetools.
the class SyncerFactoryTest method sync_AsProductsFullSyncWithExceptionDuringAttributeReferenceReplacement_ShouldContinueWithPages.
@Test
void sync_AsProductsFullSyncWithExceptionDuringAttributeReferenceReplacement_ShouldContinueWithPages() {
// preparation
final SphereClient sourceClient = mock(SphereClient.class);
when(sourceClient.getConfig()).thenReturn(SphereClientConfig.of("foo", "foo", "foo"));
final SphereClient targetClient = mock(SphereClient.class);
when(targetClient.getConfig()).thenReturn(SphereClientConfig.of("bar", "bar", "bar"));
final ProductProjection product1 = SphereJsonUtils.readObjectFromResource("product-key-7.json", Product.class).toProjection(STAGED);
final ProductProjection product2 = SphereJsonUtils.readObjectFromResource("product-key-8.json", Product.class).toProjection(STAGED);
final ProductProjection product3 = SphereJsonUtils.readObjectFromResource("product-key-9.json", Product.class).toProjection(STAGED);
final List<ProductProjection> fullPageOfProducts = IntStream.range(0, 500).mapToObj(o -> product1).collect(Collectors.toList());
when(sourceClient.execute(any(ProductProjectionQuery.class))).thenReturn(CompletableFuture.completedFuture(MockPagedQueryResult.of(fullPageOfProducts))).thenReturn(CompletableFuture.completedFuture(MockPagedQueryResult.of(asList(product3, product2))));
when(targetClient.execute(any(ProductProjectionQuery.class))).thenReturn(CompletableFuture.completedFuture(MockPagedQueryResult.of(emptyList())));
when(targetClient.execute(any(ProductTypeQuery.class))).thenReturn(CompletableFuture.completedFuture(MockPagedQueryResult.of(emptyList())));
when(targetClient.execute(any(CategoryQuery.class))).thenReturn(CompletableFuture.completedFuture(MockPagedQueryResult.of(emptyList())));
when(targetClient.execute(any(TaxCategoryQuery.class))).thenReturn(CompletableFuture.completedFuture(MockPagedQueryResult.of(emptyList())));
when(targetClient.execute(any(StateQuery.class))).thenReturn(CompletableFuture.completedFuture(MockPagedQueryResult.of(emptyList())));
when(targetClient.execute(any(CustomObjectQuery.class))).thenReturn(CompletableFuture.completedFuture(MockPagedQueryResult.of(emptyList())));
final Product product4 = SphereJsonUtils.readObjectFromResource("product-key-8.json", Product.class);
when(targetClient.execute(any(ProductCreateCommand.class))).thenReturn(CompletableFuture.completedFuture(product4));
String jsonAsString = "{\"results\":[{\"id\":\"53c4a8b4-865f-4b95-b6f2-3e1e70e3d0c1\",\"key\":\"productKey3\"}]}";
final ResourceKeyIdGraphQlResult productsResult = SphereJsonUtils.readObject(jsonAsString, ResourceKeyIdGraphQlResult.class);
String jsonStringProductTypes = "{\"results\":[{\"id\":\"53c4a8b4-865f-4b95-b6f2-3e1e70e3d0c2\",\"key\":\"prodType1\"}]}";
final ResourceKeyIdGraphQlResult productTypesResult = SphereJsonUtils.readObject(jsonStringProductTypes, ResourceKeyIdGraphQlResult.class);
String jsonStringCategories = "{\"results\":[{\"id\":\"53c4a8b4-865f-4b95-b6f2-3e1e70e3d0c3\",\"key\":\"cat1\"}]}";
final ResourceKeyIdGraphQlResult categoriesResult = SphereJsonUtils.readObject(jsonStringCategories, ResourceKeyIdGraphQlResult.class);
final BadGatewayException badGatewayException = new BadGatewayException("Error!");
when(sourceClient.execute(any(ResourceIdsGraphQlRequest.class))).thenReturn(CompletableFutureUtils.failed(badGatewayException)).thenReturn(CompletableFutureUtils.failed(badGatewayException)).thenReturn(CompletableFutureUtils.failed(badGatewayException)).thenReturn(CompletableFuture.completedFuture(productsResult)).thenReturn(CompletableFuture.completedFuture(categoriesResult)).thenReturn(CompletableFuture.completedFuture(productTypesResult));
final ResourceKeyIdGraphQlResult resourceKeyIdGraphQlResult = mock(ResourceKeyIdGraphQlResult.class);
when(resourceKeyIdGraphQlResult.getResults()).thenReturn(singleton(new ResourceKeyId("productKey3", "53c4a8b4-865f-4b95-b6f2-3e1e70e3d0c1")));
when(targetClient.execute(any(ResourceKeyIdGraphQlRequest.class))).thenReturn(CompletableFuture.completedFuture(resourceKeyIdGraphQlResult));
final SyncerFactory syncerFactory = SyncerFactory.of(() -> sourceClient, () -> targetClient, getMockedClock());
// test
syncerFactory.sync(new String[] { "products" }, "myRunnerName", true, false, null);
// assertions
verify(sourceClient, times(2)).execute(any(ProductProjectionQuery.class));
verify(sourceClient, times(9)).execute(any(ResourceIdsGraphQlRequest.class));
verifyInteractionsWithClientAfterSync(sourceClient, 1);
final Condition<LoggingEvent> startLog = new Condition<>(loggingEvent -> Level.INFO.equals(loggingEvent.getLevel()) && loggingEvent.getMessage().contains("Starting ProductSync"), "start log");
final Condition<LoggingEvent> statisticsLog = new Condition<>(loggingEvent -> Level.INFO.equals(loggingEvent.getLevel()) && loggingEvent.getMessage().contains("Summary: 2 product(s) were processed in total (2 created, 0 updated, " + "0 failed to sync and 0 product(s) with missing reference(s))."), "statistics log");
assertThat(productSyncerTestLogger.getAllLoggingEvents()).hasSize(3).haveExactly(1, startLog).haveExactly(1, statisticsLog);
assertThat(productSyncerTestLogger.getAllLoggingEvents()).anySatisfy(loggingEvent -> {
assertThat(loggingEvent.getMessage()).contains(ReferenceTransformException.class.getCanonicalName() + ": Failed to replace referenced resource ids with keys on the attributes of the " + "products in the current fetched page from the source project. " + "This page will not be synced to the target project.");
assertThat(loggingEvent.getThrowable().isPresent()).isTrue();
assertThat(loggingEvent.getThrowable().get().getCause().getCause()).isEqualTo(badGatewayException);
});
}
use of com.commercetools.sync.commons.models.ResourceKeyId in project commercetools-project-sync by commercetools.
the class CategorySyncerTest method syncWithWarning_ShouldCallWarningCallback.
@Test
void syncWithWarning_ShouldCallWarningCallback() {
// preparation: old category has category order hint,
// new category does not have category order hint
final SphereClient sourceClient = mock(SphereClient.class);
final SphereClient targetClient = mock(SphereClient.class);
when(sourceClient.getConfig()).thenReturn(SphereApiConfig.of("source-project"));
when(targetClient.getConfig()).thenReturn(SphereApiConfig.of("target-project"));
final List<Category> sourceCategories = Collections.singletonList(readObjectFromResource("category-key-2.json", Category.class));
final List<Category> targetCategories = Collections.singletonList(readObjectFromResource("category-order-hint.json", Category.class));
final PagedQueryResult<Category> sourcePagedQueryResult = mock(PagedQueryResult.class);
when(sourcePagedQueryResult.getResults()).thenReturn(sourceCategories);
when(sourceClient.execute(any(CategoryQuery.class))).thenReturn(CompletableFuture.completedFuture(sourcePagedQueryResult));
final PagedQueryResult<Category> targetPagedQueryResult = mock(PagedQueryResult.class);
when(targetPagedQueryResult.getResults()).thenReturn(targetCategories);
when(targetClient.execute(any(CategoryQuery.class))).thenReturn(CompletableFuture.completedFuture(targetPagedQueryResult));
when(targetPagedQueryResult.head()).thenReturn(Optional.of(targetCategories.get(0)));
final ResourceKeyIdGraphQlResult resourceKeyIdGraphQlResult = mock(ResourceKeyIdGraphQlResult.class);
when(resourceKeyIdGraphQlResult.getResults()).thenReturn(singleton(new ResourceKeyId("categoryKey2", "ba81a6da-cf83-435b-a89e-2afab579846f")));
when(targetClient.execute(any(ResourceKeyIdGraphQlRequest.class))).thenReturn(CompletableFuture.completedFuture(resourceKeyIdGraphQlResult));
// test
final CategorySyncer categorySyncer = CategorySyncer.of(sourceClient, targetClient, mock(Clock.class));
categorySyncer.sync(null, true).toCompletableFuture().join();
// assertion
final LoggingEvent errorLog = syncerTestLogger.getAllLoggingEvents().get(1);
assertThat(errorLog.getMessage()).isEqualTo("Warning when trying to sync category. Existing key: categoryKey2");
assertThat(errorLog.getThrowable().get().getMessage()).isEqualTo(format("Cannot unset 'orderHint' field of category with id '%s'.", sourceCategories.get(0).getId()));
}
use of com.commercetools.sync.commons.models.ResourceKeyId in project commercetools-sync-java by commercetools.
the class CategorySyncTest method sync_WithFailOnFetchingCategories_ShouldTriggerErrorCallbackAndReturnProperStats.
@Test
void sync_WithFailOnFetchingCategories_ShouldTriggerErrorCallbackAndReturnProperStats() {
// preparation
final SphereClient mockClient = mock(SphereClient.class);
final String categoryKey = "key";
final Category mockCategory = getMockCategory("foo", categoryKey);
ResourceKeyId resourceKeyId = new ResourceKeyId(categoryKey, "foo");
@SuppressWarnings("unchecked") final ResourceKeyIdGraphQlResult resourceKeyIdGraphQlResult = mock(ResourceKeyIdGraphQlResult.class);
when(resourceKeyIdGraphQlResult.getResults()).thenReturn(singleton(resourceKeyId));
// successful caching
when(mockClient.execute(any(ResourceKeyIdGraphQlRequest.class))).thenReturn(CompletableFuture.completedFuture(resourceKeyIdGraphQlResult));
// exception on fetch.
when(mockClient.execute(any(CategoryQuery.class))).thenReturn(supplyAsync(() -> {
throw new SphereException();
}));
when(mockClient.execute(any(CategoryCreateCommand.class))).thenReturn(CompletableFuture.completedFuture(mockCategory));
final CategorySyncOptions syncOptions = CategorySyncOptionsBuilder.of(mockClient).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).build();
final CategoryService categoryServiceSpy = spy(new CategoryServiceImpl(syncOptions));
final CategorySync mockCategorySync = new CategorySync(syncOptions, getMockTypeService(), categoryServiceSpy, mockUnresolvedReferencesService);
final CategoryDraft categoryDraft = CategoryDraftBuilder.of(LocalizedString.of(Locale.ENGLISH, "name"), LocalizedString.of(Locale.ENGLISH, "name")).key(categoryKey).custom(CustomFieldsDraft.ofTypeKeyAndJson("customTypeId", new HashMap<>())).build();
// test
final CategorySyncStatistics syncStatistics = mockCategorySync.sync(singletonList(categoryDraft)).toCompletableFuture().join();
// assertions
assertThat(syncStatistics).hasValues(1, 0, 0, 1);
assertThat(errorCallBackMessages).hasSize(1).singleElement(as(STRING)).contains("Failed to fetch existing categories");
assertThat(errorCallBackExceptions).hasSize(1).singleElement(as(THROWABLE)).hasCauseExactlyInstanceOf(SphereException.class);
}
use of com.commercetools.sync.commons.models.ResourceKeyId in project commercetools-sync-java by commercetools.
the class ChunkUtilsTest method executeChunks_withGraphqlRequests_ShouldReturnResults.
@Test
void executeChunks_withGraphqlRequests_ShouldReturnResults() {
final SphereClient client = mock(SphereClient.class);
final ResourceKeyIdGraphQlResult resourceKeyIdGraphQlResult = mock(ResourceKeyIdGraphQlResult.class);
when(resourceKeyIdGraphQlResult.getResults()).thenReturn(new HashSet<>(Arrays.asList(new ResourceKeyId("coKey1", "coId1"), new ResourceKeyId("coKey2", "coId2"))));
when(client.execute(any(ResourceKeyIdGraphQlRequest.class))).thenReturn(CompletableFuture.completedFuture(resourceKeyIdGraphQlResult));
final ResourceKeyIdGraphQlRequest request = new ResourceKeyIdGraphQlRequest(singleton("key-1"), GraphQlQueryResources.CATEGORIES);
final List<ResourceKeyIdGraphQlResult> results = ChunkUtils.executeChunks(client, asList(request, request, request)).join();
assertThat(results).hasSize(3);
final Set<ResourceKeyId> resourceKeyIds = ChunkUtils.flattenGraphQLBaseResults(results);
assertThat(resourceKeyIds).hasSize(2);
}
use of com.commercetools.sync.commons.models.ResourceKeyId in project commercetools-sync-java by commercetools.
the class CleanupUnresolvedReferenceCustomObjectsTest method cleanup_withNotFound404Exception_ShouldNotIncrementFailedCounter.
@Test
void cleanup_withNotFound404Exception_ShouldNotIncrementFailedCounter() {
final ResourceKeyIdGraphQlResult resourceKeyIdGraphQlResult = mock(ResourceKeyIdGraphQlResult.class);
when(resourceKeyIdGraphQlResult.getResults()).thenReturn(Collections.singleton(new ResourceKeyId("coKey1", "coId1")));
when(mockClient.execute(any(FetchCustomObjectsGraphQlRequest.class))).thenReturn(CompletableFuture.completedFuture(resourceKeyIdGraphQlResult));
when(mockClient.execute(any(CustomObjectDeleteCommand.class))).thenReturn(CompletableFuture.completedFuture(mock(CustomObject.class))).thenReturn(CompletableFutureUtils.failed(new NotFoundException()));
final CleanupUnresolvedReferenceCustomObjects.Statistics statistics = CleanupUnresolvedReferenceCustomObjects.of(mockClient).cleanup(deleteDaysAfterLastModification).join();
assertThat(statistics.getTotalDeleted()).isEqualTo(1);
assertThat(statistics.getTotalFailed()).isEqualTo(0);
assertThat(statistics.getReportMessage()).isEqualTo("Summary: 1 custom objects were deleted in total (0 failed to delete).");
}
Aggregations