Skip to content

Commit

Permalink
DEVX-250 migrate cart discount syncer (#504)
Browse files Browse the repository at this point in the history
  • Loading branch information
lojzatran authored Oct 26, 2023
1 parent 38bc4e1 commit 3910570
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// This class compiles but not tested yet
// TODO: Test class and adjust logic if needed
public final class CartDiscountSyncer
extends Syncer<
CartDiscount,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,63 @@
package com.commercetools.project.sync.cartdiscount;

import static com.commercetools.project.sync.util.TestUtils.*;
import static com.commercetools.sync.cartdiscounts.utils.CartDiscountTransformUtils.toCartDiscountDrafts;
import static java.lang.String.format;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.commercetools.api.client.ByProjectKeyCartDiscountsGet;
import com.commercetools.api.client.ByProjectKeyCartDiscountsRequestBuilder;
import com.commercetools.api.client.ProjectApiRoot;
import com.commercetools.api.models.cart_discount.CartDiscount;
import com.commercetools.api.models.cart_discount.CartDiscountDraft;
import com.commercetools.api.models.cart_discount.CartDiscountPagedQueryResponse;
import com.commercetools.sync.cartdiscounts.CartDiscountSync;
import com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl;
import com.commercetools.sync.commons.utils.ReferenceIdToKeyCache;
import io.vrap.rmf.base.client.ApiHttpResponse;
import java.time.Clock;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
import uk.org.lidalia.slf4jtest.LoggingEvent;
import uk.org.lidalia.slf4jtest.TestLogger;
import uk.org.lidalia.slf4jtest.TestLoggerFactory;

// These tests aren't migrated
// TODO: Migrate tests
class CartDiscountSyncerTest {

final ReferenceIdToKeyCache referenceIdToKeyCache = new CaffeineReferenceIdToKeyCacheImpl();

/* @Test
@Test
void of_ShouldCreateCartDiscountSyncerInstance() {
final ProjectApiRoot sourceClient = mock(ProjectApiRoot.class);
final ByProjectKeyCartDiscountsRequestBuilder byProjectKeyCartDiscountsRequestBuilder = mock();
when(sourceClient.cartDiscounts()).thenReturn(byProjectKeyCartDiscountsRequestBuilder);
final ByProjectKeyCartDiscountsGet byProjectKeyCartDiscountsGet = mock();
when(byProjectKeyCartDiscountsRequestBuilder.get()).thenReturn(byProjectKeyCartDiscountsGet);

// test
final CartDiscountSyncer cartDiscountSyncer =
CartDiscountSyncer.of(mock(SphereClient.class), mock(SphereClient.class), getMockedClock());
CartDiscountSyncer.of(sourceClient, mock(ProjectApiRoot.class), getMockedClock());

// assertions
assertThat(cartDiscountSyncer).isNotNull();
assertThat(cartDiscountSyncer.getQuery()).isEqualTo(CartDiscountQuery.of());
assertThat(cartDiscountSyncer.getQuery()).isEqualTo(byProjectKeyCartDiscountsGet);
assertThat(cartDiscountSyncer.getSync()).isExactlyInstanceOf(CartDiscountSync.class);
}

@Test
void transform_ShouldReplaceCartDiscountReferenceIdsWithKeys() {
// preparation
final SphereClient sourceClient = mock(SphereClient.class);
final ProjectApiRoot sourceClient = mock(ProjectApiRoot.class);
final CartDiscountSyncer cartDiscountSyncer =
CartDiscountSyncer.of(sourceClient, mock(SphereClient.class), getMockedClock());
CartDiscountSyncer.of(sourceClient, mock(ProjectApiRoot.class), getMockedClock());
final List<CartDiscount> cartDiscountPage =
asList(
readObjectFromResource("cart-discount-key-1.json", CartDiscount.class),
Expand All @@ -37,7 +68,10 @@ void transform_ShouldReplaceCartDiscountReferenceIdsWithKeys() {
.map(cartDiscount -> cartDiscount.getCustom().getType().getId())
.collect(Collectors.toList());
mockResourceIdsGraphQlRequest(
sourceClient, "4db98ea6-38dc-4ccb-b20f-466e1566fd03", "test cart discount custom type");
sourceClient,
"typeDefinitions",
"4db98ea6-38dc-4ccb-b20f-466e1566fd03",
"test cart discount custom type");

// test
final CompletionStage<List<CartDiscountDraft>> draftsFromPageStage =
Expand All @@ -54,35 +88,34 @@ void transform_ShouldReplaceCartDiscountReferenceIdsWithKeys() {
assertThat(draftsFromPageStage).isCompletedWithValue(expectedResult);
}

@Test
void getQuery_ShouldBuildCartDiscountQuery() {
// preparation
final CartDiscountSyncer cartDiscountSyncer =
CartDiscountSyncer.of(mock(SphereClient.class), mock(SphereClient.class), getMockedClock());
// test
final CartDiscountQuery query = cartDiscountSyncer.getQuery();
// assertion
assertThat(query).isEqualTo(CartDiscountQuery.of());
}
@Test
void syncWithError_ShouldCallErrorCallback() {
final TestLogger syncerTestLogger = TestLoggerFactory.getTestLogger(CartDiscountSyncer.class);
// preparation: cart discount with no key is synced
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 ProjectApiRoot sourceClient = mock(ProjectApiRoot.class);
final ProjectApiRoot targetClient = mock(ProjectApiRoot.class);
final List<CartDiscount> cartDiscounts =
Collections.singletonList(
readObjectFromResource("cart-discount-no-key.json", CartDiscount.class));

final PagedQueryResult<CartDiscount> pagedQueryResult = mock(PagedQueryResult.class);
when(pagedQueryResult.getResults()).thenReturn(cartDiscounts);
when(sourceClient.execute(any(CartDiscountQuery.class)))
.thenReturn(CompletableFuture.completedFuture(pagedQueryResult));
final ApiHttpResponse<CartDiscountPagedQueryResponse> apiHttpResponse =
mock(ApiHttpResponse.class);
final CartDiscountPagedQueryResponse cartDiscountPagedQueryResponse =
mock(CartDiscountPagedQueryResponse.class);
when(cartDiscountPagedQueryResponse.getResults()).thenReturn(cartDiscounts);
when(apiHttpResponse.getBody()).thenReturn(cartDiscountPagedQueryResponse);

final ByProjectKeyCartDiscountsRequestBuilder byProjectKeyCartDiscountsRequestBuilder = mock();
when(sourceClient.cartDiscounts()).thenReturn(byProjectKeyCartDiscountsRequestBuilder);
final ByProjectKeyCartDiscountsGet byProjectKeyCartDiscountsGet = mock();
when(byProjectKeyCartDiscountsRequestBuilder.get()).thenReturn(byProjectKeyCartDiscountsGet);
when(byProjectKeyCartDiscountsGet.withLimit(anyInt())).thenReturn(byProjectKeyCartDiscountsGet);
when(byProjectKeyCartDiscountsGet.withSort(anyString()))
.thenReturn(byProjectKeyCartDiscountsGet);
when(byProjectKeyCartDiscountsGet.withWithTotal(anyBoolean()))
.thenReturn(byProjectKeyCartDiscountsGet);
when(byProjectKeyCartDiscountsGet.execute())
.thenReturn(CompletableFuture.completedFuture(apiHttpResponse));

// test
final CartDiscountSyncer cartDiscountSyncer =
Expand All @@ -96,6 +129,8 @@ void syncWithError_ShouldCallErrorCallback() {
"Error when trying to sync cart discount. Existing key: <<not present>>. Update actions: []");
assertThat(errorLog.getThrowable().get().getMessage())
.isEqualTo(
"CartDiscountDraft with name: LocalizedString(en -> 1-month prepay(Go Big)) doesn't have a key. Please make sure all cart discount drafts have keys.");
}*/
format(
"CartDiscountDraft with name: %s doesn't have a key. Please make sure all cart discount drafts have keys.",
cartDiscounts.get(0).getName().toString()));
}
}

0 comments on commit 3910570

Please sign in to comment.