From eb073a812e24cf7b61db56498d2d267808b81e3d Mon Sep 17 00:00:00 2001 From: Artem Getmanskii Date: Thu, 13 Mar 2025 15:08:05 +0300 Subject: [PATCH 01/12] add junit-json-params dep --- pom.xml | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pom.xml b/pom.xml index e53e4ef..c564abf 100644 --- a/pom.xml +++ b/pom.xml @@ -66,6 +66,18 @@ 5.10.2 test + + net.joshka + junit-json-params + 5.10.2-r0 + test + + + org.eclipse.parsson + parsson + 1.1.1 + test + src From 9ef7a6d488e2192a27ef4900ec054906d525cbe0 Mon Sep 17 00:00:00 2001 From: Artem Getmanskii Date: Thu, 13 Mar 2025 16:09:10 +0300 Subject: [PATCH 02/12] fix strict pull-request status parsing --- src/io/github/artemget/prbot/domain/pr/PrJsonStrict.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/io/github/artemget/prbot/domain/pr/PrJsonStrict.java b/src/io/github/artemget/prbot/domain/pr/PrJsonStrict.java index 0fe1f9f..0fba19e 100644 --- a/src/io/github/artemget/prbot/domain/pr/PrJsonStrict.java +++ b/src/io/github/artemget/prbot/domain/pr/PrJsonStrict.java @@ -111,7 +111,7 @@ public String link() throws EmptyArgumentException { public Status status() throws EmptyArgumentException { final String status; try { - status = new EJsonStr(this.json, "link").value(); + status = new EJsonStr(this.json, "status").value(); } catch (final EntryException exception) { throw new EmptyArgumentException( String.format( @@ -122,7 +122,7 @@ public Status status() throws EmptyArgumentException { ); } try { - return PullRequest.Status.valueOf(status); + return PullRequest.Status.valueOf(status.toUpperCase()); } catch (final IllegalArgumentException exception) { throw new EmptyArgumentException( String.format( From 6ee1d35c5229f48fbc6f7ff48303feb289438f62 Mon Sep 17 00:00:00 2001 From: Artem Getmanskii Date: Thu, 13 Mar 2025 16:22:18 +0300 Subject: [PATCH 03/12] add AccJson equals/hashcode --- .../artemget/prbot/domain/pr/AccJson.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/io/github/artemget/prbot/domain/pr/AccJson.java b/src/io/github/artemget/prbot/domain/pr/AccJson.java index 4d0a77d..7653aa2 100644 --- a/src/io/github/artemget/prbot/domain/pr/AccJson.java +++ b/src/io/github/artemget/prbot/domain/pr/AccJson.java @@ -26,6 +26,7 @@ import io.github.artemget.prbot.config.EJsonStr; import io.github.artemget.prbot.config.EntryException; +import java.util.Objects; import javax.json.JsonObject; /** @@ -83,4 +84,24 @@ public String username() throws EmptyArgumentException { ); } } + + //@checkstyle NeedBracesCheck (20 lines) + //@checkstyle HiddenFieldCheck (10 lines) + @SuppressWarnings({ + "PMD.ControlStatementBraces", + "PMD.OnlyOneReturn", + "AnnotationUseStyleCheck" + }) + @Override + public boolean equals(final Object object) { + if (this == object) return true; + if (object == null || getClass() != object.getClass()) return false; + final AccJson json = (AccJson) object; + return Objects.equals(this.json, json.json); + } + + @Override + public int hashCode() { + return Objects.hash(this.json); + } } From 1d01c088b56043f1487d9789b80b3944252713af Mon Sep 17 00:00:00 2001 From: Artem Getmanskii Date: Thu, 13 Mar 2025 16:28:00 +0300 Subject: [PATCH 04/12] add AccJson equals/hashcode template todo --- src/io/github/artemget/prbot/domain/pr/AccJson.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/io/github/artemget/prbot/domain/pr/AccJson.java b/src/io/github/artemget/prbot/domain/pr/AccJson.java index 7653aa2..82fd577 100644 --- a/src/io/github/artemget/prbot/domain/pr/AccJson.java +++ b/src/io/github/artemget/prbot/domain/pr/AccJson.java @@ -85,6 +85,8 @@ public String username() throws EmptyArgumentException { } } + //@todo #20:45min Intellij generates equals and hashcode + // not the elegant way. Lets add custom template for equals. //@checkstyle NeedBracesCheck (20 lines) //@checkstyle HiddenFieldCheck (10 lines) @SuppressWarnings({ From a2b654f2590f2bf335ef8a38f51320f885b6797c Mon Sep 17 00:00:00 2001 From: Artem Getmanskii Date: Thu, 13 Mar 2025 16:31:49 +0300 Subject: [PATCH 05/12] fix strict pull-request assignees field name --- src/io/github/artemget/prbot/domain/pr/PrJsonStrict.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/io/github/artemget/prbot/domain/pr/PrJsonStrict.java b/src/io/github/artemget/prbot/domain/pr/PrJsonStrict.java index 0fba19e..a150385 100644 --- a/src/io/github/artemget/prbot/domain/pr/PrJsonStrict.java +++ b/src/io/github/artemget/prbot/domain/pr/PrJsonStrict.java @@ -152,7 +152,7 @@ public Account from() throws EmptyArgumentException { @Override public List assigners() throws EmptyArgumentException { - return this.accounts("assigners"); + return this.accounts("assignees"); } @Override From a4b8bd0ecd97fce7e3213d36ca92aec061b9a02a Mon Sep 17 00:00:00 2001 From: Artem Getmanskii Date: Thu, 13 Mar 2025 16:37:29 +0300 Subject: [PATCH 06/12] add BrJson equals/hashcode --- .../artemget/prbot/domain/pr/BrJson.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/io/github/artemget/prbot/domain/pr/BrJson.java b/src/io/github/artemget/prbot/domain/pr/BrJson.java index 12f38fc..bc91b39 100644 --- a/src/io/github/artemget/prbot/domain/pr/BrJson.java +++ b/src/io/github/artemget/prbot/domain/pr/BrJson.java @@ -26,6 +26,7 @@ import io.github.artemget.prbot.config.EJsonStr; import io.github.artemget.prbot.config.EntryException; +import java.util.Objects; import javax.json.JsonObject; /** @@ -83,4 +84,24 @@ public String link() throws EmptyArgumentException { ); } } + + //@checkstyle NeedBracesCheck (20 lines) + //@checkstyle HiddenFieldCheck (10 lines) + @SuppressWarnings({ + "PMD.ControlStatementBraces", + "PMD.OnlyOneReturn", + "AnnotationUseStyleCheck" + }) + @Override + public boolean equals(final Object object) { + if (this == object) return true; + if (object == null || getClass() != object.getClass()) return false; + final BrJson json = (BrJson) object; + return Objects.equals(this.json, json.json); + } + + @Override + public int hashCode() { + return Objects.hash(this.json); + } } From 5fde23cabc5dcd81a34fafa08a8a1e97b27b5aba Mon Sep 17 00:00:00 2001 From: Artem Getmanskii Date: Thu, 13 Mar 2025 16:41:39 +0300 Subject: [PATCH 07/12] add PrJsonStrict positive test cases --- .../prbot/domain/pr/PrJsonStrictTest.java | 160 ++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 test/io/github/artemget/prbot/domain/pr/PrJsonStrictTest.java diff --git a/test/io/github/artemget/prbot/domain/pr/PrJsonStrictTest.java b/test/io/github/artemget/prbot/domain/pr/PrJsonStrictTest.java new file mode 100644 index 0000000..70e8dba --- /dev/null +++ b/test/io/github/artemget/prbot/domain/pr/PrJsonStrictTest.java @@ -0,0 +1,160 @@ +/* + * MIT License + * + * Copyright (c) 2024-2025. Artem Getmanskii + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package io.github.artemget.prbot.domain.pr; + +import io.github.artemget.prbot.config.EntryException; +import jakarta.json.JsonObject; +import java.util.List; +import javax.json.Json; +import net.joshka.junit.json.params.JsonFileSource; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.jupiter.params.ParameterizedTest; + +/** + * Test case for {@link io.github.artemget.prbot.domain.pr.PrJsonStrict} + * + * @since 0.0.1 + */ +class PrJsonStrictTest { + + @ParameterizedTest + @JsonFileSource(resources = "/PrStrict.json") + void returnsIdentity(JsonObject object) throws EntryException, EmptyArgumentException { + MatcherAssert.assertThat( + "Wrong identity", + new PrJsonStrict(object.toString()).identity(), + Matchers.equalTo("10") + ); + } + + @ParameterizedTest + @JsonFileSource(resources = "/PrStrict.json") + void returnsLink(JsonObject object) throws EntryException, EmptyArgumentException { + MatcherAssert.assertThat( + "Wrong link", + new PrJsonStrict(object.toString()).link(), + Matchers.equalTo("https://github.com/ArtemGet/prbot/pull/10") + ); + } + + @ParameterizedTest + @JsonFileSource(resources = "/PrStrict.json") + void returnsStatus(JsonObject object) throws EntryException, EmptyArgumentException { + MatcherAssert.assertThat( + "Wrong status", + new PrJsonStrict(object.toString()).status(), + Matchers.equalTo(PullRequest.Status.OPENED) + ); + } + + @ParameterizedTest + @JsonFileSource(resources = "/PrStrict.json") + void returnsFrom(JsonObject object) throws EntryException, EmptyArgumentException { + MatcherAssert.assertThat( + "Wrong from account", + new PrJsonStrict(object.toString()).from(), + Matchers.equalTo( + new AccJson( + Json.createObjectBuilder() + .add("id", "123") + .add("username", "ArtemGet") + .build() + ) + ) + ); + } + + @ParameterizedTest + @JsonFileSource(resources = "/PrStrict.json") + void returnsAssigners(JsonObject object) throws EntryException, EmptyArgumentException { + MatcherAssert.assertThat( + "Wrong assigner account", + new PrJsonStrict(object.toString()).assigners(), + Matchers.equalTo( + List.of( + new AccJson( + Json.createObjectBuilder() + .add("id", "123") + .add("username", "ArtemGet") + .build() + ) + ) + ) + ); + } + + @ParameterizedTest + @JsonFileSource(resources = "/PrStrict.json") + void returnsReviewers(JsonObject object) throws EntryException, EmptyArgumentException { + MatcherAssert.assertThat( + "Wrong reviewer account", + new PrJsonStrict(object.toString()).reviewers(), + Matchers.equalTo( + List.of( + new AccJson( + Json.createObjectBuilder() + .add("id", "321") + .add("username", "ReviewerUser") + .build() + ) + ) + ) + ); + } + + @ParameterizedTest + @JsonFileSource(resources = "/PrStrict.json") + void returnsBranchFrom(JsonObject object) throws EntryException, EmptyArgumentException { + MatcherAssert.assertThat( + "Wrong source branch", + new PrJsonStrict(object.toString()).branchFrom(), + Matchers.equalTo( + new BrJson( + Json.createObjectBuilder() + .add("name", "Eh/#6") + .add("link", "https://github.com/ArtemGet/prbot/tree/eh/%236") + .build() + ) + ) + ); + } + @ParameterizedTest + @JsonFileSource(resources = "/PrStrict.json") + void returnsBranchTo(JsonObject object) throws EntryException, EmptyArgumentException { + MatcherAssert.assertThat( + "Wrong target branch", + new PrJsonStrict(object.toString()).branchTo(), + Matchers.equalTo( + new BrJson( + Json.createObjectBuilder() + .add("name", "main") + .add("link", "https://github.com/ArtemGet/prbot/tree/main") + .build() + ) + ) + ); + } +} From f1bf98143f0a8fc52b00cf2448b29035963cad4a Mon Sep 17 00:00:00 2001 From: Artem Getmanskii Date: Thu, 13 Mar 2025 17:05:30 +0300 Subject: [PATCH 08/12] fix qulice checkstyle --- pom.xml | 6 ++++++ src/io/github/artemget/prbot/domain/pr/PrJsonStrict.java | 3 ++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index c564abf..3b5e21d 100644 --- a/pom.xml +++ b/pom.xml @@ -246,6 +246,12 @@ dependencies:org.cactoos:cactoos:jar:0.56.1:compile + + dependencies:jakarta.json:jakarta.json-api:jar:2.1.3:provided + + + dependencies:org.junit.jupiter:junit-jupiter-params:jar:5.10.3:test + diff --git a/src/io/github/artemget/prbot/domain/pr/PrJsonStrict.java b/src/io/github/artemget/prbot/domain/pr/PrJsonStrict.java index a150385..69fc021 100644 --- a/src/io/github/artemget/prbot/domain/pr/PrJsonStrict.java +++ b/src/io/github/artemget/prbot/domain/pr/PrJsonStrict.java @@ -30,6 +30,7 @@ import io.github.artemget.prbot.config.EntryException; import java.io.StringReader; import java.util.List; +import java.util.Locale; import java.util.stream.Collectors; import javax.json.Json; import javax.json.JsonArray; @@ -122,7 +123,7 @@ public Status status() throws EmptyArgumentException { ); } try { - return PullRequest.Status.valueOf(status.toUpperCase()); + return PullRequest.Status.valueOf(status.toUpperCase(Locale.getDefault())); } catch (final IllegalArgumentException exception) { throw new EmptyArgumentException( String.format( From f017d1440712ca3d94ef2aa5bf6611b130ff5d28 Mon Sep 17 00:00:00 2001 From: Artem Getmanskii Date: Thu, 13 Mar 2025 17:08:58 +0300 Subject: [PATCH 09/12] add test --- .../artemget/prbot/domain/pr/PrJsonStrictTest.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/io/github/artemget/prbot/domain/pr/PrJsonStrictTest.java b/test/io/github/artemget/prbot/domain/pr/PrJsonStrictTest.java index 70e8dba..fb933b0 100644 --- a/test/io/github/artemget/prbot/domain/pr/PrJsonStrictTest.java +++ b/test/io/github/artemget/prbot/domain/pr/PrJsonStrictTest.java @@ -31,6 +31,8 @@ import net.joshka.junit.json.params.JsonFileSource; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; /** @@ -40,6 +42,15 @@ */ class PrJsonStrictTest { + @Test + void throwsAtWrongJsonFormatAtCreation() { + Assertions.assertThrows( + EntryException.class, + () -> new PrJsonStrict("not json"), + "Not thrown at wrong json format" + ); + } + @ParameterizedTest @JsonFileSource(resources = "/PrStrict.json") void returnsIdentity(JsonObject object) throws EntryException, EmptyArgumentException { From 0cfd2d715acb35201162f7c6c272e29c40f606cd Mon Sep 17 00:00:00 2001 From: Artem Getmanskii Date: Thu, 13 Mar 2025 17:10:21 +0300 Subject: [PATCH 10/12] remove todo --- src/io/github/artemget/prbot/domain/pr/ProjJson.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/io/github/artemget/prbot/domain/pr/ProjJson.java b/src/io/github/artemget/prbot/domain/pr/ProjJson.java index 4e5f97a..5eb2902 100644 --- a/src/io/github/artemget/prbot/domain/pr/ProjJson.java +++ b/src/io/github/artemget/prbot/domain/pr/ProjJson.java @@ -30,11 +30,6 @@ * Git project from json source. * * @since 0.0.1 - * @todo #20:45 {@link io.github.artemget.prbot.domain.pr.ProjJson} - * and {@link io.github.artemget.prbot.domain.pr.PrJsonStrict} - * is not covered by tests. Lets cover it's methods. I suggest using - * joshka/junit-json-params - * with it's @JsonSource annotation, test sources is placed under resources dir. */ public final class ProjJson implements Project { /** From 1de56ddb8008676d25b87ae02c9c40a95588803e Mon Sep 17 00:00:00 2001 From: Artem Getmanskii Date: Thu, 13 Mar 2025 17:23:21 +0300 Subject: [PATCH 11/12] fix 0pdd check --- src/io/github/artemget/prbot/domain/pr/AccJson.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/io/github/artemget/prbot/domain/pr/AccJson.java b/src/io/github/artemget/prbot/domain/pr/AccJson.java index 82fd577..3f46c5e 100644 --- a/src/io/github/artemget/prbot/domain/pr/AccJson.java +++ b/src/io/github/artemget/prbot/domain/pr/AccJson.java @@ -85,8 +85,8 @@ public String username() throws EmptyArgumentException { } } - //@todo #20:45min Intellij generates equals and hashcode - // not the elegant way. Lets add custom template for equals. + // @todo #20:45min Intellij generates equals and hashcode + // not the elegant way. Lets add custom template for equals. //@checkstyle NeedBracesCheck (20 lines) //@checkstyle HiddenFieldCheck (10 lines) @SuppressWarnings({ From 84d4f6ae9df3b8ac8118c1cd053fef904c4a4df6 Mon Sep 17 00:00:00 2001 From: Artem Getmanskii Date: Thu, 13 Mar 2025 17:33:50 +0300 Subject: [PATCH 12/12] add test resource dir configuration --- pom.xml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 3b5e21d..8b77cda 100644 --- a/pom.xml +++ b/pom.xml @@ -82,9 +82,14 @@ src test + + + resources + + - ../resources + resources