From 0d60b2a6d84c47ce0eb3fae14eb87fbc90f51400 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aria=CC=80=20Prat?= Date: Tue, 29 Oct 2019 12:15:51 +0100 Subject: [PATCH 1/3] Add expose static method scenario and test --- .../refactortechniques/scenario/Packet.kt | 16 ++++++++++ .../scenario/RSCWorkflow.kt | 17 +++++++++++ .../solution/RSCWorkflow_solution.kt | 23 +++++++++++++++ .../refactortechniques/ExampleUnitTest.kt | 17 ----------- .../refactortechniques/RSCWorkflowTest.kt | 29 +++++++++++++++++++ 5 files changed, 85 insertions(+), 17 deletions(-) create mode 100644 app/src/main/java/com/aria/refactortechniques/scenario/Packet.kt create mode 100644 app/src/main/java/com/aria/refactortechniques/scenario/RSCWorkflow.kt create mode 100644 app/src/main/java/com/aria/refactortechniques/solution/RSCWorkflow_solution.kt delete mode 100644 app/src/test/java/com/aria/refactortechniques/ExampleUnitTest.kt create mode 100644 app/src/test/java/com/aria/refactortechniques/RSCWorkflowTest.kt diff --git a/app/src/main/java/com/aria/refactortechniques/scenario/Packet.kt b/app/src/main/java/com/aria/refactortechniques/scenario/Packet.kt new file mode 100644 index 0000000..7775737 --- /dev/null +++ b/app/src/main/java/com/aria/refactortechniques/scenario/Packet.kt @@ -0,0 +1,16 @@ +package com.aria.refactortechniques.scenario + +class Packet { + + fun getOriginator(): String { + return "ORIGINATOR!" + } + + fun getLength(): Int { + return 200 + } + + fun hasValidCheckSum(): Boolean { + return true + } +} diff --git a/app/src/main/java/com/aria/refactortechniques/scenario/RSCWorkflow.kt b/app/src/main/java/com/aria/refactortechniques/scenario/RSCWorkflow.kt new file mode 100644 index 0000000..7c7b98d --- /dev/null +++ b/app/src/main/java/com/aria/refactortechniques/scenario/RSCWorkflow.kt @@ -0,0 +1,17 @@ +package com.aria.refactortechniques.scenario + +import com.aria.refactortechniques.solution.RSCWorkflow_solution + +private const val MAX_LENGTH = 100 + +class RSCWorkflow { + + fun validate(packet: Packet): Boolean { + if (packet.getOriginator() == "ORIGINATOR!" + || packet.getLength() > MAX_LENGTH + || !packet.hasValidCheckSum()) { + return false + } + return true + } +} \ No newline at end of file diff --git a/app/src/main/java/com/aria/refactortechniques/solution/RSCWorkflow_solution.kt b/app/src/main/java/com/aria/refactortechniques/solution/RSCWorkflow_solution.kt new file mode 100644 index 0000000..004e478 --- /dev/null +++ b/app/src/main/java/com/aria/refactortechniques/solution/RSCWorkflow_solution.kt @@ -0,0 +1,23 @@ +package com.aria.refactortechniques.solution + +import com.aria.refactortechniques.scenario.Packet + +private const val MAX_LENGTH = 100 + +class RSCWorkflow_solution { + + fun validate(packet: Packet) { + validatePacket(packet) + } + + companion object { + fun validatePacket(packet: Packet): Boolean { + if (packet.getOriginator() == "ORIGINATOR!" + || packet.getLength() > MAX_LENGTH + || !packet.hasValidCheckSum()) { + return false + } + return true + } + } +} \ No newline at end of file diff --git a/app/src/test/java/com/aria/refactortechniques/ExampleUnitTest.kt b/app/src/test/java/com/aria/refactortechniques/ExampleUnitTest.kt deleted file mode 100644 index 3e71b77..0000000 --- a/app/src/test/java/com/aria/refactortechniques/ExampleUnitTest.kt +++ /dev/null @@ -1,17 +0,0 @@ -package com.aria.refactortechniques - -import org.junit.Test - -import org.junit.Assert.* - -/** - * Example local unit test, which will execute on the development machine (host). - * - * See [testing documentation](http://d.android.com/tools/testing). - */ -class ExampleUnitTest { - @Test - fun addition_isCorrect() { - assertEquals(4, 2 + 2) - } -} diff --git a/app/src/test/java/com/aria/refactortechniques/RSCWorkflowTest.kt b/app/src/test/java/com/aria/refactortechniques/RSCWorkflowTest.kt new file mode 100644 index 0000000..7dc2451 --- /dev/null +++ b/app/src/test/java/com/aria/refactortechniques/RSCWorkflowTest.kt @@ -0,0 +1,29 @@ +package com.aria.refactortechniques + +import com.aria.refactortechniques.scenario.Packet +import com.aria.refactortechniques.solution.RSCWorkflow_solution +import org.junit.Assert.assertEquals +import org.junit.Before +import org.junit.Test + +class RSCWorkflowTest { + + private lateinit var workflow: RSCWorkflow_solution + private lateinit var packet: Packet + + @Before + fun setUp() { + // workflow = RSCWorkflow_solution() // Can't put it in a test harness + packet = Packet() + } + + @Test + fun `given invalid packet should fail`() { + val packet = givenInvalidPacket() + assertEquals(RSCWorkflow_solution.validatePacket(packet), false) + } + + fun givenInvalidPacket(): Packet { + return Packet() + } +} \ No newline at end of file From 44d8f3e5473a01b005d227467f5105ef1f105a4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aria=CC=80=20Prat?= Date: Tue, 29 Oct 2019 12:22:36 +0100 Subject: [PATCH 2/3] Fix packet test variable --- app/src/test/java/com/aria/refactortechniques/RSCWorkflowTest.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/app/src/test/java/com/aria/refactortechniques/RSCWorkflowTest.kt b/app/src/test/java/com/aria/refactortechniques/RSCWorkflowTest.kt index 7dc2451..eb6b13c 100644 --- a/app/src/test/java/com/aria/refactortechniques/RSCWorkflowTest.kt +++ b/app/src/test/java/com/aria/refactortechniques/RSCWorkflowTest.kt @@ -14,7 +14,6 @@ class RSCWorkflowTest { @Before fun setUp() { // workflow = RSCWorkflow_solution() // Can't put it in a test harness - packet = Packet() } @Test From 988883504969664481132910bb186fb63fb9734c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aria=CC=80=20Prat?= Date: Wed, 19 Feb 2020 12:51:13 +0100 Subject: [PATCH 3/3] Add coupons example --- .../aria/refactortechniques/MainActivity.kt | 2 +- .../refactortechniques/scenario/Coupon.kt | 6 ++++ .../scenario/CouponPresenter.kt | 8 +++++ .../scenario/CouponValidatorUseCase.kt | 8 +++++ .../refactortechniques/scenario/Packet.kt | 16 --------- .../scenario/RSCWorkflow.kt | 17 ---------- .../solution/CouponPresenterSolution.kt | 12 +++++++ .../CouponValidatorUseCaseSolution.kt | 15 ++++++++ .../solution/RSCWorkflow_solution.kt | 23 ------------- .../CouponValidatorUseCaseTest.kt | 34 +++++++++++++++++++ .../refactortechniques/RSCWorkflowTest.kt | 28 --------------- 11 files changed, 84 insertions(+), 85 deletions(-) create mode 100644 app/src/main/java/com/aria/refactortechniques/scenario/Coupon.kt create mode 100644 app/src/main/java/com/aria/refactortechniques/scenario/CouponPresenter.kt create mode 100644 app/src/main/java/com/aria/refactortechniques/scenario/CouponValidatorUseCase.kt delete mode 100644 app/src/main/java/com/aria/refactortechniques/scenario/Packet.kt delete mode 100644 app/src/main/java/com/aria/refactortechniques/scenario/RSCWorkflow.kt create mode 100644 app/src/main/java/com/aria/refactortechniques/solution/CouponPresenterSolution.kt create mode 100644 app/src/main/java/com/aria/refactortechniques/solution/CouponValidatorUseCaseSolution.kt delete mode 100644 app/src/main/java/com/aria/refactortechniques/solution/RSCWorkflow_solution.kt create mode 100644 app/src/test/java/com/aria/refactortechniques/CouponValidatorUseCaseTest.kt delete mode 100644 app/src/test/java/com/aria/refactortechniques/RSCWorkflowTest.kt diff --git a/app/src/main/java/com/aria/refactortechniques/MainActivity.kt b/app/src/main/java/com/aria/refactortechniques/MainActivity.kt index 8782011..93e5946 100644 --- a/app/src/main/java/com/aria/refactortechniques/MainActivity.kt +++ b/app/src/main/java/com/aria/refactortechniques/MainActivity.kt @@ -1,7 +1,7 @@ package com.aria.refactortechniques -import androidx.appcompat.app.AppCompatActivity import android.os.Bundle +import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { diff --git a/app/src/main/java/com/aria/refactortechniques/scenario/Coupon.kt b/app/src/main/java/com/aria/refactortechniques/scenario/Coupon.kt new file mode 100644 index 0000000..17af91d --- /dev/null +++ b/app/src/main/java/com/aria/refactortechniques/scenario/Coupon.kt @@ -0,0 +1,6 @@ +package com.aria.refactortechniques.scenario + +data class Coupon( + val title: String = "Title", + val duration: Int = 100 +) diff --git a/app/src/main/java/com/aria/refactortechniques/scenario/CouponPresenter.kt b/app/src/main/java/com/aria/refactortechniques/scenario/CouponPresenter.kt new file mode 100644 index 0000000..b1d9109 --- /dev/null +++ b/app/src/main/java/com/aria/refactortechniques/scenario/CouponPresenter.kt @@ -0,0 +1,8 @@ +package com.aria.refactortechniques.scenario + +class CouponPresenter(val couponValidatorUseCase: CouponValidatorUseCase) { + + fun setup(coupon: Coupon) { + couponValidatorUseCase.validate(coupon) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/aria/refactortechniques/scenario/CouponValidatorUseCase.kt b/app/src/main/java/com/aria/refactortechniques/scenario/CouponValidatorUseCase.kt new file mode 100644 index 0000000..de5eb65 --- /dev/null +++ b/app/src/main/java/com/aria/refactortechniques/scenario/CouponValidatorUseCase.kt @@ -0,0 +1,8 @@ +package com.aria.refactortechniques.scenario + +const val MAX_TITLE_LENGTH = 100 + +class CouponValidatorUseCase { + + fun validate(coupon: Coupon) = coupon.title.length < MAX_TITLE_LENGTH +} \ No newline at end of file diff --git a/app/src/main/java/com/aria/refactortechniques/scenario/Packet.kt b/app/src/main/java/com/aria/refactortechniques/scenario/Packet.kt deleted file mode 100644 index 7775737..0000000 --- a/app/src/main/java/com/aria/refactortechniques/scenario/Packet.kt +++ /dev/null @@ -1,16 +0,0 @@ -package com.aria.refactortechniques.scenario - -class Packet { - - fun getOriginator(): String { - return "ORIGINATOR!" - } - - fun getLength(): Int { - return 200 - } - - fun hasValidCheckSum(): Boolean { - return true - } -} diff --git a/app/src/main/java/com/aria/refactortechniques/scenario/RSCWorkflow.kt b/app/src/main/java/com/aria/refactortechniques/scenario/RSCWorkflow.kt deleted file mode 100644 index 7c7b98d..0000000 --- a/app/src/main/java/com/aria/refactortechniques/scenario/RSCWorkflow.kt +++ /dev/null @@ -1,17 +0,0 @@ -package com.aria.refactortechniques.scenario - -import com.aria.refactortechniques.solution.RSCWorkflow_solution - -private const val MAX_LENGTH = 100 - -class RSCWorkflow { - - fun validate(packet: Packet): Boolean { - if (packet.getOriginator() == "ORIGINATOR!" - || packet.getLength() > MAX_LENGTH - || !packet.hasValidCheckSum()) { - return false - } - return true - } -} \ No newline at end of file diff --git a/app/src/main/java/com/aria/refactortechniques/solution/CouponPresenterSolution.kt b/app/src/main/java/com/aria/refactortechniques/solution/CouponPresenterSolution.kt new file mode 100644 index 0000000..75afffd --- /dev/null +++ b/app/src/main/java/com/aria/refactortechniques/solution/CouponPresenterSolution.kt @@ -0,0 +1,12 @@ +package com.aria.refactortechniques.solution + +import com.aria.refactortechniques.scenario.Coupon + +class CouponPresenterSolution( + private val couponValidatorUseCaseSolution: CouponValidatorUseCaseSolution +) { + + fun setup(coupon: Coupon) { + couponValidatorUseCaseSolution.validate(coupon) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/aria/refactortechniques/solution/CouponValidatorUseCaseSolution.kt b/app/src/main/java/com/aria/refactortechniques/solution/CouponValidatorUseCaseSolution.kt new file mode 100644 index 0000000..422f851 --- /dev/null +++ b/app/src/main/java/com/aria/refactortechniques/solution/CouponValidatorUseCaseSolution.kt @@ -0,0 +1,15 @@ +package com.aria.refactortechniques.solution + +import com.aria.refactortechniques.scenario.Coupon +import com.aria.refactortechniques.scenario.MAX_TITLE_LENGTH + +class CouponValidatorUseCaseSolution { + + fun validate(coupon: Coupon): Boolean { + return validateCoupon(coupon) + } + + companion object { + fun validateCoupon(coupon: Coupon) = coupon.title.length < MAX_TITLE_LENGTH + } +} \ No newline at end of file diff --git a/app/src/main/java/com/aria/refactortechniques/solution/RSCWorkflow_solution.kt b/app/src/main/java/com/aria/refactortechniques/solution/RSCWorkflow_solution.kt deleted file mode 100644 index 004e478..0000000 --- a/app/src/main/java/com/aria/refactortechniques/solution/RSCWorkflow_solution.kt +++ /dev/null @@ -1,23 +0,0 @@ -package com.aria.refactortechniques.solution - -import com.aria.refactortechniques.scenario.Packet - -private const val MAX_LENGTH = 100 - -class RSCWorkflow_solution { - - fun validate(packet: Packet) { - validatePacket(packet) - } - - companion object { - fun validatePacket(packet: Packet): Boolean { - if (packet.getOriginator() == "ORIGINATOR!" - || packet.getLength() > MAX_LENGTH - || !packet.hasValidCheckSum()) { - return false - } - return true - } - } -} \ No newline at end of file diff --git a/app/src/test/java/com/aria/refactortechniques/CouponValidatorUseCaseTest.kt b/app/src/test/java/com/aria/refactortechniques/CouponValidatorUseCaseTest.kt new file mode 100644 index 0000000..2dcac5e --- /dev/null +++ b/app/src/test/java/com/aria/refactortechniques/CouponValidatorUseCaseTest.kt @@ -0,0 +1,34 @@ +package com.aria.refactortechniques + +import com.aria.refactortechniques.scenario.Coupon +import com.aria.refactortechniques.solution.CouponPresenterSolution +import com.aria.refactortechniques.solution.CouponValidatorUseCaseSolution +import com.nhaarman.mockitokotlin2.given +import org.junit.Before +import org.junit.Test + +class CouponValidatorUseCaseSolutionTest { + + private lateinit var sut: CouponPresenterSolution + private lateinit var couponValidatorUseCaseSolution: CouponValidatorUseCaseSolution + + @Before + fun setup() { + sut = CouponPresenterSolution(couponValidatorUseCaseSolution) + } + + @Test + fun `given valid coupon should validate it properly`() { + couponValidatorUseCaseSolution.validate(givenAnyCoupon()) + } + + @Test + fun `given Valid Coupon`() { + given(CouponValidatorUseCaseSolution.validateCoupon(givenAnyCoupon())) + .willReturn(true) + } + + fun givenAnyCoupon(): Coupon { + return Coupon("Any", 100) + } +} \ No newline at end of file diff --git a/app/src/test/java/com/aria/refactortechniques/RSCWorkflowTest.kt b/app/src/test/java/com/aria/refactortechniques/RSCWorkflowTest.kt deleted file mode 100644 index eb6b13c..0000000 --- a/app/src/test/java/com/aria/refactortechniques/RSCWorkflowTest.kt +++ /dev/null @@ -1,28 +0,0 @@ -package com.aria.refactortechniques - -import com.aria.refactortechniques.scenario.Packet -import com.aria.refactortechniques.solution.RSCWorkflow_solution -import org.junit.Assert.assertEquals -import org.junit.Before -import org.junit.Test - -class RSCWorkflowTest { - - private lateinit var workflow: RSCWorkflow_solution - private lateinit var packet: Packet - - @Before - fun setUp() { - // workflow = RSCWorkflow_solution() // Can't put it in a test harness - } - - @Test - fun `given invalid packet should fail`() { - val packet = givenInvalidPacket() - assertEquals(RSCWorkflow_solution.validatePacket(packet), false) - } - - fun givenInvalidPacket(): Packet { - return Packet() - } -} \ No newline at end of file