Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate shoppinglist syncer #511

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,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 ShoppingListSyncer
extends Syncer<
ShoppingList,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
package com.commercetools.project.sync.shoppinglist;

import static com.commercetools.project.sync.util.TestUtils.mockResourceIdsGraphQlRequest;
import static com.commercetools.project.sync.util.TestUtils.readObjectFromResource;
import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.commercetools.api.client.ByProjectKeyShoppingListsGet;
import com.commercetools.api.client.ByProjectKeyShoppingListsRequestBuilder;
import com.commercetools.api.client.ProjectApiRoot;
import com.commercetools.api.defaultconfig.ApiRootBuilder;
import com.commercetools.api.models.shopping_list.ShoppingList;
import com.commercetools.api.models.shopping_list.ShoppingListDraft;
import com.commercetools.api.models.shopping_list.ShoppingListPagedQueryResponse;
import com.commercetools.api.models.shopping_list.ShoppingListPagedQueryResponseBuilder;
import com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl;
import com.commercetools.sync.commons.utils.ReferenceIdToKeyCache;
import com.commercetools.sync.shoppinglists.ShoppingListSync;
import com.commercetools.sync.shoppinglists.utils.ShoppingListTransformUtils;
import io.vrap.rmf.base.client.ApiHttpResponse;
import java.time.Clock;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import org.junit.jupiter.api.BeforeEach;
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 ShoppingListSyncerTest {
private final TestLogger syncerTestLogger =
TestLoggerFactory.getTestLogger(ShoppingListSyncer.class);
Expand All @@ -19,88 +44,99 @@ void setup() {
syncerTestLogger.clearAll();
}

// @Test
// void of_ShouldCreateShoppingListSyncerInstance() {
// // test
// final ShoppingListSyncer shoppingListSyncer =
// ShoppingListSyncer.of(
// mock(SphereClient.class), mock(SphereClient.class), mock(Clock.class));
//
// // assertion
// assertThat(shoppingListSyncer).isNotNull();
// assertThat(shoppingListSyncer.getSync()).isInstanceOf(ShoppingListSync.class);
// }
//
// @Test
// void transform_ShouldReplaceShoppingListReferenceIdsWithKeys() {
// // preparation
// final SphereClient sourceClient = mock(SphereClient.class);
// final ShoppingListSyncer shoppingListSyncer =
// ShoppingListSyncer.of(sourceClient, mock(SphereClient.class), mock(Clock.class));
// final List<ShoppingList> shoppingList =
// Collections.singletonList(readObjectFromResource("shopping-list.json",
// ShoppingList.class));
//
// mockResourceIdsGraphQlRequest(
// sourceClient, "5ebfa80e-f4aa-4c0b-be64-e348e09a855a", "customTypeKey");
//
// // test
// final CompletionStage<List<ShoppingListDraft>> draftsFromPageStage =
// shoppingListSyncer.transform(shoppingList);
//
// // assertion
// assertThat(draftsFromPageStage)
// .isCompletedWithValue(
// ShoppingListTransformUtils.toShoppingListDrafts(
// sourceClient, referenceIdToKeyCache, shoppingList)
// .join());
// }
//
// @Test
// void getQuery_ShouldBuildShoppingListQuery() {
// final ShoppingListSyncer shoppingListSyncer =
// ShoppingListSyncer.of(
// mock(SphereClient.class), mock(SphereClient.class), mock(Clock.class));
//
// // assertion
// final ShoppingListQuery query = shoppingListSyncer.getQuery();
//
// assertThat(query.expansionPaths()).containsExactly(ExpansionPath.of("lineItems[*].variant"));
// }
//
// @Test
// void syncWithError_ShouldCallErrorCallback() {
// // preparation: shoppingList 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 List<ShoppingList> shoppingLists =
// Collections.singletonList(
// readObjectFromResource("shopping-list-no-key.json", ShoppingList.class));
//
// final PagedQueryResult<ShoppingList> pagedQueryResult = mock(PagedQueryResult.class);
// when(pagedQueryResult.getResults()).thenReturn(shoppingLists);
// when(sourceClient.execute(any(ShoppingListQuery.class)))
// .thenReturn(CompletableFuture.completedFuture(pagedQueryResult));
//
// mockResourceIdsGraphQlRequest(
// sourceClient, "5ebfa80e-f4aa-4c0b-be64-e348e09a855a", "customTypeKey");
//
// // test
// final ShoppingListSyncer shoppingListSyncer =
// ShoppingListSyncer.of(sourceClient, targetClient, mock(Clock.class));
// shoppingListSyncer.sync(null, true).toCompletableFuture().join();
//
// // assertion
// final LoggingEvent errorLog = syncerTestLogger.getAllLoggingEvents().get(1);
// assertThat(errorLog.getMessage())
// .isEqualTo(
// "Error when trying to sync shoppingList. Existing key: <<not present>>. Update
// actions: []");
// assertThat(errorLog.getThrowable().get().getMessage())
// .isEqualTo(
// "ShoppingListDraft with name: LocalizedString(en -> shoppingList-name-1) doesn't
// have a key. Please make sure all shopping list drafts have keys.");
// }
@Test
void of_ShouldCreateShoppingListSyncerInstance() {
// test
final ShoppingListSyncer shoppingListSyncer =
ShoppingListSyncer.of(
mock(ProjectApiRoot.class), mock(ProjectApiRoot.class), mock(Clock.class));

// assertion
assertThat(shoppingListSyncer).isNotNull();
assertThat(shoppingListSyncer.getSync()).isInstanceOf(ShoppingListSync.class);
}

@Test
void transform_ShouldReplaceShoppingListReferenceIdsWithKeys() {
// preparation
final ProjectApiRoot sourceClient = mock(ProjectApiRoot.class);
final ShoppingListSyncer shoppingListSyncer =
ShoppingListSyncer.of(sourceClient, mock(ProjectApiRoot.class), mock(Clock.class));
final List<ShoppingList> shoppingList =
List.of(readObjectFromResource("shopping-list.json", ShoppingList.class));

mockResourceIdsGraphQlRequest(
sourceClient, "shoppingLists", "5ebfa80e-f4aa-4c0b-be64-e348e09a855a", "customTypeKey");

// test
final CompletionStage<List<ShoppingListDraft>> draftsFromPageStage =
shoppingListSyncer.transform(shoppingList);

// assertion
assertThat(draftsFromPageStage)
.isCompletedWithValue(
ShoppingListTransformUtils.toShoppingListDrafts(
sourceClient, referenceIdToKeyCache, shoppingList)
.join());
}

@Test
void getQuery_ShouldBuildShoppingListQuery() {
final ProjectApiRoot apiRoot =
ApiRootBuilder.of().withApiBaseUrl("baseUrl").build("testProjectKey");
final ShoppingListSyncer shoppingListSyncer =
ShoppingListSyncer.of(apiRoot, apiRoot, mock(Clock.class));

// assertion
final ByProjectKeyShoppingListsGet shoppingListsGet = shoppingListSyncer.getQuery();

assertThat(shoppingListsGet.getExpand()).containsExactly("lineItems[*].variant");
}

@Test
void syncWithError_ShouldCallErrorCallback() {
// preparation: shoppingList with no key is synced
final ProjectApiRoot sourceClient = mock(ProjectApiRoot.class);
final List<ShoppingList> shoppingLists =
List.of(readObjectFromResource("shopping-list-no-key.json", ShoppingList.class));
final ByProjectKeyShoppingListsRequestBuilder projectKeyShoppingListsRequestBuilder = mock();
when(sourceClient.shoppingLists()).thenReturn(projectKeyShoppingListsRequestBuilder);
final ByProjectKeyShoppingListsGet shoppingListsGet = mock();
when(shoppingListsGet.addExpand(anyString())).thenReturn(shoppingListsGet);
when(shoppingListsGet.withLimit(anyInt())).thenReturn(shoppingListsGet);
when(shoppingListsGet.withWithTotal(anyBoolean())).thenReturn(shoppingListsGet);
when(shoppingListsGet.withSort(anyString())).thenReturn(shoppingListsGet);

final ShoppingListPagedQueryResponse shoppingListPagedQueryResponse =
ShoppingListPagedQueryResponseBuilder.of()
.results(shoppingLists)
.limit(20L)
.offset(0L)
.count(1L)
.build();
final ApiHttpResponse apiHttpResponse = mock(ApiHttpResponse.class);
when(apiHttpResponse.getBody()).thenReturn(shoppingListPagedQueryResponse);
when(shoppingListsGet.execute()).thenReturn(CompletableFuture.completedFuture(apiHttpResponse));
when(sourceClient.shoppingLists().get()).thenReturn(shoppingListsGet);

mockResourceIdsGraphQlRequest(
sourceClient, "shoppingLists", "5ebfa80e-f4aa-4c0b-be64-e348e09a855a", "customTypeKey");

// test
final ShoppingListSyncer shoppingListSyncer =
ShoppingListSyncer.of(sourceClient, mock(ProjectApiRoot.class), mock(Clock.class));
shoppingListSyncer.sync(null, true).toCompletableFuture().join();

// assertion
final LoggingEvent errorLog = syncerTestLogger.getAllLoggingEvents().get(1);
assertThat(errorLog.getMessage())
.isEqualTo(
"Error when trying to sync shoppingList. Existing key: <<not present>>. Update actions: []");
assertThat(errorLog.getThrowable().get().getMessage())
.isEqualTo(
format(
"ShoppingListDraft with name: %s doesn't have a key. Please make sure all shopping list drafts have keys"
+ ".",
shoppingLists.get(0).getName().toString()));
}
}