Skip to content
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 @@ -15,6 +15,7 @@
import com.commercetools.sync.commons.utils.ChunkUtils;
import com.commercetools.sync.services.UnresolvedReferencesService;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.vrap.rmf.base.client.error.NotFoundException;
import io.vrap.rmf.base.client.utils.json.JsonUtils;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -154,6 +155,12 @@ public CompletionStage<Optional<WaitingToBeResolvedT>> delete(
return Optional.of(
OBJECT_MAPPER.convertValue(resource.getBody().getValue(), clazz));
} else {
if (exception instanceof NotFoundException) {
// if we try to delete and the custom object is already gone, then everything is
// fine

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe remove the while line here

// and there is no need to invoke the error callback
return Optional.empty();
}
syncOptions.applyErrorCallback(
new SyncException(format(DELETE_FAILED, hash(key), key), exception));
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import io.vrap.rmf.base.client.ApiHttpResponse;
import io.vrap.rmf.base.client.error.NotFoundException;
import io.vrap.rmf.base.client.utils.CompletableFutureUtils;
import java.nio.charset.StandardCharsets;
import java.util.*;
Expand Down Expand Up @@ -405,6 +406,64 @@ void delete_WithUnsuccessfulMockCtpResponse_ShouldReturnProperException()
.hasCauseExactlyInstanceOf(BadRequestException.class);
}

@Test
void delete_With404NotFoundResponse_ShouldConsiderAsDeleted() throws JsonProcessingException {
// preparation
final String key = "product-draft-key";
final ProductDraft productDraftMock =
ProductDraftBuilder.of()
.productType(ProductTypeResourceIdentifierBuilder.of().key("product-type").build())
.key(key)
.name(LocalizedString.ofEnglish("product-name"))
.slug(LocalizedString.ofEnglish("product-slug"))
.build();

final ApiHttpResponse<State> apiHttpResponse = mock(ApiHttpResponse.class);
when(apiHttpResponse.getBody()).thenReturn(mock());
final ErrorResponse errorResponse =
ErrorResponseBuilder.of()
.statusCode(404)
.errors(Collections.emptyList())
.message("test")
.build();

final ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
final String json = ow.writeValueAsString(errorResponse);

final ByProjectKeyCustomObjectsByContainerByKeyDelete customObjectsDelete =
mock(ByProjectKeyCustomObjectsByContainerByKeyDelete.class);
when(customObjectsDelete.execute())
.thenReturn(
CompletableFutureUtils.failed(
new NotFoundException(
404,
"",
null,
"not found",
new ApiHttpResponse<>(404, null, json.getBytes(StandardCharsets.UTF_8)))));
when(productSyncOptions
.getCtpClient()
.customObjects()
.withContainerAndKey(anyString(), anyString())
.delete())
.thenReturn(customObjectsDelete);

// test
final Optional<WaitingToBeResolvedProducts> toBeResolvedOptional =
service
.delete(
"product-draft-key",
UnresolvedReferencesServiceImpl.CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY,
WaitingToBeResolvedProducts.class)
.toCompletableFuture()
.join();

// assertions
assertThat(toBeResolvedOptional).isEmpty();
assertThat(errorMessages).hasSize(0);
assertThat(errorExceptions).hasSize(0);
}

@SuppressWarnings("unchecked")
@Test
void delete_OnSuccess_ShouldRemoveTheResourceObject() {
Expand Down
Loading