Auto-resolve edge type
[aai/gizmo.git] / src / test / java / org / onap / crud / service / ChampDaoMockTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.crud.service;
22
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 import javax.ws.rs.core.MediaType;
31 import javax.ws.rs.core.MultivaluedHashMap;
32 import javax.ws.rs.core.MultivaluedMap;
33 import org.onap.aai.logging.LoggingContext;
34 import org.onap.aai.restclient.client.OperationResult;
35 import org.onap.aai.restclient.client.RestClient;
36 import org.onap.crud.dao.champ.ChampDao;
37 import org.slf4j.MDC;
38
39 public class ChampDaoMockTest {
40     // @formatter:off
41         private final String champVertex = "{" +
42             "\"key\": \"test-uuid\"," +
43             "\"type\": \"vertexType\"," +
44             "\"properties\": {" +
45             "\"fqdn\": \"myhost.onap.com\"," +
46             "\"hostname\": \"myhost\" } }";
47         
48         private final String champEdge = "{" +
49             "\"key\": \"test-uuid\"," +
50             "\"type\": \"edgeType\"," +
51             "\"properties\": {" +
52             "\"prevent-delete\": \"NONE\" }," +
53             "\"source\": {" +
54             "\"key\": \"50bdab41-ad1c-4d00-952c-a0aa5d827811\", \"type\": \"vserver\"}," +
55             "\"target\": {" +
56             "\"key\": \"1d326bc7-b985-492b-9604-0d5d1f06f908\", \"type\": \"pserver\"}" +
57             " }";
58         
59         private final String edgePayload = "{" + 
60                 "\"type\":\"tosca.relationships.HostedOn\"," + 
61                 "\"properties\":{" + 
62                 "\"prevent-delete\":\"NONE\"}," + 
63                 "\"source\":{" + 
64                 "\"key\":\"test-uuid\"," + 
65                 "\"type\":\"vserver\"," + 
66                 "\"properties\":{}}," + 
67                 "\"target\":{" + 
68                 "\"key\":\"test-uuid\"," + 
69                 "\"type\":\"pserver\"," + 
70                 "\"properties\":{" + 
71                 "\"hostname\":\"myhost\"," + 
72                 "\"fqdn\":\"myhost.onap.com\"}}}";
73
74         private final String edgePayloadForPut = "{" + 
75                 "\"key\":\"test-uuid\"," + 
76                 "\"type\":\"tosca.relationships.HostedOn\"," + 
77                 "\"properties\":{" + 
78                 "\"prevent-delete\":\"NONE\"}," + 
79                 "\"source\":{" + 
80                 "\"key\":\"50bdab41-ad1c-4d00-952c-a0aa5d827811\"," + 
81                 "\"type\":\"vserver\"," + 
82                 "\"properties\":{}}," + 
83                 "\"target\":{" + 
84                 "\"key\":\"1d326bc7-b985-492b-9604-0d5d1f06f908\"," + 
85                 "\"type\":\"pserver\"," + 
86                 "\"properties\":{}}}";
87         
88         private final String edgePayloadForPatch = "{" + 
89                 "\"key\":\"test-uuid\"," + 
90                 "\"type\":\"tosca.relationships.HostedOn\"," + 
91                 "\"properties\":{" + 
92                 "\"prevent-delete\":\"NONE\"}," + 
93                 "\"source\":{" + 
94                 "\"key\":\"50bdab41-ad1c-4d00-952c-a0aa5d827811\"," + 
95                 "\"type\":\"vserver\"}," + 
96                 "\"target\":{" + 
97                 "\"key\":\"1d326bc7-b985-492b-9604-0d5d1f06f908\"," + 
98                 "\"type\":\"pserver\"}}";
99         
100         private final String edgePayloadForPost = "{" + 
101                 "\"type\":\"tosca.relationships.HostedOn\"," + 
102                 "\"properties\":{" + 
103                 "\"SVC-INFRA\":\"OUT\"," + 
104                 "\"prevent-delete\":\"IN\"," + 
105                 "\"delete-other-v\":\"NONE\"," + 
106                 "\"contains-other-v\":\"NONE\"" + 
107                 "}," + 
108                 "\"source\":{" + 
109                 "\"key\":\"test-uuid\"," + 
110                 "\"type\":\"vserver\"," + 
111                 "\"properties\":{" + 
112                 "" + 
113                 "}" + 
114                 "}," + 
115                 "\"target\":{" + 
116                 "\"key\":\"test-uuid\"," + 
117                 "\"type\":\"pserver\"," + 
118                 "\"properties\":{" + 
119                 "\"hostname\":\"myhost\"," + 
120                 "\"fqdn\":\"myhost.onap.com\"" + 
121                 "}" + 
122                 "}" + 
123                 "}";
124         
125         private final String edgePayloadForPutNoProperties = "{" + 
126                 "\"key\":\"test-uuid\"," + 
127                 "\"type\":\"tosca.relationships.HostedOn\"," + 
128                 "\"properties\":{" + 
129                 "\"SVC-INFRA\":\"OUT\"," + 
130                 "\"prevent-delete\":\"IN\"," + 
131                 "\"delete-other-v\":\"NONE\"," + 
132                 "\"contains-other-v\":\"NONE\"" + 
133                 "}," + 
134                 "\"source\":{" + 
135                 "\"key\":\"50bdab41-ad1c-4d00-952c-a0aa5d827811\"," + 
136                 "\"type\":\"vserver\"," + 
137                 "\"properties\":{" + 
138                 "" + 
139                 "}" + 
140                 "}," + 
141                 "\"target\":{" + 
142                 "\"key\":\"1d326bc7-b985-492b-9604-0d5d1f06f908\"," + 
143                 "\"type\":\"pserver\"," + 
144                 "\"properties\":{" + 
145                 "" + 
146                 "}" + 
147                 "}" + 
148                 "}";
149         
150         private final String vertexPayload = "{" + 
151                 "\"type\":\"pserver\"," + 
152                 "\"properties\":{" + 
153                 "\"hostname\":\"myhost\"," + 
154                 "\"in-maint\":false," + 
155                 "\"fqdn\":\"myhost.onap.com\"," + 
156                 "\"last-mod-source-of-truth\":\"source-of-truth\"," + 
157                 "\"source-of-truth\":\"source-of-truth\"," + 
158                 "\"aai-node-type\":\"pserver\"}}";
159         
160         private final String vertexPayloadForVserver = "{" + 
161                 "\"type\":\"vserver\"," + 
162                 "\"properties\":{" + 
163                 "\"in-maint\":false," + 
164                 "\"vserver-name\":\"test-vserver\"," + 
165                 "\"vserver-id\":\"VSER1\"," + 
166                 "\"last-mod-source-of-truth\":\"source-of-truth\"," + 
167                 "\"vserver-name2\":\"alt-test-vserver\"," + 
168                 "\"source-of-truth\":\"source-of-truth\"," + 
169                 "\"vserver-selflink\":\"http://1.2.3.4/moreInfo\"," + 
170                 "\"is-closed-loop-disabled\":false," + 
171                 "\"aai-node-type\":\"vserver\"}}";
172         
173         private final String vertexPayloadForPserver = "{" + 
174                 "\"key\":\"50bdab41-ad1c-4d00-952c-a0aa5d827811\"," + 
175                 "\"type\":\"pserver\"," + 
176                 "\"properties\":{" + 
177                 "\"ptnii-equip-name\":\"e-name\"," + 
178                 "\"hostname\":\"steve-host2\"," + 
179                 "\"equip-type\":\"server\"," + 
180                 "\"equip-vendor\":\"HP\"," + 
181                 "\"equip-model\":\"DL380p-nd\"," + 
182                 "\"in-maint\":false," + 
183                 "\"fqdn\":\"myhost.onap.net\"," + 
184                 "\"purpose\":\"my-purpose\"," + 
185                 "\"ipv4-oam-address\":\"1.2.3.4\"," + 
186                 "\"last-mod-source-of-truth\":\"source-of-truth\"," + 
187                 "\"aai-node-type\":\"pserver\"}}";
188         
189         private final String vertexPayloadForPut = "{" + 
190                 "\"key\":\"test-uuid\"," + 
191                 "\"type\":\"pserver\"," + 
192                 "\"properties\":{" + 
193                 "\"hostname\":\"myhost\"," + 
194                 "\"in-maint\":false," + 
195                 "\"fqdn\":\"myhost.onap.com\"," + 
196                 "\"last-mod-source-of-truth\":\"source-of-truth\"," + 
197                 "\"aai-node-type\":\"pserver\"}}";
198         
199         private final String vertexPayloadForPatch = "{" + 
200                 "\"key\":\"test-uuid\"," + 
201                 "\"type\":\"pserver\"," + 
202                 "\"properties\":{" + 
203                 "\"hostname\":\"myhost\"," + 
204                 "\"fqdn\":\"myhost.onap.com\"," + 
205                 "\"last-mod-source-of-truth\":\"source-of-truth\"," + 
206                 "\"aai-node-type\":\"pserver\"}}";
207         // @formatter:on
208
209     private RestClient restClientMock;
210     private ChampDao champDao;
211
212     static final String CHAMP_URL = "https://host:9522/services/champ-service/v1/";
213     static final String OBJECT_SUB_URL = "objects";
214     static final String RELATIONSHIP_SUB_URL = "relationships";
215     static final String TRANSACTION_SUB_URL = "transaction";
216     static final String BASE_OBJECT_URL = CHAMP_URL + OBJECT_SUB_URL;
217     static final String HEADER_FROM_APP = "X-FromAppId";
218     static final String HEADER_TRANS_ID = "X-TransactionId";
219     static final String FROM_APP_NAME = "Gizmo";
220     static final String HEADER_TRANS_ID_VALUE = "1234567890";
221
222     ChampDaoMockTest() {
223         restClientMock = mock(RestClient.class);
224         init();
225         buildChampDao();
226     }
227
228     public void init() {
229
230         Map<String, String> queryParamsVertex = new HashMap<>();
231         queryParamsVertex.put("_reserved_version", "v11");
232         queryParamsVertex.put("hostname", "myhost");
233         queryParamsVertex.put("_reserved_aai-type", "pserver");
234
235         Map<String, String> queryParamsVertexV13 = new HashMap<>();
236         queryParamsVertexV13.put("_reserved_version", "v13");
237         queryParamsVertexV13.put("hostname", "myhost");
238         queryParamsVertexV13.put("_reserved_aai-type", "pserver");
239
240         Map<String, String> queryParamsVertices = new HashMap<>();
241         queryParamsVertices.put("_reserved_version", "v11");
242         queryParamsVertices.put("hostname", "myhost");
243         queryParamsVertices.put("_reserved_aai-type", "pserver");
244         queryParamsVertices.put("aai-node-type", "pserver");
245
246         Map<String, String> queryParamsVerticesV13 = new HashMap<>();
247         queryParamsVerticesV13.put("_reserved_version", "v13");
248         queryParamsVerticesV13.put("hostname", "myhost");
249         queryParamsVerticesV13.put("_reserved_aai-type", "pserver");
250         queryParamsVerticesV13.put("aai-node-type", "pserver");
251
252         Map<String, String> queryParamsEdge = new HashMap<>();
253         queryParamsEdge.put("_reserved_version", "v11");
254         queryParamsEdge.put("hostname", "myhost");
255         queryParamsEdge.put("_reserved_aai-type", "tosca.relationships.HostedOn");
256
257         Map<String, String> emptyQueryParams = null;
258
259         mockOpenTransaction();
260         mockRollbackTransaction("");
261         mockTransactionExists("");
262         mockCommitTransaction("");
263         mockGetVertex("872dd5df-0be9-4167-95e9-2cf4b21165ed", queryParamsVertex, "pserver");
264         mockGetVertex("872dd5df-0be9-4167-95e9-2cf4b21165ed", queryParamsVertexV13, "pserver");
265         mockGetVertex("50bdab41-ad1c-4d00-952c-a0aa5d827811", "", "vserver");
266         mockGetVertex("1d326bc7-b985-492b-9604-0d5d1f06f908", "", "pserver");
267         mockGetVertex("1d326bc7-b985-492b-9604-0d5d1f06f908", "?transactionId=", "pserver");
268         mockGetVertex("test-uuid", "", "pserver");
269         mockGetVertex("50bdab41-ad1c-4d00-952c-a0aa5d827811", "?transactionId=", "vserver");
270         mockGetVertices(queryParamsVertices, "pserver");
271         mockGetVertices(queryParamsVerticesV13, "pserver");
272         mockGetVertexEdges("872dd5df-0be9-4167-95e9-2cf4b21165ed", queryParamsVertex, null, "tosca.relationships.HostedOn");
273         mockGetVertexEdges("872dd5df-0be9-4167-95e9-2cf4b21165ed", queryParamsVertexV13, null,
274                 "tosca.relationships.HostedOn");
275         mockGetVertexEdges("50bdab41-ad1c-4d00-952c-a0aa5d827811", emptyQueryParams, null, "tosca.relationships.HostedOn");
276         mockGetVertexEdges("1d326bc7-b985-492b-9604-0d5d1f06f908", emptyQueryParams, null, "tosca.relationships.HostedOn");
277         mockGetVertexEdges("50bdab41-ad1c-4d00-952c-a0aa5d827811", emptyQueryParams, "?transactionId=", "tosca.relationships.HostedOn");
278         mockGetVertexEdges("1d326bc7-b985-492b-9604-0d5d1f06f908", emptyQueryParams, "?transactionId=", "tosca.relationships.HostedOn");
279         mockGetEdges("?", "tosca.relationships.HostedOn");
280         mockGetEdge("50bdab41-ad1c-4d00-952c-a0aa5d827811", "?transactionId=", "tosca.relationships.HostedOn");
281         mockGetEdge("872dd5df-0be9-4167-95e9-2cf4b21165ed", emptyQueryParams, "tosca.relationships.HostedOn");
282         mockGetEdge("872dd5df-0be9-4167-95e9-2cf4b21165ed", queryParamsEdge, "tosca.relationships.HostedOn");
283         mockGetEdge("my-uuid", emptyQueryParams, "tosca.relationships.HostedOn");
284         mockGetEdge("50bdab41-ad1c-4d00-952c-a0aa5d827811", queryParamsEdge, "tosca.relationships.HostedOn");
285         mockPostEdge("tosca.relationships.HostedOn", "", edgePayload);
286         mockPostEdge("tosca.relationships.HostedOn", "?transactionId=", edgePayloadForPost);
287         mockPostVertex("pserver", vertexPayload, "");
288         mockPostVertex("vserver", vertexPayloadForVserver, "?transactionId=");
289         mockPutVertex("test-uuid", "pserver", vertexPayloadForPut, "");
290         mockPutVertex("test-uuid", "pserver", vertexPayloadForPatch, "");
291         mockPutVertex("50bdab41-ad1c-4d00-952c-a0aa5d827811", "pserver", vertexPayloadForPserver, "?transactionId=");
292         mockPutEdge("test-uuid", "tosca.relationships.HostedOn", "", edgePayloadForPut);
293         mockPutEdge("test-uuid", "tosca.relationships.HostedOn", "", edgePayloadForPatch);
294         mockPutEdge("test-uuid", "tosca.relationships.HostedOn", "?transactionId=", edgePayloadForPutNoProperties);
295         mockDeleteVertex("872dd5df-0be9-4167-95e9-2cf4b21165ed", "pserver", "");
296         mockDeleteVertex("50bdab41-ad1c-4d00-952c-a0aa5d827811", "pserver", "?transactionId=");
297         mockDeleteEdge("872dd5df-0be9-4167-95e9-2cf4b21165ed", "tosca.relationships.HostedOn", "");
298         mockDeleteEdge("50bdab41-ad1c-4d00-952c-a0aa5d827811", "tosca.relationships.HostedOn", "?transactionId=");
299     }
300
301     public void buildChampDao() {
302         String baseRelationshipUrl = CHAMP_URL + RELATIONSHIP_SUB_URL;
303         String baseTransactionUrl = CHAMP_URL + TRANSACTION_SUB_URL;
304         champDao = new ChampDao(restClientMock, BASE_OBJECT_URL, baseRelationshipUrl, baseTransactionUrl);
305     }
306
307     public void mockOpenTransaction() {
308         OperationResult operationResult = new OperationResult();
309         operationResult.setResult("");
310         operationResult.setResultCode(200);
311         String url = CHAMP_URL + "transaction";
312
313
314         when(restClientMock.post(url, "", createHeaders(), MediaType.TEXT_PLAIN_TYPE, MediaType.TEXT_PLAIN_TYPE))
315                 .thenReturn(operationResult);
316     }
317
318     public void mockRollbackTransaction(String id) {
319         OperationResult operationResult = new OperationResult();
320         operationResult.setResult("");
321         operationResult.setResultCode(200);
322         String url = CHAMP_URL + TRANSACTION_SUB_URL + "/" + id;
323
324
325         when(restClientMock.put(url, "{\"method\": \"rollback\"}", createHeaders(), MediaType.APPLICATION_JSON_TYPE,
326                 MediaType.TEXT_PLAIN_TYPE)).thenReturn(operationResult);
327     }
328
329     public void mockCommitTransaction(String id) {
330         OperationResult operationResult = new OperationResult();
331         operationResult.setResult("");
332         operationResult.setResultCode(200);
333         String url = CHAMP_URL + TRANSACTION_SUB_URL + "/" + id;
334
335
336         when(restClientMock.put(url, "{\"method\": \"commit\"}", createHeaders(), MediaType.APPLICATION_JSON_TYPE,
337                 MediaType.TEXT_PLAIN_TYPE)).thenReturn(operationResult);
338     }
339
340     public void mockTransactionExists(String id) {
341         OperationResult operationResult = new OperationResult();
342         operationResult.setResult("");
343         operationResult.setResultCode(200);
344         String url = CHAMP_URL + TRANSACTION_SUB_URL + "/" + id;
345
346         Map<String, List<String>> headers = new HashMap<>();
347         headers.put(HEADER_FROM_APP, Arrays.asList(FROM_APP_NAME));
348         headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(LoggingContext.LoggingField.REQUEST_ID.toString())));
349
350         when(restClientMock.get(url, headers, MediaType.APPLICATION_JSON_TYPE)).thenReturn(operationResult);
351     }
352
353     public void mockGetVertex(String id, String txId, String type) {
354         String vertexResponse = champVertex.replace("vertexType", type);
355         OperationResult operationResult = new OperationResult();
356         operationResult.setResult(vertexResponse);
357         operationResult.setResultCode(200);
358
359         String url = BASE_OBJECT_URL + "/" + id + txId;
360
361
362         when(restClientMock.get(url, createHeaders(), MediaType.APPLICATION_JSON_TYPE)).thenReturn(operationResult);
363     }
364
365     public void mockGetVertex(String id, Map<String, String> queryParams, String type) {
366         String vertexResponse = champVertex.replace("vertexType", type);
367         OperationResult operationResult = new OperationResult();
368         operationResult.setResult(vertexResponse);
369         operationResult.setResultCode(200);
370
371         StringBuilder url = appendQueryParams(BASE_OBJECT_URL + "/" + id, queryParams);
372
373         when(restClientMock.get(url.toString(), createHeaders(), MediaType.APPLICATION_JSON_TYPE))
374                 .thenReturn(operationResult);
375     }
376
377     public void mockGetVertexEdges(String id, Map<String, String> queryParams, String txId, String type) {
378         String edgeResponse = champEdge.replace("edgeType", type);
379         OperationResult operationResult = new OperationResult();
380         List<String> edgeResponselist = new ArrayList<>();
381         edgeResponselist.add(edgeResponse);
382         operationResult.setResult(edgeResponselist.toString());
383         operationResult.setResultCode(200);
384         String baseUrl = BASE_OBJECT_URL + "/" + RELATIONSHIP_SUB_URL + "/" + id;
385         String url;
386         
387         if (txId != null) {
388             url = baseUrl + txId;
389         }
390         else {
391             url = appendQueryParams(baseUrl, queryParams).toString();
392         }
393
394         when(restClientMock.get(url, createHeaders(), MediaType.APPLICATION_JSON_TYPE))
395                 .thenReturn(operationResult);
396     }
397
398     public void mockGetVertices(Map<String, String> queryParams, String type) {
399         String vertexResponse = champVertex.replace("vertexType", type);
400         OperationResult operationResult = new OperationResult();
401         List<String> vertexResponselist = new ArrayList<>();
402         vertexResponselist.add(vertexResponse);
403         operationResult.setResult(vertexResponselist.toString());
404         operationResult.setResultCode(200);
405
406         StringBuilder url = appendQueryParams(BASE_OBJECT_URL + "/" + "filter", queryParams);
407
408         when(restClientMock.get(url.toString(), createHeaders(), MediaType.APPLICATION_JSON_TYPE))
409                 .thenReturn(operationResult);
410     }
411
412     public void mockGetEdges(String queryParams, String type) {
413         String edgeResponse = champEdge.replace("edgeType", type);
414         OperationResult operationResult = new OperationResult();
415         List<String> edgeResponselist = new ArrayList<>();
416         edgeResponselist.add(edgeResponse);
417         operationResult.setResult(edgeResponselist.toString());
418         operationResult.setResultCode(200);
419
420         String url = CHAMP_URL + RELATIONSHIP_SUB_URL + "/" + "filter" + queryParams;
421
422
423         when(restClientMock.get(url, createHeaders(), MediaType.APPLICATION_JSON_TYPE)).thenReturn(operationResult);
424     }
425
426     public void mockGetEdge(String id, String txId, String type) {
427         String edgeResponse = champEdge.replace("edgeType", type);
428         OperationResult operationResult = new OperationResult();
429         operationResult.setResult(edgeResponse);
430         operationResult.setResultCode(200);
431
432         String url = CHAMP_URL + RELATIONSHIP_SUB_URL + "/" + id + txId;
433
434         when(restClientMock.get(url, createHeaders(), MediaType.APPLICATION_JSON_TYPE)).thenReturn(operationResult);
435     }
436
437     public void mockGetEdge(String id, Map<String, String> queryParams, String type) {
438         String edgeResponse = champEdge.replace("edgeType", type);
439         OperationResult operationResult = new OperationResult();
440         operationResult.setResult(edgeResponse);
441         operationResult.setResultCode(200);
442
443         StringBuilder url = appendQueryParams(CHAMP_URL + RELATIONSHIP_SUB_URL + "/" + id, queryParams);
444
445         when(restClientMock.get(url.toString(), createHeaders(), MediaType.APPLICATION_JSON_TYPE))
446                 .thenReturn(operationResult);
447     }
448
449     public void mockPostEdge(String type, String txId, String payload) {
450         String edgeResponse = champEdge.replace("edgeType", type);
451         OperationResult operationResult = new OperationResult();
452         operationResult.setResult(edgeResponse);
453         operationResult.setResultCode(201);
454         MultivaluedMap<String, String> headers = new MultivaluedHashMap<String, String>();
455         headers.add("etag", "test123");
456         operationResult.setHeaders(headers);
457
458         String baseRelationshipUrl = CHAMP_URL + RELATIONSHIP_SUB_URL + txId;
459         String url = baseRelationshipUrl;
460
461
462         when(restClientMock.post(url, payload, createHeaders(), MediaType.APPLICATION_JSON_TYPE,
463                 MediaType.APPLICATION_JSON_TYPE)).thenReturn(operationResult);
464     }
465
466     public void mockPostVertex(String type, String payload, String txId) {
467         String vertexResponse = champVertex.replace("vertexType", type);
468         OperationResult operationResult = new OperationResult();
469         operationResult.setResult(vertexResponse);
470         operationResult.setResultCode(201);
471         MultivaluedMap<String, String> headers = new MultivaluedHashMap<String, String>();
472         headers.add("etag", "test123");
473         operationResult.setHeaders(headers);
474
475         String url = BASE_OBJECT_URL + txId;
476
477
478         when(restClientMock.post(url, payload, createHeaders(), MediaType.APPLICATION_JSON_TYPE,
479                 MediaType.APPLICATION_JSON_TYPE)).thenReturn(operationResult);
480     }
481
482     public void mockPutVertex(String id, String type, String payload, String txId) {
483         String vertexResponse = champVertex.replace("vertexType", type);
484         OperationResult operationResult = new OperationResult();
485         operationResult.setResult(vertexResponse);
486         operationResult.setResultCode(200);
487         MultivaluedMap<String, String> headers = new MultivaluedHashMap<String, String>();
488         headers.add("etag", "test123");
489         operationResult.setHeaders(headers);
490
491         String url = BASE_OBJECT_URL + "/" + id + txId;
492
493
494         when(restClientMock.put(url, payload, createHeaders(), MediaType.APPLICATION_JSON_TYPE,
495                 MediaType.APPLICATION_JSON_TYPE)).thenReturn(operationResult);
496     }
497
498     public void mockPutEdge(String id, String type, String txId, String payload) {
499         String edgeResponse = champEdge.replace("edgeType", type);
500         OperationResult operationResult = new OperationResult();
501         operationResult.setResult(edgeResponse);
502         operationResult.setResultCode(200);
503         MultivaluedMap<String, String> headers = new MultivaluedHashMap<String, String>();
504         headers.add("etag", "test123");
505         operationResult.setHeaders(headers);
506
507         String url = CHAMP_URL + RELATIONSHIP_SUB_URL + "/" + id + txId;
508
509
510         when(restClientMock.put(url, payload, createHeaders(), MediaType.APPLICATION_JSON_TYPE,
511                 MediaType.APPLICATION_JSON_TYPE)).thenReturn(operationResult);
512     }
513
514     public void mockDeleteVertex(String id, String type, String txId) {
515         String vertexResponse = champVertex.replace("vertexType", type);
516         OperationResult operationResult = new OperationResult();
517         operationResult.setResult(vertexResponse);
518         operationResult.setResultCode(200);
519
520         String url = BASE_OBJECT_URL + "/" + id + txId;
521
522
523         when(restClientMock.delete(url, createHeaders(), MediaType.APPLICATION_JSON_TYPE)).thenReturn(operationResult);
524     }
525
526     public void mockDeleteEdge(String id, String type, String txId) {
527         String edgeResponse = champEdge.replace("edgeType", type);
528         OperationResult operationResult = new OperationResult();
529         operationResult.setResult(edgeResponse);
530         operationResult.setResultCode(200);
531
532         String url = CHAMP_URL + RELATIONSHIP_SUB_URL + "/" + id + txId;
533
534
535         when(restClientMock.delete(url, createHeaders(), MediaType.APPLICATION_JSON_TYPE)).thenReturn(operationResult);
536     }
537
538     private Map<String, List<String>> createHeaders() {
539         Map<String, List<String>> headers = new HashMap<>();
540         List<String> listFromApp = new ArrayList<>();
541         List<String> listTransId = new ArrayList<>();
542         listFromApp.add(FROM_APP_NAME);
543         listTransId.add(HEADER_TRANS_ID_VALUE);
544         headers.put(HEADER_FROM_APP, listFromApp);
545         headers.put(HEADER_TRANS_ID, listTransId);
546
547         return headers;
548     }
549
550     private StringBuilder appendQueryParams(String url, Map<String, String> queryParams) {
551         StringBuilder strBuilder = new StringBuilder(url);
552
553         if (queryParams != null) {
554             String prefix = "?";
555             for (Map.Entry<String, String> entry : queryParams.entrySet()) {
556                 strBuilder.append(prefix);
557                 prefix = "&";
558                 strBuilder.append(entry.getKey() + "=" + entry.getValue());
559             }
560         }
561         return strBuilder;
562     }
563
564     public ChampDao getChampDao() {
565         return champDao;
566     }
567
568     public void setChampDao(ChampDao champDao) {
569         this.champDao = champDao;
570     }
571 }