All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fibanez.jsonschema.content.generator.EnumSchemaGenerator Maven / Gradle / Ivy

package com.fibanez.jsonschema.content.generator;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import org.everit.json.schema.EnumSchema;
import org.json.JSONObject;

import java.util.Optional;

import static com.fibanez.jsonschema.content.generator.util.RandomUtils.RANDOM;

@NoArgsConstructor(access = AccessLevel.PACKAGE)
public final class EnumSchemaGenerator implements SchemaGenerator {

    @Override
    public Object generate(@NonNull EnumSchema schema, JsonNode jsonNode) {
        if (schema.getPossibleValues() == null || schema.getPossibleValues().isEmpty()) {
            return JSONObject.NULL;
        }

        int skip = RANDOM.nextInt(schema.getPossibleValues().size());
        Optional value = schema.getPossibleValues().stream()
                .skip(skip)
                .findFirst();
        return value.orElse(JSONObject.NULL);
    }
}