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