From 8022b204573509f6ebe85a352f9a4cc9420f23d4 Mon Sep 17 00:00:00 2001 From: abhupadh <> Date: Fri, 5 Jan 2024 15:47:25 +0530 Subject: [PATCH 1/4] refactoring registration service to make it resilient for conflict errors --- .../event/management/RegistrationService.java | 5 +- .../management/feign/ConflictException.java | 10 ++-- .../feign/FeignRegistrationService.java | 29 ++++++++--- .../management/RegistrationServiceTester.java | 49 +++++++++++-------- .../JournalServiceIntegrationTest.java | 3 +- .../RegistrationServiceIntegrationTest.java | 2 +- .../PublishServiceIntegrationTest.java | 3 +- 7 files changed, 64 insertions(+), 37 deletions(-) diff --git a/events_mgmt/src/main/java/com/adobe/aio/event/management/RegistrationService.java b/events_mgmt/src/main/java/com/adobe/aio/event/management/RegistrationService.java index d296a9e1..88071993 100644 --- a/events_mgmt/src/main/java/com/adobe/aio/event/management/RegistrationService.java +++ b/events_mgmt/src/main/java/com/adobe/aio/event/management/RegistrationService.java @@ -12,12 +12,11 @@ package com.adobe.aio.event.management; import com.adobe.aio.event.management.feign.FeignRegistrationService; +import com.adobe.aio.event.management.model.Registration; import com.adobe.aio.event.management.model.RegistrationCreateModel; import com.adobe.aio.event.management.model.RegistrationPaginatedModel; import com.adobe.aio.event.management.model.RegistrationUpdateModel; import com.adobe.aio.workspace.Workspace; -import com.adobe.aio.event.management.model.Registration; -import com.fasterxml.jackson.core.JsonProcessingException; import java.util.List; import java.util.Optional; @@ -27,6 +26,8 @@ public interface RegistrationService { void delete(String registrationId); + Optional createOrUpdateRegistration(RegistrationCreateModel.Builder registrationCreateModelBuilder); + Optional createRegistration(RegistrationCreateModel.Builder registrationCreateModelBuilder); Optional updateRegistration(String registrationId, RegistrationUpdateModel.Builder registrationUpdateModelBuilder); diff --git a/events_mgmt/src/main/java/com/adobe/aio/event/management/feign/ConflictException.java b/events_mgmt/src/main/java/com/adobe/aio/event/management/feign/ConflictException.java index 093aa8da..4c18a23c 100644 --- a/events_mgmt/src/main/java/com/adobe/aio/event/management/feign/ConflictException.java +++ b/events_mgmt/src/main/java/com/adobe/aio/event/management/feign/ConflictException.java @@ -13,15 +13,19 @@ import feign.FeignException; import feign.Response; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import java.util.Optional; public class ConflictException extends FeignException { - private final Logger logger = LoggerFactory.getLogger(this.getClass()); + private final String conflictingId; public ConflictException(Response response, FeignException exception) { super(response.status(), exception.getMessage(), response.request(), exception); + Optional conflictingIdOptional = response.headers().get("x-conflicting-id").stream().findFirst(); + conflictingId = conflictingIdOptional.isPresent() ? conflictingIdOptional.get() : null; } + public String getConflictingId() { + return conflictingId; + } } diff --git a/events_mgmt/src/main/java/com/adobe/aio/event/management/feign/FeignRegistrationService.java b/events_mgmt/src/main/java/com/adobe/aio/event/management/feign/FeignRegistrationService.java index b0baa51d..cf32b268 100644 --- a/events_mgmt/src/main/java/com/adobe/aio/event/management/feign/FeignRegistrationService.java +++ b/events_mgmt/src/main/java/com/adobe/aio/event/management/feign/FeignRegistrationService.java @@ -11,11 +11,7 @@ */ package com.adobe.aio.event.management.feign; -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; - -import org.apache.commons.lang3.StringUtils; +import static com.adobe.aio.util.Constants.API_MANAGEMENT_URL; import com.adobe.aio.event.management.RegistrationService; import com.adobe.aio.event.management.api.RegistrationApi; @@ -29,11 +25,17 @@ import com.adobe.aio.util.feign.FeignUtil; import com.adobe.aio.workspace.Workspace; import feign.RequestInterceptor; - -import static com.adobe.aio.util.Constants.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class FeignRegistrationService implements RegistrationService { + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + private final RegistrationApi registrationApi; private final Workspace workspace; @@ -48,6 +50,7 @@ public FeignRegistrationService(final Workspace workspace, this.registrationApi = FeignUtil.getDefaultBuilder() .requestInterceptor(authInterceptor) .requestInterceptor(AIOHeaderInterceptor.builder().workspace(workspace).build()) + .errorDecoder(new ConflictErrorDecoder()) .target(RegistrationApi.class, apiUrl); this.workspace = workspace; } @@ -70,6 +73,18 @@ public void delete(String registrationId) { workspace.getWorkspaceId(), registrationId); } + @Override + public Optional createOrUpdateRegistration( + RegistrationCreateModel.Builder registrationCreateModelBuilder) { + try { + return createRegistration(registrationCreateModelBuilder); + } catch (ConflictException ex) { + String conflictingRegistrationId = ex.getConflictingId(); + logger.warn("Another registration (id `{}`) exists with conflict due to {}. Trying to update it...", conflictingRegistrationId, ex.getMessage()); + return updateRegistration(conflictingRegistrationId, registrationCreateModelBuilder); + } + } + @Override public Optional createRegistration( RegistrationCreateModel.Builder registrationCreateModelBuilder) { diff --git a/events_test/src/main/java/com/adobe/aio/event/management/RegistrationServiceTester.java b/events_test/src/main/java/com/adobe/aio/event/management/RegistrationServiceTester.java index 7e411280..a18e95ef 100644 --- a/events_test/src/main/java/com/adobe/aio/event/management/RegistrationServiceTester.java +++ b/events_test/src/main/java/com/adobe/aio/event/management/RegistrationServiceTester.java @@ -22,6 +22,7 @@ import java.net.URL; import java.util.Optional; import java.util.Set; +import java.util.function.Supplier; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -55,18 +56,18 @@ public static EventsOfInterestInputModel.Builder getTestEventsOfInterestBuilder( .providerId(providerId); } - public Registration createJournalRegistration(String registrationName, + public Registration createOrUpdateJournalRegistration(String registrationName, String providerId, String eventCode){ - return createRegistration(RegistrationCreateModel.builder() + return createOrUpdateRegistration(RegistrationCreateModel.builder() .name(registrationName) .description(TEST_DESCRIPTION) .deliveryType(DELIVERY_TYPE_JOURNAL) .addEventsOfInterests(getTestEventsOfInterestBuilder(providerId, eventCode).build())); } - public Registration createRuntimeWebhookRegistration(String registrationName, String providerId, + public Registration createOrUpdateRuntimeWebhookRegistration(String registrationName, String providerId, String eventCode, String runtimeAction) { - return createRegistration(RegistrationCreateModel.builder() + return createOrUpdateRegistration(RegistrationCreateModel.builder() .name(registrationName) .description(TEST_DESCRIPTION) .deliveryType(DELIVERY_TYPE_WEBHOOK) @@ -74,14 +75,21 @@ public Registration createRuntimeWebhookRegistration(String registrationName, St .addEventsOfInterests(getTestEventsOfInterestBuilder(providerId, eventCode).build())); } - public Registration createRegistration( + public Registration createOrUpdateRegistration( RegistrationCreateModel.Builder registrationInputModelBuilder) { + return assertRegistrationCreatedResponseWithEventsOfInterest(registrationInputModelBuilder, + () -> registrationService.createOrUpdateRegistration(registrationInputModelBuilder)); + } + + public Registration assertRegistrationCreatedResponseWithEventsOfInterest( + RegistrationCreateModel.Builder registrationInputModelBuilder, + Supplier> registrationSupplier) { RegistrationCreateModel registrationInputModel = registrationInputModelBuilder.clientId(this.workspace.getApiKey()).build(); - Optional registration = registrationService.createRegistration(registrationInputModelBuilder); - assertTrue(registration.isPresent()); - Registration registrationCreated = registration.get(); - logger.info("Created AIO Event Registration: {}", registration.get()); + Optional registrationOptional = registrationSupplier.get(); + assertTrue(registrationOptional.isPresent()); + Registration registrationCreated = registrationOptional.get(); + logger.info("Created AIO Event Registration: {}", registrationOptional.get()); assertNotNull(registrationCreated.getRegistrationId()); assertEquals(registrationInputModel.getDescription(), registrationCreated.getDescription()); @@ -95,30 +103,31 @@ public Registration createRegistration( assertEquals(registrationInputModel.getWebhookUrl(), registrationCreated.getWebhookUrl()); } } else { - assertNull(registration.get().getWebhookUrl()); - assertUrl(registration.get().getJournalUrl().getHref()); + assertNull(registrationOptional.get().getWebhookUrl()); + assertUrl(registrationOptional.get().getJournalUrl().getHref()); } - Set eventsOfInterestSet = registration.get().getEventsOfInterests(); + Set eventsOfInterestSet = registrationOptional.get().getEventsOfInterests(); assertEquals(registrationInputModel.getEventsOfInterests().size(),eventsOfInterestSet.size()); for(EventsOfInterestInputModel eventsOfInterestInput: registrationInputModel.getEventsOfInterests()){ assertTrue(eventsOfInterestSet.stream() - .anyMatch(eventsOfInterest -> eventsOfInterest.getEventCode() - .equals(eventsOfInterestInput.getEventCode()))); + .anyMatch(eventsOfInterest -> eventsOfInterest.getEventCode() + .equals(eventsOfInterestInput.getEventCode()))); } assertEquals("verified", registrationCreated.getWebhookStatus()); assertEquals(true, registrationCreated.isEnabled()); - assertUrl(registration.get().getTraceUrl().getHref()); - assertUrl(registration.get().getJournalUrl().getHref()); + assertUrl(registrationOptional.get().getTraceUrl().getHref()); + assertUrl(registrationOptional.get().getJournalUrl().getHref()); - assertNotNull(registration.get().getCreatedDate()); - assertNotNull(registration.get().getUpdatedDate()); - return registration.get(); + assertNotNull(registrationOptional.get().getCreatedDate()); + assertNotNull(registrationOptional.get().getUpdatedDate()); + return registrationOptional.get(); } - public Registration updateRegistration(Registration registrationToUpdate, String runtimeActionToUpdate) { + public Registration updateRuntimeWebhookRegistration(Registration registrationToUpdate, + String runtimeActionToUpdate) { EventsOfInterest eventsOfInterest = registrationToUpdate.getEventsOfInterests().iterator().next(); String providerId = eventsOfInterest.getProviderId(); String eventCode = eventsOfInterest.getEventCode(); diff --git a/events_test/src/test/java/com/adobe/aio/event/journal/JournalServiceIntegrationTest.java b/events_test/src/test/java/com/adobe/aio/event/journal/JournalServiceIntegrationTest.java index 863e00be..54db3e23 100644 --- a/events_test/src/test/java/com/adobe/aio/event/journal/JournalServiceIntegrationTest.java +++ b/events_test/src/test/java/com/adobe/aio/event/journal/JournalServiceIntegrationTest.java @@ -23,7 +23,6 @@ import com.adobe.aio.event.management.model.Registration; import com.adobe.aio.event.publish.PublishServiceTester; import com.adobe.aio.util.WorkspaceUtil; -import java.util.UUID; import org.apache.commons.lang3.StringUtils; import org.junit.jupiter.api.Test; @@ -67,7 +66,7 @@ public void testJournalPolling() try { providerId = providerServiceTester.createOrUpdateProvider(TEST_EVENT_PROVIDER_LABEL, ProviderServiceIntegrationTest.TEST_EVENT_CODE).getId(); - Registration registration = registrationServiceTester.createJournalRegistration( + Registration registration = registrationServiceTester.createOrUpdateJournalRegistration( TEST_REGISTRATION_NAME, providerId, TEST_EVENT_CODE); registrationId = registration.getRegistrationId(); diff --git a/events_test/src/test/java/com/adobe/aio/event/management/RegistrationServiceIntegrationTest.java b/events_test/src/test/java/com/adobe/aio/event/management/RegistrationServiceIntegrationTest.java index e40c4493..73306a28 100644 --- a/events_test/src/test/java/com/adobe/aio/event/management/RegistrationServiceIntegrationTest.java +++ b/events_test/src/test/java/com/adobe/aio/event/management/RegistrationServiceIntegrationTest.java @@ -51,7 +51,7 @@ public void createGetDeleteJournalRegistration() { try { providerId = providerServiceTester.createOrUpdateProvider(TEST_EVENT_PROVIDER_LABEL, TEST_EVENT_CODE).getId(); - Registration registration = createJournalRegistration(TEST_REGISTRATION_NAME, providerId, + Registration registration = createOrUpdateJournalRegistration(TEST_REGISTRATION_NAME, providerId, TEST_EVENT_CODE); registrationId = registration.getRegistrationId(); Optional found = registrationService.findById(registrationId); diff --git a/events_test/src/test/java/com/adobe/aio/event/publish/PublishServiceIntegrationTest.java b/events_test/src/test/java/com/adobe/aio/event/publish/PublishServiceIntegrationTest.java index e506f669..5af6f473 100644 --- a/events_test/src/test/java/com/adobe/aio/event/publish/PublishServiceIntegrationTest.java +++ b/events_test/src/test/java/com/adobe/aio/event/publish/PublishServiceIntegrationTest.java @@ -20,7 +20,6 @@ import com.adobe.aio.event.management.ProviderServiceTester; import com.adobe.aio.event.management.RegistrationServiceTester; -import java.util.UUID; import org.apache.commons.lang3.StringUtils; import org.junit.jupiter.api.Test; @@ -47,7 +46,7 @@ public void publishEventTest() { try { providerId = providerServiceTester.createOrUpdateProvider(TEST_EVENT_PROVIDER_LABEL, TEST_EVENT_CODE).getId(); - registrationId = registrationServiceTester.createJournalRegistration( + registrationId = registrationServiceTester.createOrUpdateJournalRegistration( TEST_REGISTRATION_NAME, providerId, TEST_EVENT_CODE).getRegistrationId(); assertNotNull(publishCloudEvent(providerId, TEST_EVENT_CODE)); From b92002bedc0f404da3d595d34732b64b1b2c729b Mon Sep 17 00:00:00 2001 From: abhupadh <> Date: Mon, 8 Jan 2024 13:41:05 +0530 Subject: [PATCH 2/4] using conflicting provider id from error response header, removing csm get provider call --- .../feign/FeignProviderService.java | 21 +++++-------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/events_mgmt/src/main/java/com/adobe/aio/event/management/feign/FeignProviderService.java b/events_mgmt/src/main/java/com/adobe/aio/event/management/feign/FeignProviderService.java index 90f7478c..eb3fc21b 100644 --- a/events_mgmt/src/main/java/com/adobe/aio/event/management/feign/FeignProviderService.java +++ b/events_mgmt/src/main/java/com/adobe/aio/event/management/feign/FeignProviderService.java @@ -108,22 +108,11 @@ public Optional createProvider(final ProviderInputModel providerInputM public Optional createOrUpdateProvider(final ProviderInputModel providerInputModel) { try { return createProvider(providerInputModel); - } - catch (ConflictException e){ - logger.info("Another provider (providerMetadata: `{}`, imsOrg:`{}`, instanceId: `{}` )" - + " exist with conflicting natural keys, trying to update it ...", - providerInputModel.getProviderMetadataId(), workspace.getImsOrgId(), - providerInputModel.getInstanceId()); - String providerId = this.findProviderBy(providerInputModel.getProviderMetadataId(), - providerInputModel.getInstanceId()) - .orElseThrow(() -> new AIOException("Race condition error: the provider " - + "(`" + providerInputModel.getProviderMetadataId() + "`," - + "`" + workspace.getImsOrgId() + "`," - + "`" + providerInputModel.getInstanceId()+ "`)" - + " may have been deleted just after a Conflict `" + e.getMessage() - + "` was detected while creating it")) - .getId(); - return updateProvider(providerId,providerInputModel); + } catch (ConflictException e){ + String conflictingProviderId = e.getConflictingId(); + logger.warn("Another provider (id: `{}` ) exist with conflict due to {}, trying to update it ...", + conflictingProviderId, e.getMessage()); + return updateProvider(conflictingProviderId, providerInputModel); } } From d3e3db3547ca169118b242ff55d3e71ff7d9d328 Mon Sep 17 00:00:00 2001 From: abhupadh <> Date: Tue, 9 Jan 2024 19:47:43 +0530 Subject: [PATCH 3/4] using logger info instead of warn --- .../adobe/aio/event/management/feign/FeignProviderService.java | 2 +- .../aio/event/management/feign/FeignRegistrationService.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/events_mgmt/src/main/java/com/adobe/aio/event/management/feign/FeignProviderService.java b/events_mgmt/src/main/java/com/adobe/aio/event/management/feign/FeignProviderService.java index eb3fc21b..6b68203d 100644 --- a/events_mgmt/src/main/java/com/adobe/aio/event/management/feign/FeignProviderService.java +++ b/events_mgmt/src/main/java/com/adobe/aio/event/management/feign/FeignProviderService.java @@ -110,7 +110,7 @@ public Optional createOrUpdateProvider(final ProviderInputModel provid return createProvider(providerInputModel); } catch (ConflictException e){ String conflictingProviderId = e.getConflictingId(); - logger.warn("Another provider (id: `{}` ) exist with conflict due to {}, trying to update it ...", + logger.info("Another provider (id: `{}` ) exist with conflict due to {}, trying to update it ...", conflictingProviderId, e.getMessage()); return updateProvider(conflictingProviderId, providerInputModel); } diff --git a/events_mgmt/src/main/java/com/adobe/aio/event/management/feign/FeignRegistrationService.java b/events_mgmt/src/main/java/com/adobe/aio/event/management/feign/FeignRegistrationService.java index cf32b268..0ee1cacb 100644 --- a/events_mgmt/src/main/java/com/adobe/aio/event/management/feign/FeignRegistrationService.java +++ b/events_mgmt/src/main/java/com/adobe/aio/event/management/feign/FeignRegistrationService.java @@ -80,7 +80,7 @@ public Optional createOrUpdateRegistration( return createRegistration(registrationCreateModelBuilder); } catch (ConflictException ex) { String conflictingRegistrationId = ex.getConflictingId(); - logger.warn("Another registration (id `{}`) exists with conflict due to {}. Trying to update it...", conflictingRegistrationId, ex.getMessage()); + logger.info("Another registration (id `{}`) exists with conflict due to {}. Trying to update it...", conflictingRegistrationId, ex.getMessage()); return updateRegistration(conflictingRegistrationId, registrationCreateModelBuilder); } } From 8f19718e7ef1057a50dda92a76e061fb4fc17bff Mon Sep 17 00:00:00 2001 From: abhupadh <> Date: Thu, 11 Jan 2024 15:24:31 +0530 Subject: [PATCH 4/4] leveraging createOrUpdate by calling it twice to cover both create and update flows without any conflict eexception --- .../ProviderServiceIntegrationTest.java | 37 +++++++-------- .../RegistrationServiceIntegrationTest.java | 45 ++++++++++++------- 2 files changed, 47 insertions(+), 35 deletions(-) diff --git a/events_test/src/test/java/com/adobe/aio/event/management/ProviderServiceIntegrationTest.java b/events_test/src/test/java/com/adobe/aio/event/management/ProviderServiceIntegrationTest.java index 46b494bb..3aa81039 100644 --- a/events_test/src/test/java/com/adobe/aio/event/management/ProviderServiceIntegrationTest.java +++ b/events_test/src/test/java/com/adobe/aio/event/management/ProviderServiceIntegrationTest.java @@ -11,6 +11,8 @@ */ package com.adobe.aio.event.management; +import com.adobe.aio.event.management.model.ProviderInputModel; +import java.util.Collections; import java.util.List; import java.util.Optional; @@ -107,34 +109,29 @@ public void createGetUpdateDelete() { logger.info("Deleted EventMetadata {} from AIO Events Provider `{}`", TEST_EVENT_CODE, providerById); - try { - providerService.createProvider( - getTestProviderInputModelBuilder(TEST_EVENT_PROVIDER_LABEL).instanceId(instanceId) - .build()); - fail("We should have had a ConflictException thrown"); - } catch (ConflictException ex) { - logger.info("Cannot create an AIO Events provider with the same instanceId: {}", - ex.getMessage()); - } - + // update provider and event metadata String updatedProviderDescription = "updated Provider Description"; - Optional updatedProvider = providerService.updateProvider(providerId, - getTestProviderInputModelBuilder(TEST_EVENT_PROVIDER_LABEL) + Provider updatedProvider = createOrUpdateProvider(getTestProviderInputModelBuilder(TEST_EVENT_PROVIDER_LABEL) .instanceId(instanceId) .description(updatedProviderDescription) .eventDeliveryFormat(DELIVERY_FORMAT_ADOBE_IO) - .build()); - assertTrue(updatedProvider.isPresent()); + .build(), Collections.singleton(getTestEventMetadataBuilder(TEST_EVENT_CODE).build())); + assertTrue(updatedProvider != null); logger.info("Updated AIO Events Provider: {}", provider); - assertEquals(providerId, updatedProvider.get().getId()); - assertEquals(updatedProviderDescription, updatedProvider.get().getDescription()); - assertEquals(DELIVERY_FORMAT_ADOBE_IO, updatedProvider.get().getEventDeliveryFormat()); + assertEquals(providerId, updatedProvider.getId()); + assertEquals(updatedProviderDescription, updatedProvider.getDescription()); + assertEquals(DELIVERY_FORMAT_ADOBE_IO, updatedProvider.getEventDeliveryFormat()); + + updatedEventMetadataDescription = "updated EventMetadata Description"; + eventMetadata = providerService.updateEventMetadata(providerId, + getTestEventMetadataBuilder(TEST_EVENT_CODE).description(updatedEventMetadataDescription) + .build()); - providerService.createEventMetadata(providerId, - getTestEventMetadataBuilder(TEST_EVENT_CODE).build()); assertTrue(eventMetadata.isPresent()); - logger.info("Added EventMetadata `{}` to AIO Events Provider `{}`", eventMetadata, + logger.info("Updated EventMetadata `{}` of AIO Events Provider `{}`", eventMetadata, providerId); + assertEquals(updatedEventMetadataDescription, eventMetadata.get().getDescription()); + providerService.deleteAllEventMetadata(providerId); assertTrue(providerService.getEventMetadata(providerId).isEmpty()); logger.info("Deleted All EventMetadata from AIO Events Provider `{}`", providerId); diff --git a/events_test/src/test/java/com/adobe/aio/event/management/RegistrationServiceIntegrationTest.java b/events_test/src/test/java/com/adobe/aio/event/management/RegistrationServiceIntegrationTest.java index 73306a28..3410ce22 100644 --- a/events_test/src/test/java/com/adobe/aio/event/management/RegistrationServiceIntegrationTest.java +++ b/events_test/src/test/java/com/adobe/aio/event/management/RegistrationServiceIntegrationTest.java @@ -13,6 +13,7 @@ import java.util.Optional; +import java.util.function.Supplier; import org.apache.commons.lang3.StringUtils; import com.adobe.aio.event.management.model.Registration; @@ -54,21 +55,16 @@ public void createGetDeleteJournalRegistration() { Registration registration = createOrUpdateJournalRegistration(TEST_REGISTRATION_NAME, providerId, TEST_EVENT_CODE); registrationId = registration.getRegistrationId(); - Optional found = registrationService.findById(registrationId); - assertTrue(found.isPresent()); - logger.info("Found AIO Event Registration: {}", found.get()); - assertEquals(registrationId, found.get().getRegistrationId()); - assertEquals(registration.getClientId(), found.get().getClientId()); - assertEquals(registration.getDescription(), found.get().getDescription()); - assertEquals(registration.getName(), found.get().getName()); - assertEquals(registration.getDeliveryType(), found.get().getDeliveryType()); - assertEquals(registration.getEventsOfInterests(), - found.get().getEventsOfInterests()); - assertEquals(registration.getWebhookStatus(), found.get().getWebhookStatus()); - assertEquals(registration.isEnabled(), found.get().isEnabled()); - assertEquals(registration.getWebhookUrl(), found.get().getWebhookUrl()); - assertEquals(registration.getJournalUrl().getHref(), found.get().getJournalUrl().getHref()); - assertEquals(registration.getTraceUrl().getHref(), found.get().getTraceUrl().getHref()); + String finalRegistrationId = registration.getRegistrationId(); + assertCreatedOrUpdatedRegistrationMatchesWithFoundRegistration(registration, + () -> registrationService.findById(finalRegistrationId)); + + // covering the update path + registration = createOrUpdateJournalRegistration(TEST_REGISTRATION_NAME, providerId, + TEST_EVENT_CODE); + String finalUpdatedRegistrationId = registration.getRegistrationId(); + assertCreatedOrUpdatedRegistrationMatchesWithFoundRegistration(registration, + () -> registrationService.findById(finalUpdatedRegistrationId)); } catch (Exception e) { logger.error(e.getMessage(), e); fail(e.getMessage()); @@ -81,4 +77,23 @@ public void createGetDeleteJournalRegistration() { } } } + + public void assertCreatedOrUpdatedRegistrationMatchesWithFoundRegistration(Registration registration, + Supplier> foundRegistrationSupplier) { + Optional foundRegistrationOptional = foundRegistrationSupplier.get(); + assertTrue(foundRegistrationOptional.isPresent()); + logger.info("Found AIO Event Registration: {}", foundRegistrationOptional.get()); + assertEquals(registration.getRegistrationId(), foundRegistrationOptional.get().getRegistrationId()); + assertEquals(registration.getClientId(), foundRegistrationOptional.get().getClientId()); + assertEquals(registration.getDescription(), foundRegistrationOptional.get().getDescription()); + assertEquals(registration.getName(), foundRegistrationOptional.get().getName()); + assertEquals(registration.getDeliveryType(), foundRegistrationOptional.get().getDeliveryType()); + assertEquals(registration.getEventsOfInterests(), + foundRegistrationOptional.get().getEventsOfInterests()); + assertEquals(registration.getWebhookStatus(), foundRegistrationOptional.get().getWebhookStatus()); + assertEquals(registration.isEnabled(), foundRegistrationOptional.get().isEnabled()); + assertEquals(registration.getWebhookUrl(), foundRegistrationOptional.get().getWebhookUrl()); + assertEquals(registration.getJournalUrl().getHref(), foundRegistrationOptional.get().getJournalUrl().getHref()); + assertEquals(registration.getTraceUrl().getHref(), foundRegistrationOptional.get().getTraceUrl().getHref()); + } }