2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License ati
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
25 package org.openecomp.sa.rest;
27 import com.fasterxml.jackson.core.JsonParseException;
28 import com.fasterxml.jackson.databind.JsonMappingException;
29 import com.fasterxml.jackson.databind.ObjectMapper;
30 import org.junit.Test;
31 import org.openecomp.sa.rest.DocumentSchema;
33 import java.io.BufferedReader;
35 import java.io.FileReader;
36 import java.io.IOException;
38 import static org.junit.Assert.assertTrue;
41 public class DocumentSchemaTest {
43 private final String SIMPLE_DOC_SCHEMA_JSON = "src/test/resources/json/simpleDocument.json";
44 private final String NESTED_DOC_SCHEMA_JSON = "src/test/resources/json/nested-document.json";
48 * This test validates that we convert document definitions back and
49 * forth between json strings and POJOs without any loss of data.
51 * @throws com.fasterxml.jackson.core.JsonParseException
52 * @throws com.fasterxml.jackson.databind.JsonMappingException
56 public void simpleDocSchemaFromJsonFileTest() throws com.fasterxml.jackson.core.JsonParseException, com.fasterxml.jackson.databind.JsonMappingException, IOException {
58 // Import our json format document schema from a file.
59 File schemaFile = new File(SIMPLE_DOC_SCHEMA_JSON);
60 String fileString = TestUtils.readFileToString(schemaFile);
62 // Unmarshall that to a Java POJO
63 ObjectMapper mapper = new ObjectMapper();
64 DocumentSchema docSchema = mapper.readValue(schemaFile, DocumentSchema.class);
66 // Now, for the purposes of comparison, produce a JSON string from
68 String jsonString = mapper.writeValueAsString(docSchema);
70 // Assert that the raw JSON that we read from the file matches the marshalled
71 // JSON we generated from our Java object (ie: validate that we didn't lose
72 // anything going in either direction).
73 assertTrue("Marshalled object does not match the original json source that produced it",
74 fileString.equals(jsonString));
79 * This test validates that we convert document definitions back and
80 * forth between json strings and POJOs without any loss of data in
81 * the case of document schemas which contain nested fields.
83 * @throws com.fasterxml.jackson.core.JsonParseException
84 * @throws com.fasterxml.jackson.databind.JsonMappingException
88 public void nestedDocSchemaFromJsonFileTest() throws JsonParseException, JsonMappingException, IOException {
90 // Import our json format document schema from a file.
91 File schemaFile = new File(NESTED_DOC_SCHEMA_JSON);
92 String fileString = TestUtils.readFileToString(schemaFile);
94 // Unmarshall that to a Java POJO
95 ObjectMapper mapper = new ObjectMapper();
96 DocumentSchema docSchema = mapper.readValue(schemaFile, DocumentSchema.class);
98 String jsonString = mapper.writeValueAsString(docSchema);
100 // Assert that the raw JSON that we read from the file matches the marshalled
101 // JSON we generated from our Java object (ie: validate that we didn't lose
102 // anything going in either direction).
103 assertTrue("Marshalled object does not match the original json source that produced it",
104 fileString.equals(jsonString));