Remove Multiplicity feature
[aai/gizmo.git] / src / test / java / org / onap / crud / service / BulkPayloadTest.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.is;
24 import static org.junit.Assert.assertThat;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Set;
29 import javax.ws.rs.core.Response.Status;
30 import org.junit.Test;
31 import org.onap.crud.exception.CrudException;
32
33 import com.google.gson.JsonArray;
34 import com.google.gson.JsonElement;
35 import com.google.gson.JsonObject;
36
37
38 public class BulkPayloadTest {
39
40   @Test
41   public void testBulkPayload() throws Exception {
42     BulkPayload p = new BulkPayload();
43     JsonObject root = new JsonObject();
44     JsonArray vertices = new JsonArray();
45     JsonObject v1 = new JsonObject();
46     JsonObject v2 = new JsonObject();
47     JsonObject prop = new JsonObject();
48
49     prop.addProperty("p1", "value1");
50     prop.addProperty("p2", "value2");
51     v1.add("v1", prop);
52     v2.add("v2", prop);
53
54     vertices.add(v1);
55     vertices.add(v2);
56
57     root.add("objects", vertices);
58
59     String s = "{\"objects\":[{\"v1\":{\"p1\":\"value1\",\"p2\":\"value2\"}},{\"v2\":{\"p1\":\"value1\",\"p2\":\"value2\"}}]}";
60
61     p = BulkPayload.fromJson(s);
62
63     List<JsonElement> po = p.getObjects();
64     List<String> ids = new ArrayList<String>();
65     for (JsonElement e : po) {
66       Set<Map.Entry<String, JsonElement>> entries = e.getAsJsonObject().entrySet();
67
68       for (Map.Entry<String, JsonElement> entry : entries) {
69         ids.add(entry.getKey());
70       }
71     }
72
73     System.out.println("root: " + root.toString());
74     System.out.println("payload ids: " + ids.toString());
75   }
76   
77     @Test
78     public void testExceptionHandling() {
79         String payload = null;
80         try {
81             BulkPayload.fromJson(payload);
82         } catch (CrudException e) {
83             assertThat(e.getHttpStatus(), is(Status.BAD_REQUEST));
84             assertThat(e.getMessage(), is("Invalid Json Payload"));
85         }
86
87         payload = "Invalid Json";
88         try {
89             BulkPayload.fromJson(payload);
90         } catch (CrudException e) {
91             assertThat(e.getHttpStatus(), is(Status.BAD_REQUEST));
92             assertThat(e.getMessage(), is("Invalid Json Payload"));
93         }
94     }
95 }