Merge "Sonar Fix - Type Inference"
[aai/champ.git] / champ-lib / champ-core / src / test / java / org / onap / aai / champcore / event / envelope / ChampEventEnvelopeTest.java
1 /**
2  * ============LICENSE_START==========================================
3  * org.onap.aai
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
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.champcore.event.envelope;
23
24 import static org.hamcrest.Matchers.is;
25 import static org.junit.Assert.assertThat;
26
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;
37
38 import com.fasterxml.jackson.databind.ObjectMapper;
39 import com.fasterxml.jackson.databind.node.ObjectNode;
40
41 public class ChampEventEnvelopeTest {
42
43     @Test
44     public void testVertexEventEnvelopeBodyNoKey() throws Exception {
45         String expectedEnvelope = TestUtil.getFileAsString("event/vertex-event-envelope-no-key.json");
46
47         ChampEvent body = ChampEvent.builder().entity(new ChampObject.Builder("pserver").build()).build();
48
49         String envelope = new ChampEventEnvelope(body).toJson();
50
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)));
56     }
57
58     @Test
59     public void testVertexEventEnvelopeBodyWithKey() throws Exception {
60         String expectedEnvelope = TestUtil.getFileAsString("event/vertex-event-envelope-with-key.json");
61
62         ChampEvent body = ChampEvent.builder().entity(new ChampObject.Builder("pserver").key("1234").build()).build();
63
64         String envelope = new ChampEventEnvelope(body).toJson();
65
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)));
71     }
72
73
74     @Test
75     public void testRequestIdIsTransactionId() throws Exception {
76         ChampEvent body = ChampEvent.builder().entity(new ChampObject.Builder("pserver").build()).build();
77
78         ChampEventEnvelope envelope = new ChampEventEnvelope(body);
79
80         assertThat(envelope.getHeader().getRequestId(), is(envelope.getBody().getTransactionId()));
81     }
82
83     @Test
84     public void testEdgeEventEnvelope() throws Exception {
85         String expectedEnvelope = TestUtil.getFileAsString("event/edge-event-envelope.json");
86
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);
93
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();
98
99         String envelope = new ChampEventEnvelope(body).toJson();
100
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)));
106     }
107     
108     @Test
109     public void testChampEventHeader() {
110       String link = "link1";
111       String requestId = "request-id1";
112       String entityType = "entity-type1";
113       String topEntityType = "top-entity-type1";
114       
115       ChampEventHeader header1 = new ChampEventHeader.Builder(ChampEventHeader.EventType.UPDATE_NOTIFICATION)
116         .entityLink(link)
117         .requestId(requestId)
118         .validationEntityType(entityType)
119         .validationTopEntityType(topEntityType)
120         .build();
121       
122       Assert.assertEquals(link, header1.getEntityLink());
123       Assert.assertEquals(requestId, header1.getRequestId());
124       Assert.assertEquals(entityType, header1.getValidationEntityType());
125       Assert.assertEquals(topEntityType, header1.getValidationTopEntityType());
126
127       ChampEventHeader header2 = new ChampEventHeader.Builder(ChampEventHeader.EventType.UPDATE_RESULT)
128           .entityLink("link2")
129           .requestId("request-id2")
130           .validationEntityType("entity-type2")
131           .validationTopEntityType("top-entity-type2")
132           .build();
133       
134       Assert.assertNotEquals(header1, header2);
135
136       
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());
143
144       Assert.assertEquals(header1, header2);
145       
146     }
147 }