Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package pl.piomin.services.account;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import pl.piomin.services.account.service.AccountService;
import pl.piomin.services.messaging.Order;
import tools.jackson.databind.ObjectMapper;

import java.util.function.Consumer;

Expand All @@ -20,8 +18,11 @@ public class AccountApplication {

private ObjectMapper mapper = new ObjectMapper();

@Autowired
AccountService service;
private final AccountService service;

public AccountApplication(AccountService service) {
this.service = service;
}

public static void main(String[] args) {
SpringApplication.run(AccountApplication.class, args);
Expand All @@ -30,18 +31,9 @@ public static void main(String[] args) {
@Bean
public Consumer<Order> input() {
return order -> {
try {
LOGGER.info("Order received: {}", mapper.writeValueAsString(order));
service.process(order);
} catch (JsonProcessingException e) {
LOGGER.error("Error deserializing", e);
}
LOGGER.info("Order received: {}", mapper.writeValueAsString(order));
service.process(order);
};
}

// @Bean
// public Sampler defaultSampler() {
// return new AlwaysSampler();
// }

}
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
package pl.piomin.services.account.controller;

import java.util.Collections;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.springframework.web.bind.annotation.*;
import pl.piomin.services.account.model.Account;
import pl.piomin.services.account.repository.AccountRepository;
import tools.jackson.databind.ObjectMapper;

import java.util.Collections;
import java.util.List;

@RestController
public class AccountController {
Expand All @@ -27,8 +17,11 @@ public class AccountController {

private ObjectMapper mapper = new ObjectMapper();

@Autowired
AccountRepository repository;
private final AccountRepository repository;

public AccountController(AccountRepository repository) {
this.repository = repository;
}

@PostMapping
public Account add(@RequestBody Account account) {
Expand All @@ -41,7 +34,7 @@ public Account update(@RequestBody Account account) {
}

@PutMapping("/withdraw/{id}/{amount}")
public Account withdraw(@PathVariable("id") Long id, @PathVariable("amount") int amount) throws JsonProcessingException {
public Account withdraw(@PathVariable("id") Long id, @PathVariable int amount) {
Account account = repository.findById(id);
LOGGER.info("Account found: {}", mapper.writeValueAsString(account));
account.setBalance(account.getBalance() - amount);
Expand All @@ -50,12 +43,12 @@ public Account withdraw(@PathVariable("id") Long id, @PathVariable("amount") int
}

@GetMapping("/{id}")
public Account findById(@PathVariable("id") Long id) {
public Account findById(@PathVariable Long id) {
return repository.findById(id);
}

@GetMapping("/customer/{customerId}")
public List<Account> findByCustomerId(@PathVariable("customerId") Long customerId) {
public List<Account> findByCustomerId(@PathVariable Long customerId) {
return repository.findByCustomer(customerId);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package pl.piomin.services.account.messaging;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.stereotype.Service;
Expand All @@ -9,8 +8,11 @@
@Service
public class OrderSender {

@Autowired
private StreamBridge source;
private final StreamBridge source;

public OrderSender(StreamBridge source) {
this.source = source;
}

public boolean send(Order order) {
return this.source.send("output", MessageBuilder.withPayload(order).build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import pl.piomin.services.account.messaging.OrderSender;
import pl.piomin.services.account.model.Account;
import pl.piomin.services.account.repository.AccountRepository;
import pl.piomin.services.messaging.Order;
import pl.piomin.services.messaging.OrderStatus;
import tools.jackson.databind.ObjectMapper;

@Service
public class AccountService {
Expand All @@ -23,12 +20,15 @@ public class AccountService {

private ObjectMapper mapper = new ObjectMapper();

@Autowired
AccountRepository accountRepository;
@Autowired
OrderSender orderSender;
private final AccountRepository accountRepository;
private final OrderSender orderSender;

public AccountService(AccountRepository accountRepository, OrderSender orderSender) {
this.accountRepository = accountRepository;
this.orderSender = orderSender;
}

public void process(final Order order) throws JsonProcessingException {
public void process(final Order order) {
LOGGER.info("Order processed: {}", mapper.writeValueAsString(order));
List<Account> accounts = accountRepository.findByCustomer(order.getCustomerId());
Account account = accounts.get(0);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package pl.piomin.services.account;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -14,6 +12,7 @@
import org.springframework.messaging.Message;
import pl.piomin.services.messaging.Order;
import pl.piomin.services.messaging.OrderStatus;
import tools.jackson.databind.ObjectMapper;

import java.util.Collections;

Expand All @@ -34,7 +33,7 @@ public class OrderReceiverTest {
private ObjectMapper mapper;

@Test
public void testAccepted() throws JsonProcessingException {
public void testAccepted() {
Order o = new Order();
o.setId(1L);
o.setAccountId(1L);
Expand All @@ -52,7 +51,7 @@ public void testAccepted() throws JsonProcessingException {
}

@Test
public void testRejected() throws JsonProcessingException {
public void testRejected() {
Order o = new Order();
o.setId(1L);
o.setAccountId(1L);
Expand Down
10 changes: 10 additions & 0 deletions order-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-resttestclient</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-restclient</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package pl.piomin.services.order;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import tools.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
Expand All @@ -20,8 +18,11 @@ public class OrderApplication {
private static final Logger LOGGER = LoggerFactory.getLogger(OrderApplication.class);
private ObjectMapper mapper = new ObjectMapper();

@Autowired
OrderService service;
private final OrderService service;

public OrderApplication(OrderService service) {
this.service = service;
}

public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
Expand All @@ -30,12 +31,8 @@ public static void main(String[] args) {
@Bean
public Consumer<Order> input() {
return order -> {
try {
LOGGER.info("Order received: {}", mapper.writeValueAsString(order));
service.process(order);
} catch (JsonProcessingException e) {
LOGGER.error("Error deserializing", e);
}
LOGGER.info("Order received: {}", mapper.writeValueAsString(order));
service.process(order);
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import tools.jackson.databind.ObjectMapper;

import pl.piomin.services.messaging.Order;
import pl.piomin.services.messaging.OrderStatus;
Expand All @@ -26,13 +24,16 @@ public class OrderController {

private ObjectMapper mapper = new ObjectMapper();

@Autowired
OrderRepository repository;
@Autowired
OrderSender sender;
private final OrderRepository repository;
private final OrderSender sender;

public OrderController(OrderRepository repository, OrderSender sender) {
this.repository = repository;
this.sender = sender;
}

@PostMapping("/")
public Order process(@RequestBody Order order) throws JsonProcessingException {
public Order process(@RequestBody Order order) {
Order o = repository.add(order);
LOGGER.info("Order saved: {}", mapper.writeValueAsString(order));
boolean isSent = sender.send(o);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package pl.piomin.services.order.messaging;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.stereotype.Service;
Expand All @@ -9,8 +8,11 @@
@Service
public class OrderSender {

@Autowired
private StreamBridge source;
private final StreamBridge source;

public OrderSender(StreamBridge source) {
this.source = source;
}

public boolean send(Order order) {
return this.source.send("output", MessageBuilder.withPayload(order).build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import tools.jackson.databind.ObjectMapper;

import pl.piomin.services.messaging.Order;
import pl.piomin.services.messaging.OrderStatus;
Expand All @@ -19,10 +17,13 @@ public class OrderService {

private ObjectMapper mapper = new ObjectMapper();

@Autowired
OrderRepository repository;
private final OrderRepository repository;

public OrderService(OrderRepository repository) {
this.repository = repository;
}

public void process(final Order order) throws JsonProcessingException {
public void process(final Order order) {
LOGGER.info("Order processed: {}", mapper.writeValueAsString(order));
Order o = repository.findById(order.getId());
if (o.getStatus() != OrderStatus.REJECTED) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.resttestclient.TestRestTemplate;
import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureTestRestTemplate;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.testcontainers.containers.RabbitMQContainer;
import org.testcontainers.junit.jupiter.Container;
Expand All @@ -19,6 +20,7 @@

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Testcontainers
@AutoConfigureTestRestTemplate
public class OrderControllerTest {

@Autowired
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.5.11</version>
<version>4.0.3</version>
<relativePath/>
</parent>

Expand All @@ -26,7 +26,7 @@
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2025.0.0</version>
<version>2025.1.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down
Loading