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