Reduce the number of problems in aai-common by removing unused imports
[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
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("aai-created-ts", timeNowInMs, "aai-last-mod-ts", timeNowInMs, "source-of-truth",
91                 "user_a", "last-mod-source-of-truth", "user_a");
92
93         // PATCH / MODIFY with differing source of truths
94         jsonPatchObj1.addProperty("aai-created-ts", timeNowInMs);
95         jsonPatchObj1.addProperty("aai-last-mod-ts", timeNowInMs);
96         jsonPatchObj1.addProperty("source-of-truth", "user_a");
97         jsonPatchObj1.addProperty("last-mod-source-of-truth", "user_b");
98         jsonPatchObj1.addProperty("last-action-performed", "Modified");
99
100         patchVertex1 = graph.addVertex("aai-created-ts", timeNowInMs, "aai-last-mod-ts", timeNowInMs, "source-of-truth",
101                 "user_a", "last-mod-source-of-truth", "user_b");
102
103         // PATCH / MODIFY with differing time stamps
104         jsonPatchObj2.addProperty("aai-created-ts", timeNowInMs);
105         jsonPatchObj2.addProperty("aai-last-mod-ts", Long.toString(currentTimeMs + 1000));
106         jsonPatchObj2.addProperty("source-of-truth", "user_a");
107         jsonPatchObj2.addProperty("last-mod-source-of-truth", "user_a");
108         jsonPatchObj2.addProperty("last-action-performed", "Modified");
109
110         patchVertex2 = graph.addVertex("aai-created-ts", timeNowInMs, "aai-last-mod-ts",
111                 Long.toString(currentTimeMs + 1000), "source-of-truth", "user_a", "last-mod-source-of-truth", "user_a");
112
113         graph = TinkerGraph.open();
114         createLoaderEngineSetup();
115     }
116
117     // This test is to simulate a PUT request
118     @Test
119     public void testGetJsonFromVertexWithCreateVertex() throws AAIFormatVertexException, AAIException {
120         if (putVertex == null)
121             fail("The vertex used for this test is null. Fail immediately.");
122
123         JsonObject json = resourceWithSoT.getJsonFromVertex(putVertex).get();
124         assertEquals(jsonPutObj, json);
125     }
126
127     // This test is to simulate PATCH requests
128     @Test
129     public void testGetJsonFromVertexWithModifyVertex() throws AAIFormatVertexException, AAIException {
130         if (patchVertex1 == null)
131             fail("The vertex 1 used for this test is null. Fail immediately.");
132         if (patchVertex2 == null)
133             fail("The vertex 2 used for this test is null. Fail immediately.");
134
135         // Differing Source of Truths will indicate that the action performed modified the vertex
136         JsonObject json1 = resourceWithSoT.getJsonFromVertex(patchVertex1).get();
137         assertEquals(jsonPatchObj1, json1);
138
139         // Timestamps that have a large span in time difference will (likely) indicate that the transaction was not a
140         // create (thus, modify)
141         JsonObject json2 = resourceWithSoT.getJsonFromVertex(patchVertex2).get();
142         assertEquals(jsonPatchObj2, json2);
143     }
144
145     @Test
146     public void testGetJsonFromVertexWithNullVertex() throws AAIFormatVertexException {
147         // Null check, will return not present Optional.
148         Optional<JsonObject> result = resourceWithSoT.getJsonFromVertex(null);
149         assertFalse(result.isPresent());
150     }
151
152     public void createLoaderEngineSetup() throws AAIException {
153
154         if (loader == null) {
155             loader = loaderFactory.createLoaderForVersion(factoryType, version);
156             // loader = LoaderFactory.createLoaderForVersion(factoryType, version);
157             dbEngine = spy(new JanusGraphDBEngine(QueryStyle.TRAVERSAL, loader));
158             serializer = new DBSerializer(version, dbEngine, factoryType, "Junit");
159             resourceWithSoT = new ResourceWithSoT.Builder(loader, serializer, urlBuilder).build();
160
161             TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
162
163             when(dbEngine.tx()).thenReturn(graph);
164             when(dbEngine.asAdmin()).thenReturn(spyAdmin);
165
166             when(spyAdmin.getReadOnlyTraversalSource())
167                     .thenReturn(graph.traversal().withStrategies(ReadOnlyStrategy.instance()));
168             when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());
169         }
170     }
171 }