Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,14 @@
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>wtf.g4s8</groupId>
<artifactId>matchers-json</artifactId>
<version>1.4.0</version>
<groupId>jakarta.json</groupId>
<artifactId>jakarta.json-api</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
<version>1.1.4</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.1.4</version>
<groupId>org.eclipse.parsson</groupId>
<artifactId>parsson</artifactId>
<version>1.1.7</version>
</dependency>
<dependency>
<groupId>org.jdbi</groupId>
Expand Down Expand Up @@ -75,7 +70,6 @@
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
Expand Down Expand Up @@ -243,7 +237,10 @@
dependencies:org.hamcrest:hamcrest:jar:2.2:test
</exclude>
<exclude>
dependencies:org.hamcrest:hamcrest:jar:2.2:compile
dependencies:org.hamcrest:hamcrest-all:jar:1.3:compile
</exclude>
<exclude>
dependencies:org.eclipse.parsson:parsson:jar:1.1.7:compile
</exclude>
<exclude>
dependencies:org.cactoos:cactoos:jar:0.56.1:compile
Expand Down
117 changes: 117 additions & 0 deletions src/io/github/artemget/prbot/bot/match/JsonHas.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* 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
* @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
* <a href="https://github.com/g4s8/matchers-json/issues/16">matchers-json jakarta issue</a>
* would be closed. Add matchers-json dependency and fix used imports through the project.
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public final class JsonHas extends TypeSafeMatcher<JsonObject> {

/**
* Field name.
*/
private final String field;

/**
* Value matcher.
*/
private final Matcher<? extends JsonValue> 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<? extends JsonValue> 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);
}
}
174 changes: 174 additions & 0 deletions src/io/github/artemget/prbot/bot/match/JsonValueIs.java
Original file line number Diff line number Diff line change
@@ -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<JsonValue> {

/**
* Matcher for json-null.
*/
public static final TypeSafeMatcher<JsonValue> NULL =
new JsonValueIs(
CoreMatchers.equalTo(JsonValue.ValueType.NULL),
CoreMatchers.any(String.class)
);

/**
* Json type.
*/
private final Matcher<JsonValue.ValueType> type;

/**
* Json value matcher.
*/
private final Matcher<String> 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<String> 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<String> matcher) {
this(CoreMatchers.equalTo(type), matcher);
}

/**
* Ctor.
*
* @param type Json type.
* @param value Json value.
*/
public JsonValueIs(final Matcher<JsonValue.ValueType> type,
final Matcher<String> 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;
}
}
2 changes: 0 additions & 2 deletions src/io/github/artemget/prbot/bot/match/MatchJsonVal.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand Down
Loading