ab1d293ef14612c4762b8da15cc6fcfdf3d8e4f3
[aai/search-data-service.git] / src / test / java / org / onap / aai / sa / rest / DocumentSchemaTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21 package org.onap.aai.sa.rest;
22
23 import com.fasterxml.jackson.core.JsonParseException;
24 import com.fasterxml.jackson.databind.JsonMappingException;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import org.junit.Test;
27 import org.onap.aai.sa.rest.DocumentSchema;
28
29 import java.io.BufferedReader;
30 import java.io.File;
31 import java.io.FileReader;
32 import java.io.IOException;
33
34 import static org.junit.Assert.assertTrue;
35
36
37 public class DocumentSchemaTest {
38
39   private final String SIMPLE_DOC_SCHEMA_JSON = "src/test/resources/json/simpleDocument.json";
40   private final String NESTED_DOC_SCHEMA_JSON = "src/test/resources/json/nested-document.json";
41
42
43   /**
44    * This test validates that we convert document definitions back and
45    * forth between json strings and POJOs without any loss of data.
46    *
47    * @throws com.fasterxml.jackson.core.JsonParseException
48    * @throws com.fasterxml.jackson.databind.JsonMappingException
49    * @throws IOException
50    */
51   @Test
52   public void simpleDocSchemaFromJsonFileTest() throws com.fasterxml.jackson.core.JsonParseException, com.fasterxml.jackson.databind.JsonMappingException, IOException {
53
54     // Import our json format document schema from a file.
55     File schemaFile = new File(SIMPLE_DOC_SCHEMA_JSON);
56     String fileString = TestUtils.readFileToString(schemaFile);
57
58     // Unmarshall that to a Java POJO
59     ObjectMapper mapper = new ObjectMapper();
60     DocumentSchema docSchema = mapper.readValue(schemaFile, DocumentSchema.class);
61
62     // Now, for the purposes of comparison, produce a JSON string from
63     // our Java object.
64     String jsonString = mapper.writeValueAsString(docSchema);
65
66     // Assert that the raw JSON that we read from the file matches the marshalled
67     // JSON we generated from our Java object (ie: validate that we didn't lose
68     // anything going in either direction).
69     assertTrue("Marshalled object does not match the original json source that produced it",
70         fileString.equals(jsonString));
71   }
72
73 //
74 //  /**
75 //   * This test validates that we convert document definitions back and
76 //   * forth between json strings and POJOs without any loss of data in
77 //   * the case of document schemas which contain nested fields.
78 //   *
79 //   * @throws com.fasterxml.jackson.core.JsonParseException
80 //   * @throws com.fasterxml.jackson.databind.JsonMappingException
81 //   * @throws IOException
82 //   */
83
84   @Test
85   public void nestedDocSchemaFromJsonFileTest() throws JsonParseException, JsonMappingException, IOException {
86
87     // Import our json format document schema from a file.
88     File schemaFile = new File(NESTED_DOC_SCHEMA_JSON);
89     String fileString = TestUtils.readFileToString(schemaFile);
90
91     // Unmarshall that to a Java POJO
92     ObjectMapper mapper = new ObjectMapper();
93     DocumentSchema docSchema = mapper.readValue(schemaFile, DocumentSchema.class);
94
95     String jsonString = mapper.writeValueAsString(docSchema);
96
97     // Assert that the raw JSON that we read from the file matches the marshalled
98     // JSON we generated from our Java object (ie: validate that we didn't lose
99     // anything going in either direction).
100     assertTrue("Marshalled object does not match the original json source that produced it",
101         fileString.equals(jsonString));
102   }
103 }