2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2019 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.champ.entity;
23 import static org.junit.Assert.assertTrue;
25 import java.util.ArrayList;
26 import java.util.List;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.onap.aai.champcore.model.ChampObject;
32 import org.onap.aai.champcore.model.ChampRelationship;
33 import org.onap.champ.util.ChampProperties;
34 import org.powermock.core.classloader.annotations.PrepareForTest;
35 import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
36 import org.powermock.modules.junit4.PowerMockRunner;
38 import static org.mockito.Matchers.anyString;
39 import static org.powermock.api.mockito.PowerMockito.*;
42 @RunWith(PowerMockRunner.class)
43 @SuppressStaticInitializationFor("org.onap.champ.util.ChampProperties")
44 @PrepareForTest({ChampProperties.class})
45 public class ChampBulkPayloadTest {
48 public void setUp() throws Exception {
49 mockStatic(ChampProperties.class);
50 when(ChampProperties.get(anyString())).thenReturn("");
54 public void testChampBulkPayload() throws Exception {
55 List<ChampBulkOp> ops1 = new ArrayList<ChampBulkOp>();
56 List<ChampBulkOp> ops2 = new ArrayList<ChampBulkOp>();
57 List<ChampBulkOp> ops3 = new ArrayList<ChampBulkOp>();
58 List<ChampBulkOp> ops4 = new ArrayList<ChampBulkOp>();
60 ChampBulkOp op = new ChampBulkOp();
61 op.setOperation(ChampBulkPayload.DELETE_OP);
63 op.setType("pserver");
64 op.setProperty("hostname", "some-host");
65 ChampObject champObj = op.toChampObject();
68 op = new ChampBulkOp();
69 op.setOperation(ChampBulkPayload.DELETE_OP);
71 op.setType("org.onap.aai.BelongsTo");
72 op.setLabel("BelongsTo");
73 op.setSource("pserver");
74 op.setTarget("vserver");
75 ChampRelationship champRel = op.toChampRelationship();
78 op = new ChampBulkOp();
79 op.setOperation(ChampBulkPayload.UPDATE_OP);
81 op.setType("pserver");
82 op.setProperty("hostname", "some-host2");
85 op = new ChampBulkOp();
86 op.setOperation(ChampBulkPayload.ADD_OP);
88 op.setLabel("HostedOn");
90 op.setTarget("hypervisor");
93 ChampBulkPayload payload = new ChampBulkPayload();
94 payload.setVertexDeleteOps(ops1);
95 payload.setEdgeDeleteOps(ops2);
96 payload.setVertexAddModifyOps(ops3);
97 payload.setEdgeAddModifyOps(ops4);
99 String jsonPayload = payload.toJson();
100 ChampBulkPayload payload2 = ChampBulkPayload.fromJson(jsonPayload);
102 assertTrue(payload2.getEdgeAddModifyOps().size() == 1);
103 assertTrue(payload2.getEdgeDeleteOps().size() == 1);
104 assertTrue(payload2.getVertexAddModifyOps().size() == 1);
105 assertTrue(payload2.getVertexDeleteOps().size() == 1);
107 assertTrue(payload2.getEdgeAddModifyOps().get(0).getId().equals("edge-id2"));
108 assertTrue(payload2.getEdgeAddModifyOps().get(0).getLabel().equals("HostedOn"));
109 assertTrue(payload2.getEdgeAddModifyOps().get(0).getSource().equals("vm"));
110 assertTrue(payload2.getEdgeAddModifyOps().get(0).getTarget().equals("hypervisor"));
111 assertTrue(payload2.getEdgeAddModifyOps().get(0).getOperation().equals(ChampBulkPayload.ADD_OP));
113 assertTrue(payload2.getVertexAddModifyOps().get(0).getId().equals("some-id2"));
114 assertTrue(payload2.getVertexAddModifyOps().get(0).getType().equals("pserver"));
115 assertTrue(payload2.getVertexAddModifyOps().get(0).getProperties().size() == 1);
116 assertTrue(payload2.getVertexAddModifyOps().get(0).getOperation().equals(ChampBulkPayload.UPDATE_OP));
120 public void testChampBulkResponse() throws Exception {
122 ChampBulkOp op = new ChampBulkOp();
123 op.setOperation(ChampBulkPayload.DELETE_OP);
125 op.setType("pserver");
126 op.setProperty("hostname", "some-host");
127 ChampObject champObj = op.toChampObject();
128 ChampBulkVertexResponse vResp = new ChampBulkVertexResponse("my-vertex", champObj);
129 String jsonContent = vResp.toJson();
130 ChampBulkVertexResponse vResp2 = ChampBulkVertexResponse.fromJson(jsonContent);
132 op = new ChampBulkOp();
133 op.setOperation(ChampBulkPayload.DELETE_OP);
135 op.setType("org.onap.aai.BelongsTo");
136 op.setLabel("BelongsTo");
137 op.setSource("pserver");
138 op.setTarget("vserver");
139 ChampRelationship champRel = op.toChampRelationship();
140 ChampBulkEdgeResponse eResp = new ChampBulkEdgeResponse("my-edge", champRel);
141 jsonContent = eResp.toJson();
142 ChampBulkEdgeResponse eResp2 = ChampBulkEdgeResponse.fromJson(jsonContent);
144 ChampBulkResponse response = new ChampBulkResponse();
146 List<ChampBulkVertexResponse> vRespList = new ArrayList<ChampBulkVertexResponse>();
147 vRespList.add(vResp2);
148 response.setObjects(vRespList);
150 List<ChampBulkEdgeResponse> vEdgeList = new ArrayList<ChampBulkEdgeResponse>();
151 vEdgeList.add(eResp2);
152 response.setRelationships(vEdgeList);
154 jsonContent = response.toJson();
155 ChampBulkResponse response2 = ChampBulkResponse.fromJson(jsonContent);
157 assertTrue(response2.getObjects().size() == 1);
158 assertTrue(response2.getRelationships().size() == 1);
160 assertTrue(response2.getObjects().get(0).getLabel().equals("my-vertex"));
161 assertTrue(response2.getObjects().get(0).getVertex() != null);
163 assertTrue(response2.getRelationships().get(0).getLabel().equals("my-edge"));
164 assertTrue(response2.getRelationships().get(0).getEdge() != null);