From 53786bb22eaf54d9755dad8c26aea6ece2d3ab74 Mon Sep 17 00:00:00 2001 From: riderkaman Date: Tue, 19 Nov 2024 13:26:37 +0900 Subject: [PATCH 1/7] =?UTF-8?q?1.=20=EC=B0=A8=EB=9F=89=20=EB=8B=A8?= =?UTF-8?q?=EC=9D=BC=EC=A1=B0=ED=9A=8C=202.=20=EA=B3=84=EC=A2=8C=EB=B2=88?= =?UTF-8?q?=ED=98=B8=20=EC=B6=94=EA=B0=80=203.=20=EA=B3=84=EC=95=BD?= =?UTF-8?q?=EC=83=81=ED=83=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/TruckerApiController.java | 23 ++++++++++--- .../moduletrucker/truck/domain/Account.java | 29 +++++++++++++++++ .../moduletrucker/truck/domain/Contract.java | 3 ++ .../moduletrucker/truck/dto/TruckerDTO.java | 31 ++++++++++++++++++ .../truck/repository/AccountRepository.java | 7 ++++ .../truck/service/TruckerService.java | 32 +++++++++++++++++++ 6 files changed, 121 insertions(+), 4 deletions(-) create mode 100644 module-trucker/src/main/java/com/flab/moduletrucker/truck/domain/Account.java create mode 100644 module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/AccountRepository.java diff --git a/module-trucker/src/main/java/com/flab/moduletrucker/truck/controller/TruckerApiController.java b/module-trucker/src/main/java/com/flab/moduletrucker/truck/controller/TruckerApiController.java index a913e00..b67e1a0 100644 --- a/module-trucker/src/main/java/com/flab/moduletrucker/truck/controller/TruckerApiController.java +++ b/module-trucker/src/main/java/com/flab/moduletrucker/truck/controller/TruckerApiController.java @@ -5,10 +5,7 @@ import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; @RestController @RequiredArgsConstructor @@ -23,10 +20,28 @@ public ResponseEntity createCar(@RequestBody TruckerDTO.CarRequest req) return ResponseEntity.status(HttpStatus.OK).body(carId); } + @GetMapping("/car/{carId}") + public ResponseEntity getCar(@PathVariable String carId) { + TruckerDTO.CarInfo car = truckerService.getCar(carId); + return ResponseEntity.status(HttpStatus.OK).body(car); + } + @PostMapping("/contract") public ResponseEntity createContract(@RequestBody TruckerDTO.ContractRequest req) { String contractId = truckerService.createContract(req); return ResponseEntity.status(HttpStatus.OK).body(contractId); } + @PostMapping("/account") + public ResponseEntity createAccount(@RequestBody TruckerDTO.AccountRequest req) { + String accountId = truckerService.createAccount(req); + return ResponseEntity.status(HttpStatus.OK).body(accountId); + } + + @PutMapping("/contract/{contractId}/status/{status}") + public ResponseEntity updateContractStatus(@PathVariable String contractId, @PathVariable String status) { + String resultStatus = truckerService.updateContractStatus(contractId, status); + return ResponseEntity.status(HttpStatus.OK).body(resultStatus); + } + } diff --git a/module-trucker/src/main/java/com/flab/moduletrucker/truck/domain/Account.java b/module-trucker/src/main/java/com/flab/moduletrucker/truck/domain/Account.java new file mode 100644 index 0000000..29c3b6c --- /dev/null +++ b/module-trucker/src/main/java/com/flab/moduletrucker/truck/domain/Account.java @@ -0,0 +1,29 @@ +package com.flab.moduletrucker.truck.domain; + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@Entity(name = "ACCOUNT") +@NoArgsConstructor +@Builder +@AllArgsConstructor +public class Account { + + @Id + @GeneratedValue(strategy = GenerationType.UUID) + private String accountId; + + private String accountNo; + + private String accountName; + + private String accountBank; + +} diff --git a/module-trucker/src/main/java/com/flab/moduletrucker/truck/domain/Contract.java b/module-trucker/src/main/java/com/flab/moduletrucker/truck/domain/Contract.java index 7bd9358..5104635 100644 --- a/module-trucker/src/main/java/com/flab/moduletrucker/truck/domain/Contract.java +++ b/module-trucker/src/main/java/com/flab/moduletrucker/truck/domain/Contract.java @@ -28,4 +28,7 @@ public class Contract { private String contractStatus; + public void changeStatus(String status) { + this.contractStatus = status; + } } diff --git a/module-trucker/src/main/java/com/flab/moduletrucker/truck/dto/TruckerDTO.java b/module-trucker/src/main/java/com/flab/moduletrucker/truck/dto/TruckerDTO.java index c9c5585..ec8791f 100644 --- a/module-trucker/src/main/java/com/flab/moduletrucker/truck/dto/TruckerDTO.java +++ b/module-trucker/src/main/java/com/flab/moduletrucker/truck/dto/TruckerDTO.java @@ -1,5 +1,6 @@ package com.flab.moduletrucker.truck.dto; +import com.flab.moduletrucker.truck.domain.Account; import com.flab.moduletrucker.truck.domain.Car; import com.flab.moduletrucker.truck.domain.Contract; import lombok.Data; @@ -21,6 +22,20 @@ public Car dtoToDomain(TruckerDTO.CarRequest req) { } } + @Data + @NoArgsConstructor + public static class CarInfo { + private String carId; + private String memberId; + private String carType; + + public CarInfo(Car car) { + this.carId = car.getCarId(); + this.memberId = car.getMemberId(); + this.carType = car.getCarType(); + } + } + @Data @NoArgsConstructor public static class ContractRequest { @@ -39,4 +54,20 @@ public Contract dtoToDomain(TruckerDTO.ContractRequest req) { } } + @Data + @NoArgsConstructor + public static class AccountRequest { + private String accountNo; + private String accountName; + private String accountBank; + + public Account dtoToDomain(TruckerDTO.AccountRequest req) { + return Account.builder() + .accountNo(req.getAccountNo()) + .accountName(req.getAccountName()) + .accountBank(req.getAccountBank()) + .build(); + } + } + } diff --git a/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/AccountRepository.java b/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/AccountRepository.java new file mode 100644 index 0000000..888e67c --- /dev/null +++ b/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/AccountRepository.java @@ -0,0 +1,7 @@ +package com.flab.moduletrucker.truck.repository; + +import com.flab.moduletrucker.truck.domain.Account; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface AccountRepository extends JpaRepository { +} diff --git a/module-trucker/src/main/java/com/flab/moduletrucker/truck/service/TruckerService.java b/module-trucker/src/main/java/com/flab/moduletrucker/truck/service/TruckerService.java index a5020b3..483b312 100644 --- a/module-trucker/src/main/java/com/flab/moduletrucker/truck/service/TruckerService.java +++ b/module-trucker/src/main/java/com/flab/moduletrucker/truck/service/TruckerService.java @@ -1,10 +1,12 @@ package com.flab.moduletrucker.truck.service; import com.flab.moduletrucker.shipment.dto.ShipmentDTO; +import com.flab.moduletrucker.truck.domain.Account; import com.flab.moduletrucker.truck.domain.Car; import com.flab.moduletrucker.truck.domain.Contract; import com.flab.moduletrucker.truck.dto.TruckerDTO; import com.flab.moduletrucker.truck.feign.ShipperClient; +import com.flab.moduletrucker.truck.repository.AccountRepository; import com.flab.moduletrucker.truck.repository.CarRepository; import com.flab.moduletrucker.truck.repository.ContractRepository; import lombok.RequiredArgsConstructor; @@ -12,6 +14,8 @@ import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; +import java.util.Optional; + @Slf4j @Service @RequiredArgsConstructor @@ -19,6 +23,7 @@ public class TruckerService { private final CarRepository carRepository; private final ContractRepository contractRepository; + private final AccountRepository accountRepository; private final ShipperClient shipperClient; public String createCar(TruckerDTO.CarRequest req) { @@ -41,4 +46,31 @@ public String createContract(TruckerDTO.ContractRequest req) { throw new RuntimeException("shipment status is not ready"); } } + + public String createAccount(TruckerDTO.AccountRequest req) { + Account account = req.dtoToDomain(req); + Account save = accountRepository.save(account); + return save.getAccountId(); + } + + public TruckerDTO.CarInfo getCar(String carId) { + Optional byId = carRepository.findById(carId); + if (byId.isPresent()) { + Car car = byId.get(); + return new TruckerDTO.CarInfo(car); + } else { + throw new RuntimeException("car not found"); + } + } + + public String updateContractStatus(String contractId, String status) { + Optional byId = contractRepository.findById(contractId); + if (byId.isPresent()) { + Contract contract = byId.get(); + contract.changeStatus(status); + return status; + } else { + throw new RuntimeException("contract not found"); + } + } } From aa08bf38d3c96c3406749bb246d1ea5992e79120 Mon Sep 17 00:00:00 2001 From: riderkaman Date: Tue, 19 Nov 2024 19:51:47 +0900 Subject: [PATCH 2/7] =?UTF-8?q?=EB=8B=A8=EC=9C=84=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/AccountJpaRepository.java | 7 +++ .../truck/repository/AccountRepository.java | 4 +- .../repository/AccountRepositoryImpl.java | 17 +++++++ .../truck/repository/CarJpaRepository.java | 7 +++ .../truck/repository/CarRepository.java | 9 +++- .../truck/repository/CarRepositoryImpl.java | 24 ++++++++++ .../repository/ContractJpaRepository.java | 9 ++++ .../truck/repository/ContractRepository.java | 6 ++- .../repository/ContractRepositoryImpl.java | 24 ++++++++++ .../truck/service/TruckerService.java | 2 + .../truck/mock/FakeAccountRepository.java | 18 ++++++++ .../truck/mock/FakeCarRepository.java | 19 ++++++++ .../truck/mock/FakeContractRepository.java | 22 +++++++++ .../truck/mock/FakeShipperClient.java | 30 ++++++++++++ .../truck/service/TruckerServiceTest.java | 46 +++++++++++++++++++ 15 files changed, 238 insertions(+), 6 deletions(-) create mode 100644 module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/AccountJpaRepository.java create mode 100644 module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/AccountRepositoryImpl.java create mode 100644 module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/CarJpaRepository.java create mode 100644 module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/CarRepositoryImpl.java create mode 100644 module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/ContractJpaRepository.java create mode 100644 module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/ContractRepositoryImpl.java create mode 100644 module-trucker/src/test/java/com/flab/moduletrucker/truck/mock/FakeAccountRepository.java create mode 100644 module-trucker/src/test/java/com/flab/moduletrucker/truck/mock/FakeCarRepository.java create mode 100644 module-trucker/src/test/java/com/flab/moduletrucker/truck/mock/FakeContractRepository.java create mode 100644 module-trucker/src/test/java/com/flab/moduletrucker/truck/mock/FakeShipperClient.java create mode 100644 module-trucker/src/test/java/com/flab/moduletrucker/truck/service/TruckerServiceTest.java diff --git a/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/AccountJpaRepository.java b/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/AccountJpaRepository.java new file mode 100644 index 0000000..e8e3751 --- /dev/null +++ b/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/AccountJpaRepository.java @@ -0,0 +1,7 @@ +package com.flab.moduletrucker.truck.repository; + +import com.flab.moduletrucker.truck.domain.Account; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface AccountJpaRepository extends JpaRepository { +} diff --git a/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/AccountRepository.java b/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/AccountRepository.java index 888e67c..53b6ef4 100644 --- a/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/AccountRepository.java +++ b/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/AccountRepository.java @@ -1,7 +1,7 @@ package com.flab.moduletrucker.truck.repository; import com.flab.moduletrucker.truck.domain.Account; -import org.springframework.data.jpa.repository.JpaRepository; -public interface AccountRepository extends JpaRepository { +public interface AccountRepository { + Account save(Account account); } diff --git a/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/AccountRepositoryImpl.java b/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/AccountRepositoryImpl.java new file mode 100644 index 0000000..16aae77 --- /dev/null +++ b/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/AccountRepositoryImpl.java @@ -0,0 +1,17 @@ +package com.flab.moduletrucker.truck.repository; + +import com.flab.moduletrucker.truck.domain.Account; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Repository; + +@Repository +@RequiredArgsConstructor +public class AccountRepositoryImpl implements AccountRepository { + + private final AccountJpaRepository accountJpaRepository; + + @Override + public Account save(Account account) { + return accountJpaRepository.save(account); + } +} diff --git a/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/CarJpaRepository.java b/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/CarJpaRepository.java new file mode 100644 index 0000000..f0f121e --- /dev/null +++ b/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/CarJpaRepository.java @@ -0,0 +1,7 @@ +package com.flab.moduletrucker.truck.repository; + +import com.flab.moduletrucker.truck.domain.Car; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface CarJpaRepository extends JpaRepository { +} diff --git a/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/CarRepository.java b/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/CarRepository.java index 24d4400..a78d6ff 100644 --- a/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/CarRepository.java +++ b/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/CarRepository.java @@ -1,7 +1,12 @@ package com.flab.moduletrucker.truck.repository; import com.flab.moduletrucker.truck.domain.Car; -import org.springframework.data.jpa.repository.JpaRepository; -public interface CarRepository extends JpaRepository { +import java.util.Optional; + +public interface CarRepository { + + Car save(Car car); + + Optional findById(String carId); } diff --git a/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/CarRepositoryImpl.java b/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/CarRepositoryImpl.java new file mode 100644 index 0000000..c21e4ee --- /dev/null +++ b/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/CarRepositoryImpl.java @@ -0,0 +1,24 @@ +package com.flab.moduletrucker.truck.repository; + +import com.flab.moduletrucker.truck.domain.Car; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Repository; + +import java.util.Optional; + +@Repository +@RequiredArgsConstructor +public class CarRepositoryImpl implements CarRepository { + + private final CarJpaRepository carJpaRepository; + + @Override + public Car save(Car car) { + return carJpaRepository.save(car); + } + + @Override + public Optional findById(String carId) { + return carJpaRepository.findById(carId); + } +} diff --git a/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/ContractJpaRepository.java b/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/ContractJpaRepository.java new file mode 100644 index 0000000..92d8fb1 --- /dev/null +++ b/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/ContractJpaRepository.java @@ -0,0 +1,9 @@ +package com.flab.moduletrucker.truck.repository; + +import com.flab.moduletrucker.truck.domain.Contract; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface ContractJpaRepository extends JpaRepository { + + +} diff --git a/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/ContractRepository.java b/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/ContractRepository.java index 3160f45..a76482c 100644 --- a/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/ContractRepository.java +++ b/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/ContractRepository.java @@ -1,9 +1,11 @@ package com.flab.moduletrucker.truck.repository; import com.flab.moduletrucker.truck.domain.Contract; -import org.springframework.data.jpa.repository.JpaRepository; -public interface ContractRepository extends JpaRepository { +import java.util.Optional; +public interface ContractRepository { + Contract save(Contract contract); + Optional findById(String contractId); } diff --git a/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/ContractRepositoryImpl.java b/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/ContractRepositoryImpl.java new file mode 100644 index 0000000..4573c34 --- /dev/null +++ b/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/ContractRepositoryImpl.java @@ -0,0 +1,24 @@ +package com.flab.moduletrucker.truck.repository; + +import com.flab.moduletrucker.truck.domain.Contract; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Repository; + +import java.util.Optional; + +@Repository +@RequiredArgsConstructor +public class ContractRepositoryImpl implements ContractRepository { + + private final ContractJpaRepository contractJpaRepository; + + @Override + public Contract save(Contract contract) { + return contractJpaRepository.save(contract); + } + + @Override + public Optional findById(String contractId) { + return contractJpaRepository.findById(contractId); + } +} diff --git a/module-trucker/src/main/java/com/flab/moduletrucker/truck/service/TruckerService.java b/module-trucker/src/main/java/com/flab/moduletrucker/truck/service/TruckerService.java index 483b312..55ed057 100644 --- a/module-trucker/src/main/java/com/flab/moduletrucker/truck/service/TruckerService.java +++ b/module-trucker/src/main/java/com/flab/moduletrucker/truck/service/TruckerService.java @@ -9,6 +9,7 @@ import com.flab.moduletrucker.truck.repository.AccountRepository; import com.flab.moduletrucker.truck.repository.CarRepository; import com.flab.moduletrucker.truck.repository.ContractRepository; +import lombok.Builder; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.ResponseEntity; @@ -19,6 +20,7 @@ @Slf4j @Service @RequiredArgsConstructor +@Builder public class TruckerService { private final CarRepository carRepository; diff --git a/module-trucker/src/test/java/com/flab/moduletrucker/truck/mock/FakeAccountRepository.java b/module-trucker/src/test/java/com/flab/moduletrucker/truck/mock/FakeAccountRepository.java new file mode 100644 index 0000000..1d5f9a6 --- /dev/null +++ b/module-trucker/src/test/java/com/flab/moduletrucker/truck/mock/FakeAccountRepository.java @@ -0,0 +1,18 @@ +package com.flab.moduletrucker.truck.mock; + +import com.flab.moduletrucker.truck.domain.Account; +import com.flab.moduletrucker.truck.repository.AccountRepository; + +public class FakeAccountRepository implements AccountRepository { + + + @Override + public Account save(Account account) { + return Account.builder() + .accountId("testAccountId") + .accountNo(account.getAccountNo()) + .accountName(account.getAccountName()) + .accountBank(account.getAccountBank()) + .build(); + } +} diff --git a/module-trucker/src/test/java/com/flab/moduletrucker/truck/mock/FakeCarRepository.java b/module-trucker/src/test/java/com/flab/moduletrucker/truck/mock/FakeCarRepository.java new file mode 100644 index 0000000..b8db95f --- /dev/null +++ b/module-trucker/src/test/java/com/flab/moduletrucker/truck/mock/FakeCarRepository.java @@ -0,0 +1,19 @@ +package com.flab.moduletrucker.truck.mock; + +import com.flab.moduletrucker.truck.domain.Car; +import com.flab.moduletrucker.truck.repository.CarRepository; + +import java.util.Optional; + +public class FakeCarRepository implements CarRepository { + + @Override + public Car save(Car car) { + return null; + } + + @Override + public Optional findById(String carId) { + return Optional.empty(); + } +} diff --git a/module-trucker/src/test/java/com/flab/moduletrucker/truck/mock/FakeContractRepository.java b/module-trucker/src/test/java/com/flab/moduletrucker/truck/mock/FakeContractRepository.java new file mode 100644 index 0000000..ad719d0 --- /dev/null +++ b/module-trucker/src/test/java/com/flab/moduletrucker/truck/mock/FakeContractRepository.java @@ -0,0 +1,22 @@ +package com.flab.moduletrucker.truck.mock; + +import com.flab.moduletrucker.truck.domain.Contract; +import com.flab.moduletrucker.truck.repository.ContractRepository; + +import java.util.Optional; + +public class FakeContractRepository implements ContractRepository { + + @Override + public Contract save(Contract contract) { + return Contract.builder() + .contractId("testContractId") + .contractStatus("concluded") + .build(); + } + + @Override + public Optional findById(String contractId) { + return Optional.empty(); + } +} diff --git a/module-trucker/src/test/java/com/flab/moduletrucker/truck/mock/FakeShipperClient.java b/module-trucker/src/test/java/com/flab/moduletrucker/truck/mock/FakeShipperClient.java new file mode 100644 index 0000000..1242d0c --- /dev/null +++ b/module-trucker/src/test/java/com/flab/moduletrucker/truck/mock/FakeShipperClient.java @@ -0,0 +1,30 @@ +package com.flab.moduletrucker.truck.mock; + +import com.flab.moduletrucker.shipment.dto.ShipmentDTO; +import com.flab.moduletrucker.truck.feign.ShipperClient; +import org.springframework.http.ResponseEntity; + +public class FakeShipperClient implements ShipperClient { + + @Override + public ResponseEntity get(String shipmentId) { + ShipmentDTO.BasicInfo basicInfo = new ShipmentDTO.BasicInfo(); + switch (shipmentId) { + case "ready": + basicInfo.setShipmentId("ready"); + basicInfo.setShipmentStatus("ready"); + break; + case "other": + basicInfo.setShipmentId("other"); + basicInfo.setShipmentStatus("other"); + break; + } + + return ResponseEntity.ok(basicInfo); + } + + @Override + public void shipmentStatus(String shipmentId, String status) { + + } +} diff --git a/module-trucker/src/test/java/com/flab/moduletrucker/truck/service/TruckerServiceTest.java b/module-trucker/src/test/java/com/flab/moduletrucker/truck/service/TruckerServiceTest.java new file mode 100644 index 0000000..25721d0 --- /dev/null +++ b/module-trucker/src/test/java/com/flab/moduletrucker/truck/service/TruckerServiceTest.java @@ -0,0 +1,46 @@ +package com.flab.moduletrucker.truck.service; + +import com.flab.moduletrucker.shipment.dto.ShipmentDTO; +import com.flab.moduletrucker.truck.dto.TruckerDTO; +import com.flab.moduletrucker.truck.mock.FakeAccountRepository; +import com.flab.moduletrucker.truck.mock.FakeCarRepository; +import com.flab.moduletrucker.truck.mock.FakeContractRepository; +import com.flab.moduletrucker.truck.mock.FakeShipperClient; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class TruckerServiceTest { + + private TruckerService truckerService; + + @BeforeEach + void setUp() throws Exception { + this.truckerService = TruckerService.builder() + .accountRepository(new FakeAccountRepository()) + .carRepository(new FakeCarRepository()) + .contractRepository(new FakeContractRepository()) + .shipperClient(new FakeShipperClient()) + .build(); + } + + @Test + void createContract_성공하는_경우() { + TruckerDTO.ContractRequest req = new TruckerDTO.ContractRequest(); + req.setShipmentId("ready"); + String contractId = truckerService.createContract(req); + Assertions.assertNotNull(contractId); + } + + @Test + void createContract_shipment_ready가_아닌경우() { + TruckerDTO.ContractRequest req = new TruckerDTO.ContractRequest(); + req.setShipmentId("other"); + Assertions.assertThrows(RuntimeException.class, () -> { + truckerService.createContract(req); + }); + } + +} \ No newline at end of file From 536e792388861e7ac6ae1886fff8c1ebcb846b57 Mon Sep 17 00:00:00 2001 From: riderkaman Date: Sun, 24 Nov 2024 14:31:23 +0900 Subject: [PATCH 3/7] =?UTF-8?q?1.=20service=20=EB=A5=BC=20domain=20?= =?UTF-8?q?=EB=B3=84=EB=A1=9C=20=EA=B5=AC=EB=B6=84=ED=95=A8=202.=20test=20?= =?UTF-8?q?=EB=8A=94=20Mock=20=EC=82=AC=EC=9A=A9=ED=95=98=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EC=88=98=EC=A0=95=203.=20repository=20=EB=8A=94=20?= =?UTF-8?q?interface=20=EB=A7=8C=20=EC=82=AC=EC=9A=A9=ED=95=98=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ModuleTruckerApplication.java | 3 + .../controller/TruckerApiController.java | 24 +++++-- .../moduletrucker/truck/dto/TruckerDTO.java | 18 +++++ .../repository/AccountJpaRepository.java | 7 -- .../truck/repository/AccountRepository.java | 4 +- .../repository/AccountRepositoryImpl.java | 17 ----- .../truck/repository/CarJpaRepository.java | 7 -- .../truck/repository/CarRepository.java | 9 +-- .../truck/repository/CarRepositoryImpl.java | 24 ------- .../repository/ContractJpaRepository.java | 9 --- .../truck/repository/ContractRepository.java | 6 +- .../repository/ContractRepositoryImpl.java | 24 ------- .../truck/service/AccountService.java | 22 +++++++ .../truck/service/CarService.java | 34 ++++++++++ ...uckerService.java => ContractService.java} | 39 +++-------- .../src/main/resources/application.properties | 8 ++- .../truck/mock/FakeAccountRepository.java | 18 ----- .../truck/mock/FakeCarRepository.java | 19 ------ .../truck/mock/FakeContractRepository.java | 22 ------- .../truck/service/TruckerServiceTest.java | 65 ++++++++++++++----- 20 files changed, 165 insertions(+), 214 deletions(-) delete mode 100644 module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/AccountJpaRepository.java delete mode 100644 module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/AccountRepositoryImpl.java delete mode 100644 module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/CarJpaRepository.java delete mode 100644 module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/CarRepositoryImpl.java delete mode 100644 module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/ContractJpaRepository.java delete mode 100644 module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/ContractRepositoryImpl.java create mode 100644 module-trucker/src/main/java/com/flab/moduletrucker/truck/service/AccountService.java create mode 100644 module-trucker/src/main/java/com/flab/moduletrucker/truck/service/CarService.java rename module-trucker/src/main/java/com/flab/moduletrucker/truck/service/{TruckerService.java => ContractService.java} (63%) delete mode 100644 module-trucker/src/test/java/com/flab/moduletrucker/truck/mock/FakeAccountRepository.java delete mode 100644 module-trucker/src/test/java/com/flab/moduletrucker/truck/mock/FakeCarRepository.java delete mode 100644 module-trucker/src/test/java/com/flab/moduletrucker/truck/mock/FakeContractRepository.java diff --git a/module-trucker/src/main/java/com/flab/moduletrucker/ModuleTruckerApplication.java b/module-trucker/src/main/java/com/flab/moduletrucker/ModuleTruckerApplication.java index 47112b2..c2db27e 100644 --- a/module-trucker/src/main/java/com/flab/moduletrucker/ModuleTruckerApplication.java +++ b/module-trucker/src/main/java/com/flab/moduletrucker/ModuleTruckerApplication.java @@ -1,11 +1,14 @@ package com.flab.moduletrucker; import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.openfeign.EnableFeignClients; +import org.springframework.cloud.openfeign.FeignAutoConfiguration; @EnableFeignClients @SpringBootApplication +@ImportAutoConfiguration({FeignAutoConfiguration.class}) public class ModuleTruckerApplication { public static void main(String[] args) { diff --git a/module-trucker/src/main/java/com/flab/moduletrucker/truck/controller/TruckerApiController.java b/module-trucker/src/main/java/com/flab/moduletrucker/truck/controller/TruckerApiController.java index b67e1a0..825afe8 100644 --- a/module-trucker/src/main/java/com/flab/moduletrucker/truck/controller/TruckerApiController.java +++ b/module-trucker/src/main/java/com/flab/moduletrucker/truck/controller/TruckerApiController.java @@ -1,7 +1,9 @@ package com.flab.moduletrucker.truck.controller; import com.flab.moduletrucker.truck.dto.TruckerDTO; -import com.flab.moduletrucker.truck.service.TruckerService; +import com.flab.moduletrucker.truck.service.AccountService; +import com.flab.moduletrucker.truck.service.CarService; +import com.flab.moduletrucker.truck.service.ContractService; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -12,36 +14,44 @@ @RequestMapping("/api/v1/trucker") public class TruckerApiController { - private final TruckerService truckerService; + private final AccountService accountService; + private final CarService carService; + private final ContractService contractService; @PostMapping("/car") public ResponseEntity createCar(@RequestBody TruckerDTO.CarRequest req) { - String carId = truckerService.createCar(req); + String carId = carService.createCar(req); return ResponseEntity.status(HttpStatus.OK).body(carId); } @GetMapping("/car/{carId}") public ResponseEntity getCar(@PathVariable String carId) { - TruckerDTO.CarInfo car = truckerService.getCar(carId); + TruckerDTO.CarInfo car = carService.getCar(carId); return ResponseEntity.status(HttpStatus.OK).body(car); } @PostMapping("/contract") public ResponseEntity createContract(@RequestBody TruckerDTO.ContractRequest req) { - String contractId = truckerService.createContract(req); + String contractId = contractService.createContract(req); return ResponseEntity.status(HttpStatus.OK).body(contractId); } @PostMapping("/account") public ResponseEntity createAccount(@RequestBody TruckerDTO.AccountRequest req) { - String accountId = truckerService.createAccount(req); + String accountId = accountService.createAccount(req); return ResponseEntity.status(HttpStatus.OK).body(accountId); } @PutMapping("/contract/{contractId}/status/{status}") public ResponseEntity updateContractStatus(@PathVariable String contractId, @PathVariable String status) { - String resultStatus = truckerService.updateContractStatus(contractId, status); + String resultStatus = contractService.updateContractStatus(contractId, status); return ResponseEntity.status(HttpStatus.OK).body(resultStatus); } + @GetMapping("/contract/{contractId}") + public ResponseEntity getContract(@PathVariable String contractId) { + TruckerDTO.ContractResponse contractResponse = contractService.getContract(contractId); + return ResponseEntity.status(HttpStatus.OK).body(contractResponse); + } + } diff --git a/module-trucker/src/main/java/com/flab/moduletrucker/truck/dto/TruckerDTO.java b/module-trucker/src/main/java/com/flab/moduletrucker/truck/dto/TruckerDTO.java index ec8791f..4290292 100644 --- a/module-trucker/src/main/java/com/flab/moduletrucker/truck/dto/TruckerDTO.java +++ b/module-trucker/src/main/java/com/flab/moduletrucker/truck/dto/TruckerDTO.java @@ -70,4 +70,22 @@ public Account dtoToDomain(TruckerDTO.AccountRequest req) { } } + @Data + @NoArgsConstructor + public static class ContractResponse { + private String contractId; + private String truckerMemberId; + private String shipmentId; + private String carId; + private String contractStatus; + + public ContractResponse(Contract contract) { + this.contractId = contract.getContractId(); + this.truckerMemberId = contract.getTruckerMemberId(); + this.shipmentId = contract.getShipmentId(); + this.carId = contract.getCarId(); + this.contractStatus = contract.getContractStatus(); + } + } + } diff --git a/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/AccountJpaRepository.java b/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/AccountJpaRepository.java deleted file mode 100644 index e8e3751..0000000 --- a/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/AccountJpaRepository.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.flab.moduletrucker.truck.repository; - -import com.flab.moduletrucker.truck.domain.Account; -import org.springframework.data.jpa.repository.JpaRepository; - -public interface AccountJpaRepository extends JpaRepository { -} diff --git a/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/AccountRepository.java b/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/AccountRepository.java index 53b6ef4..888e67c 100644 --- a/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/AccountRepository.java +++ b/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/AccountRepository.java @@ -1,7 +1,7 @@ package com.flab.moduletrucker.truck.repository; import com.flab.moduletrucker.truck.domain.Account; +import org.springframework.data.jpa.repository.JpaRepository; -public interface AccountRepository { - Account save(Account account); +public interface AccountRepository extends JpaRepository { } diff --git a/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/AccountRepositoryImpl.java b/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/AccountRepositoryImpl.java deleted file mode 100644 index 16aae77..0000000 --- a/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/AccountRepositoryImpl.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.flab.moduletrucker.truck.repository; - -import com.flab.moduletrucker.truck.domain.Account; -import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Repository; - -@Repository -@RequiredArgsConstructor -public class AccountRepositoryImpl implements AccountRepository { - - private final AccountJpaRepository accountJpaRepository; - - @Override - public Account save(Account account) { - return accountJpaRepository.save(account); - } -} diff --git a/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/CarJpaRepository.java b/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/CarJpaRepository.java deleted file mode 100644 index f0f121e..0000000 --- a/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/CarJpaRepository.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.flab.moduletrucker.truck.repository; - -import com.flab.moduletrucker.truck.domain.Car; -import org.springframework.data.jpa.repository.JpaRepository; - -public interface CarJpaRepository extends JpaRepository { -} diff --git a/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/CarRepository.java b/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/CarRepository.java index a78d6ff..24d4400 100644 --- a/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/CarRepository.java +++ b/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/CarRepository.java @@ -1,12 +1,7 @@ package com.flab.moduletrucker.truck.repository; import com.flab.moduletrucker.truck.domain.Car; +import org.springframework.data.jpa.repository.JpaRepository; -import java.util.Optional; - -public interface CarRepository { - - Car save(Car car); - - Optional findById(String carId); +public interface CarRepository extends JpaRepository { } diff --git a/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/CarRepositoryImpl.java b/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/CarRepositoryImpl.java deleted file mode 100644 index c21e4ee..0000000 --- a/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/CarRepositoryImpl.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.flab.moduletrucker.truck.repository; - -import com.flab.moduletrucker.truck.domain.Car; -import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Repository; - -import java.util.Optional; - -@Repository -@RequiredArgsConstructor -public class CarRepositoryImpl implements CarRepository { - - private final CarJpaRepository carJpaRepository; - - @Override - public Car save(Car car) { - return carJpaRepository.save(car); - } - - @Override - public Optional findById(String carId) { - return carJpaRepository.findById(carId); - } -} diff --git a/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/ContractJpaRepository.java b/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/ContractJpaRepository.java deleted file mode 100644 index 92d8fb1..0000000 --- a/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/ContractJpaRepository.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.flab.moduletrucker.truck.repository; - -import com.flab.moduletrucker.truck.domain.Contract; -import org.springframework.data.jpa.repository.JpaRepository; - -public interface ContractJpaRepository extends JpaRepository { - - -} diff --git a/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/ContractRepository.java b/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/ContractRepository.java index a76482c..3160f45 100644 --- a/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/ContractRepository.java +++ b/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/ContractRepository.java @@ -1,11 +1,9 @@ package com.flab.moduletrucker.truck.repository; import com.flab.moduletrucker.truck.domain.Contract; +import org.springframework.data.jpa.repository.JpaRepository; -import java.util.Optional; +public interface ContractRepository extends JpaRepository { -public interface ContractRepository { - Contract save(Contract contract); - Optional findById(String contractId); } diff --git a/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/ContractRepositoryImpl.java b/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/ContractRepositoryImpl.java deleted file mode 100644 index 4573c34..0000000 --- a/module-trucker/src/main/java/com/flab/moduletrucker/truck/repository/ContractRepositoryImpl.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.flab.moduletrucker.truck.repository; - -import com.flab.moduletrucker.truck.domain.Contract; -import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Repository; - -import java.util.Optional; - -@Repository -@RequiredArgsConstructor -public class ContractRepositoryImpl implements ContractRepository { - - private final ContractJpaRepository contractJpaRepository; - - @Override - public Contract save(Contract contract) { - return contractJpaRepository.save(contract); - } - - @Override - public Optional findById(String contractId) { - return contractJpaRepository.findById(contractId); - } -} diff --git a/module-trucker/src/main/java/com/flab/moduletrucker/truck/service/AccountService.java b/module-trucker/src/main/java/com/flab/moduletrucker/truck/service/AccountService.java new file mode 100644 index 0000000..136284c --- /dev/null +++ b/module-trucker/src/main/java/com/flab/moduletrucker/truck/service/AccountService.java @@ -0,0 +1,22 @@ +package com.flab.moduletrucker.truck.service; + +import com.flab.moduletrucker.truck.domain.Account; +import com.flab.moduletrucker.truck.dto.TruckerDTO; +import com.flab.moduletrucker.truck.repository.AccountRepository; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +@Slf4j +@Service +@RequiredArgsConstructor +public class AccountService { + + private final AccountRepository accountRepository; + + public String createAccount(TruckerDTO.AccountRequest req) { + Account account = req.dtoToDomain(req); + Account save = accountRepository.save(account); + return save.getAccountId(); + } +} diff --git a/module-trucker/src/main/java/com/flab/moduletrucker/truck/service/CarService.java b/module-trucker/src/main/java/com/flab/moduletrucker/truck/service/CarService.java new file mode 100644 index 0000000..475c551 --- /dev/null +++ b/module-trucker/src/main/java/com/flab/moduletrucker/truck/service/CarService.java @@ -0,0 +1,34 @@ +package com.flab.moduletrucker.truck.service; + +import com.flab.moduletrucker.truck.domain.Car; +import com.flab.moduletrucker.truck.dto.TruckerDTO; +import com.flab.moduletrucker.truck.repository.CarRepository; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +import java.util.Optional; + +@Slf4j +@Service +@RequiredArgsConstructor +public class CarService { + + private final CarRepository carRepository; + + public String createCar(TruckerDTO.CarRequest req) { + Car car = req.dtoToDomain(req); + Car save = carRepository.save(car); + return save.getCarId(); + } + + public TruckerDTO.CarInfo getCar(String carId) { + Optional byId = carRepository.findById(carId); + if (byId.isPresent()) { + Car car = byId.get(); + return new TruckerDTO.CarInfo(car); + } else { + throw new RuntimeException("car not found"); + } + } +} diff --git a/module-trucker/src/main/java/com/flab/moduletrucker/truck/service/TruckerService.java b/module-trucker/src/main/java/com/flab/moduletrucker/truck/service/ContractService.java similarity index 63% rename from module-trucker/src/main/java/com/flab/moduletrucker/truck/service/TruckerService.java rename to module-trucker/src/main/java/com/flab/moduletrucker/truck/service/ContractService.java index 55ed057..0af1f24 100644 --- a/module-trucker/src/main/java/com/flab/moduletrucker/truck/service/TruckerService.java +++ b/module-trucker/src/main/java/com/flab/moduletrucker/truck/service/ContractService.java @@ -1,15 +1,10 @@ package com.flab.moduletrucker.truck.service; import com.flab.moduletrucker.shipment.dto.ShipmentDTO; -import com.flab.moduletrucker.truck.domain.Account; -import com.flab.moduletrucker.truck.domain.Car; import com.flab.moduletrucker.truck.domain.Contract; import com.flab.moduletrucker.truck.dto.TruckerDTO; import com.flab.moduletrucker.truck.feign.ShipperClient; -import com.flab.moduletrucker.truck.repository.AccountRepository; -import com.flab.moduletrucker.truck.repository.CarRepository; import com.flab.moduletrucker.truck.repository.ContractRepository; -import lombok.Builder; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.ResponseEntity; @@ -20,20 +15,11 @@ @Slf4j @Service @RequiredArgsConstructor -@Builder -public class TruckerService { +public class ContractService { - private final CarRepository carRepository; private final ContractRepository contractRepository; - private final AccountRepository accountRepository; private final ShipperClient shipperClient; - public String createCar(TruckerDTO.CarRequest req) { - Car car = req.dtoToDomain(req); - Car save = carRepository.save(car); - return save.getCarId(); - } - public String createContract(TruckerDTO.ContractRequest req) { ResponseEntity basicInfoResponseEntity = shipperClient.get(req.getShipmentId()); ShipmentDTO.BasicInfo basicInfo = basicInfoResponseEntity.getBody(); @@ -49,28 +35,21 @@ public String createContract(TruckerDTO.ContractRequest req) { } } - public String createAccount(TruckerDTO.AccountRequest req) { - Account account = req.dtoToDomain(req); - Account save = accountRepository.save(account); - return save.getAccountId(); - } - - public TruckerDTO.CarInfo getCar(String carId) { - Optional byId = carRepository.findById(carId); + public String updateContractStatus(String contractId, String status) { + Optional byId = contractRepository.findById(contractId); if (byId.isPresent()) { - Car car = byId.get(); - return new TruckerDTO.CarInfo(car); + Contract contract = byId.get(); + contract.changeStatus(status); + return status; } else { - throw new RuntimeException("car not found"); + throw new RuntimeException("contract not found"); } } - public String updateContractStatus(String contractId, String status) { + public TruckerDTO.ContractResponse getContract(String contractId) { Optional byId = contractRepository.findById(contractId); if (byId.isPresent()) { - Contract contract = byId.get(); - contract.changeStatus(status); - return status; + return new TruckerDTO.ContractResponse(byId.get()); } else { throw new RuntimeException("contract not found"); } diff --git a/module-trucker/src/main/resources/application.properties b/module-trucker/src/main/resources/application.properties index 6dd9919..5ff0f0a 100644 --- a/module-trucker/src/main/resources/application.properties +++ b/module-trucker/src/main/resources/application.properties @@ -1,2 +1,8 @@ spring.application.name=module-trucker -server.port=8002 \ No newline at end of file +server.port=8002 + +# mysql +spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver +spring.datasource.url=jdbc:mysql://localhost:3306/truck_matching?serverTimezone=UTC&characterEncoding=UTF-8 +spring.datasource.username=root +spring.datasource.password=123963 \ No newline at end of file diff --git a/module-trucker/src/test/java/com/flab/moduletrucker/truck/mock/FakeAccountRepository.java b/module-trucker/src/test/java/com/flab/moduletrucker/truck/mock/FakeAccountRepository.java deleted file mode 100644 index 1d5f9a6..0000000 --- a/module-trucker/src/test/java/com/flab/moduletrucker/truck/mock/FakeAccountRepository.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.flab.moduletrucker.truck.mock; - -import com.flab.moduletrucker.truck.domain.Account; -import com.flab.moduletrucker.truck.repository.AccountRepository; - -public class FakeAccountRepository implements AccountRepository { - - - @Override - public Account save(Account account) { - return Account.builder() - .accountId("testAccountId") - .accountNo(account.getAccountNo()) - .accountName(account.getAccountName()) - .accountBank(account.getAccountBank()) - .build(); - } -} diff --git a/module-trucker/src/test/java/com/flab/moduletrucker/truck/mock/FakeCarRepository.java b/module-trucker/src/test/java/com/flab/moduletrucker/truck/mock/FakeCarRepository.java deleted file mode 100644 index b8db95f..0000000 --- a/module-trucker/src/test/java/com/flab/moduletrucker/truck/mock/FakeCarRepository.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.flab.moduletrucker.truck.mock; - -import com.flab.moduletrucker.truck.domain.Car; -import com.flab.moduletrucker.truck.repository.CarRepository; - -import java.util.Optional; - -public class FakeCarRepository implements CarRepository { - - @Override - public Car save(Car car) { - return null; - } - - @Override - public Optional findById(String carId) { - return Optional.empty(); - } -} diff --git a/module-trucker/src/test/java/com/flab/moduletrucker/truck/mock/FakeContractRepository.java b/module-trucker/src/test/java/com/flab/moduletrucker/truck/mock/FakeContractRepository.java deleted file mode 100644 index ad719d0..0000000 --- a/module-trucker/src/test/java/com/flab/moduletrucker/truck/mock/FakeContractRepository.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.flab.moduletrucker.truck.mock; - -import com.flab.moduletrucker.truck.domain.Contract; -import com.flab.moduletrucker.truck.repository.ContractRepository; - -import java.util.Optional; - -public class FakeContractRepository implements ContractRepository { - - @Override - public Contract save(Contract contract) { - return Contract.builder() - .contractId("testContractId") - .contractStatus("concluded") - .build(); - } - - @Override - public Optional findById(String contractId) { - return Optional.empty(); - } -} diff --git a/module-trucker/src/test/java/com/flab/moduletrucker/truck/service/TruckerServiceTest.java b/module-trucker/src/test/java/com/flab/moduletrucker/truck/service/TruckerServiceTest.java index 25721d0..d530925 100644 --- a/module-trucker/src/test/java/com/flab/moduletrucker/truck/service/TruckerServiceTest.java +++ b/module-trucker/src/test/java/com/flab/moduletrucker/truck/service/TruckerServiceTest.java @@ -1,36 +1,63 @@ package com.flab.moduletrucker.truck.service; import com.flab.moduletrucker.shipment.dto.ShipmentDTO; +import com.flab.moduletrucker.truck.domain.Contract; import com.flab.moduletrucker.truck.dto.TruckerDTO; -import com.flab.moduletrucker.truck.mock.FakeAccountRepository; -import com.flab.moduletrucker.truck.mock.FakeCarRepository; -import com.flab.moduletrucker.truck.mock.FakeContractRepository; +import com.flab.moduletrucker.truck.feign.ShipperClient; import com.flab.moduletrucker.truck.mock.FakeShipperClient; +import com.flab.moduletrucker.truck.repository.AccountRepository; +import com.flab.moduletrucker.truck.repository.CarRepository; +import com.flab.moduletrucker.truck.repository.ContractRepository; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; -import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; +@ExtendWith(MockitoExtension.class) class TruckerServiceTest { - private TruckerService truckerService; + @InjectMocks + private AccountService accountService; - @BeforeEach - void setUp() throws Exception { - this.truckerService = TruckerService.builder() - .accountRepository(new FakeAccountRepository()) - .carRepository(new FakeCarRepository()) - .contractRepository(new FakeContractRepository()) - .shipperClient(new FakeShipperClient()) - .build(); - } + @InjectMocks + private CarService carService; + + @InjectMocks + private ContractService contractService; + + @Mock + private AccountRepository accountRepository; + + @Mock + private CarRepository carRepository; + + @Mock + private ContractRepository contractRepository; + + @Mock + private ShipperClient shipperClient; @Test void createContract_성공하는_경우() { TruckerDTO.ContractRequest req = new TruckerDTO.ContractRequest(); req.setShipmentId("ready"); - String contractId = truckerService.createContract(req); + ShipmentDTO.BasicInfo basicInfo = new ShipmentDTO.BasicInfo(); + basicInfo.setShipmentStatus("ready"); + ResponseEntity basicInfoResponseEntity = new ResponseEntity(basicInfo, HttpStatus.OK); + Contract contract = Contract.builder().contractId("testId").build(); + + when(shipperClient.get(req.getShipmentId())).thenReturn(basicInfoResponseEntity); + when(contractRepository.save(any(Contract.class))).thenReturn(contract); + + String contractId = contractService.createContract(req); Assertions.assertNotNull(contractId); } @@ -38,8 +65,14 @@ void setUp() throws Exception { void createContract_shipment_ready가_아닌경우() { TruckerDTO.ContractRequest req = new TruckerDTO.ContractRequest(); req.setShipmentId("other"); + ShipmentDTO.BasicInfo basicInfo = new ShipmentDTO.BasicInfo(); + basicInfo.setShipmentStatus("other"); + ResponseEntity basicInfoResponseEntity = new ResponseEntity(basicInfo, HttpStatus.OK); + + when(shipperClient.get(req.getShipmentId())).thenReturn(basicInfoResponseEntity); + Assertions.assertThrows(RuntimeException.class, () -> { - truckerService.createContract(req); + contractService.createContract(req); }); } From 072b0c3da8acd9923bd8971da79bc86999d25e1b Mon Sep 17 00:00:00 2001 From: riderkaman Date: Fri, 6 Dec 2024 02:53:19 +0900 Subject: [PATCH 4/7] =?UTF-8?q?trucker=20=EB=AA=A8=EB=93=88=20=EB=B9=84?= =?UTF-8?q?=EB=8F=99=EA=B8=B0=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../truck/service/AccountService.java | 2 ++ .../truck/service/CarService.java | 3 +++ .../truck/service/ContractService.java | 4 +++ .../service/ThreadPoolConfiguration.java | 25 +++++++++++++++++++ 4 files changed, 34 insertions(+) create mode 100644 module-trucker/src/main/java/com/flab/moduletrucker/truck/service/ThreadPoolConfiguration.java diff --git a/module-trucker/src/main/java/com/flab/moduletrucker/truck/service/AccountService.java b/module-trucker/src/main/java/com/flab/moduletrucker/truck/service/AccountService.java index 136284c..a4b4859 100644 --- a/module-trucker/src/main/java/com/flab/moduletrucker/truck/service/AccountService.java +++ b/module-trucker/src/main/java/com/flab/moduletrucker/truck/service/AccountService.java @@ -5,6 +5,7 @@ import com.flab.moduletrucker.truck.repository.AccountRepository; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; @Slf4j @@ -14,6 +15,7 @@ public class AccountService { private final AccountRepository accountRepository; + @Async(value = "taskExecutor") public String createAccount(TruckerDTO.AccountRequest req) { Account account = req.dtoToDomain(req); Account save = accountRepository.save(account); diff --git a/module-trucker/src/main/java/com/flab/moduletrucker/truck/service/CarService.java b/module-trucker/src/main/java/com/flab/moduletrucker/truck/service/CarService.java index 475c551..591ea4a 100644 --- a/module-trucker/src/main/java/com/flab/moduletrucker/truck/service/CarService.java +++ b/module-trucker/src/main/java/com/flab/moduletrucker/truck/service/CarService.java @@ -5,6 +5,7 @@ import com.flab.moduletrucker.truck.repository.CarRepository; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import java.util.Optional; @@ -16,12 +17,14 @@ public class CarService { private final CarRepository carRepository; + @Async(value = "taskExecutor") public String createCar(TruckerDTO.CarRequest req) { Car car = req.dtoToDomain(req); Car save = carRepository.save(car); return save.getCarId(); } + @Async(value = "taskExecutor") public TruckerDTO.CarInfo getCar(String carId) { Optional byId = carRepository.findById(carId); if (byId.isPresent()) { diff --git a/module-trucker/src/main/java/com/flab/moduletrucker/truck/service/ContractService.java b/module-trucker/src/main/java/com/flab/moduletrucker/truck/service/ContractService.java index 0af1f24..fc95c6a 100644 --- a/module-trucker/src/main/java/com/flab/moduletrucker/truck/service/ContractService.java +++ b/module-trucker/src/main/java/com/flab/moduletrucker/truck/service/ContractService.java @@ -8,6 +8,7 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.ResponseEntity; +import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import java.util.Optional; @@ -20,6 +21,7 @@ public class ContractService { private final ContractRepository contractRepository; private final ShipperClient shipperClient; + @Async(value = "taskExecutor") public String createContract(TruckerDTO.ContractRequest req) { ResponseEntity basicInfoResponseEntity = shipperClient.get(req.getShipmentId()); ShipmentDTO.BasicInfo basicInfo = basicInfoResponseEntity.getBody(); @@ -35,6 +37,7 @@ public String createContract(TruckerDTO.ContractRequest req) { } } + @Async(value = "taskExecutor") public String updateContractStatus(String contractId, String status) { Optional byId = contractRepository.findById(contractId); if (byId.isPresent()) { @@ -46,6 +49,7 @@ public String updateContractStatus(String contractId, String status) { } } + @Async(value = "taskExecutor") public TruckerDTO.ContractResponse getContract(String contractId) { Optional byId = contractRepository.findById(contractId); if (byId.isPresent()) { diff --git a/module-trucker/src/main/java/com/flab/moduletrucker/truck/service/ThreadPoolConfiguration.java b/module-trucker/src/main/java/com/flab/moduletrucker/truck/service/ThreadPoolConfiguration.java new file mode 100644 index 0000000..04d9e89 --- /dev/null +++ b/module-trucker/src/main/java/com/flab/moduletrucker/truck/service/ThreadPoolConfiguration.java @@ -0,0 +1,25 @@ +package com.flab.moduletrucker.truck.service; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.task.TaskExecutor; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; + +import java.util.concurrent.ThreadPoolExecutor; + +@Configuration +@EnableAsync +public class ThreadPoolConfiguration { + + @Bean(name = "taskExecutor") + public TaskExecutor taskExecutor() { + ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); + executor.setCorePoolSize(5); + executor.setMaxPoolSize(10); + executor.setQueueCapacity(100); + executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); + executor.setThreadNamePrefix("taskExecutor-"); + return executor; + } +} From 3737d848d0b2daffba52349af301b2427f4b3691 Mon Sep 17 00:00:00 2001 From: riderkaman Date: Tue, 10 Dec 2024 18:07:08 +0900 Subject: [PATCH 5/7] =?UTF-8?q?CompletableFuture,=20Async=20=EB=8F=99?= =?UTF-8?q?=EC=8B=9C=20=EC=82=AC=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ShipmentApiController.java | 8 ++++--- .../shipment/service/ShipmentService.java | 21 ++++++++++++------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/module-shipper/src/main/java/com/flab/moduleshipper/shipment/controller/ShipmentApiController.java b/module-shipper/src/main/java/com/flab/moduleshipper/shipment/controller/ShipmentApiController.java index 389311c..90aeaea 100644 --- a/module-shipper/src/main/java/com/flab/moduleshipper/shipment/controller/ShipmentApiController.java +++ b/module-shipper/src/main/java/com/flab/moduleshipper/shipment/controller/ShipmentApiController.java @@ -8,6 +8,7 @@ import org.springframework.web.bind.annotation.*; import java.util.List; +import java.util.concurrent.CompletableFuture; @RestController @RequiredArgsConstructor @@ -23,9 +24,10 @@ public ResponseEntity create(@RequestBody ShipmentDTO.ShipmentRequest re } @GetMapping("/") - public ResponseEntity> getAll() { - List all = shipmentService.getAll(); - return ResponseEntity.status(HttpStatus.OK).body(all); + public CompletableFuture>> getAll() { + return shipmentService.getAll() + .thenApply(result -> new ResponseEntity<>(result, HttpStatus.OK)) + .exceptionally(ex -> ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build()); } @GetMapping("/{shipmentId}") diff --git a/module-shipper/src/main/java/com/flab/moduleshipper/shipment/service/ShipmentService.java b/module-shipper/src/main/java/com/flab/moduleshipper/shipment/service/ShipmentService.java index 503bcf6..dda5f7e 100644 --- a/module-shipper/src/main/java/com/flab/moduleshipper/shipment/service/ShipmentService.java +++ b/module-shipper/src/main/java/com/flab/moduleshipper/shipment/service/ShipmentService.java @@ -5,12 +5,14 @@ import com.flab.moduleshipper.shipment.repository.ShipmentRepository; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.List; import java.util.Optional; +import java.util.concurrent.CompletableFuture; @Slf4j @Service @@ -25,14 +27,17 @@ public String create(ShipmentDTO.ShipmentRequest req) { return save.getShipmentId(); } - public List getAll() { - List shipments = shipmentRepository.findAll(); - List basicInfoList = new ArrayList<>(); - for (Shipment shipment : shipments) { - ShipmentDTO.BasicInfo basicInfo = new ShipmentDTO.BasicInfo(shipment); - basicInfoList.add(basicInfo); - } - return basicInfoList; + @Async + public CompletableFuture> getAll() { + return CompletableFuture.supplyAsync(() -> { + List shipments = shipmentRepository.findAll(); + List basicInfoList = new ArrayList<>(); + for (Shipment shipment : shipments) { + ShipmentDTO.BasicInfo basicInfo = new ShipmentDTO.BasicInfo(shipment); + basicInfoList.add(basicInfo); + } + return basicInfoList; + }); } public ShipmentDTO.BasicInfo get(String shipmentId) { From 5ef0b3d9afc6584248e35451419e53c4091db797 Mon Sep 17 00:00:00 2001 From: riderkaman Date: Tue, 24 Dec 2024 00:35:04 +0900 Subject: [PATCH 6/7] =?UTF-8?q?=ED=99=94=EB=AC=BC=20=EB=93=B1=EB=A1=9D=20?= =?UTF-8?q?=EC=8B=9C=20=EC=B9=B4=ED=94=84=EC=B9=B4=EB=A1=9C=20=EB=A9=94?= =?UTF-8?q?=EC=8B=9C=EC=A7=80=20=EC=A0=84=EC=86=A1=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- module-admin/build.gradle | 10 +---- .../moduleadmin/ModuleAdminApplication.java | 4 +- .../moduleadmin/admin/domain/Shipment.java | 32 +++++++++++++++ .../admin/service/adminConsumer.java | 13 ++++++ .../config/KafkaConsumerConfig.java | 40 +++++++++++++++++++ .../src/main/resources/application.properties | 4 +- module-shipper/build.gradle | 3 ++ .../config/KafkaProducerConfig.java | 39 ++++++++++++++++++ .../shipment/service/ShipmentProducer.java | 26 ++++++++++++ .../shipment/service/ShipmentService.java | 2 + .../src/main/resources/application.properties | 5 ++- .../shipment/service/ShipmentServiceTest.java | 22 ++++++++++ 12 files changed, 189 insertions(+), 11 deletions(-) create mode 100644 module-admin/src/main/java/com/flab/moduleadmin/admin/domain/Shipment.java create mode 100644 module-admin/src/main/java/com/flab/moduleadmin/admin/service/adminConsumer.java create mode 100644 module-admin/src/main/java/com/flab/moduleadmin/config/KafkaConsumerConfig.java create mode 100644 module-shipper/src/main/java/com/flab/moduleshipper/config/KafkaProducerConfig.java create mode 100644 module-shipper/src/main/java/com/flab/moduleshipper/shipment/service/ShipmentProducer.java create mode 100644 module-shipper/src/test/java/com/flab/moduleshipper/shipment/service/ShipmentServiceTest.java diff --git a/module-admin/build.gradle b/module-admin/build.gradle index 4dc4f12..7cf2bea 100644 --- a/module-admin/build.gradle +++ b/module-admin/build.gradle @@ -34,14 +34,8 @@ dependencies { testImplementation 'org.springframework.security:spring-security-test' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' - // jwt - implementation 'io.jsonwebtoken:jjwt-api:0.11.5' - implementation 'io.jsonwebtoken:jjwt-impl:0.11.5' - implementation 'io.jsonwebtoken:jjwt-jackson:0.11.5' - - // mysql - implementation 'org.springframework.boot:spring-boot-starter-jdbc' - runtimeOnly 'com.mysql:mysql-connector-j' + // kafka + implementation 'org.springframework.kafka:spring-kafka' } tasks.named('test') { diff --git a/module-admin/src/main/java/com/flab/moduleadmin/ModuleAdminApplication.java b/module-admin/src/main/java/com/flab/moduleadmin/ModuleAdminApplication.java index dd7457f..01c242c 100644 --- a/module-admin/src/main/java/com/flab/moduleadmin/ModuleAdminApplication.java +++ b/module-admin/src/main/java/com/flab/moduleadmin/ModuleAdminApplication.java @@ -2,8 +2,10 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; +import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; -@SpringBootApplication +@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class }) public class ModuleAdminApplication { public static void main(String[] args) { diff --git a/module-admin/src/main/java/com/flab/moduleadmin/admin/domain/Shipment.java b/module-admin/src/main/java/com/flab/moduleadmin/admin/domain/Shipment.java new file mode 100644 index 0000000..c3a2c04 --- /dev/null +++ b/module-admin/src/main/java/com/flab/moduleadmin/admin/domain/Shipment.java @@ -0,0 +1,32 @@ +package com.flab.moduleadmin.admin.domain; + +import lombok.*; + +@Getter +@Setter +@NoArgsConstructor +public class Shipment { + + private String shipmentId; + + private String memberId; + + private String companyName; + + private String shipperName; + + private String businessRegistrationNumber; + + private String fromAddress; + + private String toAddress; + + private String shipmentDate; + + private String shipmentType; + + private String comment; + + private String shipmentStatus; + +} diff --git a/module-admin/src/main/java/com/flab/moduleadmin/admin/service/adminConsumer.java b/module-admin/src/main/java/com/flab/moduleadmin/admin/service/adminConsumer.java new file mode 100644 index 0000000..5dec238 --- /dev/null +++ b/module-admin/src/main/java/com/flab/moduleadmin/admin/service/adminConsumer.java @@ -0,0 +1,13 @@ +package com.flab.moduleadmin.admin.service; + +import org.springframework.kafka.annotation.KafkaListener; +import org.springframework.stereotype.Component; + +@Component +public class adminConsumer { + + @KafkaListener(topics = "shipment", groupId = "group_1") + public void listen(String data) { + System.out.println("data = " + data); + } +} diff --git a/module-admin/src/main/java/com/flab/moduleadmin/config/KafkaConsumerConfig.java b/module-admin/src/main/java/com/flab/moduleadmin/config/KafkaConsumerConfig.java new file mode 100644 index 0000000..98f7c1b --- /dev/null +++ b/module-admin/src/main/java/com/flab/moduleadmin/config/KafkaConsumerConfig.java @@ -0,0 +1,40 @@ +package com.flab.moduleadmin.config; + +import org.apache.kafka.clients.consumer.ConsumerConfig; +import org.apache.kafka.common.serialization.StringDeserializer; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory; +import org.springframework.kafka.core.ConsumerFactory; +import org.springframework.kafka.core.DefaultKafkaConsumerFactory; +import org.springframework.kafka.support.serializer.JsonDeserializer; + +import java.util.HashMap; +import java.util.Map; + +@Configuration +public class KafkaConsumerConfig { + + @Value("${kafka.server}") + private String kafkaServer; + + @Bean + public ConsumerFactory consumerFactory() { + Map config = new HashMap<>(); + config.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaServer); + config.put(ConsumerConfig.GROUP_ID_CONFIG, "group_1"); + config.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); + config.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); + + return new DefaultKafkaConsumerFactory<>(config); + } + + @Bean + public ConcurrentKafkaListenerContainerFactory kafkaListenerContainerFactory() { + ConcurrentKafkaListenerContainerFactory factory = new ConcurrentKafkaListenerContainerFactory<>(); + factory.setConsumerFactory(consumerFactory()); + + return factory; + } +} diff --git a/module-admin/src/main/resources/application.properties b/module-admin/src/main/resources/application.properties index 1fd303c..de6054d 100644 --- a/module-admin/src/main/resources/application.properties +++ b/module-admin/src/main/resources/application.properties @@ -1,2 +1,4 @@ spring.application.name=module-admin -server.port=8003 \ No newline at end of file +server.port=8003 + +kafka.server=localhost:9092 \ No newline at end of file diff --git a/module-shipper/build.gradle b/module-shipper/build.gradle index 0925dda..7a55014 100644 --- a/module-shipper/build.gradle +++ b/module-shipper/build.gradle @@ -42,6 +42,9 @@ dependencies { // mysql implementation 'org.springframework.boot:spring-boot-starter-jdbc' runtimeOnly 'com.mysql:mysql-connector-j' + + // kafka + implementation 'org.springframework.kafka:spring-kafka' } tasks.named('test') { diff --git a/module-shipper/src/main/java/com/flab/moduleshipper/config/KafkaProducerConfig.java b/module-shipper/src/main/java/com/flab/moduleshipper/config/KafkaProducerConfig.java new file mode 100644 index 0000000..6340c6e --- /dev/null +++ b/module-shipper/src/main/java/com/flab/moduleshipper/config/KafkaProducerConfig.java @@ -0,0 +1,39 @@ +package com.flab.moduleshipper.config; + +import org.apache.kafka.clients.producer.KafkaProducer; +import org.apache.kafka.clients.producer.ProducerConfig; +import org.apache.kafka.common.serialization.StringSerializer; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.kafka.core.DefaultKafkaProducerFactory; +import org.springframework.kafka.core.KafkaTemplate; +import org.springframework.kafka.core.ProducerFactory; +import org.springframework.kafka.support.serializer.JsonSerializer; + +import java.util.HashMap; +import java.util.Map; + +@Configuration +public class KafkaProducerConfig { + + @Value("${kafka.server}") + private String kafkaServer; + + + @Bean + public ProducerFactory producerFactory() { + Map config = new HashMap<>(); + config.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaServer); + config.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class); + config.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class); + + return new DefaultKafkaProducerFactory<>(config); + } + + @Bean + public KafkaTemplate kafkaTemplate() { + return new KafkaTemplate<>(producerFactory()); + } + +} diff --git a/module-shipper/src/main/java/com/flab/moduleshipper/shipment/service/ShipmentProducer.java b/module-shipper/src/main/java/com/flab/moduleshipper/shipment/service/ShipmentProducer.java new file mode 100644 index 0000000..abda528 --- /dev/null +++ b/module-shipper/src/main/java/com/flab/moduleshipper/shipment/service/ShipmentProducer.java @@ -0,0 +1,26 @@ +package com.flab.moduleshipper.shipment.service; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.flab.moduleshipper.shipment.domain.Shipment; +import lombok.AllArgsConstructor; +import org.springframework.kafka.core.KafkaTemplate; +import org.springframework.stereotype.Component; + +@Component +@AllArgsConstructor +public class ShipmentProducer { + + private final KafkaTemplate kafkaTemplate; + + public void sendShipment(final Shipment shipment) { + ObjectMapper objectMapper = new ObjectMapper(); + String jsonString = ""; + try { + jsonString = objectMapper.writeValueAsString(shipment); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } + kafkaTemplate.send("shipment", jsonString); + } +} diff --git a/module-shipper/src/main/java/com/flab/moduleshipper/shipment/service/ShipmentService.java b/module-shipper/src/main/java/com/flab/moduleshipper/shipment/service/ShipmentService.java index dda5f7e..03a97eb 100644 --- a/module-shipper/src/main/java/com/flab/moduleshipper/shipment/service/ShipmentService.java +++ b/module-shipper/src/main/java/com/flab/moduleshipper/shipment/service/ShipmentService.java @@ -20,10 +20,12 @@ public class ShipmentService { private final ShipmentRepository shipmentRepository; + private final ShipmentProducer shipmentProducer; public String create(ShipmentDTO.ShipmentRequest req) { Shipment shipment = req.dtoToDomain(req); Shipment save = shipmentRepository.save(shipment); + shipmentProducer.sendShipment(save); return save.getShipmentId(); } diff --git a/module-shipper/src/main/resources/application.properties b/module-shipper/src/main/resources/application.properties index 6bf98ca..7e0ed35 100644 --- a/module-shipper/src/main/resources/application.properties +++ b/module-shipper/src/main/resources/application.properties @@ -5,4 +5,7 @@ server.port=8001 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/truck_matching?serverTimezone=UTC&characterEncoding=UTF-8 spring.datasource.username=root -spring.datasource.password=123963 \ No newline at end of file +spring.datasource.password=123963 + +# kafka +kafka.server=localhost:9092 diff --git a/module-shipper/src/test/java/com/flab/moduleshipper/shipment/service/ShipmentServiceTest.java b/module-shipper/src/test/java/com/flab/moduleshipper/shipment/service/ShipmentServiceTest.java new file mode 100644 index 0000000..d124f7e --- /dev/null +++ b/module-shipper/src/test/java/com/flab/moduleshipper/shipment/service/ShipmentServiceTest.java @@ -0,0 +1,22 @@ +package com.flab.moduleshipper.shipment.service; + +import com.flab.moduleshipper.shipment.dto.ShipmentDTO; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import static org.junit.jupiter.api.Assertions.*; + +@SpringBootTest +class ShipmentServiceTest { + + @Autowired + private ShipmentService shipmentService; + + @Test + void create() { + ShipmentDTO.ShipmentRequest req = new ShipmentDTO.ShipmentRequest(); + req.setMemberId("testMemberId"); + shipmentService.create(req); + } +} \ No newline at end of file From a65947909cbffc8222f7f51d3577c4b1d6648588 Mon Sep 17 00:00:00 2001 From: riderkaman Date: Tue, 31 Dec 2024 04:59:55 +0900 Subject: [PATCH 7/7] =?UTF-8?q?protoBuf=20=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=201=EC=B0=A8=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- module-admin/build.gradle | 19 +++++++++++++++++++ .../admin/service/adminConsumer.java | 13 ++++++++++--- .../config/KafkaConsumerConfig.java | 10 ++++++---- module-admin/src/main/proto/shipment.proto | 17 +++++++++++++++++ module-shipper/build.gradle | 19 +++++++++++++++++++ .../config/KafkaProducerConfig.java | 10 +++++----- .../shipment/service/ShipmentProducer.java | 18 +++++++----------- module-shipper/src/main/proto/shipment.proto | 17 +++++++++++++++++ 8 files changed, 100 insertions(+), 23 deletions(-) create mode 100644 module-admin/src/main/proto/shipment.proto create mode 100644 module-shipper/src/main/proto/shipment.proto diff --git a/module-admin/build.gradle b/module-admin/build.gradle index 7cf2bea..db17af4 100644 --- a/module-admin/build.gradle +++ b/module-admin/build.gradle @@ -1,5 +1,6 @@ plugins { id 'java' + id 'com.google.protobuf' version '0.9.4' } group = 'com.flab' @@ -19,6 +20,7 @@ configurations { repositories { mavenCentral() + maven { url "https://packages.confluent.io/maven/" } } dependencies { @@ -36,6 +38,23 @@ dependencies { // kafka implementation 'org.springframework.kafka:spring-kafka' + + // protobuf + implementation 'com.google.protobuf:protobuf-java:3.25.5' + implementation 'io.confluent:kafka-protobuf-serializer:7.4.8' +} + +protobuf { + protoc { + artifact = "com.google.protobuf:protoc:3.25.5" + } + generateProtoTasks { + all().each { task -> + task.builtins { + java { } + } + } + } } tasks.named('test') { diff --git a/module-admin/src/main/java/com/flab/moduleadmin/admin/service/adminConsumer.java b/module-admin/src/main/java/com/flab/moduleadmin/admin/service/adminConsumer.java index 5dec238..331f9cc 100644 --- a/module-admin/src/main/java/com/flab/moduleadmin/admin/service/adminConsumer.java +++ b/module-admin/src/main/java/com/flab/moduleadmin/admin/service/adminConsumer.java @@ -1,13 +1,20 @@ package com.flab.moduleadmin.admin.service; +import com.flab.shipment.kafka.Shipment.ShipmentMessage; import org.springframework.kafka.annotation.KafkaListener; import org.springframework.stereotype.Component; @Component public class adminConsumer { - @KafkaListener(topics = "shipment", groupId = "group_1") - public void listen(String data) { - System.out.println("data = " + data); + @KafkaListener(topics = "shipmentMessage", groupId = "group_1") + public void listen(byte[] data) { + try { + ShipmentMessage shipmentMessage = ShipmentMessage.parseFrom(data); + String memberId = shipmentMessage.getMemberId(); + System.out.println("memberId = " + memberId); + } catch (Exception e) { + e.printStackTrace(); + } } } diff --git a/module-admin/src/main/java/com/flab/moduleadmin/config/KafkaConsumerConfig.java b/module-admin/src/main/java/com/flab/moduleadmin/config/KafkaConsumerConfig.java index 98f7c1b..70fd794 100644 --- a/module-admin/src/main/java/com/flab/moduleadmin/config/KafkaConsumerConfig.java +++ b/module-admin/src/main/java/com/flab/moduleadmin/config/KafkaConsumerConfig.java @@ -1,6 +1,8 @@ package com.flab.moduleadmin.config; +import io.confluent.kafka.serializers.protobuf.KafkaProtobufDeserializer; import org.apache.kafka.clients.consumer.ConsumerConfig; +import org.apache.kafka.common.serialization.ByteArrayDeserializer; import org.apache.kafka.common.serialization.StringDeserializer; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; @@ -20,19 +22,19 @@ public class KafkaConsumerConfig { private String kafkaServer; @Bean - public ConsumerFactory consumerFactory() { + public ConsumerFactory consumerFactory() { Map config = new HashMap<>(); config.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaServer); config.put(ConsumerConfig.GROUP_ID_CONFIG, "group_1"); config.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); - config.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); + config.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, ByteArrayDeserializer.class); return new DefaultKafkaConsumerFactory<>(config); } @Bean - public ConcurrentKafkaListenerContainerFactory kafkaListenerContainerFactory() { - ConcurrentKafkaListenerContainerFactory factory = new ConcurrentKafkaListenerContainerFactory<>(); + public ConcurrentKafkaListenerContainerFactory kafkaListenerContainerFactory() { + ConcurrentKafkaListenerContainerFactory factory = new ConcurrentKafkaListenerContainerFactory<>(); factory.setConsumerFactory(consumerFactory()); return factory; diff --git a/module-admin/src/main/proto/shipment.proto b/module-admin/src/main/proto/shipment.proto new file mode 100644 index 0000000..765bbea --- /dev/null +++ b/module-admin/src/main/proto/shipment.proto @@ -0,0 +1,17 @@ +syntax = "proto3"; + +package com.flab.shipment.kafka; + +message ShipmentMessage { + string shipmentId = 1; + string memberId = 2; + string companyName = 3; + string shipperName = 4; + string businessRegistrationNumber = 5; + string fromAddress = 6; + string toAddress = 7; + string shipmentDate = 8; + string shipmentType = 9; + string comment = 10; + string shipmentStatus = 11; +} diff --git a/module-shipper/build.gradle b/module-shipper/build.gradle index 7a55014..bdba9b2 100644 --- a/module-shipper/build.gradle +++ b/module-shipper/build.gradle @@ -1,5 +1,6 @@ plugins { id 'java' + id 'com.google.protobuf' version '0.9.4' } group = 'com.flab' @@ -19,6 +20,7 @@ configurations { repositories { mavenCentral() + maven { url "https://packages.confluent.io/maven/" } } dependencies { @@ -45,6 +47,23 @@ dependencies { // kafka implementation 'org.springframework.kafka:spring-kafka' + + // protobuf + implementation 'com.google.protobuf:protobuf-java:3.25.5' + implementation 'io.confluent:kafka-protobuf-serializer:7.4.8' +} + +protobuf { + protoc { + artifact = "com.google.protobuf:protoc:3.25.5" + } + generateProtoTasks { + all().each { task -> + task.builtins { + java { } + } + } + } } tasks.named('test') { diff --git a/module-shipper/src/main/java/com/flab/moduleshipper/config/KafkaProducerConfig.java b/module-shipper/src/main/java/com/flab/moduleshipper/config/KafkaProducerConfig.java index 6340c6e..fc17f52 100644 --- a/module-shipper/src/main/java/com/flab/moduleshipper/config/KafkaProducerConfig.java +++ b/module-shipper/src/main/java/com/flab/moduleshipper/config/KafkaProducerConfig.java @@ -1,7 +1,8 @@ package com.flab.moduleshipper.config; -import org.apache.kafka.clients.producer.KafkaProducer; +import io.confluent.kafka.serializers.protobuf.KafkaProtobufSerializer; import org.apache.kafka.clients.producer.ProducerConfig; +import org.apache.kafka.common.serialization.ByteArraySerializer; import org.apache.kafka.common.serialization.StringSerializer; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; @@ -9,7 +10,6 @@ import org.springframework.kafka.core.DefaultKafkaProducerFactory; import org.springframework.kafka.core.KafkaTemplate; import org.springframework.kafka.core.ProducerFactory; -import org.springframework.kafka.support.serializer.JsonSerializer; import java.util.HashMap; import java.util.Map; @@ -22,17 +22,17 @@ public class KafkaProducerConfig { @Bean - public ProducerFactory producerFactory() { + public ProducerFactory producerFactory() { Map config = new HashMap<>(); config.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaServer); config.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class); - config.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class); + config.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, ByteArraySerializer.class); return new DefaultKafkaProducerFactory<>(config); } @Bean - public KafkaTemplate kafkaTemplate() { + public KafkaTemplate kafkaTemplate() { return new KafkaTemplate<>(producerFactory()); } diff --git a/module-shipper/src/main/java/com/flab/moduleshipper/shipment/service/ShipmentProducer.java b/module-shipper/src/main/java/com/flab/moduleshipper/shipment/service/ShipmentProducer.java index abda528..05aab5f 100644 --- a/module-shipper/src/main/java/com/flab/moduleshipper/shipment/service/ShipmentProducer.java +++ b/module-shipper/src/main/java/com/flab/moduleshipper/shipment/service/ShipmentProducer.java @@ -1,8 +1,7 @@ package com.flab.moduleshipper.shipment.service; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; import com.flab.moduleshipper.shipment.domain.Shipment; +import com.flab.shipment.kafka.Shipment.ShipmentMessage; import lombok.AllArgsConstructor; import org.springframework.kafka.core.KafkaTemplate; import org.springframework.stereotype.Component; @@ -11,16 +10,13 @@ @AllArgsConstructor public class ShipmentProducer { - private final KafkaTemplate kafkaTemplate; + private final KafkaTemplate kafkaTemplate; public void sendShipment(final Shipment shipment) { - ObjectMapper objectMapper = new ObjectMapper(); - String jsonString = ""; - try { - jsonString = objectMapper.writeValueAsString(shipment); - } catch (JsonProcessingException e) { - throw new RuntimeException(e); - } - kafkaTemplate.send("shipment", jsonString); + ShipmentMessage build = ShipmentMessage.newBuilder() + .setShipmentId(shipment.getShipmentId()) + .setMemberId(shipment.getMemberId()) + .build(); + kafkaTemplate.send("shipmentMessage", build.toByteArray()); } } diff --git a/module-shipper/src/main/proto/shipment.proto b/module-shipper/src/main/proto/shipment.proto new file mode 100644 index 0000000..765bbea --- /dev/null +++ b/module-shipper/src/main/proto/shipment.proto @@ -0,0 +1,17 @@ +syntax = "proto3"; + +package com.flab.shipment.kafka; + +message ShipmentMessage { + string shipmentId = 1; + string memberId = 2; + string companyName = 3; + string shipperName = 4; + string businessRegistrationNumber = 5; + string fromAddress = 6; + string toAddress = 7; + string shipmentDate = 8; + string shipmentType = 9; + string comment = 10; + string shipmentStatus = 11; +}