Add unit test coverage
[aai/champ.git] / champ-service / src / test / java / org / onap / champ / entity / ChampBulkPayloadTest.java
1 /**
2  * ============LICENSE_START==========================================
3  * org.onap.aai
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
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.champ.entity;
22
23 import static org.junit.Assert.assertTrue;
24
25 import java.util.ArrayList;
26 import java.util.List;
27
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;
37
38 import static org.mockito.Matchers.anyString;
39 import static org.powermock.api.mockito.PowerMockito.*;
40
41
42 @RunWith(PowerMockRunner.class)
43 @SuppressStaticInitializationFor("org.onap.champ.util.ChampProperties")
44 @PrepareForTest({ChampProperties.class})
45 public class ChampBulkPayloadTest {
46
47     @Before
48     public void setUp() throws Exception {
49         mockStatic(ChampProperties.class);
50         when(ChampProperties.get(anyString())).thenReturn("");
51     }
52
53     @Test
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>();
59         
60         ChampBulkOp op = new ChampBulkOp();
61         op.setOperation(ChampBulkPayload.DELETE_OP);
62         op.setId("some-id");
63         op.setType("pserver");
64         op.setProperty("hostname", "some-host");
65         ChampObject champObj = op.toChampObject();
66         ops1.add(op);
67         
68         op = new ChampBulkOp();
69         op.setOperation(ChampBulkPayload.DELETE_OP);
70         op.setId("edge-id");
71         op.setType("org.onap.aai.BelongsTo");
72         op.setLabel("BelongsTo");
73         op.setSource("pserver");
74         op.setTarget("vserver");
75         ChampRelationship champRel = op.toChampRelationship();
76         ops2.add(op);
77         
78         op = new ChampBulkOp();
79         op.setOperation(ChampBulkPayload.UPDATE_OP);
80         op.setId("some-id2");
81         op.setType("pserver");
82         op.setProperty("hostname", "some-host2");
83         ops3.add(op);
84         
85         op = new ChampBulkOp();
86         op.setOperation(ChampBulkPayload.ADD_OP);
87         op.setId("edge-id2");
88         op.setLabel("HostedOn");
89         op.setSource("vm");
90         op.setTarget("hypervisor");
91         ops4.add(op);
92         
93         ChampBulkPayload payload = new ChampBulkPayload();
94         payload.setVertexDeleteOps(ops1);
95         payload.setEdgeDeleteOps(ops2);
96         payload.setVertexAddModifyOps(ops3);
97         payload.setEdgeAddModifyOps(ops4);
98         
99         String jsonPayload = payload.toJson();
100         ChampBulkPayload payload2 = ChampBulkPayload.fromJson(jsonPayload);
101         
102         assertTrue(payload2.getEdgeAddModifyOps().size() == 1);
103         assertTrue(payload2.getEdgeDeleteOps().size() == 1);
104         assertTrue(payload2.getVertexAddModifyOps().size() == 1);
105         assertTrue(payload2.getVertexDeleteOps().size() == 1);
106         
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));
112         
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));
117     }
118
119     @Test
120     public void testChampBulkResponse() throws Exception {
121         
122         ChampBulkOp op = new ChampBulkOp();
123         op.setOperation(ChampBulkPayload.DELETE_OP);
124         op.setId("some-id");
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);
131         
132         op = new ChampBulkOp();
133         op.setOperation(ChampBulkPayload.DELETE_OP);
134         op.setId("edge-id");
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);
143
144         ChampBulkResponse response = new ChampBulkResponse();
145         
146         List<ChampBulkVertexResponse> vRespList = new ArrayList<ChampBulkVertexResponse>();
147         vRespList.add(vResp2);
148         response.setObjects(vRespList);
149         
150         List<ChampBulkEdgeResponse> vEdgeList = new ArrayList<ChampBulkEdgeResponse>();
151         vEdgeList.add(eResp2);
152         response.setRelationships(vEdgeList);
153         
154         jsonContent = response.toJson();
155         ChampBulkResponse response2 = ChampBulkResponse.fromJson(jsonContent);
156         
157         assertTrue(response2.getObjects().size() == 1);
158         assertTrue(response2.getRelationships().size() == 1);
159         
160         assertTrue(response2.getObjects().get(0).getLabel().equals("my-vertex"));
161         assertTrue(response2.getObjects().get(0).getVertex() != null);
162         
163         assertTrue(response2.getRelationships().get(0).getLabel().equals("my-edge"));
164         assertTrue(response2.getRelationships().get(0).getEdge() != null);
165     }
166 }