-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
김연지/src/main/java/study/tdd/chap07/AutoDebitInfoRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package study.tdd.chap07; | ||
|
||
public class AutoDebitInfoRepository { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
김연지/src/main/java/study/tdd/chap07/CardNumberValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String> 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; | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
김연지/src/test/java/study/tdd/chap07/AutoDebitRegisterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |