Skip to content

Commit b587806

Browse files
authored
Merge pull request #1210 from commercetools/fix_custom_object_deletion
fix: dont report not found object as error when delete
2 parents 70ffefa + f054153 commit b587806

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

src/main/java/com/commercetools/sync/services/impl/UnresolvedReferencesServiceImpl.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.commercetools.sync.commons.utils.ChunkUtils;
1616
import com.commercetools.sync.services.UnresolvedReferencesService;
1717
import com.fasterxml.jackson.databind.ObjectMapper;
18+
import io.vrap.rmf.base.client.error.NotFoundException;
1819
import io.vrap.rmf.base.client.utils.json.JsonUtils;
1920
import java.util.Collections;
2021
import java.util.List;
@@ -154,6 +155,12 @@ public CompletionStage<Optional<WaitingToBeResolvedT>> delete(
154155
return Optional.of(
155156
OBJECT_MAPPER.convertValue(resource.getBody().getValue(), clazz));
156157
} else {
158+
if (exception instanceof NotFoundException) {
159+
// if we try to delete and the custom object is already gone, then everything is
160+
// fine
161+
// and there is no need to invoke the error callback
162+
return Optional.empty();
163+
}
157164
syncOptions.applyErrorCallback(
158165
new SyncException(format(DELETE_FAILED, hash(key), key), exception));
159166
return Optional.empty();

src/test/java/com/commercetools/sync/services/impl/UnresolvedReferencesServiceImplTest.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.fasterxml.jackson.databind.ObjectMapper;
3333
import com.fasterxml.jackson.databind.ObjectWriter;
3434
import io.vrap.rmf.base.client.ApiHttpResponse;
35+
import io.vrap.rmf.base.client.error.NotFoundException;
3536
import io.vrap.rmf.base.client.utils.CompletableFutureUtils;
3637
import java.nio.charset.StandardCharsets;
3738
import java.util.*;
@@ -405,6 +406,64 @@ void delete_WithUnsuccessfulMockCtpResponse_ShouldReturnProperException()
405406
.hasCauseExactlyInstanceOf(BadRequestException.class);
406407
}
407408

409+
@Test
410+
void delete_With404NotFoundResponse_ShouldConsiderAsDeleted() throws JsonProcessingException {
411+
// preparation
412+
final String key = "product-draft-key";
413+
final ProductDraft productDraftMock =
414+
ProductDraftBuilder.of()
415+
.productType(ProductTypeResourceIdentifierBuilder.of().key("product-type").build())
416+
.key(key)
417+
.name(LocalizedString.ofEnglish("product-name"))
418+
.slug(LocalizedString.ofEnglish("product-slug"))
419+
.build();
420+
421+
final ApiHttpResponse<State> apiHttpResponse = mock(ApiHttpResponse.class);
422+
when(apiHttpResponse.getBody()).thenReturn(mock());
423+
final ErrorResponse errorResponse =
424+
ErrorResponseBuilder.of()
425+
.statusCode(404)
426+
.errors(Collections.emptyList())
427+
.message("test")
428+
.build();
429+
430+
final ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
431+
final String json = ow.writeValueAsString(errorResponse);
432+
433+
final ByProjectKeyCustomObjectsByContainerByKeyDelete customObjectsDelete =
434+
mock(ByProjectKeyCustomObjectsByContainerByKeyDelete.class);
435+
when(customObjectsDelete.execute())
436+
.thenReturn(
437+
CompletableFutureUtils.failed(
438+
new NotFoundException(
439+
404,
440+
"",
441+
null,
442+
"not found",
443+
new ApiHttpResponse<>(404, null, json.getBytes(StandardCharsets.UTF_8)))));
444+
when(productSyncOptions
445+
.getCtpClient()
446+
.customObjects()
447+
.withContainerAndKey(anyString(), anyString())
448+
.delete())
449+
.thenReturn(customObjectsDelete);
450+
451+
// test
452+
final Optional<WaitingToBeResolvedProducts> toBeResolvedOptional =
453+
service
454+
.delete(
455+
"product-draft-key",
456+
UnresolvedReferencesServiceImpl.CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY,
457+
WaitingToBeResolvedProducts.class)
458+
.toCompletableFuture()
459+
.join();
460+
461+
// assertions
462+
assertThat(toBeResolvedOptional).isEmpty();
463+
assertThat(errorMessages).hasSize(0);
464+
assertThat(errorExceptions).hasSize(0);
465+
}
466+
408467
@SuppressWarnings("unchecked")
409468
@Test
410469
void delete_OnSuccess_ShouldRemoveTheResourceObject() {

0 commit comments

Comments
 (0)