Skip to content

Commit

Permalink
fix(#90): TextReviews
Browse files Browse the repository at this point in the history
  • Loading branch information
h1alexbel committed Apr 12, 2024
1 parent b1ada16 commit e28359b
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<String> accum = new ListOf<>();
this.reviews.forEach(
value -> {
final JsonObject json = value.asJsonObject();
final String head = json.getString("submitted");
final Collection<String> 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();
}
}
Original file line number Diff line number Diff line change
@@ -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)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"submitted": "comment",
"comments": ["test1", "test2", "test3"]
},
{
"submitted": "approve",
"comments": ["test4"]
},
{
"submitted": "r",
"comments": ["test5", "test6"]
}
]

0 comments on commit e28359b

Please sign in to comment.