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.event.response;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import org.junit.BeforeClass;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.ExpectedException;
28 import org.onap.crud.event.GraphEvent;
29 import org.onap.crud.event.GraphEvent.GraphEventOperation;
30 import org.onap.crud.event.envelope.GraphEventEnvelope;
31 import org.onap.crud.exception.CrudException;
32 import org.onap.crud.util.TestUtil;
33 import org.onap.schema.OxmModelLoader;
34 import com.google.gson.Gson;
35 import com.google.gson.JsonParser;
37 public class GraphEventResponseHandlerTest {
40 public ExpectedException expectedException = ExpectedException.none();
43 public static void setUpBeforeClass() throws Exception {
44 System.setProperty("CONFIG_HOME", "src/test/resources");
45 System.setProperty("AJSC_HOME", ".");
46 System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
48 OxmModelLoader.loadModels();
52 public void testPolicyViolationsNotDetected() throws Exception {
53 String expectedEnvelope = TestUtil.getFileAsString("event/event-envelope-sentinel-no-violations.json");
54 Gson gson = new Gson();
55 GraphEventEnvelope envelope = gson.fromJson(expectedEnvelope, GraphEventEnvelope.class);
57 GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
58 assertThat(graphEventResponseHandler.hasPolicyViolations(envelope)).isFalse();
62 public void testPolicyViolationsDetected() throws Exception {
63 String expectedEnvelope = TestUtil.getFileAsString("event/event-envelope-sentinel.json");
64 Gson gson = new Gson();
65 GraphEventEnvelope envelope = gson.fromJson(expectedEnvelope, GraphEventEnvelope.class);
67 GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
68 assertThat(graphEventResponseHandler.hasPolicyViolations(envelope)).isTrue();
72 public void testHandleVertexResponse() throws Exception {
73 String graphEvent = TestUtil.getFileAsString("event/graph-vertex-event.json");
74 String champResult = TestUtil.getFileAsString("event/champ-vertex-event.json");
75 Gson gson = new Gson();
76 GraphEvent event = gson.fromJson(graphEvent, GraphEvent.class);
77 GraphEventEnvelope result = gson.fromJson(champResult, GraphEventEnvelope.class);
79 GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
80 String response = graphEventResponseHandler.handleVertexResponse("v13", event, result);
82 assertThat(new JsonParser().parse(response).getAsJsonObject().get("url").getAsString())
83 .isEqualTo("services/inventory/v13/pserver/890c8b3f-892f-48e3-85cd-748ebf0426a5");
87 public void testHandleVertexResponseWithError() throws Exception {
88 expectedException.expect(CrudException.class);
89 expectedException.expectMessage("test error");
91 String graphEvent = TestUtil.getFileAsString("event/graph-vertex-event.json");
92 String champResult = TestUtil.getFileAsString("event/champ-vertex-event-error.json");
93 Gson gson = new Gson();
94 GraphEvent event = gson.fromJson(graphEvent, GraphEvent.class);
95 GraphEventEnvelope result = gson.fromJson(champResult, GraphEventEnvelope.class);
97 GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
98 graphEventResponseHandler.handleVertexResponse("v13", event, result);
101 @Test(expected = CrudException.class)
102 public void testHandleVertexResponseWithViolations() throws Exception {
104 String graphEvent = TestUtil.getFileAsString("event/graph-vertex-event.json");
105 String champResult = TestUtil.getFileAsString("event/champ-vertex-event-violations.json");
106 Gson gson = new Gson();
107 GraphEvent event = gson.fromJson(graphEvent, GraphEvent.class);
108 GraphEventEnvelope result = gson.fromJson(champResult, GraphEventEnvelope.class);
110 GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
111 graphEventResponseHandler.handleVertexResponse("v13", event, result);
115 public void testHandleEdgeResponse() throws Exception {
116 String graphEvent = TestUtil.getFileAsString("event/graph-edge-event.json");
117 String champResult = TestUtil.getFileAsString("event/champ-edge-event.json");
118 Gson gson = new Gson();
119 GraphEvent event = gson.fromJson(graphEvent, GraphEvent.class);
120 GraphEventEnvelope result = gson.fromJson(champResult, GraphEventEnvelope.class);
122 GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
123 String response = graphEventResponseHandler.handleEdgeResponse("v10", event, result);
125 String id = new JsonParser().parse(response).getAsJsonObject().get("id").getAsString();
126 assertThat(id).isEqualTo("test-key");
130 public void testHandleDeletionResponse() throws Exception {
131 GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
132 GraphEvent event = GraphEvent.builder(GraphEventOperation.DELETE).build();
133 String response = graphEventResponseHandler.handleDeletionResponse(event, new GraphEventEnvelope(event));
134 assertThat(response).isEqualTo("");
138 public void testHandleBulkEventResponse() throws Exception {
139 GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
140 GraphEvent event = GraphEvent.builder(GraphEventOperation.CREATE).build();
141 graphEventResponseHandler.handleBulkEventResponse(event, new GraphEventEnvelope(event));