Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

7주차 - 김연지 #80

Merged
merged 3 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package study.tdd.chap07;

public class AutoDebitInfoRepository {
/*
void save(AutoDebitInfo info);
AutoDebitInfo findOne(String userId);
*/
}
34 changes: 34 additions & 0 deletions 김연지/src/main/java/study/tdd/chap07/AutoDebitRegister.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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();
}
*/
}
38 changes: 38 additions & 0 deletions 김연지/src/main/java/study/tdd/chap07/CardNumberValidator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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,20 @@
package study.tdd.chap07;

import java.util.HashMap;

/*
public class MemoryAutoDebitInfoRepository implements AutoDebitInfoRepository {

private Map<String, AutoDebitInfo> infos = new HashMap<>();

@Override
public void save(AutoDebitInfo info) {
infos.put(info.getUserId(), info);
}

@Override
public AutoDebitInfo findOne(String userId) {
return infos.get(userId);
}
}
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package study.tdd.chap07;

public class StubCardNumberValidator extends CardNumberValidator {
/*
private String invalidNo;
private String theftNo;

public void setInvalidNo(String invalidNo) {
this.invalidNo = invalidNo;
}

public void setTheftNo(String theftNo) {
this.theftNo = theftNo;
}

@Override
public CardValidity validate(String cardNumber) {
if (invalidNo != null && invalidNo.equals(cardNumber)) {
return CardValidity.INVALID;
}
if (theftNo != null && theftNo.equals(cardNumber)) {
return CardValidity.THEFT;
}
return CardValidity.VALID;
}
*/
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package study.tdd.chap07.join;

public class DupIdException extends RuntimeException {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package study.tdd.chap07.join;

public interface EmailNotifier {
void sendRegisterEmail(String email);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package study.tdd.chap07.join;

import study.tdd.chap07.join.UserRepository;

import java.util.HashMap;
import java.util.Map;

public class MemoryUserRepository implements UserRepository {
private Map<String, User> users = new HashMap<>();

@Override
public void save(User user) {
users.put(user.getId(), user);
}

@Override
public User findById(String id) {
return users.get(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package study.tdd.chap07.join;

public class SpyEmailNotifier implements EmailNotifier {
private boolean called;
private String email;

public boolean isCalled() {
return called;
}

public String getEmail() {
return email;
}

@Override
public void sendRegisterEmail(String email) {
this.called = true;
this.email = email;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package study.tdd.chap07.join;

public class StubWeakPasswordChecker implements WeakPasswordChecker {
private boolean weak;

public void setWeak(boolean weak) {
this.weak = weak;
}

@Override
public boolean checkPasswordWeak(String pw) {
return weak;
}
}
21 changes: 21 additions & 0 deletions 김연지/src/main/java/study/tdd/chap07/join/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package study.tdd.chap07.join;

public class User {
private String id;
private String password;
private String email;

public User(String id, String password, String email) {
this.id = id;
this.password = password;
this.email = email;
}

public String getId() {
return id;
}

public String getEmail() {
return email;
}
}
25 changes: 25 additions & 0 deletions 김연지/src/main/java/study/tdd/chap07/join/UserRegister.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package study.tdd.chap07.join;

public class UserRegister {
private WeakPasswordChecker passwordChecker;
private UserRepository userRepository;
private EmailNotifier emailNotifier;

public UserRegister(WeakPasswordChecker passwordChecker, UserRepository userRepository, EmailNotifier emailNotifier) {
this.passwordChecker = passwordChecker;
this.userRepository = userRepository;
this.emailNotifier = emailNotifier;
}

public void register(String id, String pw, String email) {
if (passwordChecker.checkPasswordWeak(pw)) {
throw new WeakPasswordException();
}
User user = userRepository.findById(id);
if (user != null) {
throw new DupIdException();
}
userRepository.save(new User(id, pw, email));
emailNotifier.sendRegisterEmail(email);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package study.tdd.chap07.join;

public interface UserRepository {
void save(User user);

User findById(String id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package study.tdd.chap07.join;

public interface WeakPasswordChecker {
boolean checkPasswordWeak(String pw);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package study.tdd.chap07.join;

public class WeakPasswordException extends RuntimeException {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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());
}
*/
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package study.tdd.chap07;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class AutoDebitRegister_Fake_Test {
/*
private AutoDebitRegister register;
private StubCardNumberValidator cardNumberValidator;
private MemoryAutoDebitInfoRepository repository;

@BeforeEach
void setUp() {
cardNumberValidator = new StubCardNumberValidator();
repository = new MemoryAutoDebitInfoRepository();
register = new AutoDebitRegister(cardNumberValidator, repository);
}

@Test
void alreadyRegistered_InfoUpdated() {
repository.save(new AutoDebitInfo("user1", "111222333444", LocalDateTime.now()));

AutoDebitReq req = new AutoDebitReq("user1", "123456789012");
RegisterResult result = this.register.register(req);

AutoDebitInfo saved = repository.findOne("user1");
assertEquals("123456789012", saved.getCardNumber());
}

@Test
void notYetRegistered_newInfoRegistered() {
AutoDebitReq req = new AutoDebitReq("user1", "1234123412341234");
RegisterResult result = this.register.register(req);

AutoDebitInfo saved = repository.findOne("user1");
assertEquals("1234123412341234", saved.getCardNumber());
}
*/
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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 AutoDebitRegister_Stub_Test {
/*
private AutoDebitRegister register;
private StubCardNumberValidator stubValidator;
private StubAutoDebitInfoRepository stubRepository;

@BeforeEach
void setUp() {
stubValidator = new StubCardNumberValidator();
stubRepository = new StubAutoDebitInfoRepository();
register = new AutoDebitRegister(stubValidator, stubRepository);
}

@Test
void invalidCard() {
stubValidator.setInvalidNo("111122223333");

AutoDebitReq req = new AutoDebitReq("user1", "111122223333");
RegisterResult result = register.register(req);

assertEquals(INVALID, result.getValidity());
}

@Test
void theftCard() {
stubValidator.setTheftNo("1234567890123456");

AutoDebitReq req = new AutoDebitReq("user1", "1234567890123456");
RegisterResult result = register.register(req);

assertEquals(CardValidity.THEFT, result.getValidity());
}
*/
}
Loading