Skip to content

Commit

Permalink
PI-6525: AddonDetector uses parentAccountIdentifier field (#43)
Browse files Browse the repository at this point in the history
* PI-6525: updated the AddonDetector so it uses a better heuristic: instead of checking for edition codes, it simply checks for the presence of the payload.account.parentAccountIdentifier field. This field is only filled for add-on v2 related events.

* Review: line breaks!
  • Loading branch information
gbranchaudrubenovitch authored Jan 5, 2017
1 parent eadbdc0 commit fca1758
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 97 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ public class MinimalConnector {
* Ensure your application context includes a `DeveloperSpecificAppmarketCredentialsSupplier` bean
that returns valid appmarket credentials given a consumer key.

* Ensure your application context includes an [`EditionCodeBasedAddonDetector`](src/main/java/com/appdirect/sdk/appmarket/events/EditionCodeBasedAddonDetector.java)
bean. It determines if a given `editionCode` is related to an add-on subscription or to a regular subscription.
* The bean requires you pass it your add-on edition codes.

* Ensure your application context includes a `AppmarketEventHandler<T>` bean for every type of market events.
* Not providing handler for a mandatory event types will lead to an application context failure.
* The events you need to expose `AppmarketEventHandler`s for are
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.appdirect.sdk.appmarket.events;

import java.util.Optional;

/**
* Determines if an event is related to an add-on subscription or to a regular subscription.
* This is used to dispatch the add-on events to the add-on event handlers.
*/
public class AddonEventDetector {

/**
* Determines if a given event is related to an add-on subscription or not.
*
* @param rawEvent the event to check; can't be null.
* @return <code>true</code> if the event is related to an add-on subscription; <code>false</code> otherwise.
*/
public boolean eventIsRelatedToAddon(EventInfo rawEvent) {
return Optional.ofNullable(rawEvent.getPayload())
.map(EventPayload::getAccount)
.map(AccountInfo::getParentAccountIdentifier)
.isPresent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ class AppmarketEventDispatcher {
private final SDKEventHandler userAssignmentHandler;
private final SDKEventHandler userUnassignmentHandler;
private final SDKEventHandler unknownEventHandler;
private final EditionCodeBasedAddonDetector addonDetector;
private final AddonEventDetector addonDetector;

AppmarketEventDispatcher(Events events, AsyncEventHandler asyncHandler, SDKEventHandler subscriptionOrderHandler, SDKEventHandler subscriptionCancelHandler, SDKEventHandler subscriptionChangeHandler, SDKEventHandler subscriptionDeactivatedHandler, SDKEventHandler subscriptionReactivatedHandler, SDKEventHandler subscriptionClosedHandler, SDKEventHandler subscriptionUpcomingInvoiceHandler, SDKEventHandler addonSubscriptionOrderHandler, SDKEventHandler userAssignmentHandler, SDKEventHandler userUnassignmentHandler, SDKEventHandler unknownEventHandler, EditionCodeBasedAddonDetector addonDetector) { // NOSONAR: ctor has too many params - This is for SDK use only.
AppmarketEventDispatcher(Events events, AsyncEventHandler asyncHandler, SDKEventHandler subscriptionOrderHandler, SDKEventHandler subscriptionCancelHandler, SDKEventHandler subscriptionChangeHandler, SDKEventHandler subscriptionDeactivatedHandler, SDKEventHandler subscriptionReactivatedHandler, SDKEventHandler subscriptionClosedHandler, SDKEventHandler subscriptionUpcomingInvoiceHandler, SDKEventHandler addonSubscriptionOrderHandler, SDKEventHandler userAssignmentHandler, SDKEventHandler userUnassignmentHandler, SDKEventHandler unknownEventHandler, AddonEventDetector addonDetector) { // NOSONAR: ctor has too many params - This is for SDK use only.
this.events = events;
this.asyncHandler = asyncHandler;
this.subscriptionOrderHandler = subscriptionOrderHandler;
Expand All @@ -53,9 +53,7 @@ APIResult dispatchAndHandle(EventInfo rawEvent, EventHandlingContext eventContex
}

private SDKEventHandler getHandlerFor(final EventInfo rawEvent) {
final boolean eventIsForAddon = events.extractEditionCode(rawEvent)
.map(addonDetector::editionCodeIsRelatedToAddon)
.orElse(false);
final boolean eventIsForAddon = addonDetector.eventIsRelatedToAddon(rawEvent);

Map<EventType, Supplier<SDKEventHandler>> eventsToHandlers = new EnumMap<>(EventType.class);
eventsToHandlers.put(SUBSCRIPTION_ORDER, () -> eventIsForAddon ? addonSubscriptionOrderHandler : subscriptionOrderHandler);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public ExecutorService defaultExecutorService() {
}

@Bean
public AppmarketEventDispatcher appmarketEventDispatcher(AppmarketEventClient appmarketEventClient, EditionCodeBasedAddonDetector addonDetector) {
public AppmarketEventDispatcher appmarketEventDispatcher(AppmarketEventClient appmarketEventClient) {
return new AppmarketEventDispatcher(
new Events(),
new AsyncEventHandler(defaultExecutorService(), appmarketEventClient),
Expand All @@ -53,7 +53,7 @@ public AppmarketEventDispatcher appmarketEventDispatcher(AppmarketEventClient ap
new ParseAndHandleWrapper<>(new UserAssignmentParser(), userAssignmentHandler),
new ParseAndHandleWrapper<>(new UserUnassignmentParser(), userUnassignmentHandler),
unknownEventHandler(),
addonDetector
new AddonEventDetector()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.appdirect.sdk.appmarket.events;


import static org.assertj.core.api.Assertions.assertThat;

import org.junit.Test;

public class AddonEventDetectorTest {
private AddonEventDetector addonDetector = new AddonEventDetector();

@Test
public void eventIsFlaggedAsAddonRelated_whenParentAccountId_isPresent() throws Exception {
assertThat(addonDetector.eventIsRelatedToAddon(anEventWithParentAccountId())).isTrue();
}

@Test
public void eventIsNotFlaggedAsAddonRelated_whenParentAccountId_isAbsent() throws Exception {
assertThat(addonDetector.eventIsRelatedToAddon(anEventWithNoParentAccountId())).isFalse();
}

private EventInfo anEventWithParentAccountId() {
return EventInfo.builder()
.payload(EventPayload.builder()
.account(AccountInfo.builder().parentAccountIdentifier("some-parent-account-id").build())
.build())
.build();
}

private EventInfo anEventWithNoParentAccountId() {
return EventInfo.builder().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;

import java.util.Optional;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -80,7 +78,7 @@ public class AppmarketEventDispatcherTest {
private APIResult mockUserUnassignmentResponse;

@Mock
private EditionCodeBasedAddonDetector mockAddonDetector;
private AddonEventDetector mockAddonDetector;

@Before
public void setUp() throws Exception {
Expand All @@ -103,8 +101,6 @@ public void setUp() throws Exception {

when(mockEvents.eventShouldBeHandledAsync(any()))
.thenReturn(false);
when(mockEvents.extractEditionCode(any()))
.thenReturn(Optional.empty());

when(mockSubscriptionOrderHandler.handle(any(), any()))
.thenReturn(mockSubscriptionOrderResponse);
Expand Down Expand Up @@ -230,8 +226,7 @@ public void testDispatchAndHandle_whenTheEventIsSubscriptionUpcomingInvoice_then
public void testDispatchAndHandle_whenTheEventIsSubscriptionOrder_forAddon_thenInvokeAppropriateHandler() throws Exception {
//Given
EventInfo addonTestEvent = someSubOrderEvent();
when(mockEvents.extractEditionCode(addonTestEvent)).thenReturn(Optional.of("add-on-edition"));
when(mockAddonDetector.editionCodeIsRelatedToAddon("add-on-edition")).thenReturn(true);
when(mockAddonDetector.eventIsRelatedToAddon(addonTestEvent)).thenReturn(true);

//When
APIResult result = eventDispatcher.dispatchAndHandle(addonTestEvent, defaultEventContext());
Expand Down

This file was deleted.

6 changes: 0 additions & 6 deletions src/test/java/com/appdirect/sdk/feature/MinimalConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import com.appdirect.sdk.appmarket.Credentials;
import com.appdirect.sdk.appmarket.DeveloperSpecificAppmarketCredentialsSupplier;
import com.appdirect.sdk.appmarket.events.AddonSubscriptionOrder;
import com.appdirect.sdk.appmarket.events.EditionCodeBasedAddonDetector;
import com.appdirect.sdk.appmarket.events.SubscriptionCancel;
import com.appdirect.sdk.appmarket.events.SubscriptionChange;
import com.appdirect.sdk.appmarket.events.SubscriptionClosed;
Expand Down Expand Up @@ -88,11 +87,6 @@ public AppmarketEventHandler<SubscriptionUpcomingInvoice> subscriptionUpcomingNo
);
}

@Bean
public EditionCodeBasedAddonDetector addonDetector() {
return new EditionCodeBasedAddonDetector("addon-edition");
}

@Bean
public AppmarketEventHandler<AddonSubscriptionOrder> addonSubscriptionOrderHandler() {
return event -> success("ADDON_ORDER has been processed just now.");
Expand Down

0 comments on commit fca1758

Please sign in to comment.