Skip to content

Commit a651a16

Browse files
author
seungjoo.jeong
committed
feat(test) 테스트코드 추가
- 누락된 테스트코드 추가 - ContainsProfanityException 추가
1 parent 0df4b89 commit a651a16

File tree

6 files changed

+102
-3
lines changed

6 files changed

+102
-3
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package kitchenpos.product.application.exception;
2+
3+
import kitchenpos.product.domain.Name;
4+
5+
public final class ContainsProfanityException extends IllegalArgumentException {
6+
7+
public ContainsProfanityException(final Name name) {
8+
super(String.format("name contains profanity. name: %s", name));
9+
}
10+
}

src/main/java/kitchenpos/product/domain/DefaultProductNameFactory.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package kitchenpos.product.domain;
22

3+
import static kitchenpos.product.support.constant.Name.NAME_CANDIDATE;
4+
import static kitchenpos.support.ParameterValidateUtils.checkNotNull;
5+
6+
import kitchenpos.product.application.exception.ContainsProfanityException;
37
import kitchenpos.product.application.port.out.ProductPurgomalumChecker;
48

59
public class DefaultProductNameFactory implements ProductNameFactory {
@@ -12,9 +16,10 @@ public DefaultProductNameFactory(final ProductPurgomalumChecker checker) {
1216

1317
@Override
1418
public ProductName create(final Name nameCandidate) {
15-
if (!checker.containsProfanity(nameCandidate)) {
16-
throw new IllegalArgumentException(
17-
String.format("name has profanity. name: %s", nameCandidate));
19+
checkNotNull(nameCandidate, NAME_CANDIDATE);
20+
21+
if (checker.containsProfanity(nameCandidate)) {
22+
throw new ContainsProfanityException(nameCandidate);
1823
}
1924

2025
return ProductName.of(nameCandidate);

src/main/java/kitchenpos/product/domain/Name.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static com.google.common.base.Preconditions.checkArgument;
44

5+
import com.google.common.base.MoreObjects;
56
import com.google.common.base.Objects;
67
import org.apache.logging.log4j.util.Strings;
78

@@ -32,6 +33,13 @@ public int hashCode() {
3233
return Objects.hashCode(value);
3334
}
3435

36+
@Override
37+
public String toString() {
38+
return MoreObjects.toStringHelper(this)
39+
.add("value", value)
40+
.toString();
41+
}
42+
3543
public String getValue() {
3644
return value;
3745
}

src/main/java/kitchenpos/product/support/constant/Name.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public final class Name {
66
public static final String NAME = "name";
77
public static final String PRICE = "price";
88
public static final String ENTITY = "entity";
9+
public static final String NAME_CANDIDATE = "nameCandidate";
910

1011
private Name() {
1112
throw new UnsupportedOperationException();

src/main/java/kitchenpos/support/ParameterValidateUtils.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import static com.google.common.base.Preconditions.checkArgument;
44

5+
/**
6+
* 해당 클래스는 파라미터에 대한 유효성 검사를 위한 것이므로, 비즈니스 종속적인 곳에서는 사용하면 안된다.
7+
*/
58
public final class ParameterValidateUtils {
69

710
public static <T> T checkNotNull(final T value, final String name) {
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package kitchenpos.product.domain;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
5+
import static org.mockito.Mockito.doReturn;
6+
7+
import kitchenpos.product.application.exception.ContainsProfanityException;
8+
import kitchenpos.product.application.port.out.ProductPurgomalumChecker;
9+
import org.junit.jupiter.api.BeforeEach;
10+
import org.junit.jupiter.api.DisplayNameGeneration;
11+
import org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores;
12+
import org.junit.jupiter.api.extension.ExtendWith;
13+
import org.junit.jupiter.params.ParameterizedTest;
14+
import org.junit.jupiter.params.provider.NullSource;
15+
import org.junit.jupiter.params.provider.ValueSource;
16+
import org.mockito.Mock;
17+
import org.mockito.junit.jupiter.MockitoExtension;
18+
19+
@ExtendWith(MockitoExtension.class)
20+
@DisplayNameGeneration(ReplaceUnderscores.class)
21+
class DefaultProductNameFactoryTest {
22+
23+
@Mock
24+
private ProductPurgomalumChecker mockChecker;
25+
26+
private ProductNameFactory factory;
27+
28+
@BeforeEach
29+
void setUp() {
30+
factory = new DefaultProductNameFactory(mockChecker);
31+
}
32+
33+
@ParameterizedTest
34+
@NullSource
35+
void create_invalid_parameters_이름후보가_없으면_예외를_발생시킨다(final Name value) {
36+
37+
// when & then
38+
assertThatThrownBy(() -> factory.create(value))
39+
.isExactlyInstanceOf(IllegalArgumentException.class);
40+
}
41+
42+
@ParameterizedTest
43+
@ValueSource(strings = "비속어포함된이름")
44+
void create_이름후보에_비속어가_포함되어있으면_예외를_발생시킨다(final String value) {
45+
// given
46+
final Name profanityName = new Name(value);
47+
doReturn(true)
48+
.when(mockChecker)
49+
.containsProfanity(profanityName);
50+
51+
// when & then
52+
assertThatThrownBy(() -> factory.create(profanityName))
53+
.isExactlyInstanceOf(ContainsProfanityException.class);
54+
}
55+
56+
57+
@ParameterizedTest
58+
@ValueSource(strings = "dummy")
59+
void create_이름후보의_값을_가지고_음식이름을_만들어서_반환한다(final String value) {
60+
// given
61+
final Name name = new Name(value);
62+
63+
// when
64+
final ProductName actual = factory.create(name);
65+
66+
// then
67+
assertThat(actual).isNotNull();
68+
assertThat(actual)
69+
.extracting("value")
70+
.isEqualTo(value);
71+
}
72+
}

0 commit comments

Comments
 (0)