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 com.google.gson.Gson;
24 import com.google.gson.JsonParser;
25 import org.junit.Before;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.rules.ExpectedException;
29 import org.junit.runner.RunWith;
30 import org.mockito.junit.MockitoJUnitRunner;
31 import org.onap.crud.OXMModelLoaderSetup;
32 import org.onap.crud.event.GraphEvent;
33 import org.onap.crud.event.GraphEvent.GraphEventOperation;
34 import org.onap.crud.event.envelope.GraphEventEnvelope;
35 import org.onap.crud.exception.CrudException;
36 import org.onap.crud.util.TestUtil;
37 import org.onap.schema.EdgeRulesLoader;
38 import org.onap.schema.OxmModelLoader;
40 import static org.assertj.core.api.Assertions.assertThat;
42 @RunWith(MockitoJUnitRunner.Silent.class)
43 public class GraphEventResponseHandlerTest extends OXMModelLoaderSetup {
44 private static boolean setUpIsNotDone = true;
47 public void setUpOnce() throws CrudException {
49 System.setProperty("CONFIG_HOME", "src/test/resources");
50 System.setProperty("AJSC_HOME", ".");
51 System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
53 OxmModelLoader.loadModels();
54 EdgeRulesLoader.loadModels();
56 setUpIsNotDone = false;
61 public ExpectedException expectedException = ExpectedException.none();
64 public void testPolicyViolationsNotDetected() throws Exception {
65 String expectedEnvelope = TestUtil.getFileAsString("event/event-envelope-sentinel-no-violations.json");
66 Gson gson = new Gson();
67 GraphEventEnvelope envelope = gson.fromJson(expectedEnvelope, GraphEventEnvelope.class);
69 GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
70 assertThat(graphEventResponseHandler.hasPolicyViolations(envelope)).isFalse();
74 public void testPolicyViolationsDetected() throws Exception {
75 String expectedEnvelope = TestUtil.getFileAsString("event/event-envelope-sentinel.json");
76 Gson gson = new Gson();
77 GraphEventEnvelope envelope = gson.fromJson(expectedEnvelope, GraphEventEnvelope.class);
79 GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
80 assertThat(graphEventResponseHandler.hasPolicyViolations(envelope)).isTrue();
84 public void testHandleVertexResponse() throws Exception {
85 String graphEvent = TestUtil.getFileAsString("event/graph-vertex-event.json");
86 String champResult = TestUtil.getFileAsString("event/champ-vertex-event.json");
87 Gson gson = new Gson();
88 GraphEvent event = gson.fromJson(graphEvent, GraphEvent.class);
89 GraphEventEnvelope result = gson.fromJson(champResult, GraphEventEnvelope.class);
91 GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
92 String response = graphEventResponseHandler.handleVertexResponse("v13", event, result);
94 assertThat(new JsonParser().parse(response).getAsJsonObject().get("url").getAsString())
95 .isEqualTo("services/inventory/v13/pserver/890c8b3f-892f-48e3-85cd-748ebf0426a5");
99 public void testHandleVertexResponseWithError() throws Exception {
100 expectedException.expect(CrudException.class);
101 expectedException.expectMessage("test error");
103 String graphEvent = TestUtil.getFileAsString("event/graph-vertex-event.json");
104 String champResult = TestUtil.getFileAsString("event/champ-vertex-event-error.json");
105 Gson gson = new Gson();
106 GraphEvent event = gson.fromJson(graphEvent, GraphEvent.class);
107 GraphEventEnvelope result = gson.fromJson(champResult, GraphEventEnvelope.class);
109 GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
110 graphEventResponseHandler.handleVertexResponse("v13", event, result);
113 @Test(expected = CrudException.class)
114 public void testHandleVertexResponseWithViolations() throws Exception {
116 String graphEvent = TestUtil.getFileAsString("event/graph-vertex-event.json");
117 String champResult = TestUtil.getFileAsString("event/champ-vertex-event-violations.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 graphEventResponseHandler.handleVertexResponse("v13", event, result);
127 public void testHandleEdgeResponse() throws Exception {
128 String graphEvent = TestUtil.getFileAsString("event/graph-edge-event.json");
129 String champResult = TestUtil.getFileAsString("event/champ-edge-event.json");
130 Gson gson = new Gson();
131 GraphEvent event = gson.fromJson(graphEvent, GraphEvent.class);
132 GraphEventEnvelope result = gson.fromJson(champResult, GraphEventEnvelope.class);
134 GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
135 String response = graphEventResponseHandler.handleEdgeResponse("v10", event, result);
137 String id = new JsonParser().parse(response).getAsJsonObject().get("id").getAsString();
138 assertThat(id).isEqualTo("test-key");
142 public void testHandleDeletionResponse() throws Exception {
143 GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
144 GraphEvent event = GraphEvent.builder(GraphEventOperation.DELETE).build();
145 String response = graphEventResponseHandler.handleDeletionResponse(event, new GraphEventEnvelope(event));
146 assertThat(response).isEqualTo("");
150 public void testHandleBulkEventResponse() throws Exception {
151 GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
152 GraphEvent event = GraphEvent.builder(GraphEventOperation.CREATE).build();
153 graphEventResponseHandler.handleBulkEventResponse(event, new GraphEventEnvelope(event));