2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.aai.sa.rest;
23 import static org.junit.Assert.assertTrue;
25 import com.fasterxml.jackson.core.JsonParseException;
26 import com.fasterxml.jackson.databind.JsonMappingException;
27 import com.fasterxml.jackson.databind.ObjectMapper;
29 import java.io.IOException;
30 import org.junit.Test;
33 public class DocumentSchemaTest {
35 private final String SIMPLE_DOC_SCHEMA_JSON = "src/test/resources/json/simpleDocument.json";
36 private final String NESTED_DOC_SCHEMA_JSON = "src/test/resources/json/nested-document.json";
40 * This test validates that we convert document definitions back and forth between json strings and POJOs without
43 * @throws com.fasterxml.jackson.core.JsonParseException
44 * @throws com.fasterxml.jackson.databind.JsonMappingException
48 public void simpleDocSchemaFromJsonFileTest() throws com.fasterxml.jackson.core.JsonParseException,
49 com.fasterxml.jackson.databind.JsonMappingException, IOException {
51 // Import our json format document schema from a file.
52 File schemaFile = new File(SIMPLE_DOC_SCHEMA_JSON);
53 String fileString = TestUtils.readFileToString(schemaFile);
55 // Unmarshall that to a Java POJO
56 ObjectMapper mapper = new ObjectMapper();
57 DocumentSchema docSchema = mapper.readValue(schemaFile, DocumentSchema.class);
59 // Now, for the purposes of comparison, produce a JSON string from
61 String jsonString = mapper.writeValueAsString(docSchema);
63 // Assert that the raw JSON that we read from the file matches the marshalled
64 // JSON we generated from our Java object (ie: validate that we didn't lose
65 // anything going in either direction).
66 assertTrue("Marshalled object does not match the original json source that produced it",
67 fileString.equals(jsonString));
72 // * This test validates that we convert document definitions back and
73 // * forth between json strings and POJOs without any loss of data in
74 // * the case of document schemas which contain nested fields.
76 // * @throws com.fasterxml.jackson.core.JsonParseException
77 // * @throws com.fasterxml.jackson.databind.JsonMappingException
78 // * @throws IOException
82 public void nestedDocSchemaFromJsonFileTest() throws JsonParseException, JsonMappingException, IOException {
84 // Import our json format document schema from a file.
85 File schemaFile = new File(NESTED_DOC_SCHEMA_JSON);
86 String fileString = TestUtils.readFileToString(schemaFile);
88 // Unmarshall that to a Java POJO
89 ObjectMapper mapper = new ObjectMapper();
90 DocumentSchema docSchema = mapper.readValue(schemaFile, DocumentSchema.class);
92 String jsonString = mapper.writeValueAsString(docSchema);
94 // Assert that the raw JSON that we read from the file matches the marshalled
95 // JSON we generated from our Java object (ie: validate that we didn't lose
96 // anything going in either direction).
97 assertTrue("Marshalled object does not match the original json source that produced it",
98 fileString.equals(jsonString));