/
gns34
/
json-schema-faker-java
Обзор
Документация
Войти
/
gns34
/
json-schema-faker-java
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/test/java/com/github/jsonschemafaker/AdvancedFeaturesTest.java
335 строк
13 KB
ankit-d-sharma
Added json-schema-faker-java
01 авг 2025, 22:51
01 авг 2025, 22:51
8ff4abd
Код
Авторство
О чём код?
package com.github.jsonschemafaker; import com.fasterxml.jackson.databind.JsonNode; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; import java.util.Iterator; /** * Tests for advanced JSON Schema features. */ public class AdvancedFeaturesTest { @Test public void testAllOfComposition() throws Exception { String schema = "{\n" + " \"allOf\": [\n" + " {\n" + " \"type\": \"object\",\n" + " \"properties\": {\n" + " \"name\": {\n" + " \"type\": \"string\"\n" + " }\n" + " },\n" + " \"required\": [\"name\"]\n" + " },\n" + " {\n" + " \"type\": \"object\",\n" + " \"properties\": {\n" + " \"age\": {\n" + " \"type\": \"integer\"\n" + " }\n" + " },\n" + " \"required\": [\"age\"]\n" + " }\n" + " ]\n" + "}"; JsonSchemaFaker faker = new JsonSchemaFaker(); JsonNode result = faker.generate(schema); assertTrue(result.isObject(), "Result should be an object"); assertTrue(result.has("name"), "Should have name property from first schema"); assertTrue(result.has("age"), "Should have age property from second schema"); assertTrue(result.get("name").isTextual(), "Name should be a string"); assertTrue(result.get("age").isNumber(), "Age should be a number"); } @Test public void testAnyOfComposition() throws Exception { String schema = "{\n" + " \"anyOf\": [\n" + " {\n" + " \"type\": \"string\"\n" + " },\n" + " {\n" + " \"type\": \"integer\"\n" + " }\n" + " ]\n" + "}"; JsonSchemaFaker faker = new JsonSchemaFaker(); JsonNode result = faker.generate(schema); assertTrue(result.isTextual() || result.isNumber(), "Result should be either a string or a number"); } @Test public void testOneOfComposition() throws Exception { String schema = "{\n" + " \"oneOf\": [\n" + " {\n" + " \"type\": \"boolean\"\n" + " },\n" + " {\n" + " \"type\": \"null\"\n" + " }\n" + " ]\n" + "}"; JsonSchemaFaker faker = new JsonSchemaFaker(); JsonNode result = faker.generate(schema); assertTrue(result.isBoolean() || result.isNull(), "Result should be either a boolean or null"); } @Test public void testConditionalIfThenElse() throws Exception { String schema = "{\n" + " \"if\": {\n" + " \"type\": \"string\"\n" + " },\n" + " \"then\": {\n" + " \"type\": \"object\",\n" + " \"properties\": {\n" + " \"stringValue\": {\n" + " \"type\": \"string\"\n" + " }\n" + " }\n" + " },\n" + " \"else\": {\n" + " \"type\": \"object\",\n" + " \"properties\": {\n" + " \"otherValue\": {\n" + " \"type\": \"integer\"\n" + " }\n" + " }\n" + " }\n" + "}"; JsonSchemaFaker faker = new JsonSchemaFaker(); JsonNode result = faker.generate(schema); assertTrue(result.isObject(), "Result should be an object"); // The conditional logic should generate either stringValue or otherValue assertTrue(result.has("stringValue") || result.has("otherValue"), "Should have either stringValue or otherValue"); } @Test public void testArrayWithContains() throws Exception { String schema = "{\n" + " \"type\": \"array\",\n" + " \"items\": {\n" + " \"type\": \"string\"\n" + " },\n" + " \"contains\": {\n" + " \"type\": \"string\",\n" + " \"enum\": [\"special\"]\n" + " }\n" + "}"; JsonSchemaFaker faker = new JsonSchemaFaker(); JsonNode result = faker.generate(schema); assertTrue(result.isArray(), "Result should be an array"); assertTrue(result.size() > 0, "Array should not be empty"); // Check if at least one item is "special" boolean hasSpecial = false; for (JsonNode item : result) { if (item.asText().equals("special")) { hasSpecial = true; break; } } assertTrue(hasSpecial, "Array should contain 'special' item"); } @Test public void testArrayWithAdditionalItems() throws Exception { String schema = "{\n" + " \"type\": \"array\",\n" + " \"items\": [\n" + " {\n" + " \"type\": \"string\"\n" + " },\n" + " {\n" + " \"type\": \"integer\"\n" + " }\n" + " ],\n" + " \"additionalItems\": {\n" + " \"type\": \"boolean\"\n" + " },\n" + " \"minItems\": 4\n" + "}"; JsonSchemaFaker faker = new JsonSchemaFaker(); JsonNode result = faker.generate(schema); assertTrue(result.isArray(), "Result should be an array"); assertTrue(result.size() >= 4, "Array should have at least 4 items"); // First two items should be string and integer assertTrue(result.get(0).isTextual(), "First item should be a string"); assertTrue(result.get(1).isNumber(), "Second item should be a number"); // Additional items should be booleans for (int i = 2; i < result.size(); i++) { assertTrue(result.get(i).isBoolean(), "Additional item " + i + " should be a boolean"); } } @Test public void testObjectWithAdditionalProperties() throws Exception { String schema = "{\n" + " \"type\": \"object\",\n" + " \"properties\": {\n" + " \"name\": {\n" + " \"type\": \"string\"\n" + " }\n" + " },\n" + " \"additionalProperties\": {\n" + " \"type\": \"number\"\n" + " }\n" + "}"; JsonSchemaFaker faker = new JsonSchemaFaker(); JsonNode result = faker.generate(schema); assertTrue(result.isObject(), "Result should be an object"); assertTrue(result.has("name"), "Should have name property"); assertTrue(result.get("name").isTextual(), "Name should be a string"); // Should have additional properties that are numbers boolean hasAdditional = false; Iterator<String> fieldNames = result.fieldNames(); while (fieldNames.hasNext()) { String fieldName = fieldNames.next(); if (!fieldName.equals("name")) { assertTrue(result.get(fieldName).isNumber(), "Additional property " + fieldName + " should be a number"); hasAdditional = true; } } assertTrue(hasAdditional, "Should have at least one additional property"); } @Test public void testNumberWithMultipleOf() throws Exception { String schema = "{\n" + " \"type\": \"integer\",\n" + " \"minimum\": 0,\n" + " \"maximum\": 100,\n" + " \"multipleOf\": 5\n" + "}"; JsonSchemaFaker faker = new JsonSchemaFaker(); JsonNode result = faker.generate(schema); assertTrue(result.isNumber(), "Result should be a number"); int value = result.asInt(); assertTrue(value >= 0 && value <= 100, "Value should be between 0 and 100"); assertEquals(0, value % 5, "Value should be a multiple of 5"); } @Test public void testStringWithAdvancedFormats() throws Exception { String schema = "{\n" + " \"type\": \"string\",\n" + " \"format\": \"email\"\n" + "}"; JsonSchemaFaker faker = new JsonSchemaFaker(); JsonNode result = faker.generate(schema); assertTrue(result.isTextual(), "Result should be a string"); String email = result.asText(); assertTrue(email.contains("@"), "Email should contain @ symbol"); } @Test public void testComplexNestedSchema() throws Exception { String schema = "{\n" + " \"type\": \"object\",\n" + " \"properties\": {\n" + " \"users\": {\n" + " \"type\": \"array\",\n" + " \"items\": {\n" + " \"allOf\": [\n" + " {\n" + " \"type\": \"object\",\n" + " \"properties\": {\n" + " \"id\": {\n" + " \"type\": \"integer\"\n" + " },\n" + " \"name\": {\n" + " \"type\": \"string\"\n" + " }\n" + " },\n" + " \"required\": [\"id\", \"name\"]\n" + " },\n" + " {\n" + " \"if\": {\n" + " \"properties\": {\n" + " \"name\": {\n" + " \"type\": \"string\",\n" + " \"minLength\": 10\n" + " }\n" + " }\n" + " },\n" + " \"then\": {\n" + " \"properties\": {\n" + " \"description\": {\n" + " \"type\": \"string\"\n" + " }\n" + " }\n" + " }\n" + " }\n" + " ]\n" + " },\n" + " \"contains\": {\n" + " \"type\": \"object\",\n" + " \"properties\": {\n" + " \"role\": {\n" + " \"type\": \"string\",\n" + " \"enum\": [\"admin\"]\n" + " }\n" + " }\n" + " }\n" + " }\n" + " }\n" + "}"; JsonSchemaFaker faker = new JsonSchemaFaker(); JsonNode result = faker.generate(schema); assertTrue(result.isObject(), "Result should be an object"); assertTrue(result.has("users"), "Should have users property"); assertTrue(result.get("users").isArray(), "Users should be an array"); JsonNode users = result.get("users"); assertTrue(users.size() > 0, "Users array should not be empty"); // Check that each user has required properties for (JsonNode user : users) { assertTrue(user.has("id"), "User should have id"); assertTrue(user.has("name"), "User should have name"); assertTrue(user.get("id").isNumber(), "User id should be a number"); assertTrue(user.get("name").isTextual(), "User name should be a string"); } // Check that at least one user has role "admin" boolean hasAdmin = false; for (JsonNode user : users) { if (user.has("role") && "admin".equals(user.get("role").asText())) { hasAdmin = true; break; } } assertTrue(hasAdmin, "Should have at least one admin user"); } }