Search in sources :

Example 1 with JsonSchema

use of org.talend.sdk.component.form.model.jsonschema.JsonSchema in project component-runtime by Talend.

the class UiSpecServiceTest method jsonSchemaArray.

@Test
void jsonSchemaArray() throws Exception {
    final Ui payload = service.convert(load("rest-api.json")).toCompletableFuture().get();
    final JsonSchema commonConfig = payload.getJsonSchema().getProperties().values().iterator().next().getProperties().entrySet().stream().filter(e -> e.getKey().equals("commonConfig")).findFirst().map(Map.Entry::getValue).orElseThrow(IllegalStateException::new);
    assertTrue(commonConfig.getProperties().keySet().stream().anyMatch(p -> p.equals("fields")));
    assertTrue(commonConfig.getProperties().keySet().stream().noneMatch(p -> p.equals("fields[]")));
}
Also used : Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Disabled(org.junit.jupiter.api.Disabled) JsonbConfig(javax.json.bind.JsonbConfig) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) ComponentDetail(org.talend.sdk.component.server.front.model.ComponentDetail) ConfigTypeNodes(org.talend.sdk.component.server.front.model.ConfigTypeNodes) Collections.singleton(java.util.Collections.singleton) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Collectors.toSet(java.util.stream.Collectors.toSet) JsonbBuilder(javax.json.bind.JsonbBuilder) Iterator(java.util.Iterator) Collection(java.util.Collection) JsonSchema(org.talend.sdk.component.form.model.jsonschema.JsonSchema) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) UiSchema(org.talend.sdk.component.form.model.uischema.UiSchema) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) TreeMap(java.util.TreeMap) Ui(org.talend.sdk.component.form.model.Ui) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Jsonb(javax.json.bind.Jsonb) Comparator(java.util.Comparator) InputStream(java.io.InputStream) Ui(org.talend.sdk.component.form.model.Ui) JsonSchema(org.talend.sdk.component.form.model.jsonschema.JsonSchema) HashMap(java.util.HashMap) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) TreeMap(java.util.TreeMap) Test(org.junit.jupiter.api.Test)

Example 2 with JsonSchema

use of org.talend.sdk.component.form.model.jsonschema.JsonSchema in project component-runtime by Talend.

the class UiSpecServiceTest method jsonSchema.

@Test
void jsonSchema() throws Exception {
    final Ui payload = service.convert(load("jdbc.json")).toCompletableFuture().get();
    final JsonSchema jsonSchema = payload.getJsonSchema();
    assertNotNull(jsonSchema);
    assertEquals("JDBC Input", jsonSchema.getTitle());
    assertEquals("object", jsonSchema.getType());
    assertEquals(1, jsonSchema.getProperties().size());
    assertProperty(jsonSchema.getProperties().get("configuration"), "object", "Configuration", p -> {
        final Map<String, JsonSchema> nestedProperties = p.getProperties();
        assertEquals(2, nestedProperties.size());
        assertTrue(TreeMap.class.isInstance(nestedProperties));
        assertEquals(asList("connection", "query"), new ArrayList<>(nestedProperties.keySet()));
        assertProperty(nestedProperties.get("query"), "object", "query", q -> {
            final Map<String, JsonSchema> queryNestedProperties = q.getProperties();
            assertEquals(2, queryNestedProperties.size());
            assertProperty(queryNestedProperties.get("query"), "string", "query", query -> assertNull(query.getProperties()));
            assertProperty(queryNestedProperties.get("timeout"), "number", "timeout", timeout -> assertNull(timeout.getProperties()));
        });
        assertProperty(nestedProperties.get("connection"), "object", "JDBC Connection", q -> {
            final Map<String, JsonSchema> queryNestedProperties = q.getProperties();
            assertEquals(4, queryNestedProperties.size());
            assertProperty(queryNestedProperties.get("password"), "string", "password", pwd -> assertNull(pwd.getProperties()));
            assertProperty(queryNestedProperties.get("driver"), "string", "driver", driver -> assertNull(driver.getProperties()));
            assertProperty(queryNestedProperties.get("url"), "string", "url", url -> assertNull(url.getProperties()));
            assertProperty(queryNestedProperties.get("username"), "string", "username", username -> {
                assertNull(username.getProperties());
                assertEquals("sa", username.getDefaultValue());
            });
        });
    });
}
Also used : Ui(org.talend.sdk.component.form.model.Ui) JsonSchema(org.talend.sdk.component.form.model.jsonschema.JsonSchema) TreeMap(java.util.TreeMap) Test(org.junit.jupiter.api.Test)

Example 3 with JsonSchema

