diff --git a/pom.xml b/pom.xml index e53e4ef..8b77cda 100644 --- a/pom.xml +++ b/pom.xml @@ -66,13 +66,30 @@ 5.10.2 test + + net.joshka + junit-json-params + 5.10.2-r0 + test + + + org.eclipse.parsson + parsson + 1.1.1 + test + src test + + + resources + + - ../resources + resources @@ -234,6 +251,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/AccJson.java b/src/io/github/artemget/prbot/domain/pr/AccJson.java index 4d0a77d..3f46c5e 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,26 @@ 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({ + "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); + } } 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); + } } diff --git a/src/io/github/artemget/prbot/domain/pr/PrJsonStrict.java b/src/io/github/artemget/prbot/domain/pr/PrJsonStrict.java index 0fe1f9f..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; @@ -111,7 +112,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 +123,7 @@ public Status status() throws EmptyArgumentException { ); } try { - return PullRequest.Status.valueOf(status); + return PullRequest.Status.valueOf(status.toUpperCase(Locale.getDefault())); } catch (final IllegalArgumentException exception) { throw new EmptyArgumentException( String.format( @@ -152,7 +153,7 @@ public Account from() throws EmptyArgumentException { @Override public List assigners() throws EmptyArgumentException { - return this.accounts("assigners"); + return this.accounts("assignees"); } @Override 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 { /** 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..fb933b0 --- /dev/null +++ b/test/io/github/artemget/prbot/domain/pr/PrJsonStrictTest.java @@ -0,0 +1,171 @@ +/* + * 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.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; + +/** + * Test case for {@link io.github.artemget.prbot.domain.pr.PrJsonStrict} + * + * @since 0.0.1 + */ +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 { + 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() + ) + ) + ); + } +}