-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-449 - Creation of new AccountancyEntry now publishes an event.
The creation of a new AccountancyEntry (custom or default) now causes an AccountancyEntryCreated event to be published, carrying the entry as payload. Listeners can selectively listen to events for particular entry types by declaring generics accordingly (see AccountancyEventPublicationTests).
- Loading branch information
Showing
3 changed files
with
158 additions
and
4 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
53 changes: 53 additions & 0 deletions
53
src/main/java/org/salespointframework/accountancy/AccountancyEvents.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,53 @@ | ||
/* | ||
* Copyright 2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.salespointframework.accountancy; | ||
|
||
import lombok.Value; | ||
|
||
import org.jmolecules.event.types.DomainEvent; | ||
import org.springframework.core.ResolvableType; | ||
import org.springframework.core.ResolvableTypeProvider; | ||
|
||
/** | ||
* All events published by the accountancy. | ||
* | ||
* @author Oliver Drotbohm | ||
* @since 9.0.1 | ||
*/ | ||
public class AccountancyEvents { | ||
|
||
/** | ||
* Published whenever an {@link AccountancyEntry} is created within the system. | ||
* | ||
* @author Oliver Drotbohm | ||
* @since 9.0.1 | ||
*/ | ||
@Value | ||
public static class AccountancyEntryCreated<T extends AccountancyEntry> | ||
implements DomainEvent, ResolvableTypeProvider { | ||
|
||
AccountancyEntry entry; | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see org.springframework.core.ResolvableTypeProvider#getResolvableType() | ||
*/ | ||
@Override | ||
public ResolvableType getResolvableType() { | ||
return ResolvableType.forClassWithGenerics(AccountancyEntryCreated.class, entry.getClass()); | ||
} | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
src/test/java/org/salespointframework/accountancy/AccountancyEventPublicationTests.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,99 @@ | ||
/* | ||
* Copyright 2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.salespointframework.accountancy; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import jakarta.persistence.Entity; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import javax.money.MonetaryAmount; | ||
|
||
import org.javamoney.moneta.Money; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.salespointframework.accountancy.AccountancyEventPublicationTests.CustomAccountancyEntryListener; | ||
import org.salespointframework.accountancy.AccountancyEvents.AccountancyEntryCreated; | ||
import org.salespointframework.core.Currencies; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.modulith.test.ApplicationModuleTest; | ||
import org.springframework.modulith.test.AssertablePublishedEvents; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
/** | ||
* Integration tests related to event publication within the accountancy. | ||
* | ||
* @author Oliver Drotbohm | ||
*/ | ||
@Transactional | ||
@ApplicationModuleTest(extraIncludes = "org.salespointframework.time") | ||
@RequiredArgsConstructor | ||
@DirtiesContext | ||
@Import(CustomAccountancyEntryListener.class) | ||
class AccountancyEventPublicationTests { | ||
|
||
private final Accountancy accountancy; | ||
private final CustomAccountancyEntryListener listener; | ||
|
||
@AfterEach | ||
void afterEach() { | ||
listener.event = null; | ||
} | ||
|
||
@Test // GH-449 | ||
void publishesEventForCreation(AssertablePublishedEvents events) { | ||
|
||
var entry = accountancy.add(new AccountancyEntry(Money.of(10, Currencies.EURO))); | ||
|
||
assertThat(events).contains(AccountancyEntryCreated.class) | ||
.matching(it -> it.getEntry().equals(entry)); | ||
|
||
assertThat(listener.event).isNull(); | ||
} | ||
|
||
@Test // GH-449 | ||
void listensToCustomEventsByGenericsDeclaration() { | ||
|
||
var entry = accountancy.add(new CustomAccountancyEntry(Money.of(10, Currencies.EURO))); | ||
|
||
assertThat(listener.event).isNotNull() | ||
.satisfies(it -> { | ||
assertThat(it.getEntry()).isEqualTo(entry); | ||
}); | ||
} | ||
|
||
@Component | ||
static class CustomAccountancyEntryListener { | ||
|
||
AccountancyEntryCreated<? extends AccountancyEntry> event; | ||
|
||
@EventListener | ||
void on(AccountancyEntryCreated<CustomAccountancyEntry> event) { | ||
this.event = event; | ||
} | ||
} | ||
|
||
@Entity | ||
static class CustomAccountancyEntry extends AccountancyEntry { | ||
|
||
public CustomAccountancyEntry(MonetaryAmount amount) { | ||
super(amount, "Custom entry"); | ||
} | ||
} | ||
} |