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.IBindingContext;
14  import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingModuleLoader;
15  import gov.nist.secauto.metaschema.schemagen.json.JsonSchemaGenerator;
16  import gov.nist.secauto.metaschema.schemagen.xml.XmlSchemaGenerator;
17  
18  import org.junit.jupiter.api.Test;
19  
20  import java.io.IOException;
21  import java.io.Writer;
22  import java.nio.charset.StandardCharsets;
23  import java.nio.file.Files;
24  import java.nio.file.Path;
25  import java.nio.file.Paths;
26  import java.nio.file.StandardOpenOption;
27  
28  import edu.umd.cs.findbugs.annotations.NonNull;
29  
30  class MetaschemaModuleTest {
31    @NonNull
32    private static final Path METASCHEMA_FILE
33        = ObjectUtils.notNull(Paths.get("../core/metaschema/schema/metaschema/metaschema-module-metaschema.xml"));
34  
35    @NonNull
36    private static IBindingContext getBindingContext() throws IOException {
37      return IBindingContext.builder()
38          .compilePath(ObjectUtils.notNull(Files.createTempDirectory(Paths.get("target"), "modules-")))
39          .build();
40    }
41  
42    @Test
43    void testGenerateMetaschemaModuleJson() throws MetaschemaException, IOException {
44      IBindingContext bindingContext = getBindingContext();
45      IBindingModuleLoader loader = bindingContext.newModuleLoader();
46  
47      IModule module = loader.load(METASCHEMA_FILE);
48  
49      IMutableConfiguration<SchemaGenerationFeature<?>> features
50          = new DefaultConfiguration<>();
51      features.enableFeature(SchemaGenerationFeature.INLINE_DEFINITIONS);
52      // features.disableFeature(SchemaGenerationFeature.INLINE_DEFINITIONS);
53  
54      try (Writer writer = Files.newBufferedWriter(
55          Path.of("target/metaschema-schema.json"),
56          StandardCharsets.UTF_8,
57          StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING)) {
58        assert writer != null;
59        ISchemaGenerator schemaGenerator = new JsonSchemaGenerator();
60        schemaGenerator.generateFromModule(module, writer, features);
61      }
62    }
63  
64    @Test
65    void testGenerateMetaschemaModuleXml() throws MetaschemaException, IOException {
66      IBindingContext bindingContext = getBindingContext();
67      IBindingModuleLoader loader = bindingContext.newModuleLoader();
68  
69      IModule module = loader.load(METASCHEMA_FILE);
70  
71      IMutableConfiguration<SchemaGenerationFeature<?>> features
72          = new DefaultConfiguration<>();
73      features.enableFeature(SchemaGenerationFeature.INLINE_DEFINITIONS);
74      // features.disableFeature(SchemaGenerationFeature.INLINE_DEFINITIONS);
75  
76      try (Writer writer = Files.newBufferedWriter(
77          Path.of("target/metaschema-schema.xsd"),
78          StandardCharsets.UTF_8,
79          StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING)) {
80        assert writer != null;
81        ISchemaGenerator schemaGenerator = new XmlSchemaGenerator();
82        schemaGenerator.generateFromModule(module, writer, features);
83      }
84    }
85  }