f58bbfc0abde56ba18e6d94cfa832563038def7c
[aai/spike.git] / src / test / java / org / onap / aai / spike / schema / GraphEventTransformerTest.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.spike.schema;
22
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25
26 import java.io.BufferedReader;
27 import java.io.File;
28 import java.io.FileReader;
29 import java.io.IOException;
30 import javax.xml.bind.JAXBException;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.onap.aai.spike.event.incoming.GizmoGraphEvent;
34 import org.onap.aai.spike.exception.SpikeException;
35
36
37 /**
38  * This set of tests validates the ability of the {@link GraphEventTransformer} to produce OXM model
39  * compliant representations of generic graph entities.
40  */
41 public class GraphEventTransformerTest {
42
43     /**
44      * Performs all setup steps expected to be performed prior to each test.
45      */
46     @Before
47     public void setup() throws Exception {
48         // Load the OXM model definitions.
49         OXMModelLoader.loadModels();
50
51         // Load the relationship definitions.
52         System.setProperty("CONFIG_HOME", "src/test/resources/");
53         EdgeRulesLoader.loadModels();
54     }
55
56
57     /**
58      * Validates that, given a raw vertex from the graph abstraction layer, we can transform it into a
59      * JSON string which corresponds to the OXM model.
60      *
61      */
62     @Test
63     public void vertexToJsonTest() throws Exception {
64         // Instantiate the vertex that we will apply the translation to.
65         String vertexJson = readFileToString(new File("src/test/resources/vertex.json"));
66         GizmoGraphEvent graphEvent = GizmoGraphEvent.fromJson(vertexJson);
67         graphEvent.getVertex().getProperties().getAsJsonObject().addProperty("invalid-key1", "invalid-key2");
68
69
70         // Now, validate our raw vertex from OXM model
71         GraphEventTransformer.validateVertexModel(graphEvent.getVertex());
72
73         // Validate the marshalled string we got back against our OXM model.
74         assertTrue("Object failed to validate against OXM model.",
75                 graphEvent.getVertex().getProperties().getAsJsonObject().get("invalid-key1") == null);
76     }
77
78     @Test
79     public void edgeToJsonTest() throws Exception {
80         // Instantiate the edge that we will apply the translation to.
81         String edgeJson = readFileToString(new File("src/test/resources/edge.json"));
82         GizmoGraphEvent graphEvent = GizmoGraphEvent.fromJson(edgeJson);
83         graphEvent.getRelationship().getProperties().getAsJsonObject().addProperty("invalid-key1", "invalid-key2");
84
85         // Now, validate our raw edge from relationship model
86         GraphEventTransformer.validateEdgeModel(graphEvent.getRelationship());
87
88         // Validate the marshalled string we got back against our relationship model.
89         assertTrue("Object failed to validate against OXM model.",
90                 graphEvent.getRelationship().getProperties().getAsJsonObject().get("invalid-key1") == null);
91     }
92
93     /**
94      * This helper method reads the contents of a file into a simple string.
95      *
96      * @param file - The file to be imported.
97      * @return - The file contents expressed as a simple string.
98      * @throws IOException if there is a problem reading the file
99      */
100     public static String readFileToString(File file) throws IOException {
101
102         BufferedReader br = new BufferedReader(new FileReader(file));
103         try {
104             StringBuilder sb = new StringBuilder();
105             String line = br.readLine();
106
107             while (line != null) {
108                 sb.append(line);
109                 line = br.readLine();
110             }
111
112             return sb.toString().replaceAll("\\s+", "");
113         } finally {
114             try {
115                 br.close();
116             } catch (IOException e) {
117                 fail("Unexpected IOException: " + e.getMessage());
118             }
119         }
120     }
121 }