use of com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult in project commercetools-sync-java by commercetools.
the class CleanupUnresolvedReferenceCustomObjectsTest method cleanup_withBadRequest400Exception_ShouldIncrementFailedCounterAndTriggerErrorCallback.
@Test
void cleanup_withBadRequest400Exception_ShouldIncrementFailedCounterAndTriggerErrorCallback() {
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));
final Throwable badRequestException = new BadRequestException("key is not valid");
when(mockClient.execute(any(CustomObjectDeleteCommand.class))).thenReturn(CompletableFuture.completedFuture(mock(CustomObject.class))).thenReturn(CompletableFutureUtils.failed(badRequestException));
final List<Throwable> exceptions = new ArrayList<>();
final CleanupUnresolvedReferenceCustomObjects.Statistics statistics = CleanupUnresolvedReferenceCustomObjects.of(mockClient).errorCallback(exceptions::add).cleanup(deleteDaysAfterLastModification).join();
assertThat(statistics.getTotalDeleted()).isEqualTo(1);
assertThat(statistics.getTotalFailed()).isEqualTo(2);
assertThat(exceptions).contains(badRequestException);
assertThat(statistics.getReportMessage()).isEqualTo("Summary: 1 custom objects were deleted in total (2 failed to delete).");
}
use of com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult in project commercetools-sync-java by commercetools.
the class ProductTransformServiceImplTest method transform_WithAttributeReferences_ShouldReplaceAttributeReferenceIdsWithKeys.
@Test
void transform_WithAttributeReferences_ShouldReplaceAttributeReferenceIdsWithKeys() {
// preparation
final SphereClient sourceClient = mock(SphereClient.class);
final ProductTransformService productTransformService = new ProductTransformServiceImpl(sourceClient, referenceIdToKeyCache);
final List<ProductProjection> productPage = asList(readObjectFromResource("product-key-4.json", Product.class).toProjection(STAGED));
String jsonStringProducts = "{\"results\":[{\"id\":\"53c4a8b4-754f-4b95-b6f2-3e1e70e3d0d2\",\"key\":\"prod1\"}," + "{\"id\":\"53c4a8b4-754f-4b95-b6f2-3e1e70e3d0d6\",\"key\":\"prod2\"}]}";
final ResourceKeyIdGraphQlResult productsResult = SphereJsonUtils.readObject(jsonStringProducts, ResourceKeyIdGraphQlResult.class);
String jsonStringProductTypes = "{\"results\":[{\"id\":\"53c4a8b4-754f-4b95-b6f2-3e1e70e3d0d3\"," + "\"key\":\"prodType1\"}]}";
final ResourceKeyIdGraphQlResult productTypesResult = SphereJsonUtils.readObject(jsonStringProductTypes, ResourceKeyIdGraphQlResult.class);
String jsonStringCategories = "{\"results\":[{\"id\":\"53c4a8b4-754f-4b95-b6f2-3e1e70e3d0d4\",\"key\":\"cat1\"}," + "{\"id\":\"53c4a8b4-754f-4b95-b6f2-3e1e70e3d0d5\",\"key\":\"cat2\"}]}";
final ResourceKeyIdGraphQlResult categoriesResult = SphereJsonUtils.readObject(jsonStringCategories, ResourceKeyIdGraphQlResult.class);
when(sourceClient.execute(any(ResourceIdsGraphQlRequest.class))).thenReturn(CompletableFuture.completedFuture(productsResult)).thenReturn(CompletableFuture.completedFuture(productTypesResult)).thenReturn(CompletableFuture.completedFuture(categoriesResult));
// test
final List<ProductDraft> productsResolved = productTransformService.toProductDrafts(productPage).toCompletableFuture().join();
// assertions
final Optional<ProductDraft> productKey1 = productsResolved.stream().filter(productDraft -> "productKey4".equals(productDraft.getKey())).findFirst();
assertThat(productKey1).hasValueSatisfying(product -> assertThat(product.getMasterVariant().getAttributes()).anySatisfy(attribute -> {
assertThat(attribute.getName()).isEqualTo("productReference");
final JsonNode referenceSet = attribute.getValue();
assertThat(referenceSet).anySatisfy(reference -> assertThat(reference.get("id").asText()).isEqualTo("prod1"));
assertThat(referenceSet).anySatisfy(reference -> assertThat(reference.get("id").asText()).isEqualTo("prod2"));
}));
assertThat(productKey1).hasValueSatisfying(product -> assertThat(product.getMasterVariant().getAttributes()).anySatisfy(attribute -> {
assertThat(attribute.getName()).isEqualTo("categoryReference");
final JsonNode referenceSet = attribute.getValue();
assertThat(referenceSet).anySatisfy(reference -> assertThat(reference.get("id").asText()).isEqualTo("cat1"));
assertThat(referenceSet).anySatisfy(reference -> assertThat(reference.get("id").asText()).isEqualTo("cat2"));
}));
assertThat(productKey1).hasValueSatisfying(product -> assertThat(product.getMasterVariant().getAttributes()).anySatisfy(attribute -> {
assertThat(attribute.getName()).isEqualTo("productTypeReference");
assertThat(attribute.getValue().get("id").asText()).isEqualTo("prodType1");
}));
}
use of com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult in project commercetools-sync-java by commercetools.
the class ProductTransformServiceImplTest method transform_WithNonCachedCustomObjectAttributeReference_ShouldFetchAndTransformProduct.
@Test
void transform_WithNonCachedCustomObjectAttributeReference_ShouldFetchAndTransformProduct() {
// preparation
final SphereClient sourceClient = mock(SphereClient.class);
final ProductTransformService productTransformService = new ProductTransformServiceImpl(sourceClient, referenceIdToKeyCache);
final List<ProductProjection> productPage = asList(readObjectFromResource("product-with-unresolved-references.json", Product.class).toProjection(STAGED));
String jsonStringProductTypes = "{\"results\":[{\"id\":\"cda0dbf7-b42e-40bf-8453-241d5b587f93\"," + "\"key\":\"productTypeKey\"}]}";
final ResourceKeyIdGraphQlResult productTypesResult = SphereJsonUtils.readObject(jsonStringProductTypes, ResourceKeyIdGraphQlResult.class);
when(sourceClient.execute(any(ResourceIdsGraphQlRequest.class))).thenReturn(CompletableFuture.completedFuture(productTypesResult));
mockAttributeCustomObjectReference(sourceClient);
// test
final List<ProductDraft> productsResolved = productTransformService.toProductDrafts(productPage).join();
final Optional<ProductDraft> productKey1 = productsResolved.stream().filter(productDraft -> "productKeyResolved".equals(productDraft.getKey())).findFirst();
assertThat(productKey1).hasValueSatisfying(product -> assertThat(product.getMasterVariant().getAttributes()).anySatisfy(attribute -> {
assertThat(attribute.getName()).isEqualTo("customObjectReference");
}));
verify(sourceClient, times(1)).execute(any(CustomObjectQuery.class));
}
use of com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult in project commercetools-sync-java by commercetools.
the class ProductTransformServiceImplTest method transform_ProductWithProductTypeReferencesWithNullKey_ShouldReplaceReferencesKeyValueWithPlaceHolder.
@Test
void transform_ProductWithProductTypeReferencesWithNullKey_ShouldReplaceReferencesKeyValueWithPlaceHolder() {
// preparation
final SphereClient sourceClient = mock(SphereClient.class);
final ProductTransformService productTransformService = new ProductTransformServiceImpl(sourceClient, referenceIdToKeyCache);
final List<ProductProjection> productPage = asList(readObjectFromResource("product-with-unresolved-references.json", Product.class).toProjection(STAGED));
String jsonStringProductTypes = "{\"results\":[{\"id\":\"cda0dbf7-b42e-40bf-8453-241d5b587f93\"," + "\"key\":" + null + "}]}";
final ResourceKeyIdGraphQlResult productTypesResult = SphereJsonUtils.readObject(jsonStringProductTypes, ResourceKeyIdGraphQlResult.class);
when(sourceClient.execute(any(ResourceIdsGraphQlRequest.class))).thenReturn(CompletableFuture.completedFuture(productTypesResult));
mockAttributeCustomObjectReference(sourceClient);
// test
final List<ProductDraft> productsResolved = productTransformService.toProductDrafts(productPage).toCompletableFuture().join();
// assertions
final Optional<ProductDraft> productKey1 = productsResolved.stream().filter(productDraft -> "productKeyResolved".equals(productDraft.getKey())).findFirst();
assertThat(productKey1).hasValueSatisfying(productDraft -> assertThat(productDraft.getProductType().getKey()).isEqualTo(KEY_IS_NOT_SET_PLACE_HOLDER));
}
use of com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult in project commercetools-sync-java by commercetools.
the class BaseServiceImplTest method cacheKeysToIds_WithCachedKeysExceedingCacheSize_ShouldNotReturnLeastUsedKeys.
@Test
void cacheKeysToIds_WithCachedKeysExceedingCacheSize_ShouldNotReturnLeastUsedKeys() {
// preparation
final PagedQueryResult pagedQueryResult = mock(PagedQueryResult.class);
final ProductProjection product1 = mock(ProductProjection.class);
when(product1.getKey()).thenReturn("key-1");
when(product1.getId()).thenReturn("id-1");
final ProductProjection product2 = mock(ProductProjection.class);
when(product2.getKey()).thenReturn("key-2");
when(product2.getId()).thenReturn("id-2");
when(pagedQueryResult.getResults()).thenReturn(Arrays.asList(product1, product2));
final ResourceKeyIdGraphQlResult resourceKeyIdGraphQlResult = mock(ResourceKeyIdGraphQlResult.class);
final ResourceKeyId resourceKeyId = mock(ResourceKeyId.class);
when(resourceKeyId.getKey()).thenReturn("testKey");
when(resourceKeyId.getId()).thenReturn("testId");
when(resourceKeyIdGraphQlResult.getResults()).thenReturn(singleton(resourceKeyId));
when(client.execute(any())).thenReturn(completedFuture(pagedQueryResult)).thenReturn(completedFuture(resourceKeyIdGraphQlResult));
service.fetchMatchingProductsByKeys(Arrays.asList("key-1", "key-2").stream().collect(Collectors.toSet()));
// access the first added cache entry
service.getIdFromCacheOrFetch("key-1");
// test
final Map<String, String> optional = service.cacheKeysToIds(singleton("testKey")).toCompletableFuture().join();
// assertions
assertThat(optional).containsExactly(MapEntry.entry("key-1", "id-1"), MapEntry.entry("testKey", "testId"));
verify(client, times(1)).execute(any(ProductProjectionQuery.class));
verify(client, times(1)).execute(any(ResourceKeyIdGraphQlRequest.class));
}
Aggregations