From a6e6afb36835d0125278e9ef0a74277c5ce93dfc Mon Sep 17 00:00:00 2001 From: kyj Date: Wed, 8 Feb 2023 02:20:25 +0900 Subject: [PATCH] =?UTF-8?q?=EB=8C=80=EC=97=AD=EC=9D=98=20=ED=95=84?= =?UTF-8?q?=EC=9A=94=EC=84=B1=20-=20#72?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tdd/chap07/AutoDebitInfoRepository.java" | 4 +++ .../study/tdd/chap07/AutoDebitRegister.java" | 32 +++++++++++++++++ .../tdd/chap07/CardNumberValidator.java" | 36 +++++++++++++++++++ .../tdd/chap07/AutoDebitRegisterTest.java" | 33 +++++++++++++++++ 4 files changed, 105 insertions(+) create mode 100644 "\352\271\200\354\227\260\354\247\200/src/main/java/study/tdd/chap07/AutoDebitInfoRepository.java" create mode 100644 "\352\271\200\354\227\260\354\247\200/src/main/java/study/tdd/chap07/AutoDebitRegister.java" create mode 100644 "\352\271\200\354\227\260\354\247\200/src/main/java/study/tdd/chap07/CardNumberValidator.java" create mode 100644 "\352\271\200\354\227\260\354\247\200/src/test/java/study/tdd/chap07/AutoDebitRegisterTest.java" diff --git "a/\352\271\200\354\227\260\354\247\200/src/main/java/study/tdd/chap07/AutoDebitInfoRepository.java" "b/\352\271\200\354\227\260\354\247\200/src/main/java/study/tdd/chap07/AutoDebitInfoRepository.java" new file mode 100644 index 0000000..f38bf40 --- /dev/null +++ "b/\352\271\200\354\227\260\354\247\200/src/main/java/study/tdd/chap07/AutoDebitInfoRepository.java" @@ -0,0 +1,4 @@ +package study.tdd.chap07; + +public class AutoDebitInfoRepository { +} diff --git "a/\352\271\200\354\227\260\354\247\200/src/main/java/study/tdd/chap07/AutoDebitRegister.java" "b/\352\271\200\354\227\260\354\247\200/src/main/java/study/tdd/chap07/AutoDebitRegister.java" new file mode 100644 index 0000000..d3555ba --- /dev/null +++ "b/\352\271\200\354\227\260\354\247\200/src/main/java/study/tdd/chap07/AutoDebitRegister.java" @@ -0,0 +1,32 @@ +package study.tdd.chap07; + +import java.time.LocalDateTime; + +public class AutoDebitRegister { + private CardNumberValidator validator; + private AutoDebitInfoRepository repository; + + public AutoDebitRegister(CardNumberValidator validator, AutoDebitInfoRepository repository) { + this.validator = validator; + this.repository = repository; + } + + public RegisterResult register(AutoDebitReq req) { + CardValidity validity = validator.validate(req.getCardNumber()); + + if (validity != CardValidity.VALID) { + return RegisterResult.error(validity); + } + + AutoDebitInfo info = repository.findOne(req.getUserId()); + + if (info != null) { + info.changeCardNumber(req.getCardNumber()); + } else { + AutoDebitInfo newInfo = new AutoDebitInfo(req.getUserId(), req.getCardNumber(), LocalDateTime.now()); + repository.save(newInfo); + } + + return RegisterResult.success(); + } +} diff --git "a/\352\271\200\354\227\260\354\247\200/src/main/java/study/tdd/chap07/CardNumberValidator.java" "b/\352\271\200\354\227\260\354\247\200/src/main/java/study/tdd/chap07/CardNumberValidator.java" new file mode 100644 index 0000000..79efd82 --- /dev/null +++ "b/\352\271\200\354\227\260\354\247\200/src/main/java/study/tdd/chap07/CardNumberValidator.java" @@ -0,0 +1,36 @@ +package study.tdd.chap07; + +import java.io.IOException; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; + +public class CardNumberValidator { + public CardValidity validate(String cardNumber) { + HttpClient httpClient = HttpClient.newHttpClient(); + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create("https://some-external-pg.com/card")) + .header("Content-Type", "text/plain") + .POST(HttpRequest.BodyPublishers.ofString(cardNumber)) + .build(); + + try { + HttpResponse response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); + switch (response.body()) { + case "ok": + return CardValidity.VALID; + case "bad": + return CardValidity.INVALID; + case "expired": + return CardValidity.EXPIRED; + case "theft": + return CardValidity.THEFT; + default: + return CardValidity.UNKNOWN; + } + } catch (IOException | InterruptedException exception) { + return CardValidity.ERROR; + } + } +} diff --git "a/\352\271\200\354\227\260\354\247\200/src/test/java/study/tdd/chap07/AutoDebitRegisterTest.java" "b/\352\271\200\354\227\260\354\247\200/src/test/java/study/tdd/chap07/AutoDebitRegisterTest.java" new file mode 100644 index 0000000..b625be4 --- /dev/null +++ "b/\352\271\200\354\227\260\354\247\200/src/test/java/study/tdd/chap07/AutoDebitRegisterTest.java" @@ -0,0 +1,33 @@ +package study.tdd.chap07; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class AutoDebitRegisterTest { + private AutoDebitRegister register; + + @BeforeEach + void setUp() { + CardNumberValidator validator = new CardNumberValidator(); + AutoDebitInfoRepository repository = new JpaAutoDebitInfoRepository(); + register = new AutoDebitRegister(validator, repository); + } + + @Test + void validCard() { + // 업체에서 받은 테스트용 유효한 카드번호 사용 + AutoDebitReq req = new AutoDebitReq("user1", "1234123412341234"); + RegisterResult result = this.register.register(req); + assertEquals(VALID, result.getValidity()); + } + + @Test + void theftCard() { + // 업체에서 받은 도난 테스트용 카드번호 사용 + AutoDebitReq req = new AutoDebitReq("user1", "1234567890123456"); + RegisterResult result = this.register.register(req); + assertEquals(THEFT, result.getValidity()); + } +}