1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.metaschema.schemagen;
7   
8   import gov.nist.secauto.metaschema.core.configuration.DefaultConfiguration;
9   import gov.nist.secauto.metaschema.core.configuration.IMutableConfiguration;
10  import gov.nist.secauto.metaschema.core.model.IModule;
11  import gov.nist.secauto.metaschema.core.model.MetaschemaException;
12  import gov.nist.secauto.metaschema.core.util.ObjectUtils;
13  import gov.nist.secauto.metaschema.databind.DefaultBindingContext;
14  import gov.nist.secauto.metaschema.databind.IBindingContext;
15  import gov.nist.secauto.metaschema.databind.model.metaschema.BindingModuleLoader;
16  import gov.nist.secauto.metaschema.schemagen.json.JsonSchemaGenerator;
17  import gov.nist.secauto.metaschema.schemagen.xml.XmlSchemaGenerator;
18  
19  import org.junit.jupiter.api.Test;
20  
21  import java.io.IOException;
22  import java.io.Writer;
23  import java.nio.charset.StandardCharsets;
24  import java.nio.file.Files;
25  import java.nio.file.Path;
26  import java.nio.file.Paths;
27  import java.nio.file.StandardOpenOption;
28  
29  import edu.umd.cs.findbugs.annotations.NonNull;
30  
31  class MetaschemaModuleTest {
32    @NonNull
33    private static final Path METASCHEMA_FILE
34        = ObjectUtils.notNull(Paths.get("../core/metaschema/schema/metaschema/metaschema-module-metaschema.xml"));
35  
36    @Test
37    void testGenerateMetaschemaModuleJson() throws MetaschemaException, IOException {
38      IBindingContext context = new DefaultBindingContext();
39      BindingModuleLoader loader = new BindingModuleLoader(context);
40  
41      IModule module = loader.load(METASCHEMA_FILE);
42  
43      IMutableConfiguration<SchemaGenerationFeature<?>> features
44          = new DefaultConfiguration<>();
45      features.enableFeature(SchemaGenerationFeature.INLINE_DEFINITIONS);
46      // features.disableFeature(SchemaGenerationFeature.INLINE_DEFINITIONS);
47  
48      try (Writer writer = Files.newBufferedWriter(
49          Path.of("target/metaschema-schema.json"),
50          StandardCharsets.UTF_8,
51          StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING)) {
52        assert writer != null;
53        ISchemaGenerator schemaGenerator = new JsonSchemaGenerator();
54        schemaGenerator.generateFromModule(module, writer, features);
55      }
56    }
57  
58    @Test
59    void testGenerateMetaschemaModuleXml() throws MetaschemaException, IOException {
60      IBindingContext context = new DefaultBindingContext();
61      BindingModuleLoader loader = new BindingModuleLoader(context);
62  
63      IModule module = loader.load(METASCHEMA_FILE);
64  
65      IMutableConfiguration<SchemaGenerationFeature<?>> features
66          = new DefaultConfiguration<>();
67      features.enableFeature(SchemaGenerationFeature.INLINE_DEFINITIONS);
68      // features.disableFeature(SchemaGenerationFeature.INLINE_DEFINITIONS);
69  
70      try (Writer writer = Files.newBufferedWriter(
71          Path.of("target/metaschema-schema.xsd"),
72          StandardCharsets.UTF_8,
73          StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING)) {
74        assert writer != null;
75        ISchemaGenerator schemaGenerator = new XmlSchemaGenerator();
76        schemaGenerator.generateFromModule(module, writer, features);
77      }
78    }
79  }