-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
h1alexbel
committed
Apr 12, 2024
1 parent
b1ada16
commit e28359b
Showing
3 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
src/main/java/git/tracehub/codereview/action/prompt/TextReviews.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
src/test/java/git/tracehub/codereview/action/prompt/TextReviewsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/test/resources/git/tracehub/codereview/action/prompt/reviews.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} | ||
] |