use of org.talend.sdk.component.form.model.jsonschema.JsonSchema in project component-runtime by Talend.

the class JsonSchemaConverterTest method convert.

private Object convert(final String type, final String value) throws Exception {
    try (final Jsonb jsonb = JsonbBuilder.create()) {
        final JsonSchemaConverter instance = new JsonSchemaConverter(jsonb, new JsonSchema(), emptyList());
        final Method convertDefaultValue = JsonSchemaConverter.class.getDeclaredMethod("convertDefaultValue", String.class, String.class);
        if (!convertDefaultValue.isAccessible()) {
            convertDefaultValue.setAccessible(true);
        }
        return Optional.class.cast(convertDefaultValue.invoke(instance, type, value)).orElse(null);
    }
}
Also used : Jsonb(javax.json.bind.Jsonb) Optional(java.util.Optional) JsonSchema(org.talend.sdk.component.form.model.jsonschema.JsonSchema) Method(java.lang.reflect.Method)

Example 4 with JsonSchema

use of org.talend.sdk.component.form.model.jsonschema.JsonSchema in project component-runtime by Talend.

the class UiSpecService method convert.

private CompletionStage<Ui> convert(final Supplier<String> displayName, final Supplier<String> family, final Supplier<Collection<SimplePropertyDefinition>> properties, final Supplier<Collection<ActionReference>> actions, final Predicate<SimplePropertyDefinition> isRootProperty) {
    final Collection<SimplePropertyDefinition> props = properties.get();
    final Ui ui = new Ui();
    ui.setUiSchema(new ArrayList<>());
    ui.setProperties(new HashMap<>());
    ui.setJsonSchema(new JsonSchema());
    ui.getJsonSchema().setTitle(displayName.get());
    ui.getJsonSchema().setType("object");
    ui.getJsonSchema().setRequired(props.stream().filter(isRootProperty).filter(p -> new PropertyContext(p).isRequired()).map(SimplePropertyDefinition::getName).collect(toSet()));
    final JsonSchemaConverter jsonSchemaConverter = new JsonSchemaConverter(jsonb, ui.getJsonSchema(), props);
    final UiSchemaConverter uiSchemaConverter = new UiSchemaConverter(null, family.get(), ui.getUiSchema(), new ArrayList<>(), client, props, actions.get());
    final PropertiesConverter propertiesConverter = new PropertiesConverter(jsonb, Map.class.cast(ui.getProperties()), props);
    return CompletableFuture.allOf(props.stream().filter(Objects::nonNull).filter(isRootProperty).map(PropertyContext::new).map(CompletableFuture::completedFuture).map(jsonSchemaConverter::convert).map(uiSchemaConverter::convert).map(propertiesConverter::convert).toArray(CompletableFuture[]::new)).thenApply(r -> ui);
}
Also used : PropertiesConverter(org.talend.sdk.component.form.internal.converter.impl.PropertiesConverter) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) PropertyContext(org.talend.sdk.component.form.internal.converter.PropertyContext) Supplier(java.util.function.Supplier) JsonbConfig(javax.json.bind.JsonbConfig) ArrayList(java.util.ArrayList) ComponentDetail(org.talend.sdk.component.server.front.model.ComponentDetail) ActionReference(org.talend.sdk.component.server.front.model.ActionReference) Map(java.util.Map) JsonSchemaConverter(org.talend.sdk.component.form.internal.converter.impl.JsonSchemaConverter) Collectors.toSet(java.util.stream.Collectors.toSet) JsonbBuilder(javax.json.bind.JsonbBuilder) Iterator(java.util.Iterator) Predicate(java.util.function.Predicate) Collection(java.util.Collection) JsonSchema(org.talend.sdk.component.form.model.jsonschema.JsonSchema) Objects(java.util.Objects) Slf4j(lombok.extern.slf4j.Slf4j) CompletionStage(java.util.concurrent.CompletionStage) SimplePropertyDefinition(org.talend.sdk.component.server.front.model.SimplePropertyDefinition) Ui(org.talend.sdk.component.form.model.Ui) UiSchemaConverter(org.talend.sdk.component.form.internal.converter.impl.UiSchemaConverter) Jsonb(javax.json.bind.Jsonb) ConfigTypeNode(org.talend.sdk.component.server.front.model.ConfigTypeNode) SimplePropertyDefinition(org.talend.sdk.component.server.front.model.SimplePropertyDefinition) JsonSchema(org.talend.sdk.component.form.model.jsonschema.JsonSchema) UiSchemaConverter(org.talend.sdk.component.form.internal.converter.impl.UiSchemaConverter) CompletableFuture(java.util.concurrent.CompletableFuture) JsonSchemaConverter(org.talend.sdk.component.form.internal.converter.impl.JsonSchemaConverter) Ui(org.talend.sdk.component.form.model.Ui) PropertyContext(org.talend.sdk.component.form.internal.converter.PropertyContext) PropertiesConverter(org.talend.sdk.component.form.internal.converter.impl.PropertiesConverter) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with JsonSchema

