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