|
| 1 | +/** |
| 2 | + * Copyright 2000-2026 Vaadin Ltd. |
| 3 | + * |
| 4 | + * This program is available under Vaadin Commercial License and Service Terms. |
| 5 | + * |
| 6 | + * See {@literal <https://vaadin.com/commercial-license-and-service-terms>} for the full |
| 7 | + * license. |
| 8 | + */ |
| 9 | +package com.vaadin.flow.component.ai.chart; |
| 10 | + |
| 11 | +import java.nio.file.Path; |
| 12 | + |
| 13 | +import org.junit.jupiter.api.Assertions; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.junit.jupiter.api.io.TempDir; |
| 16 | + |
| 17 | +import com.vaadin.flow.internal.JacksonUtils; |
| 18 | + |
| 19 | +import tools.jackson.databind.JsonNode; |
| 20 | + |
| 21 | +/** |
| 22 | + * Verifies that the plot options schemas generated at build time by |
| 23 | + * {@link PlotOptionsSchemaGenerator} end up on the classpath and are usable |
| 24 | + * through {@link PlotOptionsSchema}. |
| 25 | + */ |
| 26 | +class PlotOptionsSchemaTest { |
| 27 | + |
| 28 | + private static final String RESOURCE = "plot-options-schemas.json"; |
| 29 | + |
| 30 | + @Test |
| 31 | + void schemaResource_isOnClasspath() { |
| 32 | + Assertions.assertNotNull(PlotOptionsSchema.class.getResource(RESOURCE), |
| 33 | + RESOURCE + " is missing next to PlotOptionsSchema. It is " |
| 34 | + + "generated into the build output directory by the " |
| 35 | + + "exec-maven-plugin execution in this module's pom, " |
| 36 | + + "so the module must be built with Maven."); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + void everySupportedType_hasSchema() { |
| 41 | + var types = PlotOptionsSchema.supportedTypes(); |
| 42 | + Assertions.assertFalse(types.isEmpty()); |
| 43 | + for (String type : types) { |
| 44 | + String schema = PlotOptionsSchema.getSchema(type); |
| 45 | + Assertions.assertNotNull(schema, |
| 46 | + "No generated schema for chart type '" + type + "'"); |
| 47 | + JsonNode node = JacksonUtils.readTree(schema); |
| 48 | + Assertions.assertEquals("object", node.get("type").asString()); |
| 49 | + Assertions.assertFalse(node.get("properties").isEmpty(), |
| 50 | + "Schema for '" + type + "' has no properties"); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void everySchema_hasDescriptionsFromChartSources() { |
| 56 | + for (String type : PlotOptionsSchema.supportedTypes()) { |
| 57 | + JsonNode properties = JacksonUtils |
| 58 | + .readTree(PlotOptionsSchema.getSchema(type)) |
| 59 | + .get("properties"); |
| 60 | + boolean described = false; |
| 61 | + for (var property : properties.properties()) { |
| 62 | + if (property.getValue().has("description")) { |
| 63 | + described = true; |
| 64 | + break; |
| 65 | + } |
| 66 | + } |
| 67 | + Assertions.assertTrue(described, |
| 68 | + "Schema for '" + type |
| 69 | + + "' has no property descriptions. The generator " |
| 70 | + + "could not read the chart model sources."); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void unknownType_returnsNull() { |
| 76 | + Assertions.assertNull(PlotOptionsSchema.getSchema("nonexistent")); |
| 77 | + Assertions.assertNull( |
| 78 | + PlotOptionsSchema.getPlotOptionsClass("nonexistent")); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void generator_missingSourceDirectory_fails(@TempDir Path tempDir) { |
| 83 | + var missingSources = tempDir.resolve("missing").toString(); |
| 84 | + var output = tempDir.resolve("out.json").toString(); |
| 85 | + Assertions.assertThrows(IllegalStateException.class, |
| 86 | + () -> PlotOptionsSchemaGenerator |
| 87 | + .main(new String[] { missingSources, output })); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + void generator_sourceDirectoryWithoutChartModel_fails( |
| 92 | + @TempDir Path tempDir) { |
| 93 | + var output = tempDir.resolve("out.json").toString(); |
| 94 | + Assertions.assertThrows(IllegalStateException.class, |
| 95 | + () -> PlotOptionsSchemaGenerator |
| 96 | + .main(new String[] { tempDir.toString(), output })); |
| 97 | + } |
| 98 | +} |
0 commit comments