2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.spike.schema;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
26 import java.io.BufferedReader;
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;
38 * This set of tests validates the ability of the {@link GraphEventTransformer} to produce OXM model
39 * compliant representations of generic graph entities.
41 public class GraphEventTransformerTest {
44 * Performs all setup steps expected to be performed prior to each test.
47 public void setup() throws Exception {
48 // Load the OXM model definitions.
49 OXMModelLoader.loadModels();
51 // Load the relationship definitions.
52 System.setProperty("CONFIG_HOME", "src/test/resources/");
53 EdgeRulesLoader.loadModels();
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.
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");
70 // Now, validate our raw vertex from OXM model
71 GraphEventTransformer.validateVertexModel(graphEvent.getVertex());
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);
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");
85 // Now, validate our raw edge from relationship model
86 GraphEventTransformer.validateEdgeModel(graphEvent.getRelationship());
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);
94 * This helper method reads the contents of a file into a simple string.
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
100 public static String readFileToString(File file) throws IOException {
102 BufferedReader br = new BufferedReader(new FileReader(file));
104 StringBuilder sb = new StringBuilder();
105 String line = br.readLine();
107 while (line != null) {
109 line = br.readLine();
112 return sb.toString().replaceAll("\\s+", "");
116 } catch (IOException e) {
117 fail("Unexpected IOException: " + e.getMessage());