Upgrade version of aai-common
[aai/gizmo.git] / src / test / java / org / onap / crud / event / GraphEventEnvelopeTest.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;
22
23 import static org.hamcrest.Matchers.is;
24 import static org.junit.Assert.assertThat;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.junit.MockitoJUnitRunner;
28 import org.onap.crud.OXMModelLoaderSetup;
29 import org.onap.crud.entity.Vertex;
30 import org.onap.crud.event.GraphEvent.GraphEventOperation;
31 import org.onap.crud.event.envelope.GraphEventEnvelope;
32 import org.onap.crud.util.TestUtil;
33 import org.skyscreamer.jsonassert.Customization;
34 import org.skyscreamer.jsonassert.JSONAssert;
35 import org.skyscreamer.jsonassert.JSONCompareMode;
36 import org.skyscreamer.jsonassert.comparator.CustomComparator;
37 import com.google.gson.Gson;
38 import com.google.gson.JsonElement;
39
40 @RunWith(MockitoJUnitRunner.Silent.class)
41 public class GraphEventEnvelopeTest extends OXMModelLoaderSetup {
42
43     @Test
44     public void testPublishedEventFormat() throws Exception {
45         String expectedEnvelope = TestUtil.getFileAsString("event/event-envelope.json");
46
47         GraphEvent body = GraphEvent.builder(GraphEventOperation.CREATE)
48                 .vertex(GraphEventVertex.fromVertex(new Vertex.Builder("pserver").build(), "v13")).build();
49         String graphEventEnvelope = new GraphEventEnvelope(body).toJson();
50
51         JSONAssert.assertEquals(expectedEnvelope, graphEventEnvelope,
52                 new CustomComparator(JSONCompareMode.STRICT, new Customization("header.request-id", (o1, o2) -> true),
53                         new Customization("header.timestamp", (o1, o2) -> true),
54                         new Customization("body.timestamp", (o1, o2) -> true),
55                         new Customization("body.transaction-id", (o1, o2) -> true)));
56
57         Gson gson = new Gson();
58         GraphEventEnvelope envelope = gson.fromJson(graphEventEnvelope, GraphEventEnvelope.class);
59         assertThat(envelope.getHeader().getRequestId(), is(envelope.getBody().getTransactionId()));
60     }
61
62     @Test
63     public void testConsumedEventFormat() throws Exception {
64         String expectedEnvelope = TestUtil.getFileAsString("event/event-envelope-sentinel.json");
65         Gson gson = new Gson();
66         GraphEventEnvelope envelope = gson.fromJson(expectedEnvelope, GraphEventEnvelope.class);
67         JsonElement jsonElement = envelope.getPolicyViolations().getAsJsonArray().get(0);
68
69         assertThat(jsonElement.getAsJsonObject().get("summary").getAsString(), is("a summary"));
70         assertThat(jsonElement.getAsJsonObject().get("policyName").getAsString(), is("a policy name"));
71     }
72 }