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

Feature: single responsibility principle solve exercise srp #3

Conversation

marcellodesales
Copy link
Owner

@marcellodesales marcellodesales commented Feb 27, 2023

πŸŽ‰ Resolve OCP with better tests

  • Better test cases
  • Better interface for shapes and the answer to see which share is not in the area
    public Map<Shape, Boolean> isPointInsideAreas(int x, int y, Set<Shape> shapes) {
        Map<Shape, Boolean> shapeAreas = new HashMap<>(shapes.size());
        for (var s : shapes) {
            shapeAreas.put(s, s.isPointInsideArea(x, y));
        }
        return shapeAreas;
    }
  • A more generic test verification can be done
    public static void assertResults(Map<Shape, Boolean> shapesExpectedResults,  Map<Shape, Boolean> shapesInput) {
        for (Map.Entry<Shape,Boolean> entry : shapesInput.entrySet()) {
            assert shapesExpectedResults.get(entry.getKey()).equals(entry.getValue());
        }
    }
  • The input parameters for the tests can use a Map.of() instead of a list, as we can map the expected results
        Map<Shape, Boolean> shapesExpectedResults = Map.of(
                new Rectangle(0,0,4,4), false,
                new Circle(10,10, 7), true
        );

Before

~/dev/github.com/marcellodesales/pluralsight-refactoring-solid-java17/2-ocp/exercises/exercise1/before on ξ‚  master πŸ“… 02-27-2023 ⌚10:03:13
$ mvn test
[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------< org.example:ocp-exercise-1 >---------------------
[INFO] Building ocp-exercise-1 1.0-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- resources:3.3.0:resources (default-resources) @ ocp-exercise-1 ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/marcellodesales/dev/github.com/marcellodesales/pluralsight-refactoring-solid-java17/2-ocp/exercises/exercise1/before/src/main/resources
[INFO] 
[INFO] --- compiler:3.7.0:compile (default-compile) @ ocp-exercise-1 ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 5 source files to /Users/marcellodesales/dev/github.com/marcellodesales/pluralsight-refactoring-solid-java17/2-ocp/exercises/exercise1/before/target/classes
[INFO] 
[INFO] --- resources:3.3.0:testResources (default-testResources) @ ocp-exercise-1 ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/marcellodesales/dev/github.com/marcellodesales/pluralsight-refactoring-solid-java17/2-ocp/exercises/exercise1/before/src/test/resources
[INFO] 
[INFO] --- compiler:3.7.0:testCompile (default-testCompile) @ ocp-exercise-1 ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 5 source files to /Users/marcellodesales/dev/github.com/marcellodesales/pluralsight-refactoring-solid-java17/2-ocp/exercises/exercise1/before/target/test-classes
[INFO] 
[INFO] --- surefire:2.22.2:test (default-test) @ ocp-exercise-1 ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running PointInsideAreasCalculatorTests
[INFO] Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.022 s - in PointInsideAreasCalculatorTests
[INFO] Running RectangleTests
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 s <<< FAILURE! - in RectangleTests
[ERROR] shouldImplementIsPointInAreaMethod  Time elapsed: 0 s  <<< ERROR!
java.util.NoSuchElementException: No value present
        at RectangleTests.shouldImplementIsPointInAreaMethod(RectangleTests.java:14)

[INFO] Running FileTests
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0 s <<< FAILURE! - in FileTests
[ERROR] allShapesShouldHaveIsPointInsideMethod  Time elapsed: 0 s  <<< FAILURE!
org.opentest4j.AssertionFailedError: expected: <true> but was: <false>
        at FileTests.allShapesShouldHaveIsPointInsideMethod(FileTests.java:12)

[INFO] Running CircleTests
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 s <<< FAILURE! - in CircleTests
[ERROR] shouldImplementIsPointInAreaMethod  Time elapsed: 0 s  <<< ERROR!
java.util.NoSuchElementException: No value present
        at CircleTests.shouldImplementIsPointInAreaMethod(CircleTests.java:16)

[INFO] 
[INFO] Results:
[INFO] 
[ERROR] Failures: 
[ERROR]   FileTests.allShapesShouldHaveIsPointInsideMethod:12 expected: <true> but was: <false>
[ERROR] Errors: 
[ERROR]   CircleTests.shouldImplementIsPointInAreaMethod:16 Β» NoSuchElement No value pre...
[ERROR]   RectangleTests.shouldImplementIsPointInAreaMethod:14 Β» NoSuchElement No value ...
[INFO] 
[ERROR] Tests run: 8, Failures: 1, Errors: 2, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.496 s
[INFO] Finished at: 2023-02-27T10:03:22-08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test (default-test) on project ocp-exercise-1: There are test failures.
[ERROR] 
[ERROR] Please refer to /Users/marcellodesales/dev/github.com/marcellodesales/pluralsight-refactoring-solid-java17/2-ocp/exercises/exercise1/before/target/surefire-reports for the individual test results.
[ERROR] Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

πŸ”Š After Solution

☁️  aws-cli@2.2.32 πŸ”– aws-iam-authenticator@0.5.3 
☸️  kubectl@1.25.4
KustomizeVersion: v4.5.7 πŸ“› kustomize@v4.4.0 🎑 helm@3.7.0 πŸ‘½ argocd@2.3.3 🀹 argocd@3.2.8 ✈️  glooctl@1.9.0  
πŸ‘€ AWS_PS1_PROFILE πŸ—‚οΈ   🌎 sa-east-1 
πŸ—   πŸ” arn:aws:eks:sa-east-1:806101772216:cluster/eks-ppd-prdt-super-cash 🍱 default 
~/dev/github.com/marcellodesales/pluralsight-refactoring-solid-java17/2-ocp/exercises/exercise1/before on ξ‚  feature/single-responsibility-principle-solve-exercise-srp πŸ“… 02-27-2023 ⌚10:04:11
$ mvn test                                                                
[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------< org.example:ocp-exercise-1 >---------------------
[INFO] Building ocp-exercise-1 1.0-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- resources:3.3.0:resources (default-resources) @ ocp-exercise-1 ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/marcellodesales/dev/github.com/marcellodesales/pluralsight-refactoring-solid-java17/2-ocp/exercises/exercise1/before/src/main/resources
[INFO] 
[INFO] --- compiler:3.7.0:compile (default-compile) @ ocp-exercise-1 ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 5 source files to /Users/marcellodesales/dev/github.com/marcellodesales/pluralsight-refactoring-solid-java17/2-ocp/exercises/exercise1/before/target/classes
[INFO] 
[INFO] --- resources:3.3.0:testResources (default-testResources) @ ocp-exercise-1 ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/marcellodesales/dev/github.com/marcellodesales/pluralsight-refactoring-solid-java17/2-ocp/exercises/exercise1/before/src/test/resources
[INFO] 
[INFO] --- compiler:3.7.0:testCompile (default-testCompile) @ ocp-exercise-1 ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 5 source files to /Users/marcellodesales/dev/github.com/marcellodesales/pluralsight-refactoring-solid-java17/2-ocp/exercises/exercise1/before/target/test-classes
[INFO] 
[INFO] --- surefire:2.22.2:test (default-test) @ ocp-exercise-1 ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running PointInsideAreasCalculatorTests
[INFO] Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.026 s - in PointInsideAreasCalculatorTests
[INFO] Running RectangleTests
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 s - in RectangleTests
[INFO] Running FileTests
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 s - in FileTests
[INFO] Running CircleTests
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 s - in CircleTests
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 8, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.267 s
[INFO] Finished at: 2023-02-27T10:04:16-08:00
[INFO] ------------------------------------------------------------------------

The tests now pass as they were broken before

$ mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------< org.example:ocp-checkpoint-1 >--------------------
[INFO] Building ocp-checkpoint-1 1.0-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- resources:3.3.0:resources (default-resources) @ ocp-checkpoint-1 ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/marcellodesales/dev/github.com/marcellodesales/pluralsight-refactoring-solid-java17/2-ocp/checkpoints/checkpoint1/before/src/main/resources
[INFO]
[INFO] --- compiler:3.7.0:compile (default-compile) @ ocp-checkpoint-1 ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 2 source files to /Users/marcellodesales/dev/github.com/marcellodesales/pluralsight-refactoring-solid-java17/2-ocp/checkpoints/checkpoint1/before/target/classes
[INFO]
[INFO] --- resources:3.3.0:testResources (default-testResources) @ ocp-checkpoint-1 ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/marcellodesales/dev/github.com/marcellodesales/pluralsight-refactoring-solid-java17/2-ocp/checkpoints/checkpoint1/before/src/test/resources
[INFO]
[INFO] --- compiler:3.7.0:testCompile (default-testCompile) @ ocp-checkpoint-1 ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 2 source files to /Users/marcellodesales/dev/github.com/marcellodesales/pluralsight-refactoring-solid-java17/2-ocp/checkpoints/checkpoint1/before/target/test-classes
[INFO]
[INFO] --- surefire:2.22.2:test (default-test) @ ocp-checkpoint-1 ---
[INFO]
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running TaxCalculatorTests
[ERROR] Tests run: 2, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.028 s <<< FAILURE! - in TaxCalculatorTests
[ERROR] shouldCalculateTaxesWithNationality  Time elapsed: 0.019 s  <<< ERROR!
java.util.NoSuchElementException: No value present
        at TaxCalculatorTests.shouldCalculateTaxesWithNationality(TaxCalculatorTests.java:35)

[INFO]
[INFO] Results:
[INFO]
[ERROR] Errors:
[ERROR]   TaxCalculatorTests.shouldCalculateTaxesWithNationality:35 Β» NoSuchElement No v...
[INFO]
[ERROR] Tests run: 2, Failures: 0, Errors: 1, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.550 s
[INFO] Finished at: 2023-02-25T12:54:13-08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test (default-test) on project ocp-checkpoint-1: There are test failures.
[ERROR]
[ERROR] Please refer to /Users/marcellodesales/dev/github.com/marcellodesales/pluralsight-refactoring-solid-java17/2-ocp/checkpoints/checkpoint1/before/target/surefire-reports for the individual test results.
[ERROR] Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

☁️  aws-cli@2.2.32 πŸ”– aws-iam-authenticator@0.5.3
☸️  kubectl@1.25.4
KustomizeVersion: v4.5.7 πŸ“› kustomize@v4.4.0 🎑 helm@3.7.0 πŸ‘½ argocd@2.3.3 🀹 argocd@3.2.8 ✈️  glooctl@1.9.0
πŸ‘€ AWS_PS1_PROFILE πŸ—‚οΈ   🌎 sa-east-1
πŸ—   πŸ” arn:aws:eks:sa-east-1:806101772216:cluster/eks-ppd-prdt-super-cash 🍱 default
~/dev/github.com/marcellodesales/pluralsight-refactoring-solid-java17/2-ocp/checkpoints/checkpoint1/before on ξ‚  feature/single-responsibility-principle-solve-exercise-srp! πŸ“… 02-25-2023 ⌚12:54:13
$ mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------< org.example:ocp-checkpoint-1 >--------------------
[INFO] Building ocp-checkpoint-1 1.0-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- resources:3.3.0:resources (default-resources) @ ocp-checkpoint-1 ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/marcellodesales/dev/github.com/marcellodesales/pluralsight-refactoring-solid-java17/2-ocp/checkpoints/checkpoint1/before/src/main/resources
[INFO]
[INFO] --- compiler:3.7.0:compile (default-compile) @ ocp-checkpoint-1 ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 2 source files to /Users/marcellodesales/dev/github.com/marcellodesales/pluralsight-refactoring-solid-java17/2-ocp/checkpoints/checkpoint1/before/target/classes
[INFO]
[INFO] --- resources:3.3.0:testResources (default-testResources) @ ocp-checkpoint-1 ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/marcellodesales/dev/github.com/marcellodesales/pluralsight-refactoring-solid-java17/2-ocp/checkpoints/checkpoint1/before/src/test/resources
[INFO]
[INFO] --- compiler:3.7.0:testCompile (default-testCompile) @ ocp-checkpoint-1 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- surefire:2.22.2:test (default-test) @ ocp-checkpoint-1 ---
[INFO]
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running TaxCalculatorTests
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.023 s - in TaxCalculatorTests
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.908 s
[INFO] Finished at: 2023-02-25T12:54:48-08:00
[INFO] -----------------------------
Just moving the methods from the calculator
The solution specifies specific results for better results.
The last test case was broken as well.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant