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