Update to use Schema Service
[aai/gizmo.git] / src / test / java / org / onap / crud / event / response / GraphEventResponseHandlerTest.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.event.response;
22
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;
37
38 import org.junit.runner.RunWith;
39 import org.mockito.junit.MockitoJUnitRunner;
40 import org.onap.crud.OXMModelLoaderSetup;
41
42 @RunWith(MockitoJUnitRunner.Silent.class)
43 public class GraphEventResponseHandlerTest extends OXMModelLoaderSetup {
44
45     @Rule
46     public ExpectedException expectedException = ExpectedException.none();
47
48     @BeforeClass
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");
53
54         OxmModelLoader.loadModels();
55         EdgeRulesLoader.loadModels();
56     }
57
58     @Test
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);
63
64         GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
65         assertThat(graphEventResponseHandler.hasPolicyViolations(envelope)).isFalse();
66     }
67
68     @Test
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);
73
74         GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
75         assertThat(graphEventResponseHandler.hasPolicyViolations(envelope)).isTrue();
76     }
77
78     @Test
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);
85
86         GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
87         String response = graphEventResponseHandler.handleVertexResponse("v13", event, result);
88
89         assertThat(new JsonParser().parse(response).getAsJsonObject().get("url").getAsString())
90                 .isEqualTo("services/inventory/v13/pserver/890c8b3f-892f-48e3-85cd-748ebf0426a5");
91     }
92
93     @Test
94     public void testHandleVertexResponseWithError() throws Exception {
95         expectedException.expect(CrudException.class);
96         expectedException.expectMessage("test error");
97
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);
103
104         GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
105         graphEventResponseHandler.handleVertexResponse("v13", event, result);
106     }
107
108     @Test(expected = CrudException.class)
109     public void testHandleVertexResponseWithViolations() throws Exception {
110
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);
116
117         GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
118         graphEventResponseHandler.handleVertexResponse("v13", event, result);
119     }
120
121     @Test
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);
128
129         GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
130         String response = graphEventResponseHandler.handleEdgeResponse("v10", event, result);
131
132         String id = new JsonParser().parse(response).getAsJsonObject().get("id").getAsString();
133         assertThat(id).isEqualTo("test-key");
134     }
135
136     @Test
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("");
142     }
143
144     @Test
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));
149     }
150 }