2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.champcore.event.envelope;
24 import static org.hamcrest.Matchers.is;
25 import static org.junit.Assert.assertThat;
27 import org.junit.Assert;
28 import org.junit.Test;
29 import org.onap.aai.champcore.event.ChampEvent;
30 import org.onap.aai.champcore.model.ChampObject;
31 import org.onap.aai.champcore.model.ChampRelationship;
32 import org.onap.aai.champcore.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;
38 import com.fasterxml.jackson.databind.ObjectMapper;
39 import com.fasterxml.jackson.databind.node.ObjectNode;
41 public class ChampEventEnvelopeTest {
44 public void testVertexEventEnvelopeBodyNoKey() throws Exception {
45 String expectedEnvelope = TestUtil.getFileAsString("event/vertex-event-envelope-no-key.json");
47 ChampEvent body = ChampEvent.builder().entity(new ChampObject.Builder("pserver").build()).build();
49 String envelope = new ChampEventEnvelope(body).toJson();
51 JSONAssert.assertEquals(expectedEnvelope, envelope,
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)));
59 public void testVertexEventEnvelopeBodyWithKey() throws Exception {
60 String expectedEnvelope = TestUtil.getFileAsString("event/vertex-event-envelope-with-key.json");
62 ChampEvent body = ChampEvent.builder().entity(new ChampObject.Builder("pserver").key("1234").build()).build();
64 String envelope = new ChampEventEnvelope(body).toJson();
66 JSONAssert.assertEquals(expectedEnvelope, envelope,
67 new CustomComparator(JSONCompareMode.STRICT, new Customization("header.request-id", (o1, o2) -> true),
68 new Customization("header.timestamp", (o1, o2) -> true),
69 new Customization("body.timestamp", (o1, o2) -> true),
70 new Customization("body.transaction-id", (o1, o2) -> true)));
75 public void testRequestIdIsTransactionId() throws Exception {
76 ChampEvent body = ChampEvent.builder().entity(new ChampObject.Builder("pserver").build()).build();
78 ChampEventEnvelope envelope = new ChampEventEnvelope(body);
80 assertThat(envelope.getHeader().getRequestId(), is(envelope.getBody().getTransactionId()));
84 public void testEdgeEventEnvelope() throws Exception {
85 String expectedEnvelope = TestUtil.getFileAsString("event/edge-event-envelope.json");
87 ObjectMapper mapper = new ObjectMapper();
88 ObjectNode objectNode = mapper.createObjectNode();
89 objectNode.put("inVertexId", 5678);
90 objectNode.put("typeId", 1000);
91 objectNode.put("relationId", 2000);
92 objectNode.put("outVertexId", 1234);
94 ChampRelationship relationship =
95 new ChampRelationship.Builder(new ChampObject.Builder("vserver").key("1234").build(),
96 new ChampObject.Builder("pserver").key("5678").build(), "test").key(objectNode).build();
97 ChampEvent body = ChampEvent.builder().entity(relationship).build();
99 String envelope = new ChampEventEnvelope(body).toJson();
101 JSONAssert.assertEquals(expectedEnvelope, envelope,
102 new CustomComparator(JSONCompareMode.STRICT, new Customization("header.request-id", (o1, o2) -> true),
103 new Customization("header.timestamp", (o1, o2) -> true),
104 new Customization("body.timestamp", (o1, o2) -> true),
105 new Customization("body.transaction-id", (o1, o2) -> true)));
109 public void testChampEventHeader() {
110 String link = "link1";
111 String requestId = "request-id1";
112 String entityType = "entity-type1";
113 String topEntityType = "top-entity-type1";
115 ChampEventHeader header1 = new ChampEventHeader.Builder(ChampEventHeader.EventType.UPDATE_NOTIFICATION)
117 .requestId(requestId)
118 .validationEntityType(entityType)
119 .validationTopEntityType(topEntityType)
122 Assert.assertEquals(link, header1.getEntityLink());
123 Assert.assertEquals(requestId, header1.getRequestId());
124 Assert.assertEquals(entityType, header1.getValidationEntityType());
125 Assert.assertEquals(topEntityType, header1.getValidationTopEntityType());
127 ChampEventHeader header2 = new ChampEventHeader.Builder(ChampEventHeader.EventType.UPDATE_RESULT)
129 .requestId("request-id2")
130 .validationEntityType("entity-type2")
131 .validationTopEntityType("top-entity-type2")
134 Assert.assertNotEquals(header1, header2);
137 header2.setEntityLink(link);
138 header2.setRequestId(requestId);
139 header2.setValidationEntityType(entityType);
140 header2.setValidationTopEntityType(topEntityType);
141 header2.setEventType(header1.getEventType());
142 header2.setTimestamp(header1.getTimestamp());
144 Assert.assertEquals(header1, header2);