e5ac52d569fd33138d708e37d2a76d0432b03531
[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 public class GraphEventResponseHandlerTest {
38
39     @Rule
40     public ExpectedException expectedException = ExpectedException.none();
41
42     @BeforeClass
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");
47
48         OxmModelLoader.loadModels();
49     }
50
51     @Test
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);
56
57         GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
58         assertThat(graphEventResponseHandler.hasPolicyViolations(envelope)).isFalse();
59     }
60
61     @Test
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);
66
67         GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
68         assertThat(graphEventResponseHandler.hasPolicyViolations(envelope)).isTrue();
69     }
70
71     @Test
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);
78
79         GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
80         String response = graphEventResponseHandler.handleVertexResponse("v13", event, result);
81
82         assertThat(new JsonParser().parse(response).getAsJsonObject().get("url").getAsString())
83                 .isEqualTo("services/inventory/v13/pserver/890c8b3f-892f-48e3-85cd-748ebf0426a5");
84     }
85
86     @Test
87     public void testHandleVertexResponseWithError() throws Exception {
88         expectedException.expect(CrudException.class);
89         expectedException.expectMessage("test error");
90
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);
96
97         GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
98         graphEventResponseHandler.handleVertexResponse("v13", event, result);
99     }
100
101     @Test(expected = CrudException.class)
102     public void testHandleVertexResponseWithViolations() throws Exception {
103
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);
109
110         GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
111         graphEventResponseHandler.handleVertexResponse("v13", event, result);
112     }
113
114     @Test
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);
121
122         GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
123         String response = graphEventResponseHandler.handleEdgeResponse("v10", event, result);
124
125         String id = new JsonParser().parse(response).getAsJsonObject().get("id").getAsString();
126         assertThat(id).isEqualTo("test-key");
127     }
128
129     @Test
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("");
135     }
136
137     @Test
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));
142     }
143 }