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.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26 import java.io.BufferedReader;
28 import java.io.FileReader;
29 import java.io.IOException;
30 import java.net.URISyntaxException;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.onap.aai.spike.event.envelope.EventEnvelopeParser;
34 import org.onap.aai.spike.event.incoming.GizmoGraphEvent;
35 import org.onap.aai.spike.exception.SpikeException;
36 import org.onap.aai.spike.test.util.TestFileReader;
40 * This set of tests validates the ability of the {@link GraphEventTransformer} to produce OXM model
41 * compliant representations of generic graph entities.
43 public class GraphEventTransformerTest {
45 * Performs all setup steps expected to be performed prior to each test.
48 public void setup() throws Exception {
49 // Load the OXM model definitions.
50 OXMModelLoader.loadModels();
52 // Load the relationship definitions.
53 System.setProperty("CONFIG_HOME", "src/test/resources/");
54 EdgeRulesLoader.loadModels();
59 * Validates that, given a raw vertex from the graph abstraction layer, we can transform it into a
60 * JSON string which corresponds to the OXM model.
64 public void vertexToJsonTest() throws Exception {
65 // Instantiate the vertex that we will apply the translation to.
66 String vertexJson = readFileToString(new File("src/test/resources/vertex.json"));
67 GizmoGraphEvent graphEvent = GizmoGraphEvent.fromJson(vertexJson);
68 graphEvent.getVertex().getProperties().getAsJsonObject().addProperty("invalid-key1", "invalid-key2");
71 // Now, validate our raw vertex from OXM model
72 GraphEventTransformer.validateVertexModel(graphEvent.getVertex());
74 // Validate the marshalled string we got back against our OXM model.
75 assertTrue("Object failed to validate against OXM model.",
76 graphEvent.getVertex().getProperties().getAsJsonObject().get("invalid-key1") == null);
80 public void edgeToJsonTest() throws Exception {
81 // Instantiate the edge that we will apply the translation to.
82 String edgeJson = readFileToString(new File("src/test/resources/edge.json"));
83 GizmoGraphEvent graphEvent = GizmoGraphEvent.fromJson(edgeJson);
84 graphEvent.getRelationship().getProperties().getAsJsonObject().addProperty("invalid-key1", "invalid-key2");
86 // Now, validate our raw edge from relationship model
87 GraphEventTransformer.validateEdgeModel(graphEvent.getRelationship());
89 // Validate the marshalled string we got back against our relationship model.
90 assertTrue("Object failed to validate against OXM model.",
91 graphEvent.getRelationship().getProperties().getAsJsonObject().get("invalid-key1") == null);
95 * This helper method reads the contents of a file into a simple string.
97 * @param file - The file to be imported.
98 * @return - The file contents expressed as a simple string.
99 * @throws IOException if there is a problem reading the file
101 public static String readFileToString(File file) throws IOException {
103 BufferedReader br = new BufferedReader(new FileReader(file));
105 StringBuilder sb = new StringBuilder();
106 String line = br.readLine();
108 while (line != null) {
110 line = br.readLine();
113 return sb.toString().replaceAll("\\s+", "");
117 } catch (IOException e) {
118 fail("Unexpected IOException: " + e.getMessage());
124 public void TestValidateFieldType() throws SpikeException {
129 clazz = Integer.class;
131 obj = GraphEventTransformer.validateFieldType(value, clazz);
132 assertEquals(new Integer(5), obj);
136 obj = GraphEventTransformer.validateFieldType(value, clazz);
137 assertEquals(new Long(5), obj);
141 obj = GraphEventTransformer.validateFieldType(value, clazz);
142 assertEquals(new Float(5.5), obj);
144 clazz = Double.class;
146 obj = GraphEventTransformer.validateFieldType(value, clazz);
147 assertEquals(new Double(5), obj);
149 clazz = Boolean.class;
151 obj = GraphEventTransformer.validateFieldType(value, clazz);
152 assertEquals(new Boolean(true), obj);
154 value = "NotValidBoolean";
156 GraphEventTransformer.validateFieldType(value, clazz);
157 } catch (SpikeException e) {
158 assertEquals("Invalid property value: " + value, e.getMessage());
161 clazz = String.class;
164 GraphEventTransformer.validateFieldType(value, clazz);
165 } catch (SpikeException e) {
166 assertEquals("Invalid property value: " + value, e.getMessage());
171 public void TestPopulateUUID() throws IOException, URISyntaxException, SpikeException {
172 String champNotification =
173 TestFileReader.getFileAsString("event/champ-update-notification-raw-with-relationship.json");
175 GizmoGraphEvent gizmoGraphEvent = new EventEnvelopeParser().parseEvent(champNotification);
176 GraphEventTransformer.populateUUID(gizmoGraphEvent);
178 assertEquals("b9c7d24a-64a5-4b89-a10a-a89ce58b1caa", gizmoGraphEvent.getRelationship().getId());
179 assertEquals("537494bd-1e8a-4198-9712-8cefa0f80457", gizmoGraphEvent.getRelationship().getSource().getId());
180 assertEquals("981c0494-c742-4d75-851c-8194bbbd8a96", gizmoGraphEvent.getRelationship().getTarget().getId());