Upgrade version of aai-common
[aai/gizmo.git] / src / test / java / org / onap / crud / service / ChampDaoExceptionsTest.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.hamcrest.CoreMatchers.containsString;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertThat;
26 import static org.mockito.Matchers.anyString;
27 import static org.mockito.Matchers.eq;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.when;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35 import javax.ws.rs.core.MediaType;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.onap.aai.cl.mdc.MdcContext;
39 import org.onap.aai.restclient.client.OperationResult;
40 import org.onap.aai.restclient.client.RestClient;
41 import org.onap.crud.dao.champ.ChampDao;
42 import org.onap.crud.entity.Edge;
43 import org.onap.crud.entity.Vertex;
44 import org.onap.crud.exception.CrudException;
45 import org.slf4j.MDC;
46 import org.junit.runner.RunWith;
47 import org.mockito.junit.MockitoJUnitRunner;
48 import org.onap.crud.OXMModelLoaderSetup;
49
50 @RunWith(MockitoJUnitRunner.Silent.class)
51 public class ChampDaoExceptionsTest extends OXMModelLoaderSetup {
52     // @formatter:off
53                 private final String champVertex = "{" +
54                     "\"key\": \"test-uuid\"," +
55                     "\"type\": \"vertexType\"," +
56                     "\"properties\": {" +
57                     "\"fqdn\": \"myhost.onap.com\"," +
58                     "\"hostname\": \"myhost\" } }";
59                 
60                 private final String champEdge = "{" +
61                             "\"key\": \"test-uuid\"," +
62                             "\"type\": \"edgeType\"," +
63                             "\"properties\": {" +
64                             "\"prevent-delete\": \"NONE\" }," +
65                             "\"source\": {" +
66                             "\"key\": \"50bdab41-ad1c-4d00-952c-a0aa5d827811\", \"type\": \"vserver\"}," +
67                             "\"target\": {" +
68                             "\"key\": \"1d326bc7-b985-492b-9604-0d5d1f06f908\", \"type\": \"pserver\"}" +
69                             " }";
70                 
71                 private final String vertexPayload = "{" + 
72                         "\"type\":\"pserver\"," + 
73                         "\"properties\":{" + 
74                         "\"aai-node-type\":\"pserver\"}}";
75                 // @formatter:on
76
77     private RestClient restClientMock;
78     private ChampDao champDao;
79
80     static final String CHAMP_URL = "https://host:9522/services/champ-service/v1/";
81     static final String OBJECT_SUB_URL = "objects";
82     static final String RELATIONSHIP_SUB_URL = "relationships";
83     static final String TRANSACTION_SUB_URL = "transaction";
84     static final String BASE_OBJECT_URL = CHAMP_URL + OBJECT_SUB_URL;
85     static final String HEADER_FROM_APP = "X-FromAppId";
86     static final String HEADER_TRANS_ID = "X-TransactionId";
87     static final String FROM_APP_NAME = "Gizmo";
88
89     @Before
90     public void setup() {
91         restClientMock = mock(RestClient.class);
92     }
93
94     @Test
95     public void testGetVertexIdNotExists() {
96         String id = "test-id";
97         String idNotExists = "test-id-not-exists";
98         String type = "pserver";
99         String version = "v11";
100         String failureCauseForGetVertex = "No vertex with id " + id + " found in graph";
101
102         Map<String, String> queryParams = new HashMap<>();
103         queryParams.put("hostname", "myhost");
104         mockGetVertex(idNotExists, "", "", type, 404, failureCauseForGetVertex);
105         buildChampDao();
106
107         try {
108             champDao.getVertex(idNotExists, version);
109         } catch (CrudException e) {
110             assertEquals(404, e.getHttpStatus().getStatusCode());
111             assertThat(e.getMessage(), containsString(failureCauseForGetVertex));
112         }
113     }
114
115     @Test
116     public void testGetVertexIdNotExistsWithQueryParams() {
117         String id = "test-id";
118         String idNotExists = "test-id-not-exists";
119         String queryParamsForMock = "?hostname=myhost";
120         String type = "pserver";
121         String version = "v11";
122         String failureCauseForGetVertex = "No vertex with id " + id + " found in graph";
123
124         Map<String, String> queryParams = new HashMap<>();
125         queryParams.put("hostname", "myhost");
126         mockGetVertex(idNotExists, queryParamsForMock, "", type, 404, failureCauseForGetVertex);
127         buildChampDao();
128
129         try {
130             champDao.getVertex(idNotExists, type, version, queryParams);
131         } catch (CrudException e) {
132             assertEquals(404, e.getHttpStatus().getStatusCode());
133             assertThat(e.getMessage(), containsString(failureCauseForGetVertex));
134         }
135     }
136
137     @Test
138     public void testGetVertexWithQueryParamsTypeNotMatch() {
139         String id = "test-id";
140         String queryParamsForMock = "?hostname=myhost";
141         String type = "pserver";
142         String version = "v11";
143         String failureCauseForGetVertexTypeNotMatches = "No vertex with id " + id + " and type vserver found in graph";
144
145         Map<String, String> queryParams = new HashMap<>();
146         queryParams.put("hostname", "myhost");
147         mockGetVertex(id, queryParamsForMock, "", type, 200, "");
148         buildChampDao();
149
150         try {
151             champDao.getVertex(id, "vserver", version, queryParams);
152         } catch (CrudException e) {
153             assertEquals(404, e.getHttpStatus().getStatusCode());
154             assertThat(e.getMessage(), containsString(failureCauseForGetVertexTypeNotMatches));
155         }
156     }
157
158     @Test
159     public void testGetVertexIdNotExistsWithTxId() {
160         String id = "test-id";
161         String idNotExists = "test-id-not-exists";
162         String txId = "1234";
163         String type = "pserver";
164         String version = "v11";
165         String failureCauseForGetVertex = "No vertex with id " + id + " found in graph";
166
167         Map<String, String> queryParams = new HashMap<>();
168         queryParams.put("hostname", "myhost");
169         mockGetVertex(idNotExists, "", txId, type, 404, failureCauseForGetVertex);
170         buildChampDao();
171
172         try {
173             champDao.getVertex(idNotExists, type, version, txId);
174         } catch (CrudException e) {
175             assertEquals(404, e.getHttpStatus().getStatusCode());
176             assertThat(e.getMessage(), containsString(failureCauseForGetVertex));
177         }
178     }
179
180     @Test
181     public void testGetVertexWithTxIdAndTypeNotMatch() {
182         String id = "test-id";
183         String txId = "1234";
184         String type = "pserver";
185         String version = "v11";
186         String failureCauseForGetVertexTypeNotMatches = "No vertex with id " + id + " and type vserver found in graph";
187
188         Map<String, String> queryParams = new HashMap<>();
189         queryParams.put("hostname", "myhost");
190         mockGetVertex(id, "", txId, type, 200, "");
191         buildChampDao();
192
193         try {
194             champDao.getVertex(id, "vserver", version, txId);
195         } catch (CrudException e) {
196             assertEquals(404, e.getHttpStatus().getStatusCode());
197             assertThat(e.getMessage(), containsString(failureCauseForGetVertexTypeNotMatches));
198         }
199     }
200
201     @Test
202     public void testGetVertices() {
203         String queryParamsForMockGetVertices = "?aai-node-type=pserver";
204         String type = "pserver";
205         String version = "v11";
206         String failureCauseForGetVertices = "No vertices found in graph for given filters";
207
208         Map<String, Object> filter = new HashMap<>();
209         Map<String, String> queryParams = new HashMap<>();
210         queryParams.put("hostname", "myhost");
211         mockGetVertices(queryParamsForMockGetVertices, type, 404, failureCauseForGetVertices);
212         buildChampDao();
213
214         try {
215             champDao.getVertices(type, filter, version);
216         } catch (CrudException e) {
217             assertEquals(404, e.getHttpStatus().getStatusCode());
218             assertThat(e.getMessage(), containsString(failureCauseForGetVertices));
219         }
220     }
221
222     @Test
223     public void testGetEdgeIdNotExists() {
224         String idNotExists = "test-id-not-exists";
225         String id = "test-id";
226         String txId = "1234";
227         String type = "tosca.relationships.HostedOn";
228         String failureCauseForGetEdge = "No edge with id " + id + " found in graph";
229
230         Map<String, String> queryParams = new HashMap<>();
231         queryParams.put("hostname", "myhost");
232         mockGetEdge(idNotExists, "", txId, type, 404, failureCauseForGetEdge);
233         buildChampDao();
234
235         try {
236             champDao.getEdge(idNotExists, txId);
237         } catch (CrudException e) {
238             assertEquals(404, e.getHttpStatus().getStatusCode());
239             assertThat(e.getMessage(), containsString(failureCauseForGetEdge));
240         }
241     }
242
243     @Test
244     public void testGetEdgeTypeNotMatch() {
245         String id = "test-id";
246         String txId = "1234";
247         String type = "tosca.relationships.HostedOn";
248         String failureCauseForGetEdgeTypeNotMatches = "No edge with id " + id + " and type " + "" + " found in graph";
249
250         Map<String, String> queryParams = new HashMap<>();
251         queryParams.put("hostname", "myhost");
252         mockGetEdge(id, "", txId, type, 200, "");
253         buildChampDao();
254
255         // Type not matches
256         try {
257             champDao.getEdge(id, txId);
258         } catch (CrudException e) {
259             assertEquals(404, e.getHttpStatus().getStatusCode());
260             assertThat(e.getMessage(), containsString(failureCauseForGetEdgeTypeNotMatches));
261         }
262     }
263
264     @Test
265     public void testGetEdgeIdNotExistsWithQueryParams() {
266         String idNotExists = "test-id-not-exists";
267         String id = "test-id";
268         String queryParamsForMock = "?hostname=myhost";
269         String type = "tosca.relationships.HostedOn";
270         String failureCauseForGetEdge = "No edge with id " + id + " found in graph";
271
272         Map<String, String> queryParams = new HashMap<>();
273         queryParams.put("hostname", "myhost");
274         mockGetEdge(idNotExists, queryParamsForMock, "", type, 404, failureCauseForGetEdge);
275         buildChampDao();
276
277         try {
278             champDao.getEdge(idNotExists, type, queryParams);
279         } catch (CrudException e) {
280             assertEquals(404, e.getHttpStatus().getStatusCode());
281             assertThat(e.getMessage(), containsString(failureCauseForGetEdge));
282         }
283     }
284
285     @Test
286     public void testGetEdgeTypeNotMatchWithQueryParams() {
287         String id = "test-id";
288         String queryParamsForMock = "?hostname=myhost";
289         String type = "tosca.relationships.HostedOn";
290         String failureCauseForGetEdgeTypeNotMatches = "No edge with id " + id + " and type " + "" + " found in graph";
291
292         Map<String, String> queryParams = new HashMap<>();
293         queryParams.put("hostname", "myhost");
294         mockGetEdge(id, queryParamsForMock, "", type, 200, "");
295         buildChampDao();
296
297         // Type not matches
298         try {
299             champDao.getEdge(id, "", queryParams);
300         } catch (CrudException e) {
301             assertEquals(404, e.getHttpStatus().getStatusCode());
302             assertThat(e.getMessage(), containsString(failureCauseForGetEdgeTypeNotMatches));
303         }
304     }
305
306     @Test
307     public void testGetEdges() {
308         String type = "tosca.relationships.HostedOn";
309         String failureCauseForGetEdges = "No edges found in graph for given filters";
310
311         Map<String, Object> filter = new HashMap<>();
312         Map<String, String> queryParams = new HashMap<>();
313         queryParams.put("hostname", "myhost");
314         mockGetEdges("?", type, 404, failureCauseForGetEdges);
315         buildChampDao();
316
317         try {
318             champDao.getEdges(type, filter);
319         } catch (CrudException e) {
320             assertEquals(404, e.getHttpStatus().getStatusCode());
321             assertThat(e.getMessage(), containsString(failureCauseForGetEdges));
322         }
323     }
324
325     @Test
326     public void testGetVertexEdges() {
327         String idNotExists = "test-id-not-exists";
328         String id = "test-id";
329         String queryParamsForMock = "?hostname=myhost";
330         String type = "tosca.relationships.HostedOn";
331         String failureCauseForGetVertexEdges = "No vertex with id " + id + " found in graph";
332
333         Map<String, String> queryParams = new HashMap<>();
334         queryParams.put("hostname", "myhost");
335         mockGetVertexEdges(idNotExists, queryParamsForMock, type, 404, failureCauseForGetVertexEdges);
336         buildChampDao();
337
338         try {
339             champDao.getVertexEdges(idNotExists, queryParams, null);
340         } catch (CrudException e) {
341             assertEquals(404, e.getHttpStatus().getStatusCode());
342             assertThat(e.getMessage(), containsString(failureCauseForGetVertexEdges));
343         }
344     }
345
346     @Test
347     public void addVertexTest() {
348         String type = "pserver";
349         String txId = "1234";
350         String version = "v11";
351
352         Map<String, Object> properties = new HashMap<>();
353
354         mockAddVertex(type, vertexPayload, "", 400);
355         mockAddVertex(type, vertexPayload, txId, 400);
356         buildChampDao();
357
358         try {
359             champDao.addVertex(type, properties, version);
360         } catch (CrudException e) {
361             assertEquals(400, e.getHttpStatus().getStatusCode());
362             assertThat(e.getMessage(), containsString("Failed to create vertex"));
363         }
364
365         try {
366             champDao.addVertex(type, properties, version, txId);
367         } catch (CrudException e) {
368             assertEquals(400, e.getHttpStatus().getStatusCode());
369             assertThat(e.getMessage(), containsString("Failed to create vertex"));
370         }
371     }
372
373     @Test
374     public void addEdgeTest() throws CrudException {
375         String txId = "1234";
376         String vertexType = "pserver";
377         String edgeType = "tosca.relationships.HostedOn";
378         String version = "v11";
379
380         Map<String, Object> properties = new HashMap<>();
381
382         mockGetVertex("test-uuid", "", "", "pserver", 200, "");
383         mockGetVertex("test-uuid", "", txId, "pserver", 200, "");
384         mockAddEdge(edgeType, "", 400);
385         mockAddEdge(edgeType, txId, 400);
386         buildChampDao();
387
388         String vertex = champVertex.replace("vertexType", vertexType);
389         Vertex source = Vertex.fromJson(vertex, "v11");
390         Vertex target = Vertex.fromJson(vertex, "v11");
391
392         try {
393             champDao.addEdge(edgeType, source, target, properties, version);
394         } catch (CrudException e) {
395             assertEquals(400, e.getHttpStatus().getStatusCode());
396             assertThat(e.getMessage(), containsString("Failed to create edge"));
397         }
398
399         try {
400             champDao.addEdge(edgeType, source, target, properties, version, txId);
401         } catch (CrudException e) {
402             assertEquals(400, e.getHttpStatus().getStatusCode());
403             assertThat(e.getMessage(), containsString("Failed to create edge"));
404         }
405     }
406
407     @Test
408     public void updateVertexTest() {
409         String id = "test-id";
410         String type = "pserver";
411         String txId = "1234";
412         String version = "v11";
413
414         Map<String, Object> properties = new HashMap<>();
415
416         mockPutVertex(id, type, "", 400);
417         mockPutVertex(id, type, txId, 400);
418         buildChampDao();
419
420         try {
421             champDao.updateVertex(id, type, properties, version);
422         } catch (CrudException e) {
423             assertEquals(400, e.getHttpStatus().getStatusCode());
424             assertThat(e.getMessage(), containsString("Failed to update vertex"));
425         }
426
427         try {
428             champDao.updateVertex(id, type, properties, version, txId);
429         } catch (CrudException e) {
430             assertEquals(400, e.getHttpStatus().getStatusCode());
431             assertThat(e.getMessage(), containsString("Failed to update vertex"));
432         }
433     }
434
435     @Test
436     public void updateEdgeTest() {
437         String id = "test-uuid";
438         String txId = "1234";
439         String type = "tosca.relationships.HostedOn";
440
441         mockPutEdge(id, type, "", 400);
442         mockPutEdge(id, type, txId, 400);
443         buildChampDao();
444
445         String champJson = champEdge.replace("\"test-uuid\"", "null").replace("edgeType", type);
446         Edge edge = Edge.fromJson(champJson);
447
448         try {
449             champDao.updateEdge(edge);
450         } catch (CrudException e) {
451             assertEquals(400, e.getHttpStatus().getStatusCode());
452             assertThat(e.getMessage(), containsString("Unable to identify edge"));
453         }
454
455         try {
456             champDao.updateEdge(edge, txId);
457         } catch (CrudException e) {
458             assertEquals(400, e.getHttpStatus().getStatusCode());
459             assertThat(e.getMessage(), containsString("Unable to identify edge"));
460         }
461
462         champJson = champEdge.replace("edgeType", type);
463         edge = Edge.fromJson(champJson);
464
465         try {
466             champDao.updateEdge(edge);
467         } catch (CrudException e) {
468             assertEquals(400, e.getHttpStatus().getStatusCode());
469             assertThat(e.getMessage(), containsString("Failed to update edge"));
470         }
471
472         try {
473             champDao.updateEdge(edge, txId);
474         } catch (CrudException e) {
475             assertEquals(400, e.getHttpStatus().getStatusCode());
476             assertThat(e.getMessage(), containsString("Failed to update edge"));
477         }
478     }
479
480     @Test
481     public void deleteVertexTest() {
482         String id = "test-id";
483         String type = "pserver";
484         String txId = "1234";
485
486         mockDeleteVertex(id, type, "", 400);
487         mockDeleteVertex(id, type, txId, 400);
488         buildChampDao();
489
490         try {
491             champDao.deleteVertex(id, type);
492         } catch (CrudException e) {
493             assertEquals(400, e.getHttpStatus().getStatusCode());
494             assertThat(e.getMessage(), containsString("Failed to delete vertex"));
495         }
496         try {
497             champDao.deleteVertex(id, type, txId);
498         } catch (CrudException e) {
499             assertEquals(400, e.getHttpStatus().getStatusCode());
500             assertThat(e.getMessage(), containsString("Failed to delete vertex"));
501         }
502     }
503
504     @Test
505     public void deleteEdgeTest() {
506         String id = "test-uuid";
507         String txId = "1234";
508         String type = "tosca.relationships.HostedOn";
509         String failureCauseFordeleteEdge = "No edge with id " + id + " found in graph";
510
511         mockDeleteEdge(id, type, "", 400, failureCauseFordeleteEdge);
512         mockDeleteEdge(id, type, txId, 400, failureCauseFordeleteEdge);
513         buildChampDao();
514
515         try {
516             champDao.deleteEdge(id);
517         } catch (CrudException e) {
518             assertEquals(400, e.getHttpStatus().getStatusCode());
519             assertThat(e.getMessage(), containsString(failureCauseFordeleteEdge));
520         }
521         try {
522             champDao.deleteEdge(id, txId);
523         } catch (CrudException e) {
524             assertEquals(400, e.getHttpStatus().getStatusCode());
525             assertThat(e.getMessage(), containsString(failureCauseFordeleteEdge));
526         }
527     }
528
529     @Test
530     public void transactionsTest() {
531         String id = "test-id";
532         int resultCode = 500;
533
534         mockOpenTransaction(resultCode);
535         mockRollbackTransaction(id, resultCode);
536         mockCommitTransaction(id, resultCode);
537         buildChampDao();
538
539         String response = champDao.openTransaction();
540         assertEquals(null, response);
541
542         try {
543             champDao.rollbackTransaction(id);
544         } catch (CrudException e) {
545             assertEquals(500, e.getHttpStatus().getStatusCode());
546             assertThat(e.getMessage(), containsString("Unable to rollback transaction"));
547         }
548
549         try {
550             champDao.commitTransaction(id);
551         } catch (CrudException e) {
552             assertEquals(500, e.getHttpStatus().getStatusCode());
553             assertThat(e.getMessage(), containsString("Unable to commit transaction"));
554         }
555     }
556
557     public void buildChampDao() {
558         String baseRelationshipUrl = CHAMP_URL + RELATIONSHIP_SUB_URL;
559         String baseTransactionUrl = CHAMP_URL + TRANSACTION_SUB_URL;
560         champDao = new ChampDao(restClientMock, BASE_OBJECT_URL, baseRelationshipUrl, baseTransactionUrl);
561     }
562
563     public void mockOpenTransaction(int resultCode) {
564         OperationResult operationResult = new OperationResult();
565         operationResult.setResult("");
566         operationResult.setResultCode(resultCode);
567         String url = CHAMP_URL + "transaction";
568
569         when(restClientMock.post(url, "", createHeader(), MediaType.TEXT_PLAIN_TYPE, MediaType.TEXT_PLAIN_TYPE))
570                 .thenReturn(operationResult);
571     }
572
573     public void mockRollbackTransaction(String id, int resultCode) {
574         OperationResult operationResult = new OperationResult();
575         operationResult.setResult("");
576         operationResult.setResultCode(resultCode);
577         String url = CHAMP_URL + TRANSACTION_SUB_URL + "/" + id;
578
579         when(restClientMock.put(url, "{\"method\": \"rollback\"}", createHeader(), MediaType.APPLICATION_JSON_TYPE,
580                 MediaType.TEXT_PLAIN_TYPE)).thenReturn(operationResult);
581     }
582
583     public void mockCommitTransaction(String id, int resultCode) {
584         OperationResult operationResult = new OperationResult();
585         operationResult.setResult("");
586         operationResult.setResultCode(resultCode);
587         String url = CHAMP_URL + TRANSACTION_SUB_URL + "/" + id;
588
589         when(restClientMock.put(url, "{\"method\": \"commit\"}", createHeader(), MediaType.APPLICATION_JSON_TYPE,
590                 MediaType.TEXT_PLAIN_TYPE)).thenReturn(operationResult);
591     }
592
593     public void mockGetVertex(String id, String queryParams, String txId, String type, int resultCode,
594             String failureCause) {
595         String vertexResponse = champVertex.replace("vertexType", type);
596         OperationResult operationResult = new OperationResult();
597         operationResult.setResult(vertexResponse);
598         operationResult.setResultCode(resultCode);
599         operationResult.setFailureCause(failureCause);
600         String url;
601
602         if (queryParams != null && !queryParams.isEmpty() && (txId.isEmpty() || txId == null)) {
603             url = BASE_OBJECT_URL + "/" + id + queryParams;
604         } else if (txId != null && !txId.isEmpty() && (queryParams.isEmpty() || queryParams == null)) {
605             url = BASE_OBJECT_URL + "/" + id + "?transactionId=" + txId;
606         } else {
607             url = BASE_OBJECT_URL + "/" + id;
608         }
609
610         when(restClientMock.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE)).thenReturn(operationResult);
611     }
612
613     public void mockGetVertexEdges(String id, String queryParams, String type, int resultCode, String failureCause) {
614         String edgeResponse = champEdge.replace("edgeType", type);
615         OperationResult operationResult = new OperationResult();
616         List<String> edgeResponselist = new ArrayList<>();
617         edgeResponselist.add(edgeResponse);
618         operationResult.setResult(edgeResponselist.toString());
619         operationResult.setResultCode(resultCode);
620         operationResult.setFailureCause(failureCause);
621
622         String url = BASE_OBJECT_URL + "/" + RELATIONSHIP_SUB_URL + "/" + id + queryParams;
623
624         when(restClientMock.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE)).thenReturn(operationResult);
625     }
626
627     public void mockGetVertices(String queryParams, String type, int resultCode, String failureCause) {
628         String vertexResponse = champVertex.replace("vertexType", type);
629         OperationResult operationResult = new OperationResult();
630         List<String> vertexResponselist = new ArrayList<>();
631         vertexResponselist.add(vertexResponse);
632         operationResult.setResult(vertexResponselist.toString());
633         operationResult.setResultCode(resultCode);
634         operationResult.setFailureCause(failureCause);
635
636         String url = BASE_OBJECT_URL + "/" + "filter" + queryParams;
637
638         when(restClientMock.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE)).thenReturn(operationResult);
639     }
640
641     public void mockGetEdges(String queryParams, String type, int resultCode, String failureCause) {
642         String edgeResponse = champEdge.replace("edgeType", type);
643         OperationResult operationResult = new OperationResult();
644         List<String> edgeResponselist = new ArrayList<>();
645         edgeResponselist.add(edgeResponse);
646         operationResult.setResult(edgeResponselist.toString());
647         operationResult.setResultCode(resultCode);
648         operationResult.setFailureCause(failureCause);
649
650         String url = CHAMP_URL + RELATIONSHIP_SUB_URL + "/" + "filter" + queryParams;
651
652         when(restClientMock.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE)).thenReturn(operationResult);
653     }
654
655     public void mockGetEdge(String id, String queryParams, String txId, String type, int resultCode,
656             String failureCause) {
657         String edgeResponse = champEdge.replace("edgeType", type);
658         OperationResult operationResult = new OperationResult();
659         operationResult.setResult(edgeResponse);
660         operationResult.setResultCode(resultCode);
661         operationResult.setFailureCause(failureCause);
662
663         String url;
664
665         if (queryParams != null && !queryParams.isEmpty() && (txId.isEmpty() || txId == null)) {
666             url = CHAMP_URL + RELATIONSHIP_SUB_URL + "/" + id + queryParams;
667         } else if (txId != null && !txId.isEmpty() && (queryParams.isEmpty() || queryParams == null)) {
668             url = CHAMP_URL + RELATIONSHIP_SUB_URL + "/" + id + "?transactionId=" + txId;
669         } else {
670             url = CHAMP_URL + RELATIONSHIP_SUB_URL + "/" + id;
671         }
672
673         when(restClientMock.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE)).thenReturn(operationResult);
674     }
675
676     public void mockAddEdge(String type, String txId, int resultCode) {
677         String edgeResponse = champEdge.replace("edgeType", type);
678         OperationResult operationResult = new OperationResult();
679         operationResult.setResult(edgeResponse);
680         operationResult.setResultCode(resultCode);
681
682         String url;
683         if (txId != null && !txId.isEmpty()) {
684             url = CHAMP_URL + RELATIONSHIP_SUB_URL + "?transactionId=" + txId;
685         } else {
686             url = CHAMP_URL + RELATIONSHIP_SUB_URL;
687         }
688
689         when(restClientMock.post(eq(url), anyString(), eq(createHeader()), eq(MediaType.APPLICATION_JSON_TYPE),
690                 eq(MediaType.APPLICATION_JSON_TYPE))).thenReturn(operationResult);
691     }
692
693     public void mockAddVertex(String type, String payload, String txId, int resultCode) {
694         String vertexResponse = champVertex.replace("vertexType", type);
695         OperationResult operationResult = new OperationResult();
696         operationResult.setResult(vertexResponse);
697         operationResult.setResultCode(resultCode);
698
699         String url;
700         if (txId != null && !txId.isEmpty()) {
701             url = BASE_OBJECT_URL + "?transactionId=" + txId;
702         } else {
703             url = BASE_OBJECT_URL;
704         }
705
706         when(restClientMock.post(url, payload, createHeader(), MediaType.APPLICATION_JSON_TYPE,
707                 MediaType.APPLICATION_JSON_TYPE)).thenReturn(operationResult);
708     }
709
710     public void mockPutVertex(String id, String type, String txId, int resultCode) {
711         String vertexResponse = champVertex.replace("vertexType", type);
712         OperationResult operationResult = new OperationResult();
713         operationResult.setResult(vertexResponse);
714         operationResult.setResultCode(resultCode);
715
716         String url;
717         if (txId != null && !txId.isEmpty()) {
718             url = BASE_OBJECT_URL + "/" + id + "?transactionId=" + txId;
719         } else {
720             url = BASE_OBJECT_URL + "/" + id;
721         }
722
723         when(restClientMock.put(eq(url), anyString(), eq(createHeader()), eq(MediaType.APPLICATION_JSON_TYPE),
724                 eq(MediaType.APPLICATION_JSON_TYPE))).thenReturn(operationResult);
725     }
726
727     public void mockPutEdge(String id, String type, String txId, int resultCode) {
728         String edgeResponse = champEdge.replace("edgeType", type);
729         OperationResult operationResult = new OperationResult();
730         operationResult.setResult(edgeResponse);
731         operationResult.setResultCode(resultCode);
732
733         String url;
734         if (txId != null && !txId.isEmpty()) {
735             url = CHAMP_URL + RELATIONSHIP_SUB_URL + "/" + id + "?transactionId=" + txId;
736         } else {
737             url = CHAMP_URL + RELATIONSHIP_SUB_URL + "/" + id;
738         }
739
740         when(restClientMock.put(eq(url), anyString(), eq(createHeader()), eq(MediaType.APPLICATION_JSON_TYPE),
741                 eq(MediaType.APPLICATION_JSON_TYPE))).thenReturn(operationResult);
742     }
743
744     public void mockDeleteVertex(String id, String type, String txId, int resultCode) {
745         String vertexResponse = champVertex.replace("vertexType", type);
746         OperationResult operationResult = new OperationResult();
747         operationResult.setResult(vertexResponse);
748         operationResult.setResultCode(resultCode);
749
750         String url;
751         if (txId != null && !txId.isEmpty()) {
752             url = BASE_OBJECT_URL + "/" + id + "?transactionId=" + txId;
753         } else {
754             url = BASE_OBJECT_URL + "/" + id;
755         }
756
757         when(restClientMock.delete(url, createHeader(), MediaType.APPLICATION_JSON_TYPE)).thenReturn(operationResult);
758     }
759
760     public void mockDeleteEdge(String id, String type, String txId, int resultCode, String failureCause) {
761         String edgeResponse = champEdge.replace("edgeType", type);
762         OperationResult operationResult = new OperationResult();
763         operationResult.setResult(edgeResponse);
764         operationResult.setResultCode(resultCode);
765         operationResult.setFailureCause(failureCause);
766
767         String url;
768         if (txId != null && !txId.isEmpty()) {
769             url = CHAMP_URL + RELATIONSHIP_SUB_URL + "/" + id + "?transactionId=" + txId;
770         } else {
771             url = CHAMP_URL + RELATIONSHIP_SUB_URL + "/" + id;
772         }
773
774         when(restClientMock.delete(url, createHeader(), MediaType.APPLICATION_JSON_TYPE)).thenReturn(operationResult);
775     }
776
777     public ChampDao getChampDao() {
778         return champDao;
779     }
780
781     public void setChampDao(ChampDao champDao) {
782         this.champDao = champDao;
783     }
784
785     private Map<String, List<String>> createHeader() {
786         Map<String, List<String>> headers = new HashMap<>();
787         headers.put(HEADER_FROM_APP, Arrays.asList(FROM_APP_NAME));
788         headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(MdcContext.MDC_REQUEST_ID)));
789         return headers;
790     }
791 }