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.is;
24 import static org.junit.Assert.assertThat;
25 import java.util.ArrayList;
26 import java.util.List;
29 import javax.ws.rs.core.Response.Status;
30 import org.junit.Test;
31 import org.onap.crud.exception.CrudException;
33 import com.google.gson.JsonArray;
34 import com.google.gson.JsonElement;
35 import com.google.gson.JsonObject;
38 public class BulkPayloadTest {
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();
49 prop.addProperty("p1", "value1");
50 prop.addProperty("p2", "value2");
57 root.add("objects", vertices);
59 String s = "{\"objects\":[{\"v1\":{\"p1\":\"value1\",\"p2\":\"value2\"}},{\"v2\":{\"p1\":\"value1\",\"p2\":\"value2\"}}]}";
61 p = BulkPayload.fromJson(s);
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();
68 for (Map.Entry<String, JsonElement> entry : entries) {
69 ids.add(entry.getKey());
73 System.out.println("root: " + root.toString());
74 System.out.println("payload ids: " + ids.toString());
78 public void testExceptionHandling() {
79 String payload = null;
81 BulkPayload.fromJson(payload);
82 } catch (CrudException e) {
83 assertThat(e.getHttpStatus(), is(Status.BAD_REQUEST));
84 assertThat(e.getMessage(), is("Invalid Json Payload"));
87 payload = "Invalid Json";
89 BulkPayload.fromJson(payload);
90 } catch (CrudException e) {
91 assertThat(e.getHttpStatus(), is(Status.BAD_REQUEST));
92 assertThat(e.getMessage(), is("Invalid Json Payload"));