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