Skip to content

Commit

Permalink
Inventory service testcases (#69)
Browse files Browse the repository at this point in the history
* added testcases for service

* added testcases for inventory services

* added testcases for config, events, and exceptions

* minor changes

* added few test cases for handler

* added test cases for aggregate

* removed code smells and added read me file

---------

Co-authored-by: shivamm31 <[email protected]>
  • Loading branch information
shivamm31 and shivamm31 authored Jan 16, 2024
1 parent d494ea3 commit 41ace5b
Show file tree
Hide file tree
Showing 28 changed files with 1,668 additions and 37 deletions.
5 changes: 5 additions & 0 deletions .sapient/mock_preferences.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"note" : "Please restart the plugin after making changes to the lists below. If you want to mock files of any package, please add the package to packagesToMock list ex org.apache.commons. If you don't want to mock files of any package, please add the package to packagesToNotMock list ex com.google.gson Please make sure the json is a valid json, or it will revert to default list of packages.",
"classesAndPackagesNotToMock" : [ ],
"classesAndPackagesToMock" : [ ]
}
49 changes: 49 additions & 0 deletions inventory-service/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Inventory Service

##### Application Overview

This application is conscientiously crafted to oversee the management of available cars for sale within a system. Leveraging the Axon Framework for CQRS (Command Query Responsibility Segregation) and Event Sourcing, it seamlessly orchestrates the creation of inventory, ensures the storage of product details in a database, and adeptly publishes events to a Google Cloud Pub/Sub topic.

#### Workflow

### Inventory Creation

- It all starts with a CreateProductCommand, managed by the Inventory Aggregate class.
- The aggregate takes care of the command, leading to the triggering of the ProductCreatedEvent.

### Event Handling

The ProductEventHandler takes care of the ProductCreatedEvent.

- It grabs the product information from the event.
- The details are then stored in a database through the ProductRepository.
- On top of that, the PubSubPublisherService is employed to broadcast the product details to a Google Cloud Pub/Sub topic.

### Google Cloud Pub/Sub Integration:

- The PubSubPublisherService sets up the Pub/Sub publisher when the application starts using @PostConstruct.
- It handles turning product data into a Pub/Sub message and sends it to the specified topic.
- When the application shuts down with @PreDestroy, the publisher is smoothly stopped.

### Data Storage

- Product details are persisted to a database for future reference.
- The ProductRepository handles the database interactions.

##### Key Components

### InventoryAggregate



Manages all aspects of product creation, monitors ongoing activities, and stays informed through events to ensure up-to-date awareness of the process.

### ProductEventHandler


Listens for the ProductCreatedEvent, grabs product info from the event, stores it in a database through the ProductRepository, and spreads the word about the product by sharing details on Google Cloud Pub/Sub using the PubSubPublisherService.

### PubSubPublisherService

Sets up a Google Cloud Pub/Sub publisher when the application starts, sends product information to a Pub/Sub topic, and ensures a smooth shutdown for the publisher when the application closes.

2 changes: 2 additions & 0 deletions inventory-service/lombok.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
config.stopBubbling = true
lombok.addLombokGeneratedAnnotation = true
59 changes: 47 additions & 12 deletions inventory-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,16 @@
<artifactId>inventory-service</artifactId>
<version>1.0.0</version>
<name>inventory-service</name>

<properties>
<java.version>19</java.version>
<common.version>2.2</common.version>
<validation-api.version>2.0.1.Final</validation-api.version>
<spring-cloud-gcp.version>4.1.1</spring-cloud-gcp.version>
<spring-cloud.version>2022.0.1</spring-cloud.version>
<maven.compiler.source>19</maven.compiler.source>
<maven.sonar.version>3.9.1.2184</maven.sonar.version>
<maven.jacoco.version>0.8.8</maven.jacoco.version>
</properties>
<repositories>
<repository>
<id>central</id>
Expand Down Expand Up @@ -58,17 +67,6 @@
</dependencies>
</dependencyManagement>

<properties>
<java.version>19</java.version>
<common.version>2.2</common.version>
<validation-api.version>2.0.1.Final</validation-api.version>
<spring-cloud-gcp.version>4.1.1</spring-cloud-gcp.version>
<spring-cloud.version>2022.0.1</spring-cloud.version>
<maven.compiler.source>19</maven.compiler.source>
<maven.sonar.version>3.9.1.2184</maven.sonar.version>
<maven.jacoco.version>0.8.8</maven.jacoco.version>
</properties>

<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
Expand Down Expand Up @@ -108,6 +106,43 @@
<version>4.7.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.axonframework</groupId>
<artifactId>axon-test</artifactId>
<version>4.6.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-engine</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.nashtech.inventory.command.interceptors.CreateProductCommandInterceptor;
import com.nashtech.inventory.exception.ProductsServiceEventsErrorHandler;
import lombok.AllArgsConstructor;
import org.axonframework.commandhandling.CommandBus;
import org.axonframework.commandhandling.gateway.CommandGateway;
import org.axonframework.config.EventProcessingConfigurer;
Expand All @@ -12,9 +13,10 @@


