Skip to content

Commit

Permalink
Merge pull request #45 from eamtalu/develop
Browse files Browse the repository at this point in the history
#33 OpenAccountUseCase - Publishing events
  • Loading branch information
valentinacupac committed Jun 27, 2022
2 parents 71725a2 + 020173d commit c790d74
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 18 deletions.
8 changes: 0 additions & 8 deletions README.md
Expand Up @@ -25,14 +25,6 @@ We implement a Banking system with the following use cases:

## Environment variables

When you open up the file `application.yml` we can see the usage of the following environment variables for the Postgres Database

```
url: ${POSTGRES_URL}
username: ${POSTGRES_USER}
password: ${POSTGRES_PASSWORD}
```

To be able to run the tests (since some of the tests are dependent on the database - the integration tests), we then need to set the environment variables.

In IntelliJ, for the `Tests in 'banking-kata.test'` configuration, you can copy this into the `Environment variables`
Expand Down
22 changes: 12 additions & 10 deletions docker-compose.yml
@@ -1,13 +1,15 @@
version: '3.8'
services:
postgres:
image: postgres:latest
container_name: postgres
db:
image: postgres:14.1-alpine
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
ports:
- "5432:5432"
restart: unless-stopped
- '5432:5432'
volumes:
- ./db_persist:/var/lib/postgresql/data
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB}
- db:/var/lib/postgresql/data
volumes:
db:
driver: local
@@ -0,0 +1,8 @@
package com.optivem.kata.banking.core.domain.common.events;

/**
* Event Publisher interface will provide eventpublishing functionality to usecases
*/
public interface EventPublisher {
public void publishEvent(UseCaseEvent event);
}
@@ -0,0 +1,21 @@
package com.optivem.kata.banking.core.domain.common.events;


import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;


@Component
public class EventPublisherToQueue implements EventPublisher {

private ApplicationEventPublisher applicationEventPublisher;

public EventPublisherToQueue(ApplicationEventPublisher applicationEventPublisher){
this.applicationEventPublisher = applicationEventPublisher;
}

@Override
public void publishEvent(UseCaseEvent event) {
applicationEventPublisher.publishEvent(event);
}
}
@@ -0,0 +1,24 @@
package com.optivem.kata.banking.core.domain.common.events;

import com.optivem.kata.banking.core.domain.accounts.AccountId;
import lombok.Getter;
import org.springframework.context.ApplicationEvent;

import java.time.LocalDateTime;

/**
* This class serves as the Event Object that to be puslished by ApplicationEventPublisher
*/
@Getter
public class UseCaseEvent extends ApplicationEvent {

private AccountId id;
private String eventName;
private LocalDateTime localDateTime;
public UseCaseEvent(AccountId id, String eventName) {
super(id);
this.id = id;
this.eventName = eventName;
this.localDateTime = LocalDateTime.now();
}
}
@@ -0,0 +1,31 @@
package com.optivem.kata.banking.core.domain.common.events;

import com.optivem.kata.banking.infra.real.inmemory.EventQueue;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
* EventListener class that will observe any event of type UseCaseEvent and act accordingly
* It will also insert the event into queue.
*/
@Component
public class UseCaseEventHandler{

private final EventQueue<UseCaseEvent> queue;

public UseCaseEventHandler(EventQueue<UseCaseEvent> eventEventQueue){
this.queue = eventEventQueue;
}

@EventListener
public void handleEvent(UseCaseEvent event){
queue.addEvent(event);

// var eventFromQueue = queue.next();
// System.out.println(eventFromQueue.getId());
// System.out.println(eventFromQueue.getEventName());
// System.out.println(eventFromQueue.getLocalDateTime());

}

}
@@ -0,0 +1,14 @@
package com.optivem.kata.banking.core.domain.common.events.UseCaseEvents;


import com.optivem.kata.banking.core.domain.accounts.AccountId;
import com.optivem.kata.banking.core.domain.common.events.UseCaseEvent;

public class AccountOpenedUseCaseEvent {

public static UseCaseEvent generateEventOnSuccess(AccountId id){
return new UseCaseEvent(id,"AccountOpened");
}


}
@@ -0,0 +1,30 @@
package com.optivem.kata.banking.infra.real.inmemory;

import org.springframework.stereotype.Component;

import java.util.ArrayDeque;
import java.util.NoSuchElementException;
import java.util.Queue;


@Component
public class EventQueue<T> {

private final Queue<T> queue;

public EventQueue(){
this.queue = new ArrayDeque<>();
}

public void addEvent(T event){
queue.add(event);
}

public T next(){
if(queue.isEmpty()){
throw new NoSuchElementException();
}
return queue.remove();
}

}

0 comments on commit c790d74

Please sign in to comment.