1 /*
2 * Portions of this software was developed by employees of the National Institute
3 * of Standards and Technology (NIST), an agency of the Federal Government and is
4 * being made available as a public service. Pursuant to title 17 United States
5 * Code Section 105, works of NIST employees are not subject to copyright
6 * protection in the United States. This software may be subject to foreign
7 * copyright. Permission in the United States and in foreign countries, to the
8 * extent that NIST may hold copyright, to use, copy, modify, create derivative
9 * works, and distribute this software and its documentation without fee is hereby
10 * granted on a non-exclusive basis, provided that this notice and disclaimer
11 * of warranty appears in all copies.
12 *
13 * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
14 * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
15 * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
17 * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
18 * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT
19 * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
20 * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
21 * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
22 * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
23 * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
24 * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
25 */
26
27 package gov.nist.secauto.metaschema.databind.model;
28
29 import static org.junit.jupiter.api.Assertions.assertNotNull;
30
31 import com.fasterxml.jackson.core.JsonParseException;
32 import com.fasterxml.jackson.core.JsonParser;
33
34 import gov.nist.secauto.metaschema.core.model.IModule;
35 import gov.nist.secauto.metaschema.databind.io.json.MetaschemaJsonReader;
36
37 import org.junit.jupiter.api.Test;
38
39 import java.io.BufferedReader;
40 import java.io.File;
41 import java.io.IOException;
42 import java.nio.file.Files;
43
44 class DefaultAssemblyClassBindingTest
45 extends AbstractBoundModelTestSupport {
46 @Test
47 void testMinimalJsonParse() throws JsonParseException, IOException {
48 File testContent
49 = new File(getClass().getResource("/content/minimal.json").getFile());
50 try (BufferedReader reader = Files.newBufferedReader(testContent.toPath())) {
51 assert reader != null;
52
53 IAssemblyClassBinding classBinding = getRootAssemblyClassBinding();
54
55 try (JsonParser parser = newJsonParser(reader)) {
56 Object value = new MetaschemaJsonReader(parser).read(classBinding);
57 assertNotNull(value, "root was null");
58 }
59 }
60 }
61
62 @Test
63 void testModule() {
64 IAssemblyClassBinding classBinding = getRootAssemblyClassBinding();
65 IModule module = classBinding.getContainingModule();
66 assertNotNull(module, "metaschema was null");
67 }
68
69 // @Test
70 // void testSimpleJson() throws JsonParseException, IOException,
71 // BindingException {
72 // File testContent
73 // = new
74 // File(getClass().getClassLoader().getResource("test-content/bound-class-simple.json").getFile());
75 // try (BufferedReader reader = Files.newBufferedReader(testContent.toPath())) {
76 // JsonParser jsonParser = newJsonParser(reader);
77 //
78 // assertEquals(JsonToken.START_OBJECT, jsonParser.nextToken());
79 // assertEquals(JsonToken.FIELD_NAME, jsonParser.nextToken());
80 //
81 // IAssemblyClassBinding classBinding = getAssemblyClassBinding();
82 // IBoundAssemblyInstance root = new RootAssemblyDefinition(classBinding);
83 // BoundClass obj = (BoundClass) root.read(jsonParsingContext);
84 //
85 // assertEquals(JsonToken.END_OBJECT, jsonParser.currentToken());
86 // assertSimple(obj);
87 // }
88 // }
89 //
90 // @Test
91 // void testSimpleXml() throws BindingException, XMLStreamException, IOException
92 // {
93 // File testContent
94 // = new
95 // File(getClass().getClassLoader().getResource("test-content/bound-class-simple.xml").getFile());
96 // try (BufferedReader reader = Files.newBufferedReader(testContent.toPath())) {
97 // XMLEventReader eventReader = newXmlParser(reader);
98 //
99 // IAssemblyClassBinding classBinding = getAssemblyClassBinding();
100 //
101 // // assertEquals(XMLEvent.START_DOCUMENT, parser.nextEvent().getEventType());
102 //
103 // BoundClass obj = (BoundClass) classBinding.readRoot(xmlParsingContext);
104 //
105 // assertEquals(XMLEvent.END_DOCUMENT, eventReader.peek().getEventType());
106 // assertSimple(obj);
107 // }
108 // }
109 //
110 // private void assertSimple(BoundClass obj) {
111 // assertNotNull(obj);
112 // assertEquals("idvalue", obj.getId());
113 // assertNull(obj.getSingleSimpleField());
114 // assertNotNull(obj.getGroupedListSimpleField());
115 // assertTrue(obj.getGroupedListSimpleField().isEmpty());
116 // assertNull(obj.getSingleFlaggedField());
117 // assertNotNull(obj.getGroupedListField());
118 // assertTrue(obj.getGroupedListField().isEmpty());
119 // assertNotNull(obj.getUngroupedListField());
120 // assertTrue(obj.getUngroupedListField().isEmpty());
121 // assertNotNull(obj.getMappedField());
122 // assertTrue(obj.getMappedField().isEmpty());
123 //
124 // context.assertIsSatisfied();
125 // }
126 //
127 // @Test
128 // void testComplexJson() throws BindingException, IOException {
129 // File testContent
130 // = new
131 // File(getClass().getClassLoader().getResource("test-content/bound-class-complex.json").getFile());
132 // try (BufferedReader reader = Files.newBufferedReader(testContent.toPath())) {
133 // JsonParser jsonParser = newJsonParser(reader);
134 //
135 // assertEquals(JsonToken.START_OBJECT, jsonParser.nextToken());
136 // assertEquals(JsonToken.FIELD_NAME, jsonParser.nextToken());
137 //
138 // IAssemblyClassBinding classBinding = getAssemblyClassBinding();
139 // IBoundAssemblyInstance root = new RootAssemblyDefinition(classBinding);
140 // BoundClass obj = (BoundClass) root.read(jsonParsingContext);
141 //
142 // assertEquals(JsonToken.END_OBJECT, jsonParser.currentToken());
143 // assertComplex(obj);
144 // }
145 // }
146 //
147 // @Test
148 // void testComplexXml() throws BindingException, XMLStreamException,
149 // IOException {
150 // File testContent
151 // = new
152 // File(getClass().getClassLoader().getResource("test-content/bound-class-complex.xml").getFile());
153 // try (BufferedReader reader = Files.newBufferedReader(testContent.toPath())) {
154 // XMLEventReader eventReader = newXmlParser(reader);
155 //
156 // IAssemblyClassBinding classBinding = getAssemblyClassBinding();
157 //
158 // BoundClass obj = (BoundClass) classBinding.readRoot(xmlParsingContext);
159 //
160 // assertEquals(XMLEvent.END_DOCUMENT, eventReader.peek().getEventType());
161 // assertComplex(obj);
162 // }
163 // }
164 //
165 // private void assertComplex(BoundClass obj) {
166 // assertNotNull(obj);
167 // assertEquals("idvalue", obj.getId());
168 // assertEquals("single-simple-value", obj.getSingleSimpleField());
169 // assertNotNull(obj.getGroupedListSimpleField());
170 // assertIterableEquals(List.of("grouped-list-simple-item-value-1",
171 // "grouped-list-simple-item-value-2"),
172 // obj.getGroupedListSimpleField());
173 //
174 // assertNotNull(obj.getSingleFlaggedField());
175 // FlaggedField flaggedField = obj.getSingleFlaggedField();
176 // assertEquals("single-flagged-id", flaggedField.getId());
177 // assertEquals("single-flagged-value", flaggedField.getValue());
178 // //
179 // //
180 // // assertNotNull(obj.getGroupedListField());
181 // // assertTrue(obj.getGroupedListField().isEmpty());
182 // // assertNotNull(obj.getUngroupedListField());
183 // // assertTrue(obj.getUngroupedListField().isEmpty());
184 // // assertNotNull(obj.getMappedField());
185 // // assertTrue(obj.getMappedField().isEmpty());
186 //
187 // context.assertIsSatisfied();
188 // }
189
190 }