98 lines
3.6 KiB
Java
Raw Normal View History

2017-08-19 22:06:35 +03:00
/**
* The MIT License
2019-10-12 20:05:54 +03:00
* Copyright © 2014-2019 Ilkka Seppälä
2017-08-19 22:06:35 +03:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
import com.iluwatar.event.sourcing.domain.Account;
import com.iluwatar.event.sourcing.event.AccountCreateEvent;
import com.iluwatar.event.sourcing.event.MoneyDepositEvent;
import com.iluwatar.event.sourcing.event.MoneyTransferEvent;
2017-08-19 22:06:35 +03:00
import com.iluwatar.event.sourcing.processor.DomainEventProcessor;
import com.iluwatar.event.sourcing.state.AccountAggregate;
2017-12-31 16:29:48 +09:00
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
2017-08-19 22:06:35 +03:00
import java.math.BigDecimal;
import java.util.Date;
2017-12-31 16:29:48 +09:00
import static com.iluwatar.event.sourcing.app.App.ACCOUNT_OF_DAENERYS;
import static com.iluwatar.event.sourcing.app.App.ACCOUNT_OF_JON;
import static org.junit.jupiter.api.Assertions.assertEquals;
2017-08-19 22:06:35 +03:00
/**
* Intergartion Test for Event Sourcing state recovery
*
* Created by Serdar Hamzaogullari on 19.08.2017.
*/
public class IntegrationTest {
/**
* The Domain event processor.
*/
private DomainEventProcessor eventProcessor;
2017-08-19 22:06:35 +03:00
/**
* Initialize.
*/
2017-12-31 16:29:48 +09:00
@BeforeEach
2017-08-19 22:06:35 +03:00
public void initialize() {
eventProcessor = new DomainEventProcessor();
2017-08-19 22:06:35 +03:00
}
/**
* Test state recovery.
*/
@Test
public void testStateRecovery() {
eventProcessor.reset();
eventProcessor.process(new AccountCreateEvent(
0, new Date().getTime(), ACCOUNT_OF_DAENERYS, "Daenerys Targaryen"));
2017-08-19 22:06:35 +03:00
eventProcessor.process(new AccountCreateEvent(
1, new Date().getTime(), ACCOUNT_OF_JON, "Jon Snow"));
2017-08-19 22:06:35 +03:00
eventProcessor.process(new MoneyDepositEvent(
2, new Date().getTime(), ACCOUNT_OF_DAENERYS, new BigDecimal("100000")));
2017-08-19 22:06:35 +03:00
eventProcessor.process(new MoneyDepositEvent(
3, new Date().getTime(), ACCOUNT_OF_JON, new BigDecimal("100")));
eventProcessor.process(new MoneyTransferEvent(
4, new Date().getTime(), new BigDecimal("10000"), ACCOUNT_OF_DAENERYS,
ACCOUNT_OF_JON));
2017-08-19 22:06:35 +03:00
Account accountOfDaenerysBeforeShotDown = AccountAggregate.getAccount(ACCOUNT_OF_DAENERYS);
Account accountOfJonBeforeShotDown = AccountAggregate.getAccount(ACCOUNT_OF_JON);
AccountAggregate.resetState();
eventProcessor = new DomainEventProcessor();
eventProcessor.recover();
2017-08-19 22:06:35 +03:00
Account accountOfDaenerysAfterShotDown = AccountAggregate.getAccount(ACCOUNT_OF_DAENERYS);
Account accountOfJonAfterShotDown = AccountAggregate.getAccount(ACCOUNT_OF_JON);
2017-12-31 16:29:48 +09:00
assertEquals(accountOfDaenerysBeforeShotDown.getMoney(),
2017-08-19 22:06:35 +03:00
accountOfDaenerysAfterShotDown.getMoney());
2017-12-31 16:29:48 +09:00
assertEquals(accountOfJonBeforeShotDown.getMoney(), accountOfJonAfterShotDown.getMoney());
2017-08-19 22:06:35 +03:00
}
}