@SpringBootApplication
@AllArgsConstructor
public class InventoryApplication {

@Autowired

private CommandGateway commandGateway;

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.nashtech.common.event.ProductReservedEvent;
import com.nashtech.inventory.command.CreateProductCommand;
import com.nashtech.inventory.events.ProductCreatedEvent;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.axonframework.commandhandling.CommandHandler;
import org.axonframework.common.StringUtils;
Expand All @@ -16,7 +17,7 @@
import org.axonframework.modelling.command.AggregateLifecycle;
import org.axonframework.spring.stereotype.Aggregate;


@Data
@Aggregate
@Slf4j
public class InventoryAggregate{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.axonframework.messaging.MessageDispatchInterceptor;
import org.springframework.stereotype.Component;

import javax.annotation.Nonnull;
import java.util.List;
import java.util.function.BiFunction;

Expand All @@ -21,9 +22,10 @@ public CreateProductCommandInterceptor(ProductLookupRepository productLookupRepo
this.productLookupRepository = productLookupRepository;
}

@Nonnull
@Override
public BiFunction<Integer, CommandMessage<?>, CommandMessage<?>> handle(
List<? extends CommandMessage<?>> messages) {
@Nonnull List<? extends CommandMessage<?>> messages) {

return (index, command) -> {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.nashtech.inventory.exception;

public class ProductNotFound extends RuntimeException {
private String message;

public ProductNotFound(String message) {
super(message);
this.message = message;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import org.axonframework.eventhandling.EventMessageHandler;
import org.axonframework.eventhandling.ListenerInvocationErrorHandler;

import javax.annotation.Nonnull;

public class ProductsServiceEventsErrorHandler implements ListenerInvocationErrorHandler {

@Override
public void onError(Exception exception, EventMessage<?> event, EventMessageHandler eventHandler) throws Exception {
public void onError(Exception exception, @Nonnull EventMessage<?> event, @Nonnull EventMessageHandler eventHandler) throws Exception {
throw exception;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import lombok.extern.slf4j.Slf4j;
import org.axonframework.config.ProcessingGroup;
import org.axonframework.eventhandling.EventHandler;
import org.axonframework.messaging.interceptors.ExceptionHandler;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

Expand Down Expand Up @@ -52,9 +51,4 @@ public void on(ProductReserveCancelledEvent productReservationCancelledEvent) {
productsRepository.save(currentlyStoredProductEntity);
}

@ExceptionHandler
public void handle(Exception exception) throws Exception {
throw exception;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.Data;

import lombok.*;
import java.util.Date;


@Entity
@Table(name="products")
@Data
@Getter
@Setter
@ToString
@RequiredArgsConstructor
public class ProductEntity {
@Id
private String productId;
Expand All @@ -23,5 +25,4 @@ public class ProductEntity {
private Integer quantity;
private Float tax;
private Date timestamp = new Date();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.nashtech.inventory;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import com.nashtech.inventory.command.interceptors.CreateProductCommandInterceptor;
import com.nashtech.inventory.exception.ProductsServiceEventsErrorHandler;
import org.axonframework.commandhandling.CommandBus;
import org.axonframework.commandhandling.gateway.CommandGateway;
import org.axonframework.config.EventProcessingConfigurer;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;

class InventoryApplicationTest {

CommandGateway commandGateway;
/**
* Test case to ensure that the create product command interceptor is registered successfully.
*/
@Test
void test_create_product_command_interceptor_registered_successfully() {
ApplicationContext context = mock(ApplicationContext.class);
CommandBus commandBus = mock(CommandBus.class);
CreateProductCommandInterceptor interceptor = mock(CreateProductCommandInterceptor.class);

when(context.getBean(CreateProductCommandInterceptor.class)).thenReturn(interceptor);


InventoryApplication inventoryApplication = new InventoryApplication(commandGateway);

inventoryApplication.registerCreateProductCommandInterceptor(context, commandBus);

verify(commandBus).registerDispatchInterceptor(interceptor);
}

/**
* Test case to ensure that the ProductsServiceEventsErrorHandler is configured successfully.
*/
@Test
void test_products_service_events_error_handler_configured_successfully() {
EventProcessingConfigurer config = mock(EventProcessingConfigurer.class);
mock(ProductsServiceEventsErrorHandler.class);

when(config.registerListenerInvocationErrorHandler(eq("product-group"), any())).thenReturn(config);

InventoryApplication inventoryApplication = new InventoryApplication(commandGateway);

inventoryApplication.configure(config);

verify(config).registerListenerInvocationErrorHandler(eq("product-group"), any());
}
}

This file was deleted.

Loading

0 comments on commit 41ace5b

Please sign in to comment.