Skip to content

Commit

Permalink
대역의 필요성 - #72
Browse files Browse the repository at this point in the history
  • Loading branch information
rladuswl committed Feb 7, 2023
1 parent 92dbf83 commit a6e6afb
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package study.tdd.chap07;

public class AutoDebitInfoRepository {
}
32 changes: 32 additions & 0 deletions 김연지/src/main/java/study/tdd/chap07/AutoDebitRegister.java
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 김연지/src/main/java/study/tdd/chap07/CardNumberValidator.java
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;
}
}
}
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());
}
}

0 comments on commit a6e6afb

Please sign in to comment.