Add capability to record timestamp
[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 package org.onap.aai.serialization.queryformats;
21
22 import com.google.gson.JsonObject;
23 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
24 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
25 import org.apache.tinkerpop.gremlin.structure.Graph;
26 import org.apache.tinkerpop.gremlin.structure.Vertex;
27 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.mockito.Mock;
31 import org.mockito.MockitoAnnotations;
32 import org.onap.aai.AAISetup;
33 import org.onap.aai.dbmap.DBConnectionType;
34 import org.onap.aai.exceptions.AAIException;
35 import org.onap.aai.introspection.Loader;
36 import org.onap.aai.introspection.ModelType;
37 import org.onap.aai.serialization.db.DBSerializer;
38 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
39 import org.onap.aai.serialization.engines.QueryStyle;
40 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
41 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
42 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
43 import org.onap.aai.setup.SchemaVersion;
44
45 import static org.junit.Assert.assertEquals;
46 import static org.junit.Assert.assertNull;
47 import static org.junit.Assert.assertTrue;
48 import static org.mockito.Mockito.spy;
49 import static org.mockito.Mockito.when;
50
51 public class ResourceWithSoTTest extends AAISetup {
52     @Mock
53     private UrlBuilder urlBuilder;
54
55     private Graph graph;
56     private Vertex putVertex;
57     private Vertex patchVertex1;
58     private Vertex patchVertex2;
59
60     private JsonObject jsonPutObj = new JsonObject() ;
61     private JsonObject jsonPatchObj1 = new JsonObject() ;
62     private JsonObject jsonPatchObj2 = new JsonObject() ;
63
64     private SchemaVersion version;
65     private ResourceWithSoT resourceWithSoT;
66
67     private TransactionalGraphEngine dbEngine;
68     private Loader loader;
69     private DBSerializer serializer;
70     private final ModelType factoryType = ModelType.MOXY;
71
72     @Before
73     public void setUp() throws Exception {
74
75         version = schemaVersions.getDefaultVersion();
76         MockitoAnnotations.initMocks(this);
77
78         graph = TinkerGraph.open();
79
80         Long currentTimeMs = System.currentTimeMillis();
81         String timeNowInMs = Long.toString(currentTimeMs);
82
83         // PUT / CREATE
84         jsonPutObj.addProperty("aai-created-ts", timeNowInMs);
85         jsonPutObj.addProperty("aai-last-mod-ts", timeNowInMs);
86         jsonPutObj.addProperty("source-of-truth", "user_a");
87         jsonPutObj.addProperty("last-mod-source-of-truth", "user_a");
88         jsonPutObj.addProperty("last-action-performed", "Created");
89
90         putVertex = graph.addVertex(
91             "aai-created-ts", timeNowInMs,
92             "aai-last-mod-ts", timeNowInMs,
93             "source-of-truth", "user_a",
94             "last-mod-source-of-truth", "user_a"
95         );
96
97         // PATCH / MODIFY with differing source of truths
98         jsonPatchObj1.addProperty("aai-created-ts", timeNowInMs);
99         jsonPatchObj1.addProperty("aai-last-mod-ts", timeNowInMs);
100         jsonPatchObj1.addProperty("source-of-truth", "user_a");
101         jsonPatchObj1.addProperty("last-mod-source-of-truth", "user_b");
102         jsonPatchObj1.addProperty("last-action-performed", "Modified");
103
104         patchVertex1 = graph.addVertex(
105             "aai-created-ts", timeNowInMs,
106             "aai-last-mod-ts", timeNowInMs,
107             "source-of-truth", "user_a",
108             "last-mod-source-of-truth", "user_b"
109         );
110
111         // PATCH / MODIFY with differing time stamps
112         jsonPatchObj2.addProperty("aai-created-ts", timeNowInMs);
113         jsonPatchObj2.addProperty("aai-last-mod-ts", Long.toString(currentTimeMs + 1000));
114         jsonPatchObj2.addProperty("source-of-truth", "user_a");
115         jsonPatchObj2.addProperty("last-mod-source-of-truth", "user_a");
116         jsonPatchObj2.addProperty("last-action-performed", "Modified");
117
118         patchVertex2 = graph.addVertex(
119             "aai-created-ts", timeNowInMs,
120             "aai-last-mod-ts", Long.toString(currentTimeMs + 1000),
121             "source-of-truth", "user_a",
122             "last-mod-source-of-truth", "user_a"
123         );
124
125         graph = TinkerGraph.open();
126         createLoaderEngineSetup();
127     }
128
129     // This test is to simulate a PUT request
130     @Test
131     public void testGetJsonFromVertexWithCreateVertex() throws AAIFormatVertexException, AAIException {
132         if (putVertex == null)
133             assertTrue("The vertex used for this test is null. Fail immediately.", false);
134
135         JsonObject json = resourceWithSoT.getJsonFromVertex(putVertex).get();
136         assertEquals(jsonPutObj, json);
137     }
138
139     // This test is to simulate PATCH requests
140     @Test
141     public void testGetJsonFromVertexWithModifyVertex() throws AAIFormatVertexException, AAIException {
142         if (patchVertex1 == null)
143             assertTrue("The vertex 1 used for this test is null. Fail immediately.", false);
144         if (patchVertex2 == null)
145             assertTrue("The vertex 2 used for this test is null. Fail immediately.", false);
146
147         // Differing Source of Truths will indicate that the action performed modified the vertex
148         JsonObject json1 = resourceWithSoT.getJsonFromVertex(patchVertex1).get();
149         assertEquals(jsonPatchObj1, json1);
150
151         // Timestamps that have a large span in time difference will (likely) indicate that the transaction was not a create (thus, modify)
152         JsonObject json2 = resourceWithSoT.getJsonFromVertex(patchVertex2).get();
153         assertEquals(jsonPatchObj2, json2);
154     }
155
156     @Test
157     public void testGetJsonFromVertexWithNullVertex() throws AAIFormatVertexException, AAIException {
158         // Null check, will return null.
159         assertNull(resourceWithSoT.getJsonFromVertex(null));
160     }
161
162     public void createLoaderEngineSetup() throws AAIException {
163
164         if (loader == null) {
165             loader = loaderFactory.createLoaderForVersion(factoryType, version);
166             //loader = LoaderFactory.createLoaderForVersion(factoryType, version);
167             dbEngine = spy(new JanusGraphDBEngine(QueryStyle.TRAVERSAL, DBConnectionType.CACHED, loader));
168             serializer = new DBSerializer(version, dbEngine, factoryType, "Junit");
169             resourceWithSoT = new ResourceWithSoT.Builder(loader, serializer, urlBuilder).build();
170
171             TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
172
173             when(dbEngine.tx()).thenReturn(graph);
174             when(dbEngine.asAdmin()).thenReturn(spyAdmin);
175
176             when(spyAdmin.getReadOnlyTraversalSource())
177                 .thenReturn(graph.traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance())));
178             when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());
179         }
180     }
181 }