Подключил Spring Boot и добавил API получения операций с обработкой 404 и HTTP-тестом

This commit is contained in:
Kashey committed 2026-10-20 19:41:53 +03:00
1 parent 70f61cdc64
commit cd404fae0b
11 files changed
+194 -12

No files matched your search

@@ -0,0 +1,12 @@
package net.sytes.kashey.easymoney;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class EasyMoneyApplication {
public static void main(String[] args) {
SpringApplication.run(EasyMoneyApplication.class, args);
}
}
@@ -0,0 +1,17 @@
package net.sytes.kashey.easymoney.api;
import org.springframework.http.HttpStatus;
import org.springframework.http.ProblemDetail;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import service.OperationNotFoundException;
@RestControllerAdvice
public class ApiExceptionHandler {
@ExceptionHandler(OperationNotFoundException.class)
public ProblemDetail handleOperationNotFound(OperationNotFoundException exception) {
return ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND, exception.getMessage());
}
}
@@ -0,0 +1,34 @@
package net.sytes.kashey.easymoney.api;
import model.Operation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import service.OperationService;
@RestController
@RequestMapping("/api/operations")
public class OperationController {
private final OperationService service;
public OperationController(OperationService service) {
this.service = service;
}
@GetMapping("/{id}")
public OperationResponse getById(@PathVariable("id") Long id) {
Operation operation = service.getById(id);
return new OperationResponse(
operation.getId(),
operation.getDate(),
operation.getType(),
operation.getSourceAccount() == null ? null : operation.getSourceAccount().getId(),
operation.getDestinationAccount() == null ? null : operation.getDestinationAccount().getId(),
operation.getTotal(),
service.isPosted(operation)
);
}
}
@@ -0,0 +1,17 @@
package net.sytes.kashey.easymoney.api;
import model.OperationType;
import java.math.BigDecimal;
import java.time.LocalDate;
public record OperationResponse(
Long id,
LocalDate date,
OperationType type,
Long sourceAccountId,
Long destinationAccountId,
BigDecimal total,
boolean posted
) {
}
@@ -0,0 +1,15 @@
package net.sytes.kashey.easymoney.api;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
public class StatusController {
@GetMapping("/api/status")
public Map<String, String> status() {
return Map.of("status", "ok");
}
}
@@ -0,0 +1,30 @@
package net.sytes.kashey.easymoney.config;
import model.OperationJournal;
import service.Ledger;
import service.OperationService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AccountingConfig {
@Bean
public OperationJournal operationJournal() {
return new OperationJournal();
}
@Bean
public Ledger ledger() {
return new Ledger();
}
@Bean
public OperationService operationService(
OperationJournal journal,
Ledger ledger
) {
return new OperationService(journal, ledger);
}
}
@@ -0,0 +1,8 @@
package service;
public class OperationNotFoundException extends RuntimeException {
public OperationNotFoundException(Long operationId) {
super("Операция не найдена: " + operationId);
}
}
+5 -3
View File
@@ -44,6 +44,10 @@ public class OperationService {
return ledger.isPosted(operation.getId());
}
public Operation getById(Long operationId) {
return requiredOperation(operationId);
}
private Operation requiredOperation(Long operationId) {
if (operationId == null) {
@@ -53,9 +57,7 @@ public class OperationService {
Operation operation = journal.getOperationById(operationId);
if (operation == null) {
throw new IllegalArgumentException(
"Операция не найдена: " + operationId
);
throw new OperationNotFoundException(operationId);
}
return operation;
}
@@ -0,0 +1,31 @@
package net.sytes.kashey.easymoney.api;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.webmvc.test.autoconfigure.WebMvcTest;
import org.springframework.context.annotation.Import;
import net.sytes.kashey.easymoney.config.AccountingConfig;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@WebMvcTest(OperationController.class)
@Import(AccountingConfig.class)
class OperationControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
void gettingUnknownOperationReturnsNotFound() throws Exception {
// When / Then
mockMvc.perform(get("/api/operations/999"))
.andExpect(status().isNotFound())
.andExpect(jsonPath("$.status").value(404))
.andExpect(jsonPath("$.detail")
.value("Операция не найдена: 999"));
}
}
@@ -148,7 +148,7 @@ class OperationServiceTest {
operationService.post(Operation.openingBalance(date, 101L, card, new BigDecimal(15000)));
// When / Then
assertThrows(IllegalArgumentException.class, () -> operationService.post(999L));
assertThrows(OperationNotFoundException.class, () -> operationService.post(999L));
}
@Test
@@ -162,6 +162,6 @@ class OperationServiceTest {
operationService.post(Operation.openingBalance(date, 101L, card, new BigDecimal(15000)));
// When / Then
assertThrows(IllegalArgumentException.class, () -> operationService.unpost(999L));
assertThrows(OperationNotFoundException.class, () -> operationService.unpost(999L));
}
}