AssertJson.java
/*
** Module : AssertJson.java
** Abstract : Implementation of the builtin class.
**
** Copyright (c) 2021-2023, Golden Code Development Corporation.
**
** -#- -I- --Date-- -------------------------------Description--------------------------------
** 001 ME 20210908 First version, stubs taken by converting the skeleton using FWD.
** 002 VVT 20230318 The file re-designed completely, API is compatible with the latest
** OE releases. Javadocs added. All methods are now fully implemented.
** See #3827.
** 003 CA 20231113 The 'execute' method must be annotated with LegacySignature Type.Execute, and also can be
** dropped if is a no-op.
*/
/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU Affero General Public License for more details.
**
** You may find a copy of the GNU Affero GPL version 3 at the following
** location: https://www.gnu.org/licenses/agpl-3.0.en.html
**
** Additional terms under GNU Affero GPL version 3 section 7:
**
** Under Section 7 of the GNU Affero GPL version 3, the following additional
** terms apply to the works covered under the License. These additional terms
** are non-permissive additional terms allowed under Section 7 of the GNU
** Affero GPL version 3 and may not be removed by you.
**
** 0. Attribution Requirement.
**
** You must preserve all legal notices or author attributions in the covered
** work or Appropriate Legal Notices displayed by works containing the covered
** work. You may not remove from the covered work any author or developer
** credit already included within the covered work.
**
** 1. No License To Use Trademarks.
**
** This license does not grant any license or rights to use the trademarks
** Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
** of Golden Code Development Corporation. You are not authorized to use the
** name Golden Code, FWD, or the names of any author or contributor, for
** publicity purposes without written authorization.
**
** 2. No Misrepresentation of Affiliation.
**
** You may not represent yourself as Golden Code Development Corporation or FWD.
**
** You may not represent yourself for publicity purposes as associated with
** Golden Code Development Corporation, FWD, or any author or contributor to
** the covered work, without written authorization.
**
** 3. No Misrepresentation of Source or Origin.
**
** You may not represent the covered work as solely your work. All modified
** versions of the covered work must be marked in a reasonable way to make it
** clear that the modified work is not originating from Golden Code Development
** Corporation or FWD. All modified versions must contain the notices of
** attribution required in this license.
*/
package com.goldencode.p2j.oo.core.assertion;
import static com.goldencode.p2j.report.ReportConstants.*;
import static com.goldencode.p2j.util.BlockManager.*;
import static com.goldencode.p2j.util.CompareOps.*;
import static com.goldencode.p2j.util.character.valueOf;
import static com.goldencode.p2j.util.logical._not;
import com.goldencode.p2j.oo.core.*;
import com.goldencode.p2j.oo.json.objectmodel.*;
import com.goldencode.p2j.util.*;
import com.goldencode.p2j.util.InternalEntry.*;
/**
* Business logic (converted to Java from the 4GL source code
* in OpenEdge/Core/Assertion/AssertJson.cls).
*/
@LegacyResource(resource = "OpenEdge.Core.Assertion.AssertJson")
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public class AssertJson
extends AbstractAssert
{
/**
* Static constructor
*/
@LegacySignature(type = Type.CONSTRUCTOR)
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public static void __core_assertion_AssertJson_constructor__static__()
{
externalProcedure(AssertJson.class, new Block((Body) () -> {
onBlockLevel(Condition.ERROR, Action.THROW);
}));
}
/**
* Assert that a Json object has a particularly-named property (of any type).
*
* @param json
* The JSON object being checked
* @param name
* The name of the property being checked
*/
@LegacySignature(type = Type.METHOD, name = "HasProperty", parameters = {
@LegacyParameter(name = "json", qualified = "progress.json.objectmodel.jsonobject", type = "OBJECT", mode = "INPUT"),
@LegacyParameter(name = "name", type = "CHARACTER", mode = "INPUT") })
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public static void hasProperty(final object<? extends JsonObject> json, final character name)
{
checkJson(json);
Assert.notNullOrEmptyImpl(name, "Property name");
call("HasProperty", () -> {
if (!json.ref().has(name).booleanValue())
{
raiseError("JSON Object does not have a property named '&1'", name);
}
});
}
/**
* Assert that a JSON object has a given number of properties.
*
* @param json
* The JSON object being checked
* @param nProperties
* The expected number of properties
*/
@LegacySignature(type = Type.METHOD, name = "HasPropertyCount", parameters = {
@LegacyParameter(name = "json", qualified = "progress.json.objectmodel.jsonobject", type = "OBJECT", mode = "INPUT"),
@LegacyParameter(name = "nProperties", type = "INTEGER", mode = "INPUT") })
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public static void hasPropertyCount(final object<? extends JsonObject> json,
final integer nProperties)
{
checkJson(json);
Assert.isZeroOrPositiveImpl(nProperties, "Property count");
call("HasPropertyCount", () -> {
Assert.legacyEquals(nProperties, new integer(json.ref().getNames().length));
});
}
/**
* Assert that an array element at the given index is null
*
* @param json
* The JSON array being checked
* @param index
* The array index being checked
*/
@LegacySignature(type = Type.METHOD, name = "IndexIsNull", parameters = {
@LegacyParameter(name = "json", qualified = "progress.json.objectmodel.jsonarray", type = "OBJECT", mode = "INPUT"),
@LegacyParameter(name = "index", type = "INTEGER", mode = "INPUT") })
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public static void indexIsNull(final object<? extends JsonArray> json, final integer index)
{
checkJsonArrayAndIndex(json, index);
call("IndexIsNull", () -> {
if (_not(json.ref().isNull(index)))
{
raiseError("Index &1 must be null", index);
}
});
}
/**
* Assert that the JSON Array element at the given index is of a particular type.
*
* @param json
* The JSON array being checked
* @param index
* The array index being checked
* @param type
* The JSON data type
*/
@LegacySignature(type = Type.METHOD, name = "IndexIsType", parameters = {
@LegacyParameter(name = "json", qualified = "progress.json.objectmodel.jsonarray", type = "OBJECT", mode = "INPUT"),
@LegacyParameter(name = "index", type = "INTEGER", mode = "INPUT"),
@LegacyParameter(name = "type", qualified = "openedge.core.jsondatatypeenum", type = "OBJECT", mode = "INPUT") })
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public static void indexIsType(final object<? extends JsonArray> json,
final integer index,
final object<? extends JsonDataTypeEnum> type)
{
checkJsonArrayAndIndex(json, index);
if (json.ref().getType(index).intValue() != type.ref()._getValue())
{
raiseError(TextOps.substitute("Index &1 must be of type &2", index, type.toString()));
}
}
/**
* Assert that an array element at the given index is not null
*
* @param json
* The JSON array being checked
* @param index
* The array index being checked
*/
@LegacySignature(type = Type.METHOD, name = "IndexNotNull", parameters = {
@LegacyParameter(name = "json", qualified = "progress.json.objectmodel.jsonarray", type = "OBJECT", mode = "INPUT"),
@LegacyParameter(name = "index", type = "INTEGER", mode = "INPUT") })
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public static void indexNotNull(final object<? extends JsonArray> json, final integer index)
{
call("IndexNotNull", () -> {
checkJsonArrayAndIndex(json, index);
if (json.ref().isNull(index).booleanValue())
{
raiseError("Index &1 must not be null", index);
}
});
}
/**
* Assert that a the value of a property is null.
*
* @param json
* The JSON object being checked
* @param name
* The name of the property being checked
*/
@LegacySignature(type = Type.METHOD, name = "PropertyIsNull", parameters = {
@LegacyParameter(name = "json", qualified = "progress.json.objectmodel.jsonobject", type = "OBJECT", mode = "INPUT"),
@LegacyParameter(name = "name", type = "CHARACTER", mode = "INPUT") })
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public static void propertyIsNull(final object<? extends JsonObject> json,
final character name)
{
hasProperty(json, name);
call("PropertyIsNull", () -> {
if (_not(json.ref().isNull(name)))
{
raiseError("Property '&1' must be null", name);
}
});
}
/**
* Assert that a JSON Object has a particularly-named property of a particular type.
*
* @param json
* The JSON object being checked
* @param name
* The name of the property being checked
* @param type
* The JSON data type
*/
@LegacySignature(type = Type.METHOD, name = "PropertyIsType", parameters = {
@LegacyParameter(name = "json", qualified = "progress.json.objectmodel.jsonobject", type = "OBJECT", mode = "INPUT"),
@LegacyParameter(name = "name", type = "CHARACTER", mode = "INPUT"),
@LegacyParameter(name = "type", qualified = "openedge.core.jsondatatypeenum", type = "OBJECT", mode = "INPUT") })
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public static void propertyIsType(final object<? extends JsonObject> json,
final character name,
final object<? extends JsonDataTypeEnum> type)
{
hasProperty(json, name);
AbstractAssert.notNullImpl(type, "Property type");
if (_not(isEqual(json.ref().getType(name), new integer(type))))
{
raiseError("Property '&1' must be of type &2", name, valueOf(type));
}
}
/**
* Assert that a the value of a property is not null
*
* @param json
* The JSON object being checked
* @param name
* The name of the property being checked
*/
@LegacySignature(type = Type.METHOD, name = "PropertyNotNull", parameters = {
@LegacyParameter(name = "json", qualified = "progress.json.objectmodel.jsonobject", type = "OBJECT", mode = "INPUT"),
@LegacyParameter(name = "name", type = "CHARACTER", mode = "INPUT") })
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public static void propertyNotNull(final object<? extends JsonObject> json,
final character name)
{
hasProperty(json, name);
call("PropertyNotNull", () -> {
if (json.ref().isNull(name).booleanValue())
{
raiseError("Property '&1' must not be null", name);
}
});
}
/**
* Implementation helper for all test methods.
*
* @param legacyName
* the legacy name of the method
* @param body
* the method body
*/
private static void call(final String legacyName, final Body body)
{
call(AssertFile.class, legacyName, body);
}
/**
* Check that the JSON object is known
*
* @param json
* the object to check
*/
private static void checkJson(final object<? extends JsonObject> json)
{
AbstractAssert.notNullImpl(json, "JSON Object");
}
/**
* Check that a JSON array object is known.
*
* @param json
* the array to check
*/
private static void checkJsonArray(final object<? extends JsonArray> json)
{
AbstractAssert.notNullImpl(json, "JSON Array");
}
/**
* Check that an array is valid and an array index is in range.
*
* @param json
* the array to check
* @param index
* the index to check
*/
private static void checkJsonArrayAndIndex(final object<? extends JsonArray> json, final integer index)
{
checkJsonArray(json);
Assert.isPositiveImpl(index, "Array index");
final JsonArray ref = json.ref();
final integer length = ref.getLength();
if (_isGreaterThan(index, length))
{
raiseError("Index &1 is larger than the array size &2", index, length);
}
}
/**
*
*/
@LegacySignature(type = Type.CONSTRUCTOR)
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public void __core_assertion_AssertJson_constructor__()
{
internalProcedure(AssertJson.class, this, "__core_assertion_AssertJson_constructor__",
new Block((Body) () -> {
__core_AbstractAssert_constructor__();
}));
}
}