├── .gitignore
├── README.md
├── pom.xml
└── src
├── main
├── java
│ └── io
│ │ └── redutan
│ │ └── antioop
│ │ ├── AntiOopApplication.java
│ │ └── noif
│ │ ├── Discount.java
│ │ ├── DiscountRequest.java
│ │ ├── Discountable.java
│ │ ├── DiscounterFactory.java
│ │ ├── PaymentRequest.java
│ │ ├── PaymentService.java
│ │ ├── step0
│ │ └── LegacyPaymentService.java
│ │ ├── step1
│ │ └── PaymentService1.java
│ │ ├── step2
│ │ ├── DanawaDiscountPolicy.java
│ │ ├── FancafeDiscountPolicy.java
│ │ ├── NaverDiscountPolicy.java
│ │ └── PaymentService2.java
│ │ ├── step2_2
│ │ ├── DanawaDiscountPolicy.java
│ │ ├── FancafeDiscountPolicy.java
│ │ ├── NaverDiscountPolicy.java
│ │ ├── PaymentService2_2.java
│ │ └── SimpleDiscounterFactory.java
│ │ ├── step3
│ │ ├── AbstractDiscounter.java
│ │ ├── AmtDiscounter.java
│ │ ├── PaymentService3.java
│ │ ├── RateDiscounter.java
│ │ ├── SimpleDiscounterFactory.java
│ │ └── repository
│ │ │ ├── DiscounterRepository.java
│ │ │ ├── GenericMethodDiscounterRepository.java
│ │ │ └── RateDiscounterRepository.java
│ │ └── step_enum
│ │ ├── DiscountPolicy.java
│ │ └── PaymentServiceEnum.java
└── resources
│ └── application.properties
└── test
└── java
└── io
└── redutan
└── antioop
├── AntiOopApplicationTests.java
└── noif
├── step0
└── PaymentServiceTest.java
├── step1
└── PaymentService1Test.java
├── step2
└── PaymentService2Test.java
├── step2_2
└── PaymentService2_2Test.java
├── step3
├── PaymentService3Test.java
└── repository
│ ├── DiscounterRepositoryTest.java
│ ├── GenericMethodDiscounterRepositoryTest.java
│ ├── GenericRateDiscounterRepositoryTest.java
│ └── RateDiscounterRepositoryTest.java
└── step_enum
└── PaymentServiceEnumTest.java
/.gitignore:
--------------------------------------------------------------------------------
1 |
2 | # Created by https://www.gitignore.io/api/java,eclipse,intellij,maven,gradle
3 |
4 | ### Java ###
5 | *.class
6 |
7 | # Mobile Tools for Java (J2ME)
8 | .mtj.tmp/
9 |
10 | # Package Files #
11 | *.jar
12 | *.war
13 | *.ear
14 |
15 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
16 | hs_err_pid*
17 |
18 |
19 | ### Eclipse ###
20 |
21 | .metadata
22 | bin/
23 | tmp/
24 | *.tmp
25 | *.bak
26 | /lib
27 | *.swp
28 | *~.nib
29 | local.properties
30 | .settings/
31 | .loadpath
32 | .recommenders
33 |
34 | # Eclipse Core
35 | .project
36 |
37 | # External tool builders
38 | .externalToolBuilders/
39 |
40 | # Locally stored "Eclipse launch configurations"
41 | *.launch
42 |
43 | # PyDev specific (Python IDE for Eclipse)
44 | *.pydevproject
45 |
46 | # CDT-specific (C/C++ Development Tooling)
47 | .cproject
48 |
49 | # JDT-specific (Eclipse Java Development Tools)
50 | .classpath
51 |
52 | # Java annotation processor (APT)
53 | .factorypath
54 |
55 | # PDT-specific (PHP Development Tools)
56 | .buildpath
57 |
58 | # sbteclipse plugin
59 | .target
60 |
61 | # Tern plugin
62 | .tern-project
63 |
64 | # TeXlipse plugin
65 | .texlipse
66 |
67 | # STS (Spring Tool Suite)
68 | .springBeans
69 |
70 | # Code Recommenders
71 | .recommenders/
72 |
73 |
74 | ### Intellij ###
75 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
76 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
77 |
78 | # User-specific stuff:
79 | .idea/workspace.xml
80 | .idea/tasks.xml
81 | .idea/dictionaries
82 | .idea/vcs.xml
83 | .idea/jsLibraryMappings.xml
84 |
85 | # Sensitive or high-churn files:
86 | .idea/dataSources.ids
87 | .idea/dataSources.xml
88 | .idea/dataSources.local.xml
89 | .idea/sqlDataSources.xml
90 | .idea/dynamic.xml
91 | .idea/uiDesigner.xml
92 |
93 | # Gradle:
94 | .idea/gradle.xml
95 | .idea/libraries
96 |
97 | # Mongo Explorer plugin:
98 | .idea/mongoSettings.xml
99 |
100 | ## File-based project format:
101 | *.iws
102 |
103 | ## Plugin-specific files:
104 |
105 | # IntelliJ
106 | /.idea
107 | /.mvn
108 | /mvnw.cmd
109 | /mvnw
110 | /out/
111 |
112 | # mpeltonen/sbt-idea plugin
113 | .idea_modules/
114 |
115 | # JIRA plugin
116 | atlassian-ide-plugin.xml
117 |
118 | # Crashlytics plugin (for Android Studio and IntelliJ)
119 | com_crashlytics_export_strings.xml
120 | crashlytics.properties
121 | crashlytics-build.properties
122 | fabric.properties
123 |
124 | ### Intellij Patch ###
125 | *.iml
126 |
127 |
128 | ### Maven ###
129 | target/
130 | pom.xml.tag
131 | pom.xml.releaseBackup
132 | pom.xml.versionsBackup
133 | pom.xml.next
134 | release.properties
135 | dependency-reduced-pom.xml
136 | buildNumber.properties
137 | .mvn/timing.properties
138 |
139 |
140 | ### Gradle ###
141 | .gradle
142 | build/
143 |
144 | # Ignore Gradle GUI config
145 | gradle-app.setting
146 |
147 | # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
148 | !gradle-wrapper.jar
149 |
150 | # Cache of project
151 | .gradletasknamecache
152 |
153 | # # Work around https://youtrack.jetbrains.com/issue/IDEA-116898
154 | # gradle/wrapper/gradle-wrapper.properties
155 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # anti-oop
2 |
3 | 안티-OOP에 대한 샘플코드
4 |
5 | Article
6 | * http://meetup.toast.com/posts/94
7 | * http://redutan.github.io/2016/03/31/anti-oop-if
8 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | io.redutan
7 | anti-oop
8 | 0.0.1-SNAPSHOT
9 | jar
10 |
11 | Anti-OOP
12 | Anti-OOP Sample project
13 |
14 |
15 | org.springframework.boot
16 | spring-boot-starter-parent
17 | 1.3.3.RELEASE
18 |
19 |
20 |
21 |
22 | UTF-8
23 | 1.8
24 |
25 |
26 |
27 |
28 | org.springframework.boot
29 | spring-boot-starter-data-jpa
30 |
31 |
32 | org.projectlombok
33 | lombok
34 | 1.16.6
35 |
36 |
37 |
38 | com.h2database
39 | h2
40 | runtime
41 |
42 |
43 | org.springframework.boot
44 | spring-boot-starter-test
45 | test
46 |
47 |
48 |
49 |
50 |
51 |
52 | org.springframework.boot
53 | spring-boot-maven-plugin
54 |
55 |
56 |
57 |
58 |
59 |
60 |
--------------------------------------------------------------------------------
/src/main/java/io/redutan/antioop/AntiOopApplication.java:
--------------------------------------------------------------------------------
1 | package io.redutan.antioop;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class AntiOopApplication {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(AntiOopApplication.class, args);
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/src/main/java/io/redutan/antioop/noif/Discount.java:
--------------------------------------------------------------------------------
1 | package io.redutan.antioop.noif;
2 |
3 | import lombok.Value;
4 |
5 | /**
6 | * 할인VO
7 | * Created by redutan on 2016. 4. 1..
8 | */
9 | @Value(staticConstructor = "of")
10 | public class Discount {
11 | /** 할인금액 */
12 | private long discountAmt;
13 | }
14 |
--------------------------------------------------------------------------------
/src/main/java/io/redutan/antioop/noif/DiscountRequest.java:
--------------------------------------------------------------------------------
1 | package io.redutan.antioop.noif;
2 |
3 | import lombok.Value;
4 |
5 | /**
6 | * 할인 조회 파라메터 DTO
7 | * Created by redutan on 2016. 4. 1..
8 | */
9 | @Value
10 | public class DiscountRequest {
11 | /** 할인코드 (NAVER:네이버검색-10%, DANAWA:다나와검색-15% FANCAFE:팬카페-1000원) */
12 | private String discountCode;
13 | /** 상품금액 */
14 | private long productAmt;
15 | }
16 |
--------------------------------------------------------------------------------
/src/main/java/io/redutan/antioop/noif/Discountable.java:
--------------------------------------------------------------------------------
1 | package io.redutan.antioop.noif;
2 |
3 | /**
4 | * 할인 핵심 인터페이스
5 | * Created by redutan on 2016. 4. 1..
6 | */
7 | public interface Discountable {
8 | /** 할인없음 */
9 | Discountable NONE = (originAmt) -> 0;
10 |
11 | /** 할인금액 반환 */
12 | long getDiscountAmt(long originAmt);
13 | }
14 |
--------------------------------------------------------------------------------
/src/main/java/io/redutan/antioop/noif/DiscounterFactory.java:
--------------------------------------------------------------------------------
1 | package io.redutan.antioop.noif;
2 |
3 | /**
4 | * 할인 생성 팩토리
5 | * Created by redutan on 2016. 4. 1..
6 | */
7 | public interface DiscounterFactory {
8 | /** 할인 생성 */
9 | Discountable getDiscounter(String discountCode);
10 | }
11 |
--------------------------------------------------------------------------------
/src/main/java/io/redutan/antioop/noif/PaymentRequest.java:
--------------------------------------------------------------------------------
1 | package io.redutan.antioop.noif;
2 |
3 | import lombok.Value;
4 |
5 | /**
6 | * 결제 요청 파라메터 DTO
7 | * Created by redutan on 2016. 4. 1..
8 | */
9 | @Value
10 | public class PaymentRequest {
11 | /** 할인코드 (NAVER:네이버검색-10%, DANAWA:다나와검색-15% FANCAFE:팬카페-1000원) */
12 | private String discountCode;
13 | /** 상품금액 */
14 | private long productAmt;
15 | }
16 |
--------------------------------------------------------------------------------
/src/main/java/io/redutan/antioop/noif/PaymentService.java:
--------------------------------------------------------------------------------
1 | package io.redutan.antioop.noif;
2 |
3 | /**
4 | * 결제 서비스 인터페이스
5 | * @author myeongju.jung
6 | */
7 | public interface PaymentService {
8 | // 실시간 할인내역 확인
9 | Discount getDiscount(DiscountRequest request);
10 |
11 | // 결제처리
12 | void payment(PaymentRequest request);
13 | }
14 |
--------------------------------------------------------------------------------
/src/main/java/io/redutan/antioop/noif/step0/LegacyPaymentService.java:
--------------------------------------------------------------------------------
1 | package io.redutan.antioop.noif.step0;
2 |
3 | import io.redutan.antioop.noif.Discount;
4 | import io.redutan.antioop.noif.DiscountRequest;
5 | import io.redutan.antioop.noif.PaymentRequest;
6 | import io.redutan.antioop.noif.PaymentService;
7 |
8 | /**
9 | * Created by redutan on 2016. 4. 1..
10 | */
11 | public class LegacyPaymentService implements PaymentService {
12 | // 실시간 할인내역 확인
13 | @Override
14 | public Discount getDiscount(DiscountRequest request) {
15 | // 상품금액
16 | long productAmt = request.getProductAmt();
17 | // 할인코드 (NAVER:네이버검색-10%, DANAWA:다나와검색-15% FANCAFE:팬카페-1000원)
18 | String discountCode = request.getDiscountCode();
19 |
20 | // 할인금액
21 | long discountAmt = 0;
22 | if ("NAVER".equals(discountCode)) { // 네이버검색 할인
23 | discountAmt = (long)(productAmt * 0.1);
24 | } else if ("DANAWA".equals(discountCode)) { // 다나와검색 할인
25 | discountAmt = (long)(productAmt * 0.15);
26 | } else if ("FANCAFE".equals(discountCode)) { // 팬카페인입 할인
27 | if (productAmt < 1000) // 할인쿠폰 금액보다 적은경우
28 | discountAmt = productAmt;
29 | else
30 | discountAmt = 1000;
31 | }
32 | return Discount.of(discountAmt);
33 | }
34 |
35 | // 결제처리
36 | @Override
37 | public void payment(PaymentRequest request) {
38 | // 상품금액
39 | long productAmt = request.getProductAmt();
40 | // 할인코드 (NAVER:네이버검색-10%, DANAWA:다나와검색-15% FANCAFE:팬카페-1000원)
41 | String discountCode = request.getDiscountCode();
42 |
43 | // 결제금액
44 | long paymentAmt = 0;
45 | if ("NAVER".equals(discountCode)) { // 네이버검색 할인
46 | paymentAmt = (long)(productAmt * 0.9);
47 | } else if ("DANAWA".equals(discountCode)) { // 다나와검색 할인
48 | paymentAmt = (long)(productAmt * 0.85);
49 | } else if ("FANCAFE".equals(discountCode)) { // 팬카페인입 할인
50 | if (productAmt < 1000) // 할인쿠폰 금액보다 적은경우
51 | paymentAmt = 0;
52 | else
53 | paymentAmt = productAmt - 1000;
54 | } else {
55 | paymentAmt = productAmt;
56 | }
57 | // TODO Something...
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/src/main/java/io/redutan/antioop/noif/step1/PaymentService1.java:
--------------------------------------------------------------------------------
1 | package io.redutan.antioop.noif.step1;
2 |
3 | import io.redutan.antioop.noif.Discount;
4 | import io.redutan.antioop.noif.DiscountRequest;
5 | import io.redutan.antioop.noif.PaymentRequest;
6 | import io.redutan.antioop.noif.PaymentService;
7 |
8 | /**
9 | * Created by redutan on 2016. 4. 1..
10 | */
11 | public class PaymentService1 implements PaymentService {
12 | @Override
13 | // 실시간 할인내역 확인
14 | public Discount getDiscount(DiscountRequest request) {
15 | // 상품금액
16 | long productAmt = request.getProductAmt();
17 | // 할인코드 (NAVER:네이버검색-10%, DANAWA:다나와검색-15% FANCAFE:팬카페-1000원)
18 | String discountCode = request.getDiscountCode();
19 |
20 | // 할인금액
21 | long discountAmt = getDiscountAmt(discountCode, productAmt);
22 | return Discount.of(discountAmt);
23 | }
24 |
25 | @Override
26 | // 결제처리
27 | public void payment(PaymentRequest request) {
28 | // 상품금액
29 | long productAmt = request.getProductAmt();
30 | // 할인코드 (NAVER:네이버검색-10%, DANAWA:다나와검색-15% FANCAFE:팬카페-1000원)
31 | String discountCode = request.getDiscountCode();
32 |
33 | // 결제금액
34 | long paymentAmt = productAmt - getDiscountAmt(discountCode, productAmt);
35 | // TODO Something...
36 | }
37 |
38 | private long getDiscountAmt(String discountCode, long productAmt) {
39 | long discountAmt = 0;
40 | if ("NAVER".equals(discountCode)) { // 네이버검색 할인
41 | discountAmt = (long)(productAmt * 0.1);
42 | } else if ("DANAWA".equals(discountCode)) { // 다나와검색 할인
43 | discountAmt = (long)(productAmt * 0.15);
44 | } else if ("FANCAFE".equals(discountCode)) { // 팬카페인입 할인
45 | if (productAmt < 1000) // 할인쿠폰 금액보다 적은경우
46 | discountAmt = productAmt;
47 | else
48 | discountAmt = 1000;
49 | }
50 | return discountAmt;
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/src/main/java/io/redutan/antioop/noif/step2/DanawaDiscountPolicy.java:
--------------------------------------------------------------------------------
1 | package io.redutan.antioop.noif.step2;
2 |
3 | import io.redutan.antioop.noif.Discountable;
4 |
5 | /**
6 | * Created by redutan on 2016. 4. 1..
7 | */
8 | class DanawaDiscountPolicy implements Discountable {
9 | @Override
10 | public long getDiscountAmt(long originAmt) {
11 | return (long)(originAmt * 0.15);
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/main/java/io/redutan/antioop/noif/step2/FancafeDiscountPolicy.java:
--------------------------------------------------------------------------------
1 | package io.redutan.antioop.noif.step2;
2 |
3 | import io.redutan.antioop.noif.Discountable;
4 |
5 | /**
6 | * Created by redutan on 2016. 4. 1..
7 | */
8 | class FancafeDiscountPolicy implements Discountable {
9 | private long discountAmt = 1000L;
10 |
11 | @Override
12 | public long getDiscountAmt(long originAmt) {
13 | if (originAmt < discountAmt)
14 | return originAmt;
15 | return discountAmt;
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/main/java/io/redutan/antioop/noif/step2/NaverDiscountPolicy.java:
--------------------------------------------------------------------------------
1 | package io.redutan.antioop.noif.step2;
2 |
3 | import io.redutan.antioop.noif.Discountable;
4 |
5 | /**
6 | * Created by redutan on 2016. 4. 1..
7 | */
8 | class NaverDiscountPolicy implements Discountable {
9 | @Override
10 | public long getDiscountAmt(long originAmt) {
11 | return (long)(originAmt * 0.1);
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/main/java/io/redutan/antioop/noif/step2/PaymentService2.java:
--------------------------------------------------------------------------------
1 | package io.redutan.antioop.noif.step2;
2 |
3 | import io.redutan.antioop.noif.*;
4 |
5 | /**
6 | * Created by redutan on 2016. 4. 1..
7 | */
8 | public class PaymentService2 implements PaymentService {
9 | // 실시간 할인내역 확인
10 | @Override
11 | public Discount getDiscount(DiscountRequest request) {
12 | // 상품금액
13 | long productAmt = request.getProductAmt();
14 | // 할인코드 (NAVER:네이버검색-10%, DANAWA:다나와검색-15% FANCAFE:팬카페-1000원)
15 | String discountCode = request.getDiscountCode();
16 | // 할인정책
17 | Discountable discountPolicy = getDiscounter(discountCode);
18 |
19 | // 할인금액
20 | long discountAmt = discountPolicy.getDiscountAmt(productAmt);
21 | return Discount.of(discountAmt);
22 | }
23 |
24 | // 결제처리
25 | @Override
26 | public void payment(PaymentRequest request) {
27 | // 상품금액
28 | long productAmt = request.getProductAmt();
29 | // 할인코드 (NAVER:네이버검색-10%, DANAWA:다나와검색-15% FANCAFE:팬카페-1000원)
30 | String discountCode = request.getDiscountCode();
31 | // 할인정책
32 | Discountable discountPolicy = getDiscounter(discountCode);
33 |
34 | // 결제금액
35 | long paymentAmt = productAmt - discountPolicy.getDiscountAmt(productAmt);
36 | // TODO Something...
37 | }
38 |
39 | // 팩토리 메서드
40 | private Discountable getDiscounter(String discountCode) {
41 | if ("NAVER".equals(discountCode)) { // 네이버검색 할인
42 | return new NaverDiscountPolicy();
43 | } else if ("DANAWA".equals(discountCode)) { // 다나와검색 할인
44 | return new DanawaDiscountPolicy();
45 | } else if ("FANCAFE".equals(discountCode)) { // 팬카페 할인
46 | return new FancafeDiscountPolicy();
47 | } else {
48 | return Discountable.NONE;
49 | }
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/src/main/java/io/redutan/antioop/noif/step2_2/DanawaDiscountPolicy.java:
--------------------------------------------------------------------------------
1 | package io.redutan.antioop.noif.step2_2;
2 |
3 | import io.redutan.antioop.noif.Discountable;
4 |
5 | /**
6 | * Created by redutan on 2016. 4. 1..
7 | */
8 | class DanawaDiscountPolicy implements Discountable {
9 | @Override
10 | public long getDiscountAmt(long originAmt) {
11 | return (long)(originAmt * 0.15);
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/main/java/io/redutan/antioop/noif/step2_2/FancafeDiscountPolicy.java:
--------------------------------------------------------------------------------
1 | package io.redutan.antioop.noif.step2_2;
2 |
3 | import io.redutan.antioop.noif.Discountable;
4 |
5 | /**
6 | * Created by redutan on 2016. 4. 1..
7 | */
8 | class FancafeDiscountPolicy implements Discountable {
9 | private long discountAmt = 1000L;
10 |
11 | @Override
12 | public long getDiscountAmt(long originAmt) {
13 | if (originAmt < discountAmt)
14 | return originAmt;
15 | return discountAmt;
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/main/java/io/redutan/antioop/noif/step2_2/NaverDiscountPolicy.java:
--------------------------------------------------------------------------------
1 | package io.redutan.antioop.noif.step2_2;
2 |
3 | import io.redutan.antioop.noif.Discountable;
4 |
5 | /**
6 | * Created by redutan on 2016. 4. 1..
7 | */
8 | class NaverDiscountPolicy implements Discountable {
9 | @Override
10 | public long getDiscountAmt(long originAmt) {
11 | return (long)(originAmt * 0.1);
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/main/java/io/redutan/antioop/noif/step2_2/PaymentService2_2.java:
--------------------------------------------------------------------------------
1 | package io.redutan.antioop.noif.step2_2;
2 |
3 | import io.redutan.antioop.noif.*;
4 |
5 | /**
6 | * Created by redutan on 2016. 4. 1..
7 | */
8 | public class PaymentService2_2 implements PaymentService {
9 |
10 | private DiscounterFactory discounterFactory;
11 |
12 | public PaymentService2_2(DiscounterFactory discounterFactory) {
13 | this.discounterFactory = discounterFactory;
14 | }
15 |
16 | // 실시간 할인내역 확인
17 | @Override
18 | public Discount getDiscount(DiscountRequest request) {
19 | // 상품금액
20 | long productAmt = request.getProductAmt();
21 | // 할인코드 (NAVER:네이버검색-10%, DANAWA:다나와검색-15% FANCAFE:팬카페-1000원)
22 | String discountCode = request.getDiscountCode();
23 | // 할인정책
24 | Discountable discountPolicy = getDiscounter(discountCode);
25 |
26 | // 할인금액
27 | long discountAmt = discountPolicy.getDiscountAmt(productAmt);
28 | return Discount.of(discountAmt);
29 | }
30 |
31 | // 결제처리
32 | @Override
33 | public void payment(PaymentRequest request) {
34 | // 상품금액
35 | long productAmt = request.getProductAmt();
36 | // 할인코드 (NAVER:네이버검색-10%, DANAWA:다나와검색-15% FANCAFE:팬카페-1000원)
37 | String discountCode = request.getDiscountCode();
38 | // 할인정책
39 | Discountable discountPolicy = getDiscounter(discountCode);
40 |
41 | // 결제금액
42 | long paymentAmt = productAmt - discountPolicy.getDiscountAmt(productAmt);
43 | // TODO Something...
44 | }
45 |
46 | // 팩토리 메서드
47 | private Discountable getDiscounter(String discountCode) {
48 | if ("NAVER".equals(discountCode)) { // 네이버검색 할인
49 | return new NaverDiscountPolicy();
50 | } else if ("DANAWA".equals(discountCode)) { // 다나와검색 할인
51 | return new DanawaDiscountPolicy();
52 | } else if ("FANCAFE".equals(discountCode)) { // 팬카페 할인
53 | return new FancafeDiscountPolicy();
54 | } else {
55 | return Discountable.NONE;
56 | }
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/src/main/java/io/redutan/antioop/noif/step2_2/SimpleDiscounterFactory.java:
--------------------------------------------------------------------------------
1 | package io.redutan.antioop.noif.step2_2;
2 |
3 | import io.redutan.antioop.noif.Discountable;
4 | import io.redutan.antioop.noif.DiscounterFactory;
5 |
6 | /**
7 | * Created by redutan on 2016. 4. 1..
8 | */
9 | class SimpleDiscounterFactory implements DiscounterFactory {
10 | @Override
11 | public Discountable getDiscounter(String discountCode) {
12 | if ("NAVER".equals(discountCode)) { // 네이버검색 할인
13 | return new NaverDiscountPolicy();
14 | } else if ("DANAWA".equals(discountCode)) { // 다나와검색 할인
15 | return new DanawaDiscountPolicy();
16 | } else if ("FANCAFE".equals(discountCode)) { // 팬카페 할인
17 | return new FancafeDiscountPolicy();
18 | } else {
19 | return Discountable.NONE;
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/io/redutan/antioop/noif/step3/AbstractDiscounter.java:
--------------------------------------------------------------------------------
1 | package io.redutan.antioop.noif.step3;
2 |
3 | import io.redutan.antioop.noif.Discountable;
4 | import lombok.Data;
5 |
6 | import javax.persistence.*;
7 |
8 | /**
9 | * Created by redutan on 2016. 4. 1..
10 | */
11 | @Data
12 | @Entity
13 | @Inheritance
14 | public abstract class AbstractDiscounter implements Discountable {
15 | /** 아이디 */
16 | @Id
17 | @GeneratedValue
18 | @Column
19 | private long id;
20 | /** 할인코드 */
21 | @Column
22 | private String code;
23 | /** 할인명 */
24 | @Column
25 | private String name;
26 | }
27 |
--------------------------------------------------------------------------------
/src/main/java/io/redutan/antioop/noif/step3/AmtDiscounter.java:
--------------------------------------------------------------------------------
1 | package io.redutan.antioop.noif.step3;
2 |
3 | import lombok.Data;
4 | import lombok.EqualsAndHashCode;
5 | import lombok.ToString;
6 |
7 | import javax.persistence.Column;
8 | import javax.persistence.DiscriminatorValue;
9 | import javax.persistence.Entity;
10 |
11 | /**
12 | * Created by redutan on 2016. 4. 1..
13 | */
14 | @Data
15 | @ToString(callSuper = true)
16 | @EqualsAndHashCode(callSuper = true)
17 | @Entity
18 | @DiscriminatorValue("AMT")
19 | public class AmtDiscounter extends AbstractDiscounter {
20 | /** 할인금액 */
21 | @Column
22 | private long amt;
23 |
24 | @Override
25 | public long getDiscountAmt(long originAmt) {
26 | if (originAmt < amt)
27 | return originAmt;
28 | return amt;
29 | }
30 | }
31 |
32 |
--------------------------------------------------------------------------------
/src/main/java/io/redutan/antioop/noif/step3/PaymentService3.java:
--------------------------------------------------------------------------------
1 | package io.redutan.antioop.noif.step3;
2 |
3 | import io.redutan.antioop.noif.*;
4 | import org.springframework.beans.factory.annotation.Autowired;
5 | import org.springframework.stereotype.Service;
6 |
7 | /**
8 | * Created by redutan on 2016. 4. 1..
9 | */
10 | @Service
11 | public class PaymentService3 implements PaymentService {
12 |
13 | @Autowired
14 | private DiscounterFactory discounterFactory;
15 |
16 | // 실시간 할인내역 확인
17 | @Override
18 | public Discount getDiscount(DiscountRequest request) {
19 | // 상품금액
20 | long productAmt = request.getProductAmt();
21 | // 할인코드 (NAVER:네이버검색-10%, DANAWA:다나와검색-15% FANCAFE:팬카페-1000원)
22 | String discountCode = request.getDiscountCode();
23 | // 할인정책
24 | Discountable discountPolicy = getDiscounter(discountCode);
25 |
26 | // 할인금액
27 | long discountAmt = discountPolicy.getDiscountAmt(productAmt);
28 | return Discount.of(discountAmt);
29 | }
30 |
31 | // 결제처리
32 | @Override
33 | public void payment(PaymentRequest request) {
34 | // 상품금액
35 | long productAmt = request.getProductAmt();
36 | // 할인코드 (NAVER:네이버검색-10%, DANAWA:다나와검색-15% FANCAFE:팬카페-1000원)
37 | String discountCode = request.getDiscountCode();
38 | // 할인정책
39 | Discountable discountPolicy = getDiscounter(discountCode);
40 |
41 | // 결제금액
42 | long paymentAmt = productAmt - discountPolicy.getDiscountAmt(productAmt);
43 | // TODO Something...
44 | }
45 |
46 | // 팩토리 메서드
47 | private Discountable getDiscounter(String discountCode) {
48 | return discounterFactory.getDiscounter(discountCode);
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/src/main/java/io/redutan/antioop/noif/step3/RateDiscounter.java:
--------------------------------------------------------------------------------
1 | package io.redutan.antioop.noif.step3;
2 |
3 | import lombok.Data;
4 | import lombok.EqualsAndHashCode;
5 | import lombok.ToString;
6 |
7 | import javax.persistence.Column;
8 | import javax.persistence.DiscriminatorValue;
9 | import javax.persistence.Entity;
10 |
11 | /**
12 | * Created by redutan on 2016. 4. 1..
13 | */
14 | @Data
15 | @ToString(callSuper = true)
16 | @EqualsAndHashCode(callSuper = true)
17 | @Entity
18 | @DiscriminatorValue("RATE")
19 | public class RateDiscounter extends AbstractDiscounter {
20 | /** 할인율 */
21 | @Column
22 | private int rate;
23 |
24 | @Override
25 | public long getDiscountAmt(long originAmt) {
26 | return originAmt * rate / 100;
27 | }
28 | }
29 |
30 |
--------------------------------------------------------------------------------
/src/main/java/io/redutan/antioop/noif/step3/SimpleDiscounterFactory.java:
--------------------------------------------------------------------------------
1 | package io.redutan.antioop.noif.step3;
2 |
3 | import io.redutan.antioop.noif.Discountable;
4 | import io.redutan.antioop.noif.DiscounterFactory;
5 | import io.redutan.antioop.noif.step3.repository.DiscounterRepository;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.stereotype.Component;
8 |
9 | /**
10 | * Created by redutan on 2016. 4. 1..
11 | */
12 | @Component
13 | class SimpleDiscounterFactory implements DiscounterFactory {
14 | @Autowired
15 | private DiscounterRepository discounterRepository;
16 |
17 | @Override
18 | public Discountable getDiscounter(String discountCode) {
19 | if (discountCode == null)
20 | return Discountable.NONE;
21 | AbstractDiscounter discounter = discounterRepository.findByCode(discountCode);
22 | return discounter == null ? Discountable.NONE : discounter;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/io/redutan/antioop/noif/step3/repository/DiscounterRepository.java:
--------------------------------------------------------------------------------
1 | package io.redutan.antioop.noif.step3.repository;
2 |
3 | import io.redutan.antioop.noif.step3.AbstractDiscounter;
4 | import org.springframework.data.jpa.repository.JpaRepository;
5 | import org.springframework.stereotype.Repository;
6 |
7 | /**
8 | * Created by redutan on 2016. 4. 1..
9 | */
10 | @Repository
11 | public interface DiscounterRepository extends JpaRepository {
12 | /**
13 | * 할인코드로 할인 조회
14 | * @param code 할인코드
15 | * @return
16 | */
17 | T findByCode(String code);
18 | }
19 |
--------------------------------------------------------------------------------
/src/main/java/io/redutan/antioop/noif/step3/repository/GenericMethodDiscounterRepository.java:
--------------------------------------------------------------------------------
1 | package io.redutan.antioop.noif.step3.repository;
2 |
3 | import io.redutan.antioop.noif.step3.AbstractDiscounter;
4 | import org.springframework.data.jpa.repository.JpaRepository;
5 | import org.springframework.stereotype.Repository;
6 |
7 | /**
8 | * Created by redutan on 2016. 4. 1..
9 | */
10 | @Repository
11 | public interface GenericMethodDiscounterRepository extends JpaRepository {
12 | /**
13 | * 할인코드로 할인 조회
14 | * @param code 할인코드
15 | * @return
16 | */
17 | T findByCode(String code);
18 | }
19 |
--------------------------------------------------------------------------------
/src/main/java/io/redutan/antioop/noif/step3/repository/RateDiscounterRepository.java:
--------------------------------------------------------------------------------
1 | package io.redutan.antioop.noif.step3.repository;
2 |
3 | import io.redutan.antioop.noif.step3.AbstractDiscounter;
4 | import io.redutan.antioop.noif.step3.RateDiscounter;
5 | import org.springframework.data.jpa.repository.JpaRepository;
6 | import org.springframework.stereotype.Repository;
7 |
8 | /**
9 | * Created by redutan on 2016. 4. 1..
10 | */
11 | @Repository
12 | public interface RateDiscounterRepository extends JpaRepository {
13 | /**
14 | * 할인코드로 할인 조회
15 | * @param code 할인코드
16 | * @return
17 | */
18 | RateDiscounter findByCode(String code);
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/io/redutan/antioop/noif/step_enum/DiscountPolicy.java:
--------------------------------------------------------------------------------
1 | package io.redutan.antioop.noif.step_enum;
2 |
3 | import io.redutan.antioop.noif.Discountable;
4 |
5 | /**
6 | * Created by redutan on 2016. 4. 1..
7 | */
8 | public enum DiscountPolicy implements Discountable {
9 | /** 네이버 할인 */
10 | NAVER(10, 0L) {
11 | @Override
12 | public long getDiscountAmt(long originAmt) {
13 | return originAmt * this.discountRate / 100;
14 | }
15 | },
16 | /** 다나와 할인 */
17 | DANAWA(15, 0L) {
18 | @Override
19 | public long getDiscountAmt(long originAmt) {
20 | return originAmt * this.discountRate / 100;
21 | }
22 | },
23 | /** 팬카페 할인 */
24 | FANCAFE(0, 1000L) {
25 | @Override
26 | public long getDiscountAmt(long originAmt) {
27 | if (originAmt < this.discountAmt)
28 | return originAmt;
29 | return this.discountAmt;
30 | }
31 | }
32 | ;
33 | final int discountRate;
34 | final long discountAmt;
35 |
36 | DiscountPolicy(int discountRate, long discountAmt) {
37 | this.discountRate = discountRate;
38 | this.discountAmt = discountAmt;
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/main/java/io/redutan/antioop/noif/step_enum/PaymentServiceEnum.java:
--------------------------------------------------------------------------------
1 | package io.redutan.antioop.noif.step_enum;
2 |
3 | import io.redutan.antioop.noif.*;
4 | import lombok.extern.slf4j.Slf4j;
5 |
6 | /**
7 | * Created by redutan on 2016. 4. 1..
8 | */
9 | @Slf4j
10 | public class PaymentServiceEnum implements PaymentService {
11 | // 실시간 할인내역 확인
12 | @Override
13 | public Discount getDiscount(DiscountRequest request) {
14 | // 상품금액
15 | long productAmt = request.getProductAmt();
16 | // 할인코드 (NAVER:네이버검색-10%, DANAWA:다나와검색-15% FANCAFE:팬카페-1000원)
17 | String discountCode = request.getDiscountCode();
18 | // 할인정책
19 | Discountable discountPolicy = getDiscounter(discountCode);
20 |
21 | // 할인금액
22 | long discountAmt = discountPolicy.getDiscountAmt(productAmt);
23 | return Discount.of(discountAmt);
24 | }
25 |
26 | // 결제처리
27 | @Override
28 | public void payment(PaymentRequest request) {
29 | // 상품금액
30 | long productAmt = request.getProductAmt();
31 | // 할인코드 (NAVER:네이버검색-10%, DANAWA:다나와검색-15% FANCAFE:팬카페-1000원)
32 | String discountCode = request.getDiscountCode();
33 | // 할인정책
34 | Discountable discountPolicy = getDiscounter(discountCode);
35 |
36 | // 결제금액
37 | long paymentAmt = productAmt - discountPolicy.getDiscountAmt(productAmt);
38 | // TODO Something...
39 | }
40 |
41 | // 팩토리 메서드
42 | private Discountable getDiscounter(String discountCode) {
43 | if (discountCode == null)
44 | return Discountable.NONE;
45 | try {
46 | return DiscountPolicy.valueOf(discountCode);
47 | } catch (IllegalArgumentException iae) {
48 | log.warn("Not found discountCode : {}", discountCode);
49 | return Discountable.NONE;
50 | }
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/src/main/resources/application.properties:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/redutan/anti-oop/60023bebbe1dd4bea993d72123c10f6d7131ab64/src/main/resources/application.properties
--------------------------------------------------------------------------------
/src/test/java/io/redutan/antioop/AntiOopApplicationTests.java:
--------------------------------------------------------------------------------
1 | package io.redutan.antioop;
2 |
3 | import org.junit.Test;
4 | import org.junit.runner.RunWith;
5 | import org.springframework.boot.test.SpringApplicationConfiguration;
6 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
7 |
8 | @RunWith(SpringJUnit4ClassRunner.class)
9 | @SpringApplicationConfiguration(classes = AntiOopApplication.class)
10 | public class AntiOopApplicationTests {
11 |
12 | @Test
13 | public void contextLoads() {
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/src/test/java/io/redutan/antioop/noif/step0/PaymentServiceTest.java:
--------------------------------------------------------------------------------
1 | package io.redutan.antioop.noif.step0;
2 |
3 | import io.redutan.antioop.noif.Discount;
4 | import io.redutan.antioop.noif.DiscountRequest;
5 | import org.junit.Before;
6 | import org.junit.Test;
7 |
8 | import static org.hamcrest.CoreMatchers.is;
9 | import static org.junit.Assert.assertThat;
10 |
11 | /**
12 | * Created by redutan on 2016. 4. 1..
13 | */
14 | public class PaymentServiceTest {
15 | LegacyPaymentService service;
16 |
17 | @Before
18 | public void setUp() throws Exception {
19 | service = new LegacyPaymentService();
20 | }
21 |
22 | @Test
23 | public void getDiscount_Naver() throws Exception {
24 | // Given
25 | DiscountRequest request1 = new DiscountRequest("NAVER", 20000);
26 | // When
27 | Discount discount1 = service.getDiscount(request1);
28 | // Then
29 | assertThat(discount1.getDiscountAmt(), is(2000L));
30 | }
31 |
32 | @Test
33 | public void getDiscount_Danawa() throws Exception {
34 | // Given
35 | DiscountRequest request1 = new DiscountRequest("DANAWA", 20000);
36 | // When
37 | Discount discount1 = service.getDiscount(request1);
38 | // Then
39 | assertThat(discount1.getDiscountAmt(), is(3000L));
40 | }
41 |
42 | @Test
43 | public void getDiscount_Fancafe() throws Exception {
44 | // Given
45 | DiscountRequest request1 = new DiscountRequest("FANCAFE", 20000);
46 | // When
47 | Discount discount1 = service.getDiscount(request1);
48 | // Then
49 | assertThat(discount1.getDiscountAmt(), is(1000L));
50 | }
51 |
52 | @Test
53 | public void getDiscount_FancafeLessThenAmt() throws Exception {
54 | // Given
55 | DiscountRequest request1 = new DiscountRequest("FANCAFE", 500L);
56 | // When
57 | Discount discount1 = service.getDiscount(request1);
58 | // Then
59 | assertThat(discount1.getDiscountAmt(), is(500L));
60 | }
61 |
62 | @Test
63 | public void getDiscount_EmptyAndNull() throws Exception {
64 | // Given
65 | DiscountRequest request1 = new DiscountRequest("", 10000);
66 | // When empty
67 | Discount discount1 = service.getDiscount(request1);
68 | // Then
69 | assertThat(discount1.getDiscountAmt(), is(0L));
70 |
71 | // Given
72 | DiscountRequest request2 = new DiscountRequest(null, 10000);
73 | // When null
74 | Discount discount2 = service.getDiscount(request2);
75 | // Then
76 | assertThat(discount2.getDiscountAmt(), is(0L));
77 | }
78 |
79 | @Test
80 | public void getDiscount_Invalid() throws Exception {
81 | // Given
82 | DiscountRequest request1 = new DiscountRequest("ABCDEFG", 10000);
83 | // When
84 | Discount discount1 = service.getDiscount(request1);
85 | // Then
86 | assertThat(discount1.getDiscountAmt(), is(0L));
87 | }
88 | }
--------------------------------------------------------------------------------
/src/test/java/io/redutan/antioop/noif/step1/PaymentService1Test.java:
--------------------------------------------------------------------------------
1 | package io.redutan.antioop.noif.step1;
2 |
3 | import io.redutan.antioop.noif.Discount;
4 | import io.redutan.antioop.noif.DiscountRequest;
5 | import org.junit.Before;
6 | import org.junit.Test;
7 |
8 | import static org.hamcrest.CoreMatchers.is;
9 | import static org.junit.Assert.assertThat;
10 |
11 | /**
12 | * Created by redutan on 2016. 4. 1..
13 | */
14 | public class PaymentService1Test {
15 | PaymentService1 service;
16 |
17 | @Before
18 | public void setUp() throws Exception {
19 | service = new PaymentService1();
20 | }
21 |
22 | @Test
23 | public void getDiscount_Naver() throws Exception {
24 | // Given
25 | DiscountRequest request1 = new DiscountRequest("NAVER", 20000);
26 | // When
27 | Discount discount1 = service.getDiscount(request1);
28 | // Then
29 | assertThat(discount1.getDiscountAmt(), is(2000L));
30 | }
31 |
32 | @Test
33 | public void getDiscount_Danawa() throws Exception {
34 | // Given
35 | DiscountRequest request1 = new DiscountRequest("DANAWA", 20000);
36 | // When
37 | Discount discount1 = service.getDiscount(request1);
38 | // Then
39 | assertThat(discount1.getDiscountAmt(), is(3000L));
40 | }
41 |
42 | @Test
43 | public void getDiscount_Fancafe() throws Exception {
44 | // Given
45 | DiscountRequest request1 = new DiscountRequest("FANCAFE", 20000);
46 | // When
47 | Discount discount1 = service.getDiscount(request1);
48 | // Then
49 | assertThat(discount1.getDiscountAmt(), is(1000L));
50 | }
51 |
52 | @Test
53 | public void getDiscount_FancafeLessThenAmt() throws Exception {
54 | // Given
55 | DiscountRequest request1 = new DiscountRequest("FANCAFE", 500L);
56 | // When
57 | Discount discount1 = service.getDiscount(request1);
58 | // Then
59 | assertThat(discount1.getDiscountAmt(), is(500L));
60 | }
61 |
62 | @Test
63 | public void getDiscount_EmptyAndNull() throws Exception {
64 | // Given
65 | DiscountRequest request1 = new DiscountRequest("", 10000);
66 | // When empty
67 | Discount discount1 = service.getDiscount(request1);
68 | // Then
69 | assertThat(discount1.getDiscountAmt(), is(0L));
70 |
71 | // Given
72 | DiscountRequest request2 = new DiscountRequest(null, 10000);
73 | // When null
74 | Discount discount2 = service.getDiscount(request2);
75 | // Then
76 | assertThat(discount2.getDiscountAmt(), is(0L));
77 | }
78 |
79 | @Test
80 | public void getDiscount_Invalid() throws Exception {
81 | // Given
82 | DiscountRequest request1 = new DiscountRequest("ABCDEFG", 10000);
83 | // When
84 | Discount discount1 = service.getDiscount(request1);
85 | // Then
86 | assertThat(discount1.getDiscountAmt(), is(0L));
87 | }
88 | }
--------------------------------------------------------------------------------
/src/test/java/io/redutan/antioop/noif/step2/PaymentService2Test.java:
--------------------------------------------------------------------------------
1 | package io.redutan.antioop.noif.step2;
2 |
3 | import io.redutan.antioop.noif.Discount;
4 | import io.redutan.antioop.noif.DiscountRequest;
5 | import org.junit.Before;
6 | import org.junit.Test;
7 |
8 | import static org.hamcrest.CoreMatchers.is;
9 | import static org.junit.Assert.assertThat;
10 |
11 | /**
12 | * Created by redutan on 2016. 4. 1..
13 | */
14 | public class PaymentService2Test {
15 | PaymentService2 service;
16 |
17 | @Before
18 | public void setUp() throws Exception {
19 | service = new PaymentService2();
20 | }
21 |
22 | @Test
23 | public void getDiscount_Naver() throws Exception {
24 | // Given
25 | DiscountRequest request1 = new DiscountRequest("NAVER", 20000);
26 | // When
27 | Discount discount1 = service.getDiscount(request1);
28 | // Then
29 | assertThat(discount1.getDiscountAmt(), is(2000L));
30 | }
31 |
32 | @Test
33 | public void getDiscount_Danawa() throws Exception {
34 | // Given
35 | DiscountRequest request1 = new DiscountRequest("DANAWA", 20000);
36 | // When
37 | Discount discount1 = service.getDiscount(request1);
38 | // Then
39 | assertThat(discount1.getDiscountAmt(), is(3000L));
40 | }
41 |
42 | @Test
43 | public void getDiscount_Fancafe() throws Exception {
44 | // Given
45 | DiscountRequest request1 = new DiscountRequest("FANCAFE", 20000);
46 | // When
47 | Discount discount1 = service.getDiscount(request1);
48 | // Then
49 | assertThat(discount1.getDiscountAmt(), is(1000L));
50 | }
51 |
52 | @Test
53 | public void getDiscount_FancafeLessThenAmt() throws Exception {
54 | // Given
55 | DiscountRequest request1 = new DiscountRequest("FANCAFE", 500L);
56 | // When
57 | Discount discount1 = service.getDiscount(request1);
58 | // Then
59 | assertThat(discount1.getDiscountAmt(), is(500L));
60 | }
61 |
62 | @Test
63 | public void getDiscount_EmptyAndNull() throws Exception {
64 | // Given
65 | DiscountRequest request1 = new DiscountRequest("", 10000);
66 | // When empty
67 | Discount discount1 = service.getDiscount(request1);
68 | // Then
69 | assertThat(discount1.getDiscountAmt(), is(0L));
70 |
71 | // Given
72 | DiscountRequest request2 = new DiscountRequest(null, 10000);
73 | // When null
74 | Discount discount2 = service.getDiscount(request2);
75 | // Then
76 | assertThat(discount2.getDiscountAmt(), is(0L));
77 | }
78 |
79 | @Test
80 | public void getDiscount_Invalid() throws Exception {
81 | // Given
82 | DiscountRequest request1 = new DiscountRequest("ABCDEFG", 10000);
83 | // When
84 | Discount discount1 = service.getDiscount(request1);
85 | // Then
86 | assertThat(discount1.getDiscountAmt(), is(0L));
87 | }
88 | }
--------------------------------------------------------------------------------
/src/test/java/io/redutan/antioop/noif/step2_2/PaymentService2_2Test.java:
--------------------------------------------------------------------------------
1 | package io.redutan.antioop.noif.step2_2;
2 |
3 | import io.redutan.antioop.noif.Discount;
4 | import io.redutan.antioop.noif.DiscountRequest;
5 | import org.junit.Before;
6 | import org.junit.Test;
7 |
8 | import static org.hamcrest.CoreMatchers.is;
9 | import static org.junit.Assert.assertThat;
10 |
11 | /**
12 | * Created by redutan on 2016. 4. 1..
13 | */
14 | public class PaymentService2_2Test {
15 | PaymentService2_2 service;
16 |
17 | @Before
18 | public void setUp() throws Exception {
19 | service = new PaymentService2_2(new SimpleDiscounterFactory());
20 | }
21 |
22 | @Test
23 | public void getDiscount_Naver() throws Exception {
24 | // Given
25 | DiscountRequest request1 = new DiscountRequest("NAVER", 20000);
26 | // When
27 | Discount discount1 = service.getDiscount(request1);
28 | // Then
29 | assertThat(discount1.getDiscountAmt(), is(2000L));
30 | }
31 |
32 | @Test
33 | public void getDiscount_Danawa() throws Exception {
34 | // Given
35 | DiscountRequest request1 = new DiscountRequest("DANAWA", 20000);
36 | // When
37 | Discount discount1 = service.getDiscount(request1);
38 | // Then
39 | assertThat(discount1.getDiscountAmt(), is(3000L));
40 | }
41 |
42 | @Test
43 | public void getDiscount_Fancafe() throws Exception {
44 | // Given
45 | DiscountRequest request1 = new DiscountRequest("FANCAFE", 20000);
46 | // When
47 | Discount discount1 = service.getDiscount(request1);
48 | // Then
49 | assertThat(discount1.getDiscountAmt(), is(1000L));
50 | }
51 |
52 | @Test
53 | public void getDiscount_FancafeLessThenAmt() throws Exception {
54 | // Given
55 | DiscountRequest request1 = new DiscountRequest("FANCAFE", 500L);
56 | // When
57 | Discount discount1 = service.getDiscount(request1);
58 | // Then
59 | assertThat(discount1.getDiscountAmt(), is(500L));
60 | }
61 |
62 | @Test
63 | public void getDiscount_EmptyAndNull() throws Exception {
64 | // Given
65 | DiscountRequest request1 = new DiscountRequest("", 10000);
66 | // When empty
67 | Discount discount1 = service.getDiscount(request1);
68 | // Then
69 | assertThat(discount1.getDiscountAmt(), is(0L));
70 |
71 | // Given
72 | DiscountRequest request2 = new DiscountRequest(null, 10000);
73 | // When null
74 | Discount discount2 = service.getDiscount(request2);
75 | // Then
76 | assertThat(discount2.getDiscountAmt(), is(0L));
77 | }
78 |
79 | @Test
80 | public void getDiscount_Invalid() throws Exception {
81 | // Given
82 | DiscountRequest request1 = new DiscountRequest("ABCDEFG", 10000);
83 | // When
84 | Discount discount1 = service.getDiscount(request1);
85 | // Then
86 | assertThat(discount1.getDiscountAmt(), is(0L));
87 | }
88 | }
--------------------------------------------------------------------------------
/src/test/java/io/redutan/antioop/noif/step3/PaymentService3Test.java:
--------------------------------------------------------------------------------
1 | package io.redutan.antioop.noif.step3;
2 |
3 | import io.redutan.antioop.AntiOopApplication;
4 | import io.redutan.antioop.noif.Discount;
5 | import io.redutan.antioop.noif.DiscountRequest;
6 | import io.redutan.antioop.noif.step3.repository.DiscounterRepository;
7 | import org.junit.Before;
8 | import org.junit.Test;
9 | import org.junit.runner.RunWith;
10 | import org.springframework.beans.factory.annotation.Autowired;
11 | import org.springframework.boot.test.SpringApplicationConfiguration;
12 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
13 | import org.springframework.transaction.annotation.Transactional;
14 |
15 | import java.util.Arrays;
16 | import java.util.List;
17 |
18 | import static org.hamcrest.CoreMatchers.is;
19 | import static org.junit.Assert.assertThat;
20 |
21 | /**
22 | * Created by redutan on 2016. 4. 1..
23 | */
24 | @RunWith(SpringJUnit4ClassRunner.class)
25 | @SpringApplicationConfiguration(classes = AntiOopApplication.class)
26 | @Transactional
27 | public class PaymentService3Test {
28 | @Autowired
29 | PaymentService3 service;
30 |
31 | @Autowired
32 | DiscounterRepository repository;
33 |
34 | @Before
35 | public void setUp() throws Exception {
36 | RateDiscounter naverDiscounter = new RateDiscounter();
37 | naverDiscounter.setCode("NAVER");
38 | naverDiscounter.setName("네이버할인");
39 | naverDiscounter.setRate(10);
40 |
41 | RateDiscounter danawaDiscounter = new RateDiscounter();
42 | danawaDiscounter.setCode("DANAWA");
43 | danawaDiscounter.setName("다나와할인");
44 | danawaDiscounter.setRate(15);
45 |
46 | AmtDiscounter fancafeDiscounter = new AmtDiscounter();
47 | fancafeDiscounter.setCode("FANCAFE");
48 | fancafeDiscounter.setName("팬카페할인");
49 | fancafeDiscounter.setAmt(1000);
50 |
51 | List list = Arrays.asList(naverDiscounter, danawaDiscounter, fancafeDiscounter);
52 |
53 | repository.save(list);
54 | }
55 |
56 | @Test
57 | public void getDiscount_Naver() throws Exception {
58 | // Given
59 | DiscountRequest request1 = new DiscountRequest("NAVER", 20000);
60 | // When
61 | Discount discount1 = service.getDiscount(request1);
62 | // Then
63 | assertThat(discount1.getDiscountAmt(), is(2000L));
64 | }
65 |
66 | @Test
67 | public void getDiscount_Danawa() throws Exception {
68 | // Given
69 | DiscountRequest request1 = new DiscountRequest("DANAWA", 20000);
70 | // When
71 | Discount discount1 = service.getDiscount(request1);
72 | // Then
73 | assertThat(discount1.getDiscountAmt(), is(3000L));
74 | }
75 |
76 | @Test
77 | public void getDiscount_Fancafe() throws Exception {
78 | // Given
79 | DiscountRequest request1 = new DiscountRequest("FANCAFE", 20000);
80 | // When
81 | Discount discount1 = service.getDiscount(request1);
82 | // Then
83 | assertThat(discount1.getDiscountAmt(), is(1000L));
84 | }
85 |
86 | @Test
87 | public void getDiscount_FancafeLessThenAmt() throws Exception {
88 | // Given
89 | DiscountRequest request1 = new DiscountRequest("FANCAFE", 500L);
90 | // When
91 | Discount discount1 = service.getDiscount(request1);
92 | // Then
93 | assertThat(discount1.getDiscountAmt(), is(500L));
94 | }
95 |
96 | @Test
97 | public void getDiscount_EmptyAndNull() throws Exception {
98 | // Given
99 | DiscountRequest request1 = new DiscountRequest("", 10000);
100 | // When empty
101 | Discount discount1 = service.getDiscount(request1);
102 | // Then
103 | assertThat(discount1.getDiscountAmt(), is(0L));
104 |
105 | // Given
106 | DiscountRequest request2 = new DiscountRequest(null, 10000);
107 | // When null
108 | Discount discount2 = service.getDiscount(request2);
109 | // Then
110 | assertThat(discount2.getDiscountAmt(), is(0L));
111 | }
112 |
113 | @Test
114 | public void getDiscount_Invalid() throws Exception {
115 | // Given
116 | DiscountRequest request1 = new DiscountRequest("ABCDEFG", 10000);
117 | // When
118 | Discount discount1 = service.getDiscount(request1);
119 | // Then
120 | assertThat(discount1.getDiscountAmt(), is(0L));
121 | }
122 | }
--------------------------------------------------------------------------------
/src/test/java/io/redutan/antioop/noif/step3/repository/DiscounterRepositoryTest.java:
--------------------------------------------------------------------------------
1 | package io.redutan.antioop.noif.step3.repository;
2 |
3 | import io.redutan.antioop.AntiOopApplication;
4 | import io.redutan.antioop.noif.step3.AbstractDiscounter;
5 | import io.redutan.antioop.noif.step3.AmtDiscounter;
6 | import io.redutan.antioop.noif.step3.RateDiscounter;
7 | import lombok.extern.slf4j.Slf4j;
8 | import org.junit.Before;
9 | import org.junit.Test;
10 | import org.junit.runner.RunWith;
11 | import org.springframework.beans.factory.annotation.Autowired;
12 | import org.springframework.boot.test.SpringApplicationConfiguration;
13 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
14 | import org.springframework.transaction.annotation.Transactional;
15 |
16 | import java.util.Arrays;
17 | import java.util.List;
18 |
19 | import static org.hamcrest.CoreMatchers.is;
20 | import static org.junit.Assert.assertThat;
21 |
22 | /**
23 | * Created by redutan on 2016. 4. 1..
24 | */
25 | @RunWith(SpringJUnit4ClassRunner.class)
26 | @SpringApplicationConfiguration(classes = AntiOopApplication.class)
27 | @Transactional
28 | @Slf4j
29 | public class DiscounterRepositoryTest {
30 |
31 | @Autowired
32 | DiscounterRepository repository;
33 |
34 | @Before
35 | public void setUp() throws Exception {
36 | RateDiscounter naverDiscounter = new RateDiscounter();
37 | naverDiscounter.setCode("NAVER");
38 | naverDiscounter.setName("네이버할인");
39 | naverDiscounter.setRate(10);
40 |
41 | RateDiscounter danawaDiscounter = new RateDiscounter();
42 | danawaDiscounter.setCode("DANAWA");
43 | danawaDiscounter.setName("다나와할인");
44 | danawaDiscounter.setRate(15);
45 |
46 | AmtDiscounter fancafeDiscounter = new AmtDiscounter();
47 | fancafeDiscounter.setCode("FANCAFE");
48 | fancafeDiscounter.setName("팬카페할인");
49 | fancafeDiscounter.setAmt(1000);
50 |
51 | List list = Arrays.asList(naverDiscounter, danawaDiscounter, fancafeDiscounter);
52 |
53 | repository.save(list);
54 | }
55 |
56 | @Test
57 | public void findByCode_Naver() throws Exception {
58 | // Given
59 | final String code = "NAVER";
60 | // When
61 | AbstractDiscounter discounter = repository.findByCode(code);
62 | log.info("{} discounter = {}", code, discounter);
63 | // Then
64 | assertDiscounter(discounter, code, 20000, 2000);
65 | }
66 |
67 | @Test
68 | public void findByCode_Danawa() throws Exception {
69 | // Given
70 | final String code = "DANAWA";
71 | // When
72 | AbstractDiscounter discounter = repository.findByCode(code);
73 | log.info("{} discounter = {}", code, discounter);
74 | // Then
75 | assertDiscounter(discounter, code, 20000, 3000);
76 | }
77 |
78 | @Test
79 | public void findByCode_Fancafe() throws Exception {
80 | // Given
81 | final String code = "FANCAFE";
82 | // When
83 | AbstractDiscounter discounter = repository.findByCode(code);
84 | log.info("{} discounter = {}", code, discounter);
85 | // Then
86 | assertDiscounter(discounter, code, 20000, 1000);
87 | }
88 |
89 | private void assertDiscounter(AbstractDiscounter discounter, String code, long amt, long discountAmt) {
90 | assertThat(discounter.getCode(), is(code));
91 | assertThat(discounter.getDiscountAmt(amt), is(discountAmt));
92 | }
93 | }
--------------------------------------------------------------------------------
/src/test/java/io/redutan/antioop/noif/step3/repository/GenericMethodDiscounterRepositoryTest.java:
--------------------------------------------------------------------------------
1 | package io.redutan.antioop.noif.step3.repository;
2 |
3 | import io.redutan.antioop.AntiOopApplication;
4 | import io.redutan.antioop.noif.step3.AbstractDiscounter;
5 | import io.redutan.antioop.noif.step3.AmtDiscounter;
6 | import io.redutan.antioop.noif.step3.RateDiscounter;
7 | import lombok.extern.slf4j.Slf4j;
8 | import org.junit.Before;
9 | import org.junit.Test;
10 | import org.junit.runner.RunWith;
11 | import org.springframework.beans.factory.annotation.Autowired;
12 | import org.springframework.boot.test.SpringApplicationConfiguration;
13 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
14 | import org.springframework.transaction.annotation.Transactional;
15 |
16 | import java.util.Arrays;
17 | import java.util.List;
18 |
19 | import static org.hamcrest.CoreMatchers.is;
20 | import static org.junit.Assert.*;
21 |
22 | /**
23 | * Created by myeongju.jung on 2016. 4. 1..
24 | */
25 | @RunWith(SpringJUnit4ClassRunner.class)
26 | @SpringApplicationConfiguration(classes = AntiOopApplication.class)
27 | @Transactional
28 | @Slf4j
29 | public class GenericMethodDiscounterRepositoryTest {
30 | @Autowired
31 | GenericMethodDiscounterRepository repository;
32 |
33 | @Autowired
34 | DiscounterRepository abstractRepository;
35 |
36 | @Before
37 | public void setUp() throws Exception {
38 | RateDiscounter naverDiscounter = new RateDiscounter();
39 | naverDiscounter.setCode("NAVER");
40 | naverDiscounter.setName("네이버할인");
41 | naverDiscounter.setRate(10);
42 |
43 | RateDiscounter danawaDiscounter = new RateDiscounter();
44 | danawaDiscounter.setCode("DANAWA");
45 | danawaDiscounter.setName("다나와할인");
46 | danawaDiscounter.setRate(15);
47 |
48 | AmtDiscounter fancafeDiscounter = new AmtDiscounter();
49 | fancafeDiscounter.setCode("FANCAFE");
50 | fancafeDiscounter.setName("팬카페할인");
51 | fancafeDiscounter.setAmt(1000);
52 |
53 | List list = Arrays.asList(naverDiscounter, danawaDiscounter, fancafeDiscounter);
54 |
55 | abstractRepository.save(list);
56 | }
57 |
58 | @Test
59 | public void findByCode_Naver() throws Exception {
60 | // Given
61 | final String code = "NAVER";
62 | // When
63 | RateDiscounter discounter = repository.findByCode(code);
64 | log.info("{} discounter = {}", code, discounter);
65 | // Then
66 | assertDiscounter(discounter, code, 20000, 2000);
67 | }
68 |
69 | @Test
70 | public void findByCode_Danawa() throws Exception {
71 | // Given
72 | final String code = "DANAWA";
73 | // When
74 | RateDiscounter discounter = repository.findByCode(code);
75 | log.info("{} discounter = {}", code, discounter);
76 | // Then
77 | assertDiscounter(discounter, code, 20000, 3000);
78 | }
79 |
80 | @Test
81 | public void findByCode_Fancafe() throws Exception {
82 | // Given
83 | final String code = "FANCAFE";
84 | // When
85 | AmtDiscounter discounter = repository.findByCode(code);
86 | log.info("{} discounter = {}", code, discounter);
87 | // Then
88 | assertDiscounter(discounter, code, 20000, 1000);
89 | }
90 |
91 | @Test
92 | public void findByCode_FancafeAbstract() throws Exception {
93 | // Given
94 | final String code = "FANCAFE";
95 | // When
96 | AbstractDiscounter discounter = repository.findByCode(code);
97 | log.info("{} discounter = {}", code, discounter);
98 | // Then
99 | assertDiscounter(discounter, code, 20000, 1000);
100 | }
101 |
102 | // 예상했던 대로 casting 오류가 발생한다
103 | // java.lang.ClassCastException:
104 | // AmtDiscounter cannot be cast to RateDiscounter
105 | @Test(expected = ClassCastException.class)
106 | public void findByCode_FancafeRate() throws Exception {
107 | // Given
108 | final String code = "FANCAFE";
109 | // When
110 | RateDiscounter discounter = repository.findByCode(code);
111 | log.info("{} discounter = {}", code, discounter);
112 | // Then
113 | assertDiscounter(discounter, code, 20000, 1000);
114 | }
115 |
116 | private void assertDiscounter(AbstractDiscounter discounter, String code, long amt, long discountAmt) {
117 | assertThat(discounter.getCode(), is(code));
118 | assertThat(discounter.getDiscountAmt(amt), is(discountAmt));
119 | }
120 | }
--------------------------------------------------------------------------------
/src/test/java/io/redutan/antioop/noif/step3/repository/GenericRateDiscounterRepositoryTest.java:
--------------------------------------------------------------------------------
1 | package io.redutan.antioop.noif.step3.repository;
2 |
3 | import io.redutan.antioop.AntiOopApplication;
4 | import io.redutan.antioop.noif.step3.AbstractDiscounter;
5 | import io.redutan.antioop.noif.step3.AmtDiscounter;
6 | import io.redutan.antioop.noif.step3.RateDiscounter;
7 | import lombok.extern.slf4j.Slf4j;
8 | import org.junit.Before;
9 | import org.junit.Test;
10 | import org.junit.runner.RunWith;
11 | import org.springframework.beans.factory.annotation.Autowired;
12 | import org.springframework.boot.test.SpringApplicationConfiguration;
13 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
14 | import org.springframework.transaction.annotation.Transactional;
15 |
16 | import java.util.Arrays;
17 | import java.util.List;
18 |
19 | import static org.hamcrest.CoreMatchers.is;
20 | import static org.hamcrest.CoreMatchers.nullValue;
21 | import static org.junit.Assert.*;
22 |
23 | /**
24 | * Created by myeongju.jung on 2016. 4. 1..
25 | */
26 | @RunWith(SpringJUnit4ClassRunner.class)
27 | @SpringApplicationConfiguration(classes = AntiOopApplication.class)
28 | @Transactional
29 | @Slf4j
30 | public class GenericRateDiscounterRepositoryTest {
31 | @Autowired
32 | DiscounterRepository rateDiscounterRepository;
33 |
34 | @Autowired
35 | DiscounterRepository repository;
36 |
37 | @Before
38 | public void setUp() throws Exception {
39 | RateDiscounter naverDiscounter = new RateDiscounter();
40 | naverDiscounter.setCode("NAVER");
41 | naverDiscounter.setName("네이버할인");
42 | naverDiscounter.setRate(10);
43 |
44 | RateDiscounter danawaDiscounter = new RateDiscounter();
45 | danawaDiscounter.setCode("DANAWA");
46 | danawaDiscounter.setName("다나와할인");
47 | danawaDiscounter.setRate(15);
48 |
49 | AmtDiscounter fancafeDiscounter = new AmtDiscounter();
50 | fancafeDiscounter.setCode("FANCAFE");
51 | fancafeDiscounter.setName("팬카페할인");
52 | fancafeDiscounter.setAmt(1000);
53 |
54 | List list = Arrays.asList(naverDiscounter, danawaDiscounter, fancafeDiscounter);
55 |
56 | repository.save(list);
57 | }
58 |
59 | @Test
60 | public void findByCode_Naver() throws Exception {
61 | // Given
62 | final String code = "NAVER";
63 | // When
64 | RateDiscounter discounter = rateDiscounterRepository.findByCode(code);
65 | log.info("{} discounter = {}", code, discounter);
66 | // Then
67 | assertDiscounter(discounter, code, 20000, 2000);
68 | }
69 |
70 | @Test
71 | public void findByCode_Danawa() throws Exception {
72 | // Given
73 | final String code = "DANAWA";
74 | // When
75 | RateDiscounter discounter = rateDiscounterRepository.findByCode(code);
76 | log.info("{} discounter = {}", code, discounter);
77 | // Then
78 | assertDiscounter(discounter, code, 20000, 3000);
79 | }
80 |
81 | /**
82 | * casting 오류 발생. 개인적으로 null일 발생하지 않을까 했는데 조회가 가능하긴 하다.
83 | * Entity가 다른데 조회가 되는 것 자체가 신기하다.
84 | * @throws Exception
85 | */
86 | @Test(expected = ClassCastException.class)
87 | public void findByCode_Fancafe() throws Exception {
88 | // Given
89 | final String code = "FANCAFE";
90 | // When
91 | RateDiscounter discounter = rateDiscounterRepository.findByCode(code);
92 | fail();
93 | }
94 |
95 | // RateDiscounter repository를 사용할지라도 AmtDiscounter를 조회할 수 있다. (SINGE_TABLE, JOINED 상속 전략)
96 | @Test
97 | public void findByCode_FancafeAbstract() throws Exception {
98 | // Given
99 | final String code = "FANCAFE";
100 | // When
101 | AbstractDiscounter discounter = rateDiscounterRepository.findByCode(code);
102 | log.info("{} discounter = {}", code, discounter);
103 | // Then
104 | assertDiscounter(discounter, code, 20000, 1000);
105 | }
106 |
107 | private void assertDiscounter(AbstractDiscounter discounter, String code, long amt, long discountAmt) {
108 | assertThat(discounter.getCode(), is(code));
109 | assertThat(discounter.getDiscountAmt(amt), is(discountAmt));
110 | }
111 | }
--------------------------------------------------------------------------------
/src/test/java/io/redutan/antioop/noif/step3/repository/RateDiscounterRepositoryTest.java:
--------------------------------------------------------------------------------
1 | package io.redutan.antioop.noif.step3.repository;
2 |
3 | import io.redutan.antioop.AntiOopApplication;
4 | import io.redutan.antioop.noif.step3.AbstractDiscounter;
5 | import io.redutan.antioop.noif.step3.AmtDiscounter;
6 | import io.redutan.antioop.noif.step3.RateDiscounter;
7 | import lombok.extern.slf4j.Slf4j;
8 | import org.junit.Before;
9 | import org.junit.Test;
10 | import org.junit.runner.RunWith;
11 | import org.springframework.beans.factory.annotation.Autowired;
12 | import org.springframework.boot.test.SpringApplicationConfiguration;
13 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
14 | import org.springframework.transaction.annotation.Transactional;
15 |
16 | import java.util.Arrays;
17 | import java.util.List;
18 |
19 | import static org.hamcrest.CoreMatchers.is;
20 | import static org.hamcrest.CoreMatchers.nullValue;
21 | import static org.junit.Assert.*;
22 |
23 | /**
24 | * Created by myeongju.jung on 2016. 4. 1..
25 | */
26 | @RunWith(SpringJUnit4ClassRunner.class)
27 | @SpringApplicationConfiguration(classes = AntiOopApplication.class)
28 | @Transactional
29 | @Slf4j
30 | public class RateDiscounterRepositoryTest {
31 | @Autowired
32 | RateDiscounterRepository repository;
33 |
34 | @Autowired
35 | DiscounterRepository abstractRepository;
36 |
37 | @Before
38 | public void setUp() throws Exception {
39 | RateDiscounter naverDiscounter = new RateDiscounter();
40 | naverDiscounter.setCode("NAVER");
41 | naverDiscounter.setName("네이버할인");
42 | naverDiscounter.setRate(10);
43 |
44 | RateDiscounter danawaDiscounter = new RateDiscounter();
45 | danawaDiscounter.setCode("DANAWA");
46 | danawaDiscounter.setName("다나와할인");
47 | danawaDiscounter.setRate(15);
48 |
49 | AmtDiscounter fancafeDiscounter = new AmtDiscounter();
50 | fancafeDiscounter.setCode("FANCAFE");
51 | fancafeDiscounter.setName("팬카페할인");
52 | fancafeDiscounter.setAmt(1000);
53 |
54 | List list = Arrays.asList(naverDiscounter, danawaDiscounter, fancafeDiscounter);
55 |
56 | abstractRepository.save(list);
57 | }
58 |
59 | @Test
60 | public void findByCode_Naver() throws Exception {
61 | // Given
62 | final String code = "NAVER";
63 | // When
64 | RateDiscounter discounter = repository.findByCode(code);
65 | log.info("{} discounter = {}", code, discounter);
66 | // Then
67 | assertDiscounter(discounter, code, 20000, 2000);
68 | }
69 |
70 | @Test
71 | public void findByCode_Danawa() throws Exception {
72 | // Given
73 | final String code = "DANAWA";
74 | // When
75 | RateDiscounter discounter = repository.findByCode(code);
76 | log.info("{} discounter = {}", code, discounter);
77 | // Then
78 | assertDiscounter(discounter, code, 20000, 3000);
79 | }
80 |
81 | // 예상대로 null이 발생한다.
82 | @Test
83 | public void findByCode_Fancafe() throws Exception {
84 | // Given
85 | final String code = "FANCAFE";
86 | // When
87 | RateDiscounter discounter = repository.findByCode(code);
88 | log.info("{} discounter = {}", code, discounter);
89 | // Then
90 | assertThat(discounter, is(nullValue()));
91 | }
92 |
93 | private void assertDiscounter(AbstractDiscounter discounter, String code, long amt, long discountAmt) {
94 | assertThat(discounter.getCode(), is(code));
95 | assertThat(discounter.getDiscountAmt(amt), is(discountAmt));
96 | }
97 | }
--------------------------------------------------------------------------------
/src/test/java/io/redutan/antioop/noif/step_enum/PaymentServiceEnumTest.java:
--------------------------------------------------------------------------------
1 | package io.redutan.antioop.noif.step_enum;
2 |
3 | import io.redutan.antioop.noif.Discount;
4 | import io.redutan.antioop.noif.DiscountRequest;
5 | import org.junit.Before;
6 | import org.junit.Test;
7 |
8 | import static org.hamcrest.CoreMatchers.is;
9 | import static org.junit.Assert.assertThat;
10 |
11 | /**
12 | * Created by redutan on 2016. 4. 1..
13 | */
14 | public class PaymentServiceEnumTest {
15 | PaymentServiceEnum service;
16 |
17 | @Before
18 | public void setUp() throws Exception {
19 | service = new PaymentServiceEnum();
20 | }
21 |
22 | @Test
23 | public void getDiscount_Naver() throws Exception {
24 | // Given
25 | DiscountRequest request1 = new DiscountRequest("NAVER", 20000);
26 | // When
27 | Discount discount1 = service.getDiscount(request1);
28 | // Then
29 | assertThat(discount1.getDiscountAmt(), is(2000L));
30 | }
31 |
32 | @Test
33 | public void getDiscount_Danawa() throws Exception {
34 | // Given
35 | DiscountRequest request1 = new DiscountRequest("DANAWA", 20000);
36 | // When
37 | Discount discount1 = service.getDiscount(request1);
38 | // Then
39 | assertThat(discount1.getDiscountAmt(), is(3000L));
40 | }
41 |
42 | @Test
43 | public void getDiscount_Fancafe() throws Exception {
44 | // Given
45 | DiscountRequest request1 = new DiscountRequest("FANCAFE", 20000);
46 | // When
47 | Discount discount1 = service.getDiscount(request1);
48 | // Then
49 | assertThat(discount1.getDiscountAmt(), is(1000L));
50 | }
51 |
52 | @Test
53 | public void getDiscount_FancafeLessThenAmt() throws Exception {
54 | // Given
55 | DiscountRequest request1 = new DiscountRequest("FANCAFE", 500L);
56 | // When
57 | Discount discount1 = service.getDiscount(request1);
58 | // Then
59 | assertThat(discount1.getDiscountAmt(), is(500L));
60 | }
61 |
62 | @Test
63 | public void getDiscount_EmptyAndNull() throws Exception {
64 | // Given
65 | DiscountRequest request1 = new DiscountRequest("", 10000);
66 | // When empty
67 | Discount discount1 = service.getDiscount(request1);
68 | // Then
69 | assertThat(discount1.getDiscountAmt(), is(0L));
70 |
71 | // Given
72 | DiscountRequest request2 = new DiscountRequest(null, 10000);
73 | // When null
74 | Discount discount2 = service.getDiscount(request2);
75 | // Then
76 | assertThat(discount2.getDiscountAmt(), is(0L));
77 | }
78 |
79 | @Test
80 | public void getDiscount_Invalid() throws Exception {
81 | // Given
82 | DiscountRequest request1 = new DiscountRequest("ABCDEFG", 10000);
83 | // When
84 | Discount discount1 = service.getDiscount(request1);
85 | // Then
86 | assertThat(discount1.getDiscountAmt(), is(0L));
87 | }
88 | }
--------------------------------------------------------------------------------