Increase code coverage
[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.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26 import java.io.BufferedReader;
27 import java.io.File;
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;
37
38
39 /**
40  * This set of tests validates the ability of the {@link GraphEventTransformer} to produce OXM model
41  * compliant representations of generic graph entities.
42  */
43 public class GraphEventTransformerTest {
44     /**
45      * Performs all setup steps expected to be performed prior to each test.
46      */
47     @Before
48     public void setup() throws Exception {
49         // Load the OXM model definitions.
50         OXMModelLoader.loadModels();
51
52         // Load the relationship definitions.
53         System.setProperty("CONFIG_HOME", "src/test/resources/");
54         EdgeRulesLoader.loadModels();
55     }
56
57
58     /**
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.
61      *
62      */
63     @Test
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");
69
70
71         // Now, validate our raw vertex from OXM model
72         GraphEventTransformer.validateVertexModel(graphEvent.getVertex());
73
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);
77     }
78
79     @Test
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");
85
86         // Now, validate our raw edge from relationship model
87         GraphEventTransformer.validateEdgeModel(graphEvent.getRelationship());
88
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);
92     }
93
94     /**
95      * This helper method reads the contents of a file into a simple string.
96      *
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
100      */
101     public static String readFileToString(File file) throws IOException {
102
103         BufferedReader br = new BufferedReader(new FileReader(file));
104         try {
105             StringBuilder sb = new StringBuilder();
106             String line = br.readLine();
107
108             while (line != null) {
109                 sb.append(line);
110                 line = br.readLine();
111             }
112
113             return sb.toString().replaceAll("\\s+", "");
114         } finally {
115             try {
116                 br.close();
117             } catch (IOException e) {
118                 fail("Unexpected IOException: " + e.getMessage());
119             }
120         }
121     }
122     
123     @Test
124     public void TestValidateFieldType() throws SpikeException {
125         String value = null;
126         Class clazz;
127         Object obj;
128
129         clazz = Integer.class;
130         value = "5";
131         obj = GraphEventTransformer.validateFieldType(value, clazz);
132         assertEquals(new Integer(5), obj);
133
134         clazz = Long.class;
135         value = "5";
136         obj = GraphEventTransformer.validateFieldType(value, clazz);
137         assertEquals(new Long(5), obj);
138
139         clazz = Float.class;
140         value = "5.5";
141         obj = GraphEventTransformer.validateFieldType(value, clazz);
142         assertEquals(new Float(5.5), obj);
143
144         clazz = Double.class;
145         value = "5";
146         obj = GraphEventTransformer.validateFieldType(value, clazz);
147         assertEquals(new Double(5), obj);
148
149         clazz = Boolean.class;
150         value = "true";
151         obj = GraphEventTransformer.validateFieldType(value, clazz);
152         assertEquals(new Boolean(true), obj);
153
154         value = "NotValidBoolean";
155         try {
156             GraphEventTransformer.validateFieldType(value, clazz);
157         } catch (SpikeException e) {
158             assertEquals("Invalid property value: " + value, e.getMessage());
159         }
160
161         clazz = String.class;
162         value = "11";
163         try {
164             GraphEventTransformer.validateFieldType(value, clazz);
165         } catch (SpikeException e) {
166             assertEquals("Invalid property value: " + value, e.getMessage());
167         }
168     }
169
170     @Test
171     public void TestPopulateUUID() throws IOException, URISyntaxException, SpikeException {
172         String champNotification =
173                 TestFileReader.getFileAsString("event/champ-update-notification-raw-with-relationship.json");
174
175         GizmoGraphEvent gizmoGraphEvent = new EventEnvelopeParser().parseEvent(champNotification);
176         GraphEventTransformer.populateUUID(gizmoGraphEvent);
177
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());
181     }
182 }