package io.bootique.jetty;

import io.bootique.BQRuntime;
import io.bootique.meta.config.ConfigListMetadata;
import io.bootique.meta.config.ConfigMapMetadata;
import io.bootique.meta.config.ConfigMetadataNode;
import io.bootique.meta.config.ConfigMetadataVisitor;
import io.bootique.meta.config.ConfigObjectMetadata;
import io.bootique.meta.config.ConfigValueMetadata;
import io.bootique.meta.module.ModuleMetadata;
import io.bootique.meta.module.ModulesMetadata;
import io.bootique.test.junit.BQTestFactory;
import org.junit.Rule;
import org.junit.Test;

import java.util.Comparator;
import java.util.Optional;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class JettyModuleProvider_MetadataIT {

    @Rule
    public BQTestFactory testFactory = new BQTestFactory();

    @Test
    public void testMetadata() {

        BQRuntime runtime = testFactory.app().autoLoadModules().createRuntime();

        ModulesMetadata modulesMetadata = runtime.getInstance(ModulesMetadata.class);
        Optional<ModuleMetadata> jettyOpt = modulesMetadata.getModules()
                .stream()
                .filter(m -> "JettyModule".equals(m.getName()))
                .findFirst();

        assertTrue(jettyOpt.isPresent());
        ModuleMetadata jetty = jettyOpt.get();

        assertTrue(jetty.getDescription().startsWith("Integrates Jetty web server"));

        assertEquals(1, jetty.getConfigs().size());
        ConfigMetadataNode rootConfig = jetty.getConfigs().stream().findFirst().get();

        assertEquals("jetty", rootConfig.getName());

        String result = rootConfig.accept(new ConfigMetadataVisitor<String>() {

            @Override
            public String visitObjectMetadata(ConfigObjectMetadata metadata) {

                StringBuilder out = new StringBuilder(metadata.getName());

                metadata.getProperties()
                        .stream()
                        .sorted(Comparator.comparing(ConfigMetadataNode::getName))
                        .forEach(p -> out.append("[").append(p.accept(this)).append("]"));

                return out.toString();
            }

            @Override
            public String visitValueMetadata(ConfigValueMetadata metadata) {
                return metadata.getName() + ":" + metadata.getType().getTypeName();
            }

            @Override
            public String visitListMetadata(ConfigListMetadata metadata) {
                return "list:" + metadata.getName() +  "<" + metadata.getElementType().getType().getTypeName() + ">";
            }

            @Override
            public String visitMapMetadata(ConfigMapMetadata metadata) {
                return "map:" + metadata.getName() + "<" + metadata.getKeysType().getTypeName() + "," +
                        metadata.getValuesType().getType().getTypeName() + ">";
            }
        });

        assertEquals("jetty" +
                "[compression:boolean]" +
                "[list:connectors<io.bootique.jetty.connector.ConnectorFactory>]" +
                "[context:java.lang.String]" +
                "[map:filters<java.lang.String,io.bootique.jetty.server.FilterFactory>]" +
                "[idleThreadTimeout:int]" +
                "[maxQueuedRequests:int]" +
                "[maxThreads:int]" +
                "[minThreads:int]" +
                "[map:params<java.lang.String,java.lang.String>]" +
                "[map:servlets<java.lang.String,io.bootique.jetty.server.ServletFactory>]" +
                "[sessions:boolean]" +
                "[staticResourceBase:io.bootique.resource.FolderResourceFactory]", result);
    }
}

@@ -3218,32 +3218,115 @@
 Timeout:int%5D%22 +%0A
+                %22%5BmaxFormContentSize:int%5D%22 +%0A                %22%5BmaxFormKeys:int%5D%22 +%0A
                 

Support for maxFormContentSize and maxFormKeys #61