From 26ab70bda5d9ffa02346b5f36c6d9ad242c730d4 Mon Sep 17 00:00:00 2001 From: Artem Getmanskii Date: Sat, 8 Mar 2025 19:45:38 +0300 Subject: [PATCH 01/16] remove matchers-json dep --- pom.xml | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/pom.xml b/pom.xml index d1ff7ea..f5fc115 100644 --- a/pom.xml +++ b/pom.xml @@ -29,19 +29,14 @@ 1.0.1 - wtf.g4s8 - matchers-json - 1.4.0 + jakarta.json + jakarta.json-api + 2.1.3 - javax.json - javax.json-api - 1.1.4 - - - org.glassfish - javax.json - 1.1.4 + org.eclipse.parsson + parsson + 1.1.7 org.jdbi @@ -75,7 +70,6 @@ org.hamcrest hamcrest-all 1.3 - test @@ -243,7 +237,10 @@ dependencies:org.hamcrest:hamcrest:jar:2.2:test - dependencies:org.hamcrest:hamcrest:jar:2.2:compile + dependencies:org.hamcrest:hamcrest-all:jar:1.3:compile + + + dependencies:org.eclipse.parsson:parsson:jar:1.1.7:compile dependencies:org.cactoos:cactoos:jar:0.56.1:compile From 2f9592dd24af98e616544d15a2c977acf3006e41 Mon Sep 17 00:00:00 2001 From: Artem Getmanskii Date: Sat, 8 Mar 2025 19:45:58 +0300 Subject: [PATCH 02/16] fix import AccJson --- src/io/github/artemget/prbot/domain/pr/AccJson.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/io/github/artemget/prbot/domain/pr/AccJson.java b/src/io/github/artemget/prbot/domain/pr/AccJson.java index 4d0a77d..703bf59 100644 --- a/src/io/github/artemget/prbot/domain/pr/AccJson.java +++ b/src/io/github/artemget/prbot/domain/pr/AccJson.java @@ -26,7 +26,7 @@ import io.github.artemget.prbot.config.EJsonStr; import io.github.artemget.prbot.config.EntryException; -import javax.json.JsonObject; +import jakarta.json.JsonObject; /** * User's account from json source. From 7a3a11f4c9d363dc9570693e2e61247104302dc9 Mon Sep 17 00:00:00 2001 From: Artem Getmanskii Date: Sat, 8 Mar 2025 19:46:07 +0300 Subject: [PATCH 03/16] fix import AccJsonTest --- test/io/github/artemget/prbot/domain/pr/AccJsonTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/io/github/artemget/prbot/domain/pr/AccJsonTest.java b/test/io/github/artemget/prbot/domain/pr/AccJsonTest.java index 32019da..2dfdc86 100644 --- a/test/io/github/artemget/prbot/domain/pr/AccJsonTest.java +++ b/test/io/github/artemget/prbot/domain/pr/AccJsonTest.java @@ -24,7 +24,7 @@ package io.github.artemget.prbot.domain.pr; -import javax.json.Json; +import jakarta.json.Json; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.jupiter.api.Assertions; From 5efc9166963fcfabaa3037ca5d9a0ed6bc5a2b8c Mon Sep 17 00:00:00 2001 From: Artem Getmanskii Date: Sat, 8 Mar 2025 19:46:17 +0300 Subject: [PATCH 04/16] fix import BrJson --- src/io/github/artemget/prbot/domain/pr/BrJson.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/io/github/artemget/prbot/domain/pr/BrJson.java b/src/io/github/artemget/prbot/domain/pr/BrJson.java index 12f38fc..bb6991a 100644 --- a/src/io/github/artemget/prbot/domain/pr/BrJson.java +++ b/src/io/github/artemget/prbot/domain/pr/BrJson.java @@ -26,7 +26,7 @@ import io.github.artemget.prbot.config.EJsonStr; import io.github.artemget.prbot.config.EntryException; -import javax.json.JsonObject; +import jakarta.json.JsonObject; /** * Git branch from json source. From df51362faf68ee7b9ec033ad10f76a1a0a26e3c3 Mon Sep 17 00:00:00 2001 From: Artem Getmanskii Date: Sat, 8 Mar 2025 19:46:24 +0300 Subject: [PATCH 05/16] fix import BrJsonTest --- test/io/github/artemget/prbot/domain/pr/BrJsonTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/io/github/artemget/prbot/domain/pr/BrJsonTest.java b/test/io/github/artemget/prbot/domain/pr/BrJsonTest.java index 293ab84..a9be908 100644 --- a/test/io/github/artemget/prbot/domain/pr/BrJsonTest.java +++ b/test/io/github/artemget/prbot/domain/pr/BrJsonTest.java @@ -24,7 +24,7 @@ package io.github.artemget.prbot.domain.pr; -import javax.json.Json; +import jakarta.json.Json; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.jupiter.api.Assertions; From 291c2dd89740418621669f4fe9fcb5ef90b2ec90 Mon Sep 17 00:00:00 2001 From: Artem Getmanskii Date: Sat, 8 Mar 2025 19:46:33 +0300 Subject: [PATCH 06/16] fix import EJsonArr --- src/io/github/artemget/prbot/config/EJsonArr.java | 4 ++-- test/io/github/artemget/prbot/config/EJsonArrTest.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/io/github/artemget/prbot/config/EJsonArr.java b/src/io/github/artemget/prbot/config/EJsonArr.java index 894e8d2..01b4c5c 100644 --- a/src/io/github/artemget/prbot/config/EJsonArr.java +++ b/src/io/github/artemget/prbot/config/EJsonArr.java @@ -24,8 +24,8 @@ package io.github.artemget.prbot.config; -import javax.json.JsonArray; -import javax.json.JsonObject; +import jakarta.json.JsonArray; +import jakarta.json.JsonObject; import org.cactoos.Scalar; /** diff --git a/test/io/github/artemget/prbot/config/EJsonArrTest.java b/test/io/github/artemget/prbot/config/EJsonArrTest.java index 3fc7f49..042b280 100644 --- a/test/io/github/artemget/prbot/config/EJsonArrTest.java +++ b/test/io/github/artemget/prbot/config/EJsonArrTest.java @@ -1,7 +1,7 @@ package io.github.artemget.prbot.config; -import javax.json.Json; -import javax.json.JsonObject; +import jakarta.json.Json; +import jakarta.json.JsonObject; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.jupiter.api.Assertions; From 0ed38494fa89716d051e6735180fa1ad8a600c8e Mon Sep 17 00:00:00 2001 From: Artem Getmanskii Date: Sat, 8 Mar 2025 19:46:42 +0300 Subject: [PATCH 07/16] fix import EJsonObj --- src/io/github/artemget/prbot/config/EJsonObj.java | 2 +- test/io/github/artemget/prbot/config/EJsonObjTest.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/io/github/artemget/prbot/config/EJsonObj.java b/src/io/github/artemget/prbot/config/EJsonObj.java index fa322a5..11158f3 100644 --- a/src/io/github/artemget/prbot/config/EJsonObj.java +++ b/src/io/github/artemget/prbot/config/EJsonObj.java @@ -24,7 +24,7 @@ package io.github.artemget.prbot.config; -import javax.json.JsonObject; +import jakarta.json.JsonObject; import org.cactoos.Scalar; /** diff --git a/test/io/github/artemget/prbot/config/EJsonObjTest.java b/test/io/github/artemget/prbot/config/EJsonObjTest.java index f547735..ca3f962 100644 --- a/test/io/github/artemget/prbot/config/EJsonObjTest.java +++ b/test/io/github/artemget/prbot/config/EJsonObjTest.java @@ -24,8 +24,8 @@ package io.github.artemget.prbot.config; -import javax.json.Json; -import javax.json.JsonObject; +import jakarta.json.Json; +import jakarta.json.JsonObject; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.jupiter.api.Assertions; From 6eb61fdf0ab50f372a7bcd34a12e1f891a8e4f03 Mon Sep 17 00:00:00 2001 From: Artem Getmanskii Date: Sat, 8 Mar 2025 19:46:49 +0300 Subject: [PATCH 08/16] fix import EJsonStrTest --- src/io/github/artemget/prbot/config/EJsonStr.java | 2 +- test/io/github/artemget/prbot/config/EJsonStrTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/io/github/artemget/prbot/config/EJsonStr.java b/src/io/github/artemget/prbot/config/EJsonStr.java index 60f63ed..f77544f 100644 --- a/src/io/github/artemget/prbot/config/EJsonStr.java +++ b/src/io/github/artemget/prbot/config/EJsonStr.java @@ -24,7 +24,7 @@ package io.github.artemget.prbot.config; -import javax.json.JsonObject; +import jakarta.json.JsonObject; import org.cactoos.Scalar; /** diff --git a/test/io/github/artemget/prbot/config/EJsonStrTest.java b/test/io/github/artemget/prbot/config/EJsonStrTest.java index 77c70e4..80ac9a7 100644 --- a/test/io/github/artemget/prbot/config/EJsonStrTest.java +++ b/test/io/github/artemget/prbot/config/EJsonStrTest.java @@ -24,7 +24,7 @@ package io.github.artemget.prbot.config; -import javax.json.Json; +import jakarta.json.Json; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.jupiter.api.Assertions; From 71f539a1393855dd352d29e735cf5e5514e84ae0 Mon Sep 17 00:00:00 2001 From: Artem Getmanskii Date: Sat, 8 Mar 2025 19:47:01 +0300 Subject: [PATCH 09/16] fix import MatchJsonVal --- src/io/github/artemget/prbot/bot/match/MatchJsonVal.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/io/github/artemget/prbot/bot/match/MatchJsonVal.java b/src/io/github/artemget/prbot/bot/match/MatchJsonVal.java index 1c1975c..66ecd0f 100644 --- a/src/io/github/artemget/prbot/bot/match/MatchJsonVal.java +++ b/src/io/github/artemget/prbot/bot/match/MatchJsonVal.java @@ -28,8 +28,6 @@ import java.util.function.Predicate; import org.hamcrest.TypeSafeMatcher; import org.telegram.telegrambots.meta.api.objects.Update; -import wtf.g4s8.hamcrest.json.JsonHas; -import wtf.g4s8.hamcrest.json.StringIsJson; /** * Wraps {@link MatchJson}. From 39ff81222e0b1a14028eb2f267438b7821b3dd27 Mon Sep 17 00:00:00 2001 From: Artem Getmanskii Date: Sat, 8 Mar 2025 19:47:11 +0300 Subject: [PATCH 10/16] fix import PrJsonStrict --- src/io/github/artemget/prbot/domain/pr/PrJsonStrict.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 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..ce31f2d 100644 --- a/src/io/github/artemget/prbot/domain/pr/PrJsonStrict.java +++ b/src/io/github/artemget/prbot/domain/pr/PrJsonStrict.java @@ -28,13 +28,13 @@ import io.github.artemget.prbot.config.EJsonObj; import io.github.artemget.prbot.config.EJsonStr; import io.github.artemget.prbot.config.EntryException; +import jakarta.json.Json; +import jakarta.json.JsonArray; +import jakarta.json.JsonObject; +import jakarta.json.JsonReader; import java.io.StringReader; import java.util.List; import java.util.stream.Collectors; -import javax.json.Json; -import javax.json.JsonArray; -import javax.json.JsonObject; -import javax.json.JsonReader; /** * Pull request from source strict json. From 2fc2a9822f351b164d3298cb1bea1bc1540f30da Mon Sep 17 00:00:00 2001 From: Artem Getmanskii Date: Sat, 8 Mar 2025 19:47:30 +0300 Subject: [PATCH 11/16] fix import ProjJson --- src/io/github/artemget/prbot/domain/pr/ProjJson.java | 2 +- test/io/github/artemget/prbot/webhook/TkHookGitlabTest.java | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/io/github/artemget/prbot/domain/pr/ProjJson.java b/src/io/github/artemget/prbot/domain/pr/ProjJson.java index be414a7..2c2cf75 100644 --- a/src/io/github/artemget/prbot/domain/pr/ProjJson.java +++ b/src/io/github/artemget/prbot/domain/pr/ProjJson.java @@ -24,7 +24,7 @@ package io.github.artemget.prbot.domain.pr; -import javax.json.JsonObject; +import jakarta.json.JsonObject; /** * Git project from json source. diff --git a/test/io/github/artemget/prbot/webhook/TkHookGitlabTest.java b/test/io/github/artemget/prbot/webhook/TkHookGitlabTest.java index 12eb6af..e937e6c 100644 --- a/test/io/github/artemget/prbot/webhook/TkHookGitlabTest.java +++ b/test/io/github/artemget/prbot/webhook/TkHookGitlabTest.java @@ -29,7 +29,6 @@ import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; import org.takes.rq.RqFake; import org.takes.rq.RqWithBody; import org.telegram.telegrambots.meta.api.objects.User; From efe4a64e79e62d2e362c51fa59b3ae8d4f261a1f Mon Sep 17 00:00:00 2001 From: Artem Getmanskii Date: Sat, 8 Mar 2025 19:47:36 +0300 Subject: [PATCH 12/16] fix import ReqHkGitlab --- src/io/github/artemget/prbot/domain/pr/ReqHkGitlab.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/io/github/artemget/prbot/domain/pr/ReqHkGitlab.java b/src/io/github/artemget/prbot/domain/pr/ReqHkGitlab.java index 8b2c067..8ee9f2f 100644 --- a/src/io/github/artemget/prbot/domain/pr/ReqHkGitlab.java +++ b/src/io/github/artemget/prbot/domain/pr/ReqHkGitlab.java @@ -26,10 +26,10 @@ import io.github.artemget.prbot.config.EJsonObj; import io.github.artemget.prbot.config.EntryException; +import jakarta.json.Json; +import jakarta.json.JsonObject; +import jakarta.json.JsonReader; import java.io.StringReader; -import javax.json.Json; -import javax.json.JsonObject; -import javax.json.JsonReader; /** * Gitlab webhook request. @@ -54,7 +54,7 @@ public final class ReqHkGitlab implements RequestWebhook { private final JsonObject json; /** - * Wraps json string in to {@link javax.json.JsonObject}. + * Wraps json string in to {@link JsonObject}. * * @param json String * @throws EntryException If fails From d5662bb0e720994d7812c581f0bcb753ffedee3d Mon Sep 17 00:00:00 2001 From: Artem Getmanskii Date: Sat, 8 Mar 2025 19:47:42 +0300 Subject: [PATCH 13/16] fix import RouteHkGitlab --- src/io/github/artemget/prbot/bot/route/RouteHkGitlab.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/io/github/artemget/prbot/bot/route/RouteHkGitlab.java b/src/io/github/artemget/prbot/bot/route/RouteHkGitlab.java index fa69521..71f7379 100644 --- a/src/io/github/artemget/prbot/bot/route/RouteHkGitlab.java +++ b/src/io/github/artemget/prbot/bot/route/RouteHkGitlab.java @@ -29,7 +29,9 @@ import io.github.artemget.prbot.bot.command.CmdPrMerge; import io.github.artemget.prbot.bot.command.CmdPrOpen; import io.github.artemget.prbot.bot.command.CmdPrReject; +import io.github.artemget.prbot.bot.match.JsonHas; import io.github.artemget.prbot.bot.match.MatchJsonVal; +import io.github.artemget.prbot.bot.match.StringIsJson; import io.github.artemget.prbot.config.EProp; import io.github.artemget.prbot.config.Entry; import io.github.artemget.prbot.config.EntryException; @@ -39,8 +41,6 @@ import io.github.artemget.teleroute.route.RouteFork; import org.telegram.telegrambots.meta.api.objects.Update; import org.telegram.telegrambots.meta.bots.AbsSender; -import wtf.g4s8.hamcrest.json.JsonHas; -import wtf.g4s8.hamcrest.json.StringIsJson; /** * Routes for gitlab merge request webhook. From ede986f05cb8b4ecace31cd201b86621f270f586 Mon Sep 17 00:00:00 2001 From: Artem Getmanskii Date: Sat, 8 Mar 2025 19:48:28 +0300 Subject: [PATCH 14/16] move classes from matchers-json due to they are not supporting jakarta.json --- .../artemget/prbot/bot/match/JsonHas.java | 112 +++++++++++ .../artemget/prbot/bot/match/JsonValueIs.java | 174 ++++++++++++++++++ .../prbot/bot/match/StringIsJson.java | 140 ++++++++++++++ 3 files changed, 426 insertions(+) create mode 100644 src/io/github/artemget/prbot/bot/match/JsonHas.java create mode 100644 src/io/github/artemget/prbot/bot/match/JsonValueIs.java create mode 100644 src/io/github/artemget/prbot/bot/match/StringIsJson.java diff --git a/src/io/github/artemget/prbot/bot/match/JsonHas.java b/src/io/github/artemget/prbot/bot/match/JsonHas.java new file mode 100644 index 0000000..48a85cc --- /dev/null +++ b/src/io/github/artemget/prbot/bot/match/JsonHas.java @@ -0,0 +1,112 @@ +/* + * 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.bot.match; + +import jakarta.json.JsonObject; +import jakarta.json.JsonValue; +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.TypeSafeMatcher; + +/** + * Matches json object fields. + * + * @since 0.1 + */ +@SuppressWarnings("PMD.AvoidDuplicateLiterals") +public final class JsonHas extends TypeSafeMatcher { + + /** + * Field name. + */ + private final String field; + + /** + * Value matcher. + */ + private final Matcher matcher; + + /** + * JSON has a string value for field. + * @param field Name + * @param value Expected string + */ + public JsonHas(final String field, final String value) { + this(field, new JsonValueIs(value)); + } + + /** + * JSON has a number value for field. + * @param field Name + * @param value Expected string + */ + public JsonHas(final String field, final Number value) { + this(field, new JsonValueIs(value)); + } + + /** + * JSON has a boolean value for field. + * @param field Name + * @param value Expected string + */ + public JsonHas(final String field, final boolean value) { + this(field, new JsonValueIs(value)); + } + + /** + * Ctor. + * + * @param field Field name + * @param matcher Value matcher + */ + public JsonHas(final String field, + final Matcher matcher) { + super(); + this.field = field; + this.matcher = matcher; + } + + @Override + public void describeTo(final Description description) { + description.appendText("field ") + .appendValue(this.field) + .appendText(" with ") + .appendDescriptionOf(this.matcher); + } + + @Override + public boolean matchesSafely(final JsonObject item) { + return this.matcher.matches(item.get(this.field)); + } + + @Override + public void describeMismatchSafely(final JsonObject item, + final Description desc) { + desc.appendText("field ") + .appendValue(this.field) + .appendText(" "); + this.matcher.describeMismatch(item.get(this.field), desc); + } +} diff --git a/src/io/github/artemget/prbot/bot/match/JsonValueIs.java b/src/io/github/artemget/prbot/bot/match/JsonValueIs.java new file mode 100644 index 0000000..89cfeb8 --- /dev/null +++ b/src/io/github/artemget/prbot/bot/match/JsonValueIs.java @@ -0,0 +1,174 @@ +/* + * 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.bot.match; + +import jakarta.json.JsonValue; +import java.util.Locale; +import org.hamcrest.CoreMatchers; +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.TypeSafeMatcher; + +/** + * Matches json values. + * + * @since 0.1 + */ +@SuppressWarnings("PMD.AvoidDuplicateLiterals") +public final class JsonValueIs extends TypeSafeMatcher { + + /** + * Matcher for json-null. + */ + public static final TypeSafeMatcher NULL = + new JsonValueIs( + CoreMatchers.equalTo(JsonValue.ValueType.NULL), + CoreMatchers.any(String.class) + ); + + /** + * Json type. + */ + private final Matcher type; + + /** + * Json value matcher. + */ + private final Matcher value; + + /** + * Ctor for boolean. + * + * @param bool Boolean value + */ + public JsonValueIs(final boolean bool) { + this( + CoreMatchers.equalTo( + JsonValue.ValueType.valueOf(Boolean.toString(bool) + .toUpperCase(Locale.US) + ) + ), + CoreMatchers.equalTo(Boolean.toString(bool)) + ); + } + + /** + * Ctor for number. + * + * @param number Expected number + */ + public JsonValueIs(final Number number) { + this( + CoreMatchers.equalTo(JsonValue.ValueType.NUMBER), + CoreMatchers.equalTo(number.toString()) + ); + } + + /** + * Ctor for string. + * + * @param value Expected value + */ + public JsonValueIs(final String value) { + this( + CoreMatchers.equalTo(JsonValue.ValueType.STRING), + CoreMatchers.equalTo(value) + ); + } + + /** + * Ctor for string. + * + * @param value Json value. + */ + public JsonValueIs(final Matcher value) { + this(CoreMatchers.equalTo(JsonValue.ValueType.STRING), value); + } + + /** + * Ctor. + * + * @param type Value type + * @param matcher Value matcher + */ + public JsonValueIs(final JsonValue.ValueType type, + final Matcher matcher) { + this(CoreMatchers.equalTo(type), matcher); + } + + /** + * Ctor. + * + * @param type Json type. + * @param value Json value. + */ + public JsonValueIs(final Matcher type, + final Matcher value) { + super(); + this.type = type; + this.value = value; + } + + @Override + public void describeTo(final Description desc) { + desc.appendText("value ") + .appendDescriptionOf(this.value) + .appendText(" of type ") + .appendDescriptionOf(this.type); + } + + @Override + public boolean matchesSafely(final JsonValue item) { + return item != null + && this.type.matches(item.getValueType()) + && this.value.matches(JsonValueIs.escaped(item)); + } + + @Override + public void describeMismatchSafely(final JsonValue item, + final Description desc) { + desc.appendText("value ") + .appendValue(JsonValueIs.escaped(item)) + .appendText(" of type ") + .appendValue(item.getValueType()); + } + + /** + * Escaped string. + * + * @param value Json value + * @return Escaped string + */ + private static String escaped(final JsonValue value) { + final String esc; + final String src = value.toString(); + if (value.getValueType().equals(JsonValue.ValueType.STRING)) { + esc = src.substring(1).substring(0, src.length() - 2); + } else { + esc = src; + } + return esc; + } +} diff --git a/src/io/github/artemget/prbot/bot/match/StringIsJson.java b/src/io/github/artemget/prbot/bot/match/StringIsJson.java new file mode 100644 index 0000000..a571f93 --- /dev/null +++ b/src/io/github/artemget/prbot/bot/match/StringIsJson.java @@ -0,0 +1,140 @@ +/* + * 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.bot.match; + +import jakarta.json.Json; +import jakarta.json.JsonArray; +import jakarta.json.JsonObject; +import jakarta.json.JsonReader; +import jakarta.json.JsonValue; +import jakarta.json.stream.JsonParsingException; +import java.io.StringReader; +import java.util.function.Function; +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.TypeSafeMatcher; + +/** + * Match a string against json matcher. + * + * @since 0.2 + */ +@SuppressWarnings("PMD.MissingStaticMethodInNonInstantiatableClass") +public abstract class StringIsJson extends TypeSafeMatcher { + + /** + * Json matcher. + */ + private final Matcher matcher; + + /** + * Parsing of json. + */ + private final Function parsing; + + /** + * Ctor. + * + * @param matcher Json matcher + * @param parsing Json parsing + */ + private StringIsJson( + final Matcher matcher, + final Function parsing + ) { + super(); + this.matcher = matcher; + this.parsing = parsing; + } + + @Override + public void describeTo(final Description description) { + description.appendText("string ") + .appendDescriptionOf(this.matcher); + } + + @Override + public void describeMismatchSafely( + final String item, + final Description description + ) { + description.appendText("string: '") + .appendValue(item) + .appendText("' "); + try { + this.matcher.describeMismatch( + this.parsing.apply(Json.createReader(new StringReader(item))), + description + ); + } catch (final JsonParsingException err) { + description.appendText("is not a valid json: ") + .appendText(err.getMessage()); + } + } + + @Override + public boolean matchesSafely(final String item) { + boolean success; + try { + success = this.matcher.matches( + this.parsing.apply(Json.createReader(new StringReader(item))) + ); + } catch (final JsonParsingException ignored) { + success = false; + } + return success; + } + + /** + * Json object as string. + * @since 0.2 + */ + public static final class Object extends StringIsJson { + + /** + * String is JSON object. + * + * @param matcher Object matcher + */ + public Object(final Matcher matcher) { + super(matcher, JsonReader::readObject); + } + } + + /** + * Json array as string. + * @since 0.2 + */ + public static final class Array extends StringIsJson { + + /** + * String is JSON array. + * @param matcher Array matcher + */ + public Array(final Matcher matcher) { + super(matcher, JsonReader::readArray); + } + } +} From 0cc18e3e41cca78f093728e0622f1f23ed2a2ad7 Mon Sep 17 00:00:00 2001 From: Artem Getmanskii Date: Sat, 8 Mar 2025 19:51:53 +0300 Subject: [PATCH 15/16] update todo --- src/io/github/artemget/prbot/domain/pr/ProjJson.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/io/github/artemget/prbot/domain/pr/ProjJson.java b/src/io/github/artemget/prbot/domain/pr/ProjJson.java index 2c2cf75..36cc92d 100644 --- a/src/io/github/artemget/prbot/domain/pr/ProjJson.java +++ b/src/io/github/artemget/prbot/domain/pr/ProjJson.java @@ -30,13 +30,11 @@ * Git project from json source. * * @since 0.0.1 - * @todo #20:90 {@link io.github.artemget.prbot.domain.pr.ProjJson} + * @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. - * Note that using of junit-json-params requires to switch javax.json to - * jakarta.json. */ public final class ProjJson implements Project { /** From 2350de9528176ed47552fca16bf82d994dcb177d Mon Sep 17 00:00:00 2001 From: Artem Getmanskii Date: Sat, 8 Mar 2025 20:00:42 +0300 Subject: [PATCH 16/16] add matchers-json todo --- src/io/github/artemget/prbot/bot/match/JsonHas.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/io/github/artemget/prbot/bot/match/JsonHas.java b/src/io/github/artemget/prbot/bot/match/JsonHas.java index 48a85cc..dcf7fa3 100644 --- a/src/io/github/artemget/prbot/bot/match/JsonHas.java +++ b/src/io/github/artemget/prbot/bot/match/JsonHas.java @@ -34,6 +34,11 @@ * Matches json object fields. * * @since 0.1 + * @todo #20:15 remove {@link JsonHas}, + * {@link io.github.artemget.prbot.bot.match.JsonValueIs} + * and {@link io.github.artemget.prbot.bot.match.StringIsJson} from this project when + * matchers-json jakarta issue + * would be closed. Add matchers-json dependency and fix used imports through the project. */ @SuppressWarnings("PMD.AvoidDuplicateLiterals") public final class JsonHas extends TypeSafeMatcher {