Fix dockerfile to reflect common image naming
[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 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;
39
40 import static org.assertj.core.api.Assertions.assertThat;
41
42 @RunWith(MockitoJUnitRunner.Silent.class)
43 public class GraphEventResponseHandlerTest extends OXMModelLoaderSetup {
44     private static boolean setUpIsNotDone = true;
45     
46     @Before
47     public void setUpOnce() throws CrudException {
48         if (setUpIsNotDone) {
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             EdgeRulesLoader.loadModels();
55
56             setUpIsNotDone = false;
57         }
58     }
59
60     @Rule
61     public ExpectedException expectedException = ExpectedException.none();
62
63     @Test
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);
68
69         GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
70         assertThat(graphEventResponseHandler.hasPolicyViolations(envelope)).isFalse();
71     }
72
73     @Test
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);
78
79         GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
80         assertThat(graphEventResponseHandler.hasPolicyViolations(envelope)).isTrue();
81     }
82
83     @Test
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);
90
91         GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
92         String response = graphEventResponseHandler.handleVertexResponse("v13", event, result);
93
94         assertThat(new JsonParser().parse(response).getAsJsonObject().get("url").getAsString())
95                 .isEqualTo("services/inventory/v13/pserver/890c8b3f-892f-48e3-85cd-748ebf0426a5");
96     }
97
98     @Test
99     public void testHandleVertexResponseWithError() throws Exception {
100         expectedException.expect(CrudException.class);
101         expectedException.expectMessage("test error");
102
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);
108
109         GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
110         graphEventResponseHandler.handleVertexResponse("v13", event, result);
111     }
112
113     @Test(expected = CrudException.class)
114     public void testHandleVertexResponseWithViolations() throws Exception {
115
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);
121
122         GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
123         graphEventResponseHandler.handleVertexResponse("v13", event, result);
124     }
125
126     @Test
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);
133
134         GraphEventResponseHandler graphEventResponseHandler = new GraphEventResponseHandler();
135         String response = graphEventResponseHandler.handleEdgeResponse("v10", event, result);
136
137         String id = new JsonParser().parse(response).getAsJsonObject().get("id").getAsString();
138         assertThat(id).isEqualTo("test-key");
139     }
140
141     @Test
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("");
147     }
148
149     @Test
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));
154     }
155 }