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.EdgeRulesLoader;
34 import org.onap.schema.OxmModelLoader;
35 import com.google.gson.Gson;
36 import com.google.gson.JsonParser;
38 import org.junit.runner.RunWith;
39 import org.mockito.junit.MockitoJUnitRunner;
40 import org.onap.crud.OXMModelLoaderSetup;
42 @RunWith(MockitoJUnitRunner.Silent.class)
43 public class GraphEventResponseHandlerTest extends OXMModelLoaderSetup {
46 public ExpectedException expectedException = ExpectedException.none();
49 public static void setUpBeforeClass() throws Exception {
50 System.setProperty("CONFIG_HOME", "src/test/resources");
51 System.setProperty("AJSC_HOME", ".");
52 System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
54 OxmModelLoader.loadModels();
55 EdgeRulesLoader.loadModels();
59 public void testPolicyViolationsNotDetected() throws Exception {
60 String expectedEnvelope = TestUtil.getFileAsString("event/event-envelope-sentinel-no-violations.json");
61 Gson gson = new Gson();
62 GraphEventEnvelope envelope = gson.fromJson(expectedEnvelope, GraphEventEnvelope.class);
64 GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
65 assertThat(graphEventResponseHandler.hasPolicyViolations(envelope)).isFalse();
69 public void testPolicyViolationsDetected() throws Exception {
70 String expectedEnvelope = TestUtil.getFileAsString("event/event-envelope-sentinel.json");
71 Gson gson = new Gson();
72 GraphEventEnvelope envelope = gson.fromJson(expectedEnvelope, GraphEventEnvelope.class);
74 GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
75 assertThat(graphEventResponseHandler.hasPolicyViolations(envelope)).isTrue();
79 public void testHandleVertexResponse() throws Exception {
80 String graphEvent = TestUtil.getFileAsString("event/graph-vertex-event.json");
81 String champResult = TestUtil.getFileAsString("event/champ-vertex-event.json");
82 Gson gson = new Gson();
83 GraphEvent event = gson.fromJson(graphEvent, GraphEvent.class);
84 GraphEventEnvelope result = gson.fromJson(champResult, GraphEventEnvelope.class);
86 GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
87 String response = graphEventResponseHandler.handleVertexResponse("v13", event, result);
89 assertThat(new JsonParser().parse(response).getAsJsonObject().get("url").getAsString())
90 .isEqualTo("services/inventory/v13/pserver/890c8b3f-892f-48e3-85cd-748ebf0426a5");
94 public void testHandleVertexResponseWithError() throws Exception {
95 expectedException.expect(CrudException.class);
96 expectedException.expectMessage("test error");
98 String graphEvent = TestUtil.getFileAsString("event/graph-vertex-event.json");
99 String champResult = TestUtil.getFileAsString("event/champ-vertex-event-error.json");
100 Gson gson = new Gson();
101 GraphEvent event = gson.fromJson(graphEvent, GraphEvent.class);
102 GraphEventEnvelope result = gson.fromJson(champResult, GraphEventEnvelope.class);
104 GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
105 graphEventResponseHandler.handleVertexResponse("v13", event, result);
108 @Test(expected = CrudException.class)
109 public void testHandleVertexResponseWithViolations() throws Exception {
111 String graphEvent = TestUtil.getFileAsString("event/graph-vertex-event.json");
112 String champResult = TestUtil.getFileAsString("event/champ-vertex-event-violations.json");
113 Gson gson = new Gson();
114 GraphEvent event = gson.fromJson(graphEvent, GraphEvent.class);
115 GraphEventEnvelope result = gson.fromJson(champResult, GraphEventEnvelope.class);
117 GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
118 graphEventResponseHandler.handleVertexResponse("v13", event, result);
122 public void testHandleEdgeResponse() throws Exception {
123 String graphEvent = TestUtil.getFileAsString("event/graph-edge-event.json");
124 String champResult = TestUtil.getFileAsString("event/champ-edge-event.json");
125 Gson gson = new Gson();
126 GraphEvent event = gson.fromJson(graphEvent, GraphEvent.class);
127 GraphEventEnvelope result = gson.fromJson(champResult, GraphEventEnvelope.class);
129 GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
130 String response = graphEventResponseHandler.handleEdgeResponse("v10", event, result);
132 String id = new JsonParser().parse(response).getAsJsonObject().get("id").getAsString();
133 assertThat(id).isEqualTo("test-key");
137 public void testHandleDeletionResponse() throws Exception {
138 GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
139 GraphEvent event = GraphEvent.builder(GraphEventOperation.DELETE).build();
140 String response = graphEventResponseHandler.handleDeletionResponse(event, new GraphEventEnvelope(event));
141 assertThat(response).isEqualTo("");
145 public void testHandleBulkEventResponse() throws Exception {
146 GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
147 GraphEvent event = GraphEvent.builder(GraphEventOperation.CREATE).build();
148 graphEventResponseHandler.handleBulkEventResponse(event, new GraphEventEnvelope(event));