use of org.talend.sdk.component.form.model.jsonschema.JsonSchema in project component-runtime by Talend.

the class JsonSchemaConverter method convert.

@Override
public CompletionStage<PropertyContext> convert(final CompletionStage<PropertyContext> cs) {
    return cs.thenCompose(context -> {
        final JsonSchema jsonSchema = new JsonSchema();
        jsonSchema.setTitle(context.getProperty().getDisplayName());
        final String type = context.getProperty().getType();
        switch(type.toLowerCase(ROOT)) {
            case "enum":
                return new EnumPropertyConverter(jsonSchema).convert(CompletableFuture.completedFuture(context)).thenCompose(c -> postHandling(context, jsonSchema, type));
            case "array":
                return new ArrayPropertyConverter(jsonb, jsonSchema, properties).convert(CompletableFuture.completedFuture(context)).thenCompose(c -> postHandling(context, jsonSchema, type));
            default:
                if (context.getProperty().getPath().endsWith("[]")) {
                    return CompletableFuture.completedFuture(context);
                }
                jsonSchema.setType(type.toLowerCase(ROOT));
                jsonSchema.setRequired(properties.stream().filter(context::isDirectChild).filter(nested -> new PropertyContext(nested).isRequired()).map(SimplePropertyDefinition::getName).collect(toSet()));
                return CompletableFuture.completedFuture(context).thenCompose(c -> postHandling(context, jsonSchema, type));
        }
    });
}
Also used : EnumPropertyConverter(org.talend.sdk.component.form.internal.converter.impl.schema.EnumPropertyConverter) Optional.ofNullable(java.util.Optional.ofNullable) Collection(java.util.Collection) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) PropertyContext(org.talend.sdk.component.form.internal.converter.PropertyContext) JsonSchema(org.talend.sdk.component.form.model.jsonschema.JsonSchema) ArrayPropertyConverter(org.talend.sdk.component.form.internal.converter.impl.schema.ArrayPropertyConverter) PropertyConverter(org.talend.sdk.component.form.internal.converter.PropertyConverter) ArrayList(java.util.ArrayList) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) TreeMap(java.util.TreeMap) SimplePropertyDefinition(org.talend.sdk.component.server.front.model.SimplePropertyDefinition) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) Optional(java.util.Optional) PropertyValidation(org.talend.sdk.component.server.front.model.PropertyValidation) Jsonb(javax.json.bind.Jsonb) ROOT(java.util.Locale.ROOT) Comparator(java.util.Comparator) AllArgsConstructor(lombok.AllArgsConstructor) Collectors.toSet(java.util.stream.Collectors.toSet) SimplePropertyDefinition(org.talend.sdk.component.server.front.model.SimplePropertyDefinition) PropertyContext(org.talend.sdk.component.form.internal.converter.PropertyContext) JsonSchema(org.talend.sdk.component.form.model.jsonschema.JsonSchema) ArrayPropertyConverter(org.talend.sdk.component.form.internal.converter.impl.schema.ArrayPropertyConverter) EnumPropertyConverter(org.talend.sdk.component.form.internal.converter.impl.schema.EnumPropertyConverter)

Aggregations

JsonSchema (org.talend.sdk.component.form.model.jsonschema.JsonSchema)8 Jsonb (javax.json.bind.Jsonb)6 Collection (java.util.Collection)5 CompletionStage (java.util.concurrent.CompletionStage)5 HashMap (java.util.HashMap)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 PropertyContext (org.talend.sdk.component.form.internal.converter.PropertyContext)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Map (java.util.Map)3 TreeMap (java.util.TreeMap)3 Collectors.toList (java.util.stream.Collectors.toList)3 Collectors.toSet (java.util.stream.Collectors.toSet)3 Test (org.junit.jupiter.api.Test)3 Ui (org.talend.sdk.component.form.model.Ui)3 SimplePropertyDefinition (org.talend.sdk.component.server.front.model.SimplePropertyDefinition)3 Arrays.asList (java.util.Arrays.asList)2 Collections.emptyList (java.util.Collections.emptyList)2 Comparator (java.util.Comparator)2 Iterator (java.util.Iterator)2