Skip to content

Commit

Permalink
Merge branch 'DEVX-256_migrate-tax-category-syncer' into DEVX-245_mig…
Browse files Browse the repository at this point in the history
…rate-it-for-cli-runner
  • Loading branch information
salander85 committed Oct 31, 2023
2 parents d7f6577 + 65d3ceb commit 1c2d5a3
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,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 TaxCategorySyncer
extends Syncer<
TaxCategory,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,38 @@
package com.commercetools.project.sync.taxcategory;

import static com.commercetools.project.sync.util.TestUtils.getMockedClock;
import static com.commercetools.project.sync.util.TestUtils.readObjectFromResource;
import static java.util.stream.Collectors.toList;
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.ByProjectKeyTaxCategoriesGet;
import com.commercetools.api.client.ProjectApiRoot;
import com.commercetools.api.models.tax_category.TaxCategory;
import com.commercetools.api.models.tax_category.TaxCategoryDraft;
import com.commercetools.api.models.tax_category.TaxCategoryDraftBuilder;
import com.commercetools.api.models.tax_category.TaxCategoryPagedQueryResponse;
import com.commercetools.api.models.tax_category.TaxCategoryPagedQueryResponseBuilder;
import com.commercetools.api.models.tax_category.TaxRate;
import com.commercetools.api.models.tax_category.TaxRateDraft;
import com.commercetools.api.models.tax_category.TaxRateDraftBuilder;
import com.commercetools.sync.taxcategories.TaxCategorySync;
import io.vrap.rmf.base.client.ApiHttpResponse;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
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 TaxCategorySyncerTest {
private final TestLogger syncerTestLogger =
TestLoggerFactory.getTestLogger(TaxCategorySyncer.class);
Expand All @@ -15,97 +42,114 @@ void setup() {
syncerTestLogger.clearAll();
}

// @Test
// void of_ShouldCreateTaxCategorySyncerInstance() {
// // test
// final TaxCategorySyncer taxCategorySyncer =
// TaxCategorySyncer.of(mock(SphereClient.class), mock(SphereClient.class),
// getMockedClock());
//
// // assertions
// assertThat(taxCategorySyncer).isNotNull();
// assertThat(taxCategorySyncer.getQuery()).isEqualTo(TaxCategoryQuery.of());
// assertThat(taxCategorySyncer.getSync()).isInstanceOf(TaxCategorySync.class);
// }
//
// @Test
// void transform_ShouldConvertResourcesToDrafts() {
// // preparation
// final TaxCategorySyncer taxCategorySyncer =
// TaxCategorySyncer.of(mock(SphereClient.class), mock(SphereClient.class),
// getMockedClock());
// final List<TaxCategory> taxCategoryPage =
// asList(
// readObjectFromResource("tax-category-key-1.json", TaxCategory.class),
// readObjectFromResource("tax-category-key-2.json", TaxCategory.class));
//
// // test
// final CompletionStage<List<TaxCategoryDraft>> draftsFromPageStage =
// taxCategorySyncer.transform(taxCategoryPage);
//
// // assertions
// assertThat(draftsFromPageStage)
// .isCompletedWithValue(
// taxCategoryPage.stream()
// .map(
// taxCategory -> {
// List<TaxRateDraft> taxRateDrafts =
// taxCategory.getTaxRates().stream()
// .map(taxRate -> TaxRateDraftBuilder.of(taxRate).build())
// .collect(Collectors.toList());
// return TaxCategoryDraftBuilder.of(
// taxCategory.getName(), taxRateDrafts,
// taxCategory.getDescription())
// .key(taxCategory.getKey());
// })
// .map(TaxCategoryDraftBuilder::build)
// .collect(toList()));
// }
//
// @Test
// void getQuery_ShouldBuildTaxCategoryQuery() {
// // preparation
// final TaxCategorySyncer taxCategorySyncer =
// TaxCategorySyncer.of(mock(SphereClient.class), mock(SphereClient.class),
// getMockedClock());
//
// // test
// final TaxCategoryQuery query = taxCategorySyncer.getQuery();
//
// // assertion
// assertThat(query).isEqualTo(TaxCategoryQuery.of());
// }
//
// @Test
// void syncWithError_WhenNoKeyIsProvided_ShouldCallErrorCallback() {
// // preparation
// 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<TaxCategory> taxCategoryPage =
// asList(readObjectFromResource("tax-category-without-key.json", TaxCategory.class));
//
// final PagedQueryResult<TaxCategory> pagedQueryResult = mock(PagedQueryResult.class);
// when(pagedQueryResult.getResults()).thenReturn(taxCategoryPage);
// when(sourceClient.execute(any(TaxCategoryQuery.class)))
// .thenReturn(CompletableFuture.completedFuture(pagedQueryResult));
//
// // test
// final TaxCategorySyncer taxCategorySyncer =
// TaxCategorySyncer.of(sourceClient, targetClient, mock(Clock.class));
// taxCategorySyncer.sync(null, true).toCompletableFuture().join();
//
// // assertion
// final LoggingEvent errorLog = syncerTestLogger.getAllLoggingEvents().get(1);
// assertThat(errorLog.getMessage())
// .isEqualTo(
// "Error when trying to sync tax category. Existing key: <<not present>>. Update
// actions: []");
// assertThat(errorLog.getThrowable().get().getMessage())
// .isEqualTo(
// "TaxCategoryDraft with name: Standard tax category doesn't have a key. "
// + "Please make sure all tax category drafts have keys.");
// }
@Test
void of_ShouldCreateTaxCategorySyncerInstance() {
// preparation
final ProjectApiRoot sourceClient = mock(ProjectApiRoot.class);
when(sourceClient.taxCategories()).thenReturn(mock());
when(sourceClient.taxCategories().get()).thenReturn(mock());

// test
final TaxCategorySyncer taxCategorySyncer =
TaxCategorySyncer.of(sourceClient, mock(ProjectApiRoot.class), getMockedClock());

// assertions
assertThat(taxCategorySyncer).isNotNull();
assertThat(taxCategorySyncer.getQuery()).isInstanceOf(ByProjectKeyTaxCategoriesGet.class);
assertThat(taxCategorySyncer.getSync()).isInstanceOf(TaxCategorySync.class);
}

@Test
void transform_ShouldConvertResourcesToDrafts() {
// preparation
final TaxCategorySyncer taxCategorySyncer =
TaxCategorySyncer.of(
mock(ProjectApiRoot.class), mock(ProjectApiRoot.class), getMockedClock());
final List<TaxCategory> taxCategoryPage =
List.of(
readObjectFromResource("tax-category-key-1.json", TaxCategory.class),
readObjectFromResource("tax-category-key-2.json", TaxCategory.class));

// test
final CompletionStage<List<TaxCategoryDraft>> draftsFromPageStage =
taxCategorySyncer.transform(taxCategoryPage);

// assertions
assertThat(draftsFromPageStage)
.isCompletedWithValue(
taxCategoryPage.stream()
.map(
taxCategory -> {
final List<TaxRateDraft> taxRateDrafts =
convertTaxRateToTaxRateDraft(taxCategory.getRates());
return TaxCategoryDraftBuilder.of()
.name(taxCategory.getName())
.rates(taxRateDrafts)
.description(taxCategory.getDescription())
.key(taxCategory.getKey());
})
.map(TaxCategoryDraftBuilder::build)
.collect(toList()));
}

private List<TaxRateDraft> convertTaxRateToTaxRateDraft(@Nonnull final List<TaxRate> taxRates) {

return taxRates.stream()
.map(
taxRate ->
TaxRateDraftBuilder.of()
.name(taxRate.getName())
.country(taxRate.getCountry())
.state(taxRate.getState())
.includedInPrice(taxRate.getIncludedInPrice())
.amount(taxRate.getAmount())
.key(taxRate.getKey())
.subRates(taxRate.getSubRates())
.build())
.collect(Collectors.toList());
}

@Test
void syncWithError_WhenNoKeyIsProvided_ShouldCallErrorCallback() {
// preparation
final ProjectApiRoot sourceClient = mock(ProjectApiRoot.class);
final List<TaxCategory> taxCategoryPage =
List.of(readObjectFromResource("tax-category-without-key.json", TaxCategory.class));

final TaxCategoryPagedQueryResponse queryResponse =
TaxCategoryPagedQueryResponseBuilder.of()
.results(taxCategoryPage)
.limit(20L)
.offset(0L)
.count(1L)
.build();

final ApiHttpResponse apiHttpResponse = mock(ApiHttpResponse.class);
when(apiHttpResponse.getBody()).thenReturn(queryResponse);
final ByProjectKeyTaxCategoriesGet byProjectKeyTaxCategoriesGet =
mock(ByProjectKeyTaxCategoriesGet.class);
when(sourceClient.taxCategories()).thenReturn(mock());
when(sourceClient.taxCategories().get()).thenReturn(byProjectKeyTaxCategoriesGet);
when(byProjectKeyTaxCategoriesGet.withLimit(anyInt())).thenReturn(byProjectKeyTaxCategoriesGet);
when(byProjectKeyTaxCategoriesGet.withWithTotal(anyBoolean()))
.thenReturn(byProjectKeyTaxCategoriesGet);
when(byProjectKeyTaxCategoriesGet.withSort(anyString()))
.thenReturn(byProjectKeyTaxCategoriesGet);
when(byProjectKeyTaxCategoriesGet.execute())
.thenReturn(CompletableFuture.completedFuture(apiHttpResponse));
// test
final TaxCategorySyncer taxCategorySyncer =
TaxCategorySyncer.of(sourceClient, mock(ProjectApiRoot.class), getMockedClock());
taxCategorySyncer.sync(null, true).toCompletableFuture().join();

// assertion
final LoggingEvent errorLog = syncerTestLogger.getAllLoggingEvents().get(1);
assertThat(errorLog.getMessage())
.isEqualTo(
"Error when trying to sync tax category. Existing key: <<not present>>. Update actions: []");
assertThat(errorLog.getThrowable().get().getMessage())
.isEqualTo(
"TaxCategoryDraft with name: Standard tax category doesn't have a key. "
+ "Please make sure all tax category drafts have keys.");
}
}

0 comments on commit 1c2d5a3

Please sign in to comment.