-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from CJSCommonPlatform/subscriptio-prioritisation
Event Catch-Up Subscription prioritisation
- Loading branch information
Showing
17 changed files
with
191 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...cing/event-subscription/src/main/java/uk/gov/justice/subscription/SubscriptionHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package uk.gov.justice.subscription; | ||
|
||
import static java.lang.Integer.parseInt; | ||
import static java.lang.String.format; | ||
import static java.util.Comparator.comparing; | ||
import static java.util.Comparator.naturalOrder; | ||
|
||
import uk.gov.justice.subscription.domain.subscriptiondescriptor.SubscriptionsDescriptor; | ||
import uk.gov.justice.subscription.yaml.parser.YamlParserException; | ||
|
||
public class SubscriptionHelper { | ||
|
||
private final static int HIGHEST_PRIORITY = 0; | ||
|
||
public void sortSubscriptionsByPrioritisation(final SubscriptionsDescriptor subscriptionsDescriptor) { | ||
if (!subscriptionsDescriptor.getSubscriptions().isEmpty()) { | ||
subscriptionsDescriptor.getSubscriptions().sort(comparing(subscription -> getPrioritisationNumber(subscription.getPrioritisation()), naturalOrder())); | ||
} | ||
} | ||
|
||
private Integer getPrioritisationNumber(final String prioritisation) { | ||
try { | ||
if (null != prioritisation) { | ||
return parseInt(prioritisation); | ||
} | ||
return HIGHEST_PRIORITY; | ||
} catch (final NumberFormatException nfe) { | ||
throw new YamlParserException(format("Incorrect prioritisation number: %s in subscription-descriptor.yaml", prioritisation)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
.../event-subscription/src/test/java/uk/gov/justice/subscription/SubscriptionHelperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package uk.gov.justice.subscription; | ||
|
||
|
||
import static org.hamcrest.Matchers.is; | ||
import static java.util.Arrays.asList; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.junit.Assert.fail; | ||
import static org.mockito.Mockito.mock; | ||
|
||
import uk.gov.justice.subscription.domain.subscriptiondescriptor.Event; | ||
import uk.gov.justice.subscription.domain.subscriptiondescriptor.Subscription; | ||
import uk.gov.justice.subscription.domain.subscriptiondescriptor.SubscriptionsDescriptor; | ||
import uk.gov.justice.subscription.yaml.parser.YamlParserException; | ||
|
||
import java.util.List; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class SubscriptionHelperTest { | ||
|
||
@Test | ||
public void shouldSortBySubscriptionPrioritisation() { | ||
|
||
final List<Event> eventList = asList(mock(Event.class),mock(Event.class)); | ||
|
||
final Subscription subscription1 = new Subscription("name1",eventList,"eventSourceName1", "1"); | ||
final Subscription subscription2 = new Subscription("name2",eventList,"eventSourceName3", "2"); | ||
final Subscription subscription3 = new Subscription("name3",eventList,"eventSourceName1", "3"); | ||
|
||
final List<Subscription> subscriptions = asList(subscription2,subscription3,subscription1); | ||
final SubscriptionsDescriptor subscriptionsDescriptor = new SubscriptionsDescriptor("specVersion", "service", "serviceComponent", subscriptions); | ||
|
||
final SubscriptionHelper subscriptionHelper = new SubscriptionHelper(); | ||
|
||
subscriptionHelper.sortSubscriptionsByPrioritisation(subscriptionsDescriptor); | ||
|
||
assertThat(subscriptionsDescriptor.getSubscriptions().get(0), is(subscription1)); | ||
assertThat(subscriptionsDescriptor.getSubscriptions().get(1), is(subscription2)); | ||
assertThat(subscriptionsDescriptor.getSubscriptions().get(2), is(subscription3)); | ||
|
||
} | ||
|
||
@Test | ||
public void shouldSortBySubscriptionPrioritisationNullsFirst() { | ||
|
||
final List<Event> eventList = asList(mock(Event.class),mock(Event.class)); | ||
|
||
final Subscription subscription1 = new Subscription("name1",eventList,"eventSourceName1", "1"); | ||
final Subscription subscription2 = new Subscription("name2",eventList,"eventSourceName3", null); | ||
final Subscription subscription3 = new Subscription("name3",eventList,"eventSourceName1", "3"); | ||
|
||
final List<Subscription> subscriptions = asList(subscription2,subscription3,subscription1); | ||
|
||
final SubscriptionsDescriptor subscriptionsDescriptor = new SubscriptionsDescriptor("specVersion", "service", "serviceComponent", subscriptions); | ||
|
||
final SubscriptionHelper subscriptionHelper = new SubscriptionHelper(); | ||
|
||
subscriptionHelper.sortSubscriptionsByPrioritisation(subscriptionsDescriptor); | ||
|
||
assertThat(subscriptionsDescriptor.getSubscriptions().get(0), is(subscription2)); | ||
assertThat(subscriptionsDescriptor.getSubscriptions().get(1), is(subscription1)); | ||
assertThat(subscriptionsDescriptor.getSubscriptions().get(2), is(subscription3)); | ||
|
||
} | ||
|
||
@Test | ||
public void shouldThrowExceptionIfPrioritisationNotNumber() { | ||
|
||
final List<Event> eventList = asList(mock(Event.class),mock(Event.class)); | ||
|
||
final Subscription subscription1 = new Subscription("name1",eventList,"eventSourceName1", "1"); | ||
final Subscription subscription2 = new Subscription("name2",eventList,"eventSourceName3", "wrongPrioritisation"); | ||
final Subscription subscription3 = new Subscription("name3",eventList,"eventSourceName1", "3"); | ||
|
||
final List<Subscription> subscriptions = asList(subscription2,subscription3,subscription1); | ||
|
||
final SubscriptionsDescriptor subscriptionsDescriptor = new SubscriptionsDescriptor("specVersion", "service", "serviceComponent", subscriptions); | ||
|
||
final SubscriptionHelper subscriptionHelper = new SubscriptionHelper(); | ||
|
||
try { | ||
subscriptionHelper.sortSubscriptionsByPrioritisation(subscriptionsDescriptor); | ||
fail(); | ||
}catch (final YamlParserException expected){ | ||
assertThat(expected.getMessage(), is("Incorrect prioritisation number: wrongPrioritisation in subscription-descriptor.yaml")); | ||
} | ||
} | ||
} |
Oops, something went wrong.