Skip to content

Commit

Permalink
Approved tasks from syncer factory base (#497)
Browse files Browse the repository at this point in the history
* Migrate SyncerFactory

* Migrate client utils and rename

* Migrate CliRunner

* Migrate unittests for SyncerFactory

* Migrate unittests for CLIRunner

* Cleanup test code

* DEVX-259 fix tests (#494)

* DEVX-246 migrate solution info (#491)

---------

Co-authored-by: Lam Tran <[email protected]>
  • Loading branch information
salander85 and lojzatran authored Oct 23, 2023
1 parent f389920 commit 0b1a239
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,16 @@ public CompletableFuture<Optional<LastSyncCustomObject>> getLastSyncCustomObject
.withContainerAndKey(containerName, sourceProjectKey)
.get()
.execute()
.handle(
(customObjectApiHttpResponse, throwable) -> {
if (throwable != null) {
return Optional.empty();
} else {
final CustomObject responseBody = customObjectApiHttpResponse.getBody();
final ObjectMapper objectMapper = JsonUtils.getConfiguredObjectMapper();
final LastSyncCustomObject lastSyncCustomObject =
responseBody == null
? null
: objectMapper.convertValue(
responseBody.getValue(), LastSyncCustomObject.class);
return Optional.ofNullable(lastSyncCustomObject);
}
.thenApply(
(customObjectApiHttpResponse) -> {
final CustomObject responseBody = customObjectApiHttpResponse.getBody();
final ObjectMapper objectMapper = JsonUtils.getConfiguredObjectMapper();
final LastSyncCustomObject lastSyncCustomObject =
responseBody == null
? null
: objectMapper.convertValue(
responseBody.getValue(), LastSyncCustomObject.class);
return Optional.ofNullable(lastSyncCustomObject);
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.commercetools.project.sync.util;

import io.sphere.sdk.client.SolutionInfo;
import io.vrap.rmf.base.client.SolutionInfo;

public final class ProjectSyncSolutionInfo extends SolutionInfo {
private static final String LIB_NAME = "commercetools-project-sync";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import java.time.ZonedDateTime;
import org.junit.jupiter.api.Test;

// These tests do compile but not valid
// TODO: Make tests valid
class LastSyncCustomObjectTest {

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.when;

import com.commercetools.api.client.ByProjectKeyCustomObjectsByContainerByKeyGet;
import com.commercetools.api.client.ByProjectKeyCustomObjectsByContainerByKeyRequestBuilder;
import com.commercetools.api.client.ByProjectKeyCustomObjectsPost;
import com.commercetools.api.client.ByProjectKeyCustomObjectsRequestBuilder;
import com.commercetools.api.client.ProjectApiRoot;
import com.commercetools.api.models.custom_object.CustomObject;
import com.commercetools.api.models.custom_object.CustomObjectDraft;
Expand All @@ -19,41 +23,60 @@
import io.vrap.rmf.base.client.ApiHttpResponse;
import io.vrap.rmf.base.client.error.BadGatewayException;
import io.vrap.rmf.base.client.utils.CompletableFutureUtils;
import java.time.Duration;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;

// These tests do compile but not valid
// TODO: Make tests valid
class CustomObjectServiceImplTest {

private ProjectApiRoot ctpClient;

@SuppressWarnings("unchecked")
private static CustomObject STRING_CUSTOM_OBJECT = mock(CustomObject.class);
private static final ZonedDateTime now = ZonedDateTime.now();

@SuppressWarnings("unchecked")
private static LastSyncCustomObject LAST_SYNC_CUSTOM_OBJECT = mock(LastSyncCustomObject.class);
private static final CustomObject STRING_CUSTOM_OBJECT = mock(CustomObject.class);

private static final LastSyncCustomObject<ProductSyncStatistics> LAST_SYNC_CUSTOM_OBJECT =
LastSyncCustomObject.of(now, new ProductSyncStatistics(), 0);

private final ByProjectKeyCustomObjectsRequestBuilder byProjectKeyCustomObjectsRequestBuilder =
mock();
private final ByProjectKeyCustomObjectsPost byProjectKeyCustomObjectsPost = mock();
private final ByProjectKeyCustomObjectsByContainerByKeyGet
byProjectKeyCustomObjectsByContainerByKeyGet = mock();

private ApiHttpResponse apiHttpResponse;
private ApiHttpResponse<CustomObject> apiHttpResponse;

@BeforeAll
static void setup() {
when(STRING_CUSTOM_OBJECT.getLastModifiedAt()).thenReturn(ZonedDateTime.now());
when(LAST_SYNC_CUSTOM_OBJECT.getLastSyncTimestamp()).thenReturn(ZonedDateTime.now());
when(STRING_CUSTOM_OBJECT.getLastModifiedAt()).thenReturn(now);
when(STRING_CUSTOM_OBJECT.getValue()).thenReturn(LAST_SYNC_CUSTOM_OBJECT);
}

@BeforeEach
void init() {
apiHttpResponse = mock(ApiHttpResponse.class);
ctpClient = mock(ProjectApiRoot.class);

when(ctpClient.customObjects()).thenReturn(byProjectKeyCustomObjectsRequestBuilder);
when(byProjectKeyCustomObjectsRequestBuilder.post(any(CustomObjectDraft.class)))
.thenReturn(byProjectKeyCustomObjectsPost);

final ByProjectKeyCustomObjectsByContainerByKeyRequestBuilder
byProjectKeyCustomObjectsByContainerByKeyRequestBuilder = mock();
when(byProjectKeyCustomObjectsRequestBuilder.withContainerAndKey(anyString(), anyString()))
.thenReturn(byProjectKeyCustomObjectsByContainerByKeyRequestBuilder);
when(byProjectKeyCustomObjectsByContainerByKeyRequestBuilder.get())
.thenReturn(byProjectKeyCustomObjectsByContainerByKeyGet);
}

@AfterEach
Expand All @@ -66,7 +89,7 @@ void cleanup() {
void getCurrentCtpTimestamp_OnSuccessfulUpsert_ShouldCompleteWithCtpTimestamp() {
// preparation
when(apiHttpResponse.getBody()).thenReturn(STRING_CUSTOM_OBJECT);
when(ctpClient.customObjects().post(any(CustomObjectDraft.class)).execute())
when(byProjectKeyCustomObjectsPost.execute())
.thenReturn(CompletableFuture.completedFuture(apiHttpResponse));
final CustomObjectService customObjectService = new CustomObjectServiceImpl(ctpClient);

Expand All @@ -83,7 +106,8 @@ void getCurrentCtpTimestamp_OnSuccessfulUpsert_ShouldCompleteWithCtpTimestamp()
void getCurrentCtpTimestamp_OnFailedUpsert_ShouldCompleteExceptionally() {
// preparation
final BadGatewayException badGatewayException = TestUtils.createBadGatewayException();
when(ctpClient.customObjects().post(any(CustomObjectDraft.class)).execute())

when(byProjectKeyCustomObjectsPost.execute())
.thenReturn(CompletableFutureUtils.exceptionallyCompletedFuture(badGatewayException));
final CustomObjectService customObjectService = new CustomObjectServiceImpl(ctpClient);

Expand All @@ -93,36 +117,56 @@ void getCurrentCtpTimestamp_OnFailedUpsert_ShouldCompleteExceptionally() {

// assertions
assertThat(ctpTimestamp)
.hasFailedWithThrowableThat()
.isInstanceOf(BadGatewayException.class)
.hasMessageContaining("CTP error!");
.failsWithin(Duration.ZERO)
.withThrowableOfType(ExecutionException.class)
.withCauseExactlyInstanceOf(BadGatewayException.class)
.withMessageContaining("test");
}

@Test
@SuppressWarnings("unchecked")
@Disabled("https://commercetools.atlassian.net/browse/DEVX-272")
void
getLastSyncCustomObject_OnSuccessfulQueryWithResults_ShouldCompleteWithLastSyncCustomObject() {
// preparation
when(apiHttpResponse.getBody()).thenReturn(STRING_CUSTOM_OBJECT);
when(ctpClient.customObjects().withContainerAndKey(anyString(), anyString()).get().execute())

when(byProjectKeyCustomObjectsByContainerByKeyGet.execute())
.thenReturn(CompletableFuture.completedFuture(apiHttpResponse));

final CustomObjectService customObjectService = new CustomObjectServiceImpl(ctpClient);

// test
final CompletionStage<Optional<LastSyncCustomObject>> lastSyncCustomObject =
final CompletionStage<Optional<LastSyncCustomObject>> lastSyncCustomObjectCompletionStage =
customObjectService.getLastSyncCustomObject("foo", "bar", DEFAULT_RUNNER_NAME);

final LastSyncCustomObject lastSyncCustomObject =
lastSyncCustomObjectCompletionStage.toCompletableFuture().join().get();

// assertions
assertThat(lastSyncCustomObject).isCompletedWithValue(Optional.of(LAST_SYNC_CUSTOM_OBJECT));
assertThat(
lastSyncCustomObject
.getLastSyncTimestamp()
.withZoneSameInstant(ZoneId.of("Etc/UTC"))
.withNano(0))
.isEqualTo(
LAST_SYNC_CUSTOM_OBJECT
.getLastSyncTimestamp()
.withZoneSameInstant(ZoneId.of("Etc/UTC"))
.withNano(0));
assertThat(lastSyncCustomObject.getLastSyncStatistics())
.isEqualTo(LAST_SYNC_CUSTOM_OBJECT.getLastSyncStatistics());
assertThat(lastSyncCustomObject.getLastSyncDurationInMillis())
.isEqualTo(LAST_SYNC_CUSTOM_OBJECT.getLastSyncDurationInMillis());
}

@Test
@SuppressWarnings("unchecked")
void getLastSyncCustomObject_OnSuccessfulQueryWithNoResults_ShouldCompleteWithEmptyOptional() {
// preparation
when(apiHttpResponse.getBody()).thenReturn(null);
when(ctpClient.customObjects().withContainerAndKey(anyString(), anyString()).get().execute())

when(byProjectKeyCustomObjectsByContainerByKeyGet.execute())
.thenReturn(CompletableFuture.completedFuture(apiHttpResponse));

final CustomObjectService customObjectService = new CustomObjectServiceImpl(ctpClient);
Expand All @@ -139,7 +183,7 @@ void getLastSyncCustomObject_OnSuccessfulQueryWithNoResults_ShouldCompleteWithEm
@SuppressWarnings("unchecked")
void getLastSyncCustomObject_OnFailedQuery_ShouldCompleteExceptionally() {
// preparation
when(ctpClient.customObjects().withContainerAndKey(anyString(), anyString()).get().execute())
when(byProjectKeyCustomObjectsByContainerByKeyGet.execute())
.thenReturn(
CompletableFutureUtils.exceptionallyCompletedFuture(
TestUtils.createBadGatewayException()));
Expand All @@ -152,17 +196,19 @@ void getLastSyncCustomObject_OnFailedQuery_ShouldCompleteExceptionally() {

// assertions
assertThat(lastSyncCustomObject)
.hasFailedWithThrowableThat()
.isInstanceOf(BadGatewayException.class)
.hasMessageContaining("CTP error!");
.failsWithin(Duration.ZERO)
.withThrowableOfType(ExecutionException.class)
.withCauseExactlyInstanceOf(BadGatewayException.class)
.withMessageContaining("test");
}

@Test
@SuppressWarnings("unchecked")
void createLastSyncCustomObject_OnSuccessfulCreation_ShouldCompleteWithLastSyncCustomObject() {
// preparation
when(apiHttpResponse.getBody()).thenReturn(LAST_SYNC_CUSTOM_OBJECT);
when(ctpClient.customObjects().post(any(CustomObjectDraft.class)).execute())
when(apiHttpResponse.getBody()).thenReturn(STRING_CUSTOM_OBJECT);

when(byProjectKeyCustomObjectsPost.execute())
.thenReturn(CompletableFuture.completedFuture(apiHttpResponse));

final CustomObjectService customObjectService = new CustomObjectServiceImpl(ctpClient);
Expand All @@ -171,21 +217,25 @@ void createLastSyncCustomObject_OnSuccessfulCreation_ShouldCompleteWithLastSyncC
final LastSyncCustomObject<ProductSyncStatistics> lastSyncCustomObject =
LastSyncCustomObject.of(ZonedDateTime.now(), new ProductSyncStatistics(), 100);

final ApiHttpResponse<CustomObject> createdCustomObject =
final CustomObject createdCustomObject =
customObjectService
.createLastSyncCustomObject("foo", "bar", DEFAULT_RUNNER_NAME, lastSyncCustomObject)
.toCompletableFuture()
.join();
.join()
.getBody();

final LastSyncCustomObject customObjectValue =
(LastSyncCustomObject) createdCustomObject.getValue();

// assertions
assertThat(createdCustomObject.getBody()).isEqualTo(LAST_SYNC_CUSTOM_OBJECT);
assertThat(customObjectValue).isEqualTo(LAST_SYNC_CUSTOM_OBJECT);
}

@Test
@SuppressWarnings("unchecked")
void createLastSyncCustomObject_OnFailedCreation_ShouldCompleteExceptionally() {
// preparation
when(ctpClient.customObjects().post(any(CustomObjectDraft.class)).execute())
when(byProjectKeyCustomObjectsPost.execute())
.thenReturn(
CompletableFutureUtils.exceptionallyCompletedFuture(
TestUtils.createBadGatewayException()));
Expand All @@ -199,16 +249,20 @@ void createLastSyncCustomObject_OnFailedCreation_ShouldCompleteExceptionally() {

// assertions
assertThat(createdCustomObject)
.isCompletedExceptionally()
.isInstanceOf(BadGatewayException.class);
.failsWithin(Duration.ZERO)
.withThrowableOfType(ExecutionException.class)
.withCauseExactlyInstanceOf(BadGatewayException.class)
.withMessageContaining("test");
}

@Test
@SuppressWarnings("unchecked")
void createLastSyncCustomObject_WithValidTestRunnerName_ShouldCreateCorrectCustomObjectDraft() {
// preparation
final ArgumentCaptor<CustomObjectDraft> arg = ArgumentCaptor.forClass(CustomObjectDraft.class);
when(ctpClient.customObjects().post(arg.capture()).execute()).thenReturn(null);
when(byProjectKeyCustomObjectsRequestBuilder.post(arg.capture()))
.thenReturn(byProjectKeyCustomObjectsPost);
when(byProjectKeyCustomObjectsPost.execute()).thenReturn(null);

final CustomObjectService customObjectService = new CustomObjectServiceImpl(ctpClient);

Expand All @@ -233,7 +287,9 @@ void createLastSyncCustomObject_WithValidTestRunnerName_ShouldCreateCorrectCusto
void createLastSyncCustomObject_WithEmptyRunnerName_ShouldCreateCorrectCustomObjectDraft() {
// preparation
final ArgumentCaptor<CustomObjectDraft> arg = ArgumentCaptor.forClass(CustomObjectDraft.class);
when(ctpClient.customObjects().post(arg.capture()).execute()).thenReturn(null);
when(byProjectKeyCustomObjectsRequestBuilder.post(arg.capture()))
.thenReturn(byProjectKeyCustomObjectsPost);
when(byProjectKeyCustomObjectsPost.execute()).thenReturn(null);

final CustomObjectService customObjectService = new CustomObjectServiceImpl(ctpClient);

Expand All @@ -256,7 +312,9 @@ void createLastSyncCustomObject_WithEmptyRunnerName_ShouldCreateCorrectCustomObj
void createLastSyncCustomObject_WithNullRunnerName_ShouldCreateCorrectCustomObjectDraft() {
// preparation
final ArgumentCaptor<CustomObjectDraft> arg = ArgumentCaptor.forClass(CustomObjectDraft.class);
when(ctpClient.customObjects().post(arg.capture()).execute()).thenReturn(null);
when(byProjectKeyCustomObjectsRequestBuilder.post(arg.capture()))
.thenReturn(byProjectKeyCustomObjectsPost);
when(byProjectKeyCustomObjectsPost.execute()).thenReturn(null);

final CustomObjectService customObjectService = new CustomObjectServiceImpl(ctpClient);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import uk.org.lidalia.slf4jtest.TestLogger;
import uk.org.lidalia.slf4jtest.TestLoggerFactory;

// These tests do compile but are not valid
// TODO: Make tests valid
class SyncUtilsTest {

@BeforeEach
Expand Down

0 comments on commit 0b1a239

Please sign in to comment.