diff --git a/src/main/java/git/tracehub/codereview/action/prompt/TextReviews.java b/src/main/java/git/tracehub/codereview/action/prompt/TextReviews.java new file mode 100644 index 0000000..a0e8ded --- /dev/null +++ b/src/main/java/git/tracehub/codereview/action/prompt/TextReviews.java @@ -0,0 +1,69 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2024 Tracehub.git + * + * 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 NON-INFRINGEMENT. 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 git.tracehub.codereview.action.prompt; + +import java.util.Collection; +import javax.json.JsonArray; +import javax.json.JsonObject; +import lombok.RequiredArgsConstructor; +import org.cactoos.Text; +import org.cactoos.list.ListOf; + +/** + * Reviews into text. + * + * @since 0.0.0 + */ +@RequiredArgsConstructor +public final class TextReviews implements Text { + + /** + * Reviews. + */ + private final JsonArray reviews; + + @Override + public String asString() throws Exception { + final Collection accum = new ListOf<>(); + this.reviews.forEach( + value -> { + final JsonObject json = value.asJsonObject(); + final String head = json.getString("submitted"); + final Collection comments = new ListOf<>(); + json.getJsonArray("comments") + .forEach(comment -> comments.add(comment.toString())); + accum.add( + String.format( + "Feedback: %s; Comments: %s", + head, + comments + ) + ); + } + ); + final StringBuilder builder = new StringBuilder(0); + accum.forEach(r -> builder.append(r).append('\n')); + return builder.toString(); + } +} diff --git a/src/test/java/git/tracehub/codereview/action/prompt/TextReviewsTest.java b/src/test/java/git/tracehub/codereview/action/prompt/TextReviewsTest.java new file mode 100644 index 0000000..ede835a --- /dev/null +++ b/src/test/java/git/tracehub/codereview/action/prompt/TextReviewsTest.java @@ -0,0 +1,92 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2024 Tracehub.git + * + * 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 NON-INFRINGEMENT. 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 git.tracehub.codereview.action.prompt; + +import java.io.InputStreamReader; +import javax.json.Json; +import org.cactoos.io.ResourceOf; +import org.hamcrest.MatcherAssert; +import org.hamcrest.core.IsEqual; +import org.junit.jupiter.api.Test; + +/** + * Test case for {@link TextReviews}. + * + * @since 0.1.22 + */ +final class TextReviewsTest { + + @Test + void transformsReviewIntoText() throws Exception { + final String text = new TextReviews( + Json.createArrayBuilder() + .add( + Json.createObjectBuilder() + .add("submitted", "just one") + .add( + "comments", + Json.createArrayBuilder() + .add("one comment") + .build() + ).build() + ).build() + ).asString(); + final String expected = "Feedback: just one; Comments: [\"one comment\"]\n"; + MatcherAssert.assertThat( + String.format( + "Output text (%s) does not match with expected (%s)", + text, + expected + ), + text, + new IsEqual<>(expected) + ); + } + + @Test + void transformsMultipleReviewsIntoText() throws Exception { + final String text = new TextReviews( + Json.createReader( + new InputStreamReader( + new ResourceOf( + "git/tracehub/codereview/action/prompt/reviews.json" + ).stream() + ) + ).readArray() + ).asString(); + final String expected = + "Feedback: comment; Comments: [\"test1\", \"test2\", \"test3\"]" + + "\nFeedback: approve; Comments: [\"test4\"]\nFeedback: r;" + + " Comments: [\"test5\", \"test6\"]\n"; + MatcherAssert.assertThat( + String.format( + "Output text (%s) does not match with expected (%s)", + text, + expected + ), + text, + new IsEqual<>(expected) + ); + } +} diff --git a/src/test/resources/git/tracehub/codereview/action/prompt/reviews.json b/src/test/resources/git/tracehub/codereview/action/prompt/reviews.json new file mode 100644 index 0000000..20bd43f --- /dev/null +++ b/src/test/resources/git/tracehub/codereview/action/prompt/reviews.json @@ -0,0 +1,14 @@ +[ + { + "submitted": "comment", + "comments": ["test1", "test2", "test3"] + }, + { + "submitted": "approve", + "comments": ["test4"] + }, + { + "submitted": "r", + "comments": ["test5", "test6"] + } +]