2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.crud.service;
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;
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;
47 public class ChampDaoExceptionsTest {
49 private final String champVertex = "{" +
50 "\"key\": \"test-uuid\"," +
51 "\"type\": \"vertexType\"," +
53 "\"fqdn\": \"myhost.onap.com\"," +
54 "\"hostname\": \"myhost\" } }";
56 private final String champEdge = "{" +
57 "\"key\": \"test-uuid\"," +
58 "\"type\": \"edgeType\"," +
60 "\"prevent-delete\": \"NONE\" }," +
62 "\"key\": \"50bdab41-ad1c-4d00-952c-a0aa5d827811\", \"type\": \"vserver\"}," +
64 "\"key\": \"1d326bc7-b985-492b-9604-0d5d1f06f908\", \"type\": \"pserver\"}" +
67 private final String vertexPayload = "{" +
68 "\"type\":\"pserver\"," +
70 "\"aai-node-type\":\"pserver\"}}";
73 private RestClient restClientMock;
74 private ChampDao champDao;
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";
87 restClientMock = mock(RestClient.class);
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";
98 Map<String, String> queryParams = new HashMap<>();
99 queryParams.put("hostname", "myhost");
100 mockGetVertex(idNotExists, "", "", type, 404, failureCauseForGetVertex);
104 champDao.getVertex(idNotExists, version);
105 } catch (CrudException e) {
106 assertEquals(404, e.getHttpStatus().getStatusCode());
107 assertThat(e.getMessage(), containsString(failureCauseForGetVertex));
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";
120 Map<String, String> queryParams = new HashMap<>();
121 queryParams.put("hostname", "myhost");
122 mockGetVertex(idNotExists, queryParamsForMock, "", type, 404, failureCauseForGetVertex);
126 champDao.getVertex(idNotExists, type, version, queryParams);
127 } catch (CrudException e) {
128 assertEquals(404, e.getHttpStatus().getStatusCode());
129 assertThat(e.getMessage(), containsString(failureCauseForGetVertex));
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";
141 Map<String, String> queryParams = new HashMap<>();
142 queryParams.put("hostname", "myhost");
143 mockGetVertex(id, queryParamsForMock, "", type, 200, "");
147 champDao.getVertex(id, "vserver", version, queryParams);
148 } catch (CrudException e) {
149 assertEquals(404, e.getHttpStatus().getStatusCode());
150 assertThat(e.getMessage(), containsString(failureCauseForGetVertexTypeNotMatches));
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";
163 Map<String, String> queryParams = new HashMap<>();
164 queryParams.put("hostname", "myhost");
165 mockGetVertex(idNotExists, "", txId, type, 404, failureCauseForGetVertex);
169 champDao.getVertex(idNotExists, type, version, txId);
170 } catch (CrudException e) {
171 assertEquals(404, e.getHttpStatus().getStatusCode());
172 assertThat(e.getMessage(), containsString(failureCauseForGetVertex));
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";
184 Map<String, String> queryParams = new HashMap<>();
185 queryParams.put("hostname", "myhost");
186 mockGetVertex(id, "", txId, type, 200, "");
190 champDao.getVertex(id, "vserver", version, txId);
191 } catch (CrudException e) {
192 assertEquals(404, e.getHttpStatus().getStatusCode());
193 assertThat(e.getMessage(), containsString(failureCauseForGetVertexTypeNotMatches));
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";
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);
211 champDao.getVertices(type, filter, version);
212 } catch (CrudException e) {
213 assertEquals(404, e.getHttpStatus().getStatusCode());
214 assertThat(e.getMessage(), containsString(failureCauseForGetVertices));
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";
226 Map<String, String> queryParams = new HashMap<>();
227 queryParams.put("hostname", "myhost");
228 mockGetEdge(idNotExists, "", txId, type, 404, failureCauseForGetEdge);
232 champDao.getEdge(idNotExists, txId);
233 } catch (CrudException e) {
234 assertEquals(404, e.getHttpStatus().getStatusCode());
235 assertThat(e.getMessage(), containsString(failureCauseForGetEdge));
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";
246 Map<String, String> queryParams = new HashMap<>();
247 queryParams.put("hostname", "myhost");
248 mockGetEdge(id, "", txId, type, 200, "");
253 champDao.getEdge(id, txId);
254 } catch (CrudException e) {
255 assertEquals(404, e.getHttpStatus().getStatusCode());
256 assertThat(e.getMessage(), containsString(failureCauseForGetEdgeTypeNotMatches));
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";
268 Map<String, String> queryParams = new HashMap<>();
269 queryParams.put("hostname", "myhost");
270 mockGetEdge(idNotExists, queryParamsForMock, "", type, 404, failureCauseForGetEdge);
274 champDao.getEdge(idNotExists, type, queryParams);
275 } catch (CrudException e) {
276 assertEquals(404, e.getHttpStatus().getStatusCode());
277 assertThat(e.getMessage(), containsString(failureCauseForGetEdge));
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";
288 Map<String, String> queryParams = new HashMap<>();
289 queryParams.put("hostname", "myhost");
290 mockGetEdge(id, queryParamsForMock, "", type, 200, "");
295 champDao.getEdge(id, "", queryParams);
296 } catch (CrudException e) {
297 assertEquals(404, e.getHttpStatus().getStatusCode());
298 assertThat(e.getMessage(), containsString(failureCauseForGetEdgeTypeNotMatches));
303 public void testGetEdges() {
304 String type = "tosca.relationships.HostedOn";
305 String failureCauseForGetEdges = "No edges found in graph for given filters";
307 Map<String, Object> filter = new HashMap<>();
308 Map<String, String> queryParams = new HashMap<>();
309 queryParams.put("hostname", "myhost");
310 mockGetEdges("?", type, 404, failureCauseForGetEdges);
314 champDao.getEdges(type, filter);
315 } catch (CrudException e) {
316 assertEquals(404, e.getHttpStatus().getStatusCode());
317 assertThat(e.getMessage(), containsString(failureCauseForGetEdges));
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";
329 Map<String, String> queryParams = new HashMap<>();
330 queryParams.put("hostname", "myhost");
331 mockGetVertexEdges(idNotExists, queryParamsForMock, type, 404, failureCauseForGetVertexEdges);
335 champDao.getVertexEdges(idNotExists, queryParams, null);
336 } catch (CrudException e) {
337 assertEquals(404, e.getHttpStatus().getStatusCode());
338 assertThat(e.getMessage(), containsString(failureCauseForGetVertexEdges));
343 public void addVertexTest() {
344 String type = "pserver";
345 String txId = "1234";
346 String version = "v11";
348 Map<String, Object> properties = new HashMap<>();
350 mockAddVertex(type, vertexPayload, "", 400);
351 mockAddVertex(type, vertexPayload, txId, 400);
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"));
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"));
370 public void addEdgeTest() throws CrudException {
371 String txId = "1234";
372 String vertexType = "pserver";
373 String edgeType = "tosca.relationships.HostedOn";
374 String version = "v11";
376 Map<String, Object> properties = new HashMap<>();
378 mockGetVertex("test-uuid", "", "", "pserver", 200, "");
379 mockGetVertex("test-uuid", "", txId, "pserver", 200, "");
380 mockAddEdge(edgeType, "", 400);
381 mockAddEdge(edgeType, txId, 400);
384 String vertex = champVertex.replace("vertexType", vertexType);
385 Vertex source = Vertex.fromJson(vertex, "v11");
386 Vertex target = Vertex.fromJson(vertex, "v11");
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"));
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"));
404 public void updateVertexTest() {
405 String id = "test-id";
406 String type = "pserver";
407 String txId = "1234";
408 String version = "v11";
410 Map<String, Object> properties = new HashMap<>();
412 mockPutVertex(id, type, "", 400);
413 mockPutVertex(id, type, txId, 400);
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"));
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"));
432 public void updateEdgeTest() {
433 String id = "test-uuid";
434 String txId = "1234";
435 String type = "tosca.relationships.HostedOn";
437 mockPutEdge(id, type, "", 400);
438 mockPutEdge(id, type, txId, 400);
441 String champJson = champEdge.replace("\"test-uuid\"", "null").replace("edgeType", type);
442 Edge edge = Edge.fromJson(champJson);
445 champDao.updateEdge(edge);
446 } catch (CrudException e) {
447 assertEquals(400, e.getHttpStatus().getStatusCode());
448 assertThat(e.getMessage(), containsString("Unable to identify edge"));
452 champDao.updateEdge(edge, txId);
453 } catch (CrudException e) {
454 assertEquals(400, e.getHttpStatus().getStatusCode());
455 assertThat(e.getMessage(), containsString("Unable to identify edge"));
458 champJson = champEdge.replace("edgeType", type);
459 edge = Edge.fromJson(champJson);
462 champDao.updateEdge(edge);
463 } catch (CrudException e) {
464 assertEquals(400, e.getHttpStatus().getStatusCode());
465 assertThat(e.getMessage(), containsString("Failed to update edge"));
469 champDao.updateEdge(edge, txId);
470 } catch (CrudException e) {
471 assertEquals(400, e.getHttpStatus().getStatusCode());
472 assertThat(e.getMessage(), containsString("Failed to update edge"));
477 public void deleteVertexTest() {
478 String id = "test-id";
479 String type = "pserver";
480 String txId = "1234";
482 mockDeleteVertex(id, type, "", 400);
483 mockDeleteVertex(id, type, txId, 400);
487 champDao.deleteVertex(id, type);
488 } catch (CrudException e) {
489 assertEquals(400, e.getHttpStatus().getStatusCode());
490 assertThat(e.getMessage(), containsString("Failed to delete vertex"));
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"));
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";
507 mockDeleteEdge(id, type, "", 400, failureCauseFordeleteEdge);
508 mockDeleteEdge(id, type, txId, 400, failureCauseFordeleteEdge);
512 champDao.deleteEdge(id);
513 } catch (CrudException e) {
514 assertEquals(400, e.getHttpStatus().getStatusCode());
515 assertThat(e.getMessage(), containsString(failureCauseFordeleteEdge));
518 champDao.deleteEdge(id, txId);
519 } catch (CrudException e) {
520 assertEquals(400, e.getHttpStatus().getStatusCode());
521 assertThat(e.getMessage(), containsString(failureCauseFordeleteEdge));
526 public void transactionsTest() {
527 String id = "test-id";
528 int resultCode = 500;
530 mockOpenTransaction(resultCode);
531 mockRollbackTransaction(id, resultCode);
532 mockCommitTransaction(id, resultCode);
535 String response = champDao.openTransaction();
536 assertEquals(null, response);
539 champDao.rollbackTransaction(id);
540 } catch (CrudException e) {
541 assertEquals(500, e.getHttpStatus().getStatusCode());
542 assertThat(e.getMessage(), containsString("Unable to rollback transaction"));
546 champDao.commitTransaction(id);
547 } catch (CrudException e) {
548 assertEquals(500, e.getHttpStatus().getStatusCode());
549 assertThat(e.getMessage(), containsString("Unable to commit transaction"));
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);
559 public void mockOpenTransaction(int resultCode) {
560 OperationResult operationResult = new OperationResult();
561 operationResult.setResult("");
562 operationResult.setResultCode(resultCode);
563 String url = CHAMP_URL + "transaction";
565 when(restClientMock.post(url, "", createHeader(), MediaType.TEXT_PLAIN_TYPE, MediaType.TEXT_PLAIN_TYPE))
566 .thenReturn(operationResult);
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;
575 when(restClientMock.put(url, "{\"method\": \"rollback\"}", createHeader(), MediaType.APPLICATION_JSON_TYPE,
576 MediaType.TEXT_PLAIN_TYPE)).thenReturn(operationResult);
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;
585 when(restClientMock.put(url, "{\"method\": \"commit\"}", createHeader(), MediaType.APPLICATION_JSON_TYPE,
586 MediaType.TEXT_PLAIN_TYPE)).thenReturn(operationResult);
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);
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;
603 url = BASE_OBJECT_URL + "/" + id;
606 when(restClientMock.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE)).thenReturn(operationResult);
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);
618 String url = BASE_OBJECT_URL + "/" + RELATIONSHIP_SUB_URL + "/" + id + queryParams;
620 when(restClientMock.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE)).thenReturn(operationResult);
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);
632 String url = BASE_OBJECT_URL + "/" + "filter" + queryParams;
634 when(restClientMock.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE)).thenReturn(operationResult);
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);
646 String url = CHAMP_URL + RELATIONSHIP_SUB_URL + "/" + "filter" + queryParams;
648 when(restClientMock.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE)).thenReturn(operationResult);
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);
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;
666 url = CHAMP_URL + RELATIONSHIP_SUB_URL + "/" + id;
669 when(restClientMock.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE)).thenReturn(operationResult);
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);
679 if (txId != null && !txId.isEmpty()) {
680 url = CHAMP_URL + RELATIONSHIP_SUB_URL + "?transactionId=" + txId;
682 url = CHAMP_URL + RELATIONSHIP_SUB_URL;
685 when(restClientMock.post(eq(url), anyString(), eq(createHeader()), eq(MediaType.APPLICATION_JSON_TYPE),
686 eq(MediaType.APPLICATION_JSON_TYPE))).thenReturn(operationResult);
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);
696 if (txId != null && !txId.isEmpty()) {
697 url = BASE_OBJECT_URL + "?transactionId=" + txId;
699 url = BASE_OBJECT_URL;
702 when(restClientMock.post(url, payload, createHeader(), MediaType.APPLICATION_JSON_TYPE,
703 MediaType.APPLICATION_JSON_TYPE)).thenReturn(operationResult);
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);
713 if (txId != null && !txId.isEmpty()) {
714 url = BASE_OBJECT_URL + "/" + id + "?transactionId=" + txId;
716 url = BASE_OBJECT_URL + "/" + id;
719 when(restClientMock.put(eq(url), anyString(), eq(createHeader()), eq(MediaType.APPLICATION_JSON_TYPE),
720 eq(MediaType.APPLICATION_JSON_TYPE))).thenReturn(operationResult);
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);
730 if (txId != null && !txId.isEmpty()) {
731 url = CHAMP_URL + RELATIONSHIP_SUB_URL + "/" + id + "?transactionId=" + txId;
733 url = CHAMP_URL + RELATIONSHIP_SUB_URL + "/" + id;
736 when(restClientMock.put(eq(url), anyString(), eq(createHeader()), eq(MediaType.APPLICATION_JSON_TYPE),
737 eq(MediaType.APPLICATION_JSON_TYPE))).thenReturn(operationResult);
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);
747 if (txId != null && !txId.isEmpty()) {
748 url = BASE_OBJECT_URL + "/" + id + "?transactionId=" + txId;
750 url = BASE_OBJECT_URL + "/" + id;
753 when(restClientMock.delete(url, createHeader(), MediaType.APPLICATION_JSON_TYPE)).thenReturn(operationResult);
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);
764 if (txId != null && !txId.isEmpty()) {
765 url = CHAMP_URL + RELATIONSHIP_SUB_URL + "/" + id + "?transactionId=" + txId;
767 url = CHAMP_URL + RELATIONSHIP_SUB_URL + "/" + id;
770 when(restClientMock.delete(url, createHeader(), MediaType.APPLICATION_JSON_TYPE)).thenReturn(operationResult);
773 public ChampDao getChampDao() {
777 public void setChampDao(ChampDao champDao) {
778 this.champDao = champDao;
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)));