Подключил 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

+23 -7
View File
@@ -5,20 +5,37 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>4.0.8</version>
<relativePath/>
</parent>
<groupId>net.sytes.kashey</groupId> <groupId>net.sytes.kashey</groupId>
<artifactId>easy-money</artifactId> <artifactId>easy-money</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<properties> <properties>
<maven.compiler.release>21</maven.compiler.release> <java.version>21</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> </properties>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.junit.jupiter</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>junit-jupiter</artifactId> <artifactId>spring-boot-starter-webmvc</artifactId>
<version>5.11.4</version> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webmvc-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
@@ -26,9 +43,8 @@
<build> <build>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>maven-surefire-plugin</artifactId> <artifactId>spring-boot-maven-plugin</artifactId>
<version>3.5.2</version>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
@@ -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()); return ledger.isPosted(operation.getId());
} }
public Operation getById(Long operationId) {
return requiredOperation(operationId);
}
private Operation requiredOperation(Long operationId) { private Operation requiredOperation(Long operationId) {
if (operationId == null) { if (operationId == null) {
@@ -53,9 +57,7 @@ public class OperationService {
Operation operation = journal.getOperationById(operationId); Operation operation = journal.getOperationById(operationId);
if (operation == null) { if (operation == null) {
throw new IllegalArgumentException( throw new OperationNotFoundException(operationId);
"Операция не найдена: " + operationId
);
} }
return operation; 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))); operationService.post(Operation.openingBalance(date, 101L, card, new BigDecimal(15000)));
// When / Then // When / Then
assertThrows(IllegalArgumentException.class, () -> operationService.post(999L)); assertThrows(OperationNotFoundException.class, () -> operationService.post(999L));
} }
@Test @Test
@@ -162,6 +162,6 @@ class OperationServiceTest {
operationService.post(Operation.openingBalance(date, 101L, card, new BigDecimal(15000))); operationService.post(Operation.openingBalance(date, 101L, card, new BigDecimal(15000)));
// When / Then // When / Then
assertThrows(IllegalArgumentException.class, () -> operationService.unpost(999L)); assertThrows(OperationNotFoundException.class, () -> operationService.unpost(999L));
} }
} }