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