Initial search service commit
[aai/search-data-service.git] / src / test / java / org / openecomp / sa / rest / DocumentSchemaTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * Search Data Service
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
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
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25 package org.openecomp.sa.rest;
26
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;
32
33 import java.io.BufferedReader;
34 import java.io.File;
35 import java.io.FileReader;
36 import java.io.IOException;
37
38 import static org.junit.Assert.assertTrue;
39
40
41 public class DocumentSchemaTest {
42
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";
45
46
47   /**
48    * This test validates that we convert document definitions back and
49    * forth between json strings and POJOs without any loss of data.
50    *
51    * @throws com.fasterxml.jackson.core.JsonParseException
52    * @throws com.fasterxml.jackson.databind.JsonMappingException
53    * @throws IOException
54    */
55   @Test
56   public void simpleDocSchemaFromJsonFileTest() throws com.fasterxml.jackson.core.JsonParseException, com.fasterxml.jackson.databind.JsonMappingException, IOException {
57
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);
61
62     // Unmarshall that to a Java POJO
63     ObjectMapper mapper = new ObjectMapper();
64     DocumentSchema docSchema = mapper.readValue(schemaFile, DocumentSchema.class);
65
66     // Now, for the purposes of comparison, produce a JSON string from
67     // our Java object.
68     String jsonString = mapper.writeValueAsString(docSchema);
69
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));
75   }
76
77
78   /**
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.
82    *
83    * @throws com.fasterxml.jackson.core.JsonParseException
84    * @throws com.fasterxml.jackson.databind.JsonMappingException
85    * @throws IOException
86    */
87   @Test
88   public void nestedDocSchemaFromJsonFileTest() throws JsonParseException, JsonMappingException, IOException {
89
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);
93
94     // Unmarshall that to a Java POJO
95     ObjectMapper mapper = new ObjectMapper();
96     DocumentSchema docSchema = mapper.readValue(schemaFile, DocumentSchema.class);
97
98     String jsonString = mapper.writeValueAsString(docSchema);
99
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));
105   }
106 }