diff --git a/src/main/java/com/commercetools/project/sync/service/impl/CustomObjectServiceImpl.java b/src/main/java/com/commercetools/project/sync/service/impl/CustomObjectServiceImpl.java index 6f368d44..09164941 100644 --- a/src/main/java/com/commercetools/project/sync/service/impl/CustomObjectServiceImpl.java +++ b/src/main/java/com/commercetools/project/sync/service/impl/CustomObjectServiceImpl.java @@ -77,20 +77,16 @@ public CompletableFuture> 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); }); } diff --git a/src/main/java/com/commercetools/project/sync/util/ProjectSyncSolutionInfo.java b/src/main/java/com/commercetools/project/sync/util/ProjectSyncSolutionInfo.java index ae664fb3..bfffcaa2 100644 --- a/src/main/java/com/commercetools/project/sync/util/ProjectSyncSolutionInfo.java +++ b/src/main/java/com/commercetools/project/sync/util/ProjectSyncSolutionInfo.java @@ -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"; diff --git a/src/main/resources/META-INF/services/io.sphere.sdk.client.SolutionInfo b/src/main/resources/META-INF/services/io.vrap.rmf.base.client.SolutionInfo similarity index 100% rename from src/main/resources/META-INF/services/io.sphere.sdk.client.SolutionInfo rename to src/main/resources/META-INF/services/io.vrap.rmf.base.client.SolutionInfo diff --git a/src/test/java/com/commercetools/project/sync/model/response/LastSyncCustomObjectTest.java b/src/test/java/com/commercetools/project/sync/model/response/LastSyncCustomObjectTest.java index 1ec0c2a3..fa46534a 100644 --- a/src/test/java/com/commercetools/project/sync/model/response/LastSyncCustomObjectTest.java +++ b/src/test/java/com/commercetools/project/sync/model/response/LastSyncCustomObjectTest.java @@ -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 diff --git a/src/test/java/com/commercetools/project/sync/service/impl/CustomObjectServiceImplTest.java b/src/test/java/com/commercetools/project/sync/service/impl/CustomObjectServiceImplTest.java index fcf34d34..dc4ae6eb 100644 --- a/src/test/java/com/commercetools/project/sync/service/impl/CustomObjectServiceImplTest.java +++ b/src/test/java/com/commercetools/project/sync/service/impl/CustomObjectServiceImplTest.java @@ -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; @@ -19,34 +23,42 @@ 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 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 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); } @@ -54,6 +66,17 @@ static void setup() { 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 @@ -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); @@ -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); @@ -93,28 +117,47 @@ 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> lastSyncCustomObject = + final CompletionStage> 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 @@ -122,7 +165,8 @@ void getCurrentCtpTimestamp_OnFailedUpsert_ShouldCompleteExceptionally() { 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); @@ -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())); @@ -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); @@ -171,21 +217,25 @@ void createLastSyncCustomObject_OnSuccessfulCreation_ShouldCompleteWithLastSyncC final LastSyncCustomObject lastSyncCustomObject = LastSyncCustomObject.of(ZonedDateTime.now(), new ProductSyncStatistics(), 100); - final ApiHttpResponse 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())); @@ -199,8 +249,10 @@ void createLastSyncCustomObject_OnFailedCreation_ShouldCompleteExceptionally() { // assertions assertThat(createdCustomObject) - .isCompletedExceptionally() - .isInstanceOf(BadGatewayException.class); + .failsWithin(Duration.ZERO) + .withThrowableOfType(ExecutionException.class) + .withCauseExactlyInstanceOf(BadGatewayException.class) + .withMessageContaining("test"); } @Test @@ -208,7 +260,9 @@ void createLastSyncCustomObject_OnFailedCreation_ShouldCompleteExceptionally() { void createLastSyncCustomObject_WithValidTestRunnerName_ShouldCreateCorrectCustomObjectDraft() { // preparation final ArgumentCaptor 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); @@ -233,7 +287,9 @@ void createLastSyncCustomObject_WithValidTestRunnerName_ShouldCreateCorrectCusto void createLastSyncCustomObject_WithEmptyRunnerName_ShouldCreateCorrectCustomObjectDraft() { // preparation final ArgumentCaptor 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); @@ -256,7 +312,9 @@ void createLastSyncCustomObject_WithEmptyRunnerName_ShouldCreateCorrectCustomObj void createLastSyncCustomObject_WithNullRunnerName_ShouldCreateCorrectCustomObjectDraft() { // preparation final ArgumentCaptor 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); diff --git a/src/test/java/com/commercetools/project/sync/util/SyncUtilsTest.java b/src/test/java/com/commercetools/project/sync/util/SyncUtilsTest.java index 79d5e899..84224666 100644 --- a/src/test/java/com/commercetools/project/sync/util/SyncUtilsTest.java +++ b/src/test/java/com/commercetools/project/sync/util/SyncUtilsTest.java @@ -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