Enhance SchemaGenerator to also generate indices for relationships
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / serialization / queryformats / ResourceWithSoTTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.aai.serialization.queryformats;
22
23 import static org.junit.Assert.*;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.when;
26
27 import com.google.gson.JsonObject;
28
29 import java.util.Optional;
30
31 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
32 import org.apache.tinkerpop.gremlin.structure.Graph;
33 import org.apache.tinkerpop.gremlin.structure.Vertex;
34 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 import org.onap.aai.AAISetup;
40 import org.onap.aai.exceptions.AAIException;
41 import org.onap.aai.introspection.Loader;
42 import org.onap.aai.introspection.ModelType;
43 import org.onap.aai.serialization.db.DBSerializer;
44 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
45 import org.onap.aai.serialization.engines.QueryStyle;
46 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
47 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
48 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
49 import org.onap.aai.setup.SchemaVersion;
50 import org.springframework.test.annotation.DirtiesContext;
51 import org.springframework.test.annotation.DirtiesContext.ClassMode;
52
53 @DirtiesContext(classMode=ClassMode.BEFORE_CLASS)
54 public class ResourceWithSoTTest extends AAISetup {
55     @Mock
56     private UrlBuilder urlBuilder;
57
58     private Graph graph;
59     private Vertex putVertex;
60     private Vertex patchVertex1;
61     private Vertex patchVertex2;
62
63     private JsonObject jsonPutObj = new JsonObject();
64     private JsonObject jsonPatchObj1 = new JsonObject();
65     private JsonObject jsonPatchObj2 = new JsonObject();
66
67     private SchemaVersion version;
68     private ResourceWithSoT resourceWithSoT;
69
70     private TransactionalGraphEngine dbEngine;
71     private Loader loader;
72     private DBSerializer serializer;
73     private final ModelType factoryType = ModelType.MOXY;
74
75     @Before
76     public void setUp() throws Exception {
77
78         version = schemaVersions.getDefaultVersion();
79         MockitoAnnotations.openMocks(this);
80
81         graph = TinkerGraph.open();
82
83         long currentTimeMs = System.currentTimeMillis();
84         final String timeNowInMs = Long.toString(currentTimeMs);
85
86         // PUT / CREATE
87         jsonPutObj.addProperty("aai-created-ts", timeNowInMs);
88         jsonPutObj.addProperty("aai-last-mod-ts", timeNowInMs);
89         jsonPutObj.addProperty("source-of-truth", "user_a");
90         jsonPutObj.addProperty("last-mod-source-of-truth", "user_a");
91         jsonPutObj.addProperty("last-action-performed", "Created");
92
93         putVertex = graph.addVertex("aai-created-ts", timeNowInMs, "aai-last-mod-ts", timeNowInMs, "source-of-truth",
94                 "user_a", "last-mod-source-of-truth", "user_a");
95
96         // PATCH / MODIFY with differing source of truths
97         jsonPatchObj1.addProperty("aai-created-ts", timeNowInMs);
98         jsonPatchObj1.addProperty("aai-last-mod-ts", timeNowInMs);
99         jsonPatchObj1.addProperty("source-of-truth", "user_a");
100         jsonPatchObj1.addProperty("last-mod-source-of-truth", "user_b");
101         jsonPatchObj1.addProperty("last-action-performed", "Modified");
102
103         patchVertex1 = graph.addVertex("aai-created-ts", timeNowInMs, "aai-last-mod-ts", timeNowInMs, "source-of-truth",
104                 "user_a", "last-mod-source-of-truth", "user_b");
105
106         // PATCH / MODIFY with differing time stamps
107         jsonPatchObj2.addProperty("aai-created-ts", timeNowInMs);
108         jsonPatchObj2.addProperty("aai-last-mod-ts", Long.toString(currentTimeMs + 1000));
109         jsonPatchObj2.addProperty("source-of-truth", "user_a");
110         jsonPatchObj2.addProperty("last-mod-source-of-truth", "user_a");
111         jsonPatchObj2.addProperty("last-action-performed", "Modified");
112
113         patchVertex2 = graph.addVertex("aai-created-ts", timeNowInMs, "aai-last-mod-ts",
114                 Long.toString(currentTimeMs + 1000), "source-of-truth", "user_a", "last-mod-source-of-truth", "user_a");
115
116         graph = TinkerGraph.open();
117         createLoaderEngineSetup();
118     }
119
120     public void createLoaderEngineSetup() throws AAIException {
121
122         loader = loaderFactory.createLoaderForVersion(factoryType, version);
123
124         dbEngine = spy(new JanusGraphDBEngine(QueryStyle.TRAVERSAL, loader));
125         serializer = new DBSerializer(version, dbEngine, factoryType, "Junit");
126         resourceWithSoT = new ResourceWithSoT.Builder(loader, serializer, urlBuilder).build();
127
128         TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
129
130         when(dbEngine.tx()).thenReturn(graph);
131         when(dbEngine.asAdmin()).thenReturn(spyAdmin);
132
133         when(spyAdmin.getReadOnlyTraversalSource())
134                 .thenReturn(graph.traversal().withStrategies(ReadOnlyStrategy.instance()));
135         when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());
136     }
137
138     // This test is to simulate a PUT request
139     @Test
140     public void testGetJsonFromVertexWithCreateVertex() throws AAIFormatVertexException, AAIException {
141         if (putVertex == null)
142             fail("The vertex used for this test is null. Fail immediately.");
143
144         JsonObject json = resourceWithSoT.getJsonFromVertex(putVertex).get();
145         assertEquals(jsonPutObj, json);
146     }
147
148     // This test is to simulate PATCH requests
149     @Test
150     public void testGetJsonFromVertexWithModifyVertex() throws AAIFormatVertexException, AAIException {
151         if (patchVertex1 == null)
152             fail("The vertex 1 used for this test is null. Fail immediately.");
153         if (patchVertex2 == null)
154             fail("The vertex 2 used for this test is null. Fail immediately.");
155
156         // Differing Source of Truths will indicate that the action performed modified the vertex
157         JsonObject json1 = resourceWithSoT.getJsonFromVertex(patchVertex1).get();
158         assertEquals(jsonPatchObj1, json1);
159
160         // Timestamps that have a large span in time difference will (likely) indicate that the transaction was not a
161         // create (thus, modify)
162         JsonObject json2 = resourceWithSoT.getJsonFromVertex(patchVertex2).get();
163         assertEquals(jsonPatchObj2, json2);
164     }
165
166     @Test
167     public void testGetJsonFromVertexWithNullVertex() throws AAIFormatVertexException {
168         // Null check, will return not present Optional.
169         Optional<JsonObject> result = resourceWithSoT.getJsonFromVertex(null);
170         assertFalse(result.isPresent());
171     }
172 }