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