Remove DMaaP dependency from AAI-Common
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / util / StoreNotificationEventTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.aai.util;
22
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25
26 import com.fasterxml.jackson.core.JsonGenerationException;
27 import com.fasterxml.jackson.databind.JsonMappingException;
28
29 import java.io.IOException;
30
31 import javax.json.Json;
32 import javax.json.JsonObject;
33
34 import org.eclipse.persistence.dynamic.DynamicEntity;
35 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
36 import org.junit.Before;
37 import org.junit.BeforeClass;
38 import org.junit.Ignore;
39 import org.junit.Test;
40 import org.mockito.Mockito;
41 import org.onap.aai.AAISetup;
42 import org.onap.aai.domain.notificationEvent.NotificationEvent.EventHeader;
43 import org.onap.aai.exceptions.AAIException;
44 import org.onap.aai.introspection.Introspector;
45 import org.onap.aai.introspection.Loader;
46 import org.onap.aai.introspection.ModelType;
47 import org.onap.aai.kafka.AAIKafkaEventJMSProducer;
48
49 public class StoreNotificationEventTest extends AAISetup {
50
51     private static AAIKafkaEventJMSProducer producer;
52     private static StoreNotificationEvent sne;
53
54     @BeforeClass
55     public static void setUp() {
56         producer = Mockito.mock(AAIKafkaEventJMSProducer.class);
57         // sne = new StoreNotificationEvent(producer, "transiationId", "sourceOfTruth");
58     }
59
60     @Before
61     public void setUpBefore() {
62         producer = Mockito.mock(AAIKafkaEventJMSProducer.class);
63         sne = new StoreNotificationEvent(producer, "transiationId", "sourceOfTruth");
64
65     }
66
67     @Test(expected = AAIException.class)
68     public void testStoreEventNullObj() throws AAIException {
69         sne.storeEventAndSendToJms(new EventHeader(), null);
70     }
71
72     @Test(expected = AAIException.class)
73     public void testStoreEventInvalidObjForPojoUtils() throws AAIException {
74         sne.storeEventAndSendToJms(new EventHeader(), new Object());
75     }
76
77     @Test
78     public void testStoreEventEmptyEventHeader()
79             throws AAIException, JsonGenerationException, JsonMappingException, IOException {
80         JsonObject object = Json.createObjectBuilder().add("hello", "world").build();
81         String res = sne.storeEventAndSendToJms(new EventHeader(), object);
82
83         assertNotNull(res);
84         assertTrue(res.contains("\"cambria.partition\" : \"" + AAIConstants.UEB_PUB_PARTITION_AAI + "\""));
85         assertTrue(res.contains("\"event-header\""));
86         assertTrue(res.contains("\"id\""));
87         assertTrue(res.contains("\"timestamp\""));
88         assertTrue(res
89                 .contains("\"source-name\" : \"" + AAIConfig.get("aai.notificationEvent.default.sourceName") + "\""));
90         assertTrue(res.contains("\"domain\" : \"" + AAIConfig.get("aai.notificationEvent.default.domain") + "\""));
91         assertTrue(res.contains(
92                 "\"sequence-number\" : \"" + AAIConfig.get("aai.notificationEvent.default.sequenceNumber") + "\""));
93         assertTrue(res.contains("\"severity\" : \"" + AAIConfig.get("aai.notificationEvent.default.severity") + "\""));
94         assertTrue(
95                 res.contains("\"event-type\" : \"" + AAIConfig.get("aai.notificationEvent.default.eventType") + "\""));
96         assertTrue(res.contains("\"version\" : \"" + AAIConfig.get("aai.notificationEvent.default.version") + "\""));
97         assertTrue(res.contains("\"action\" : \"UNK\""));
98         assertTrue(res.contains("\"entity-link\" : \"UNK\""));
99         assertTrue(res.contains("\"entity\""));
100         assertTrue(res.contains("\"hello\""));
101         assertTrue(res.contains("\"chars\" : \"world\""));
102         assertTrue(res.contains("\"string\" : \"world\""));
103         assertTrue(res.contains("\"valueType\" : \"STRING\""));
104     }
105
106     @Test
107     public void testStoreEvent() throws AAIException, JsonGenerationException, JsonMappingException, IOException {
108         JsonObject object = Json.createObjectBuilder().add("hello", "world").build();
109         EventHeader eh = new EventHeader();
110         eh.setId("123");
111         eh.setTimestamp("current-time");
112         eh.setEntityLink("entity-link");
113         eh.setAction("action!");
114         eh.setEventType("surprise");
115         eh.setDomain("PROD");
116         eh.setSourceName("source");
117         eh.setSequenceNumber("23");
118         eh.setSeverity("ALERT");
119         eh.setVersion("v12");
120
121         String res = sne.storeEventAndSendToJms(eh, object);
122
123         assertNotNull(res);
124         assertTrue(res.contains("\"cambria.partition\" : \"" + AAIConstants.UEB_PUB_PARTITION_AAI + "\""));
125         assertTrue(res.contains("\"event-header\""));
126         assertTrue(res.contains("\"id\" : \"123\""));
127         assertTrue(res.contains("\"timestamp\" : \"current-time\""));
128         assertTrue(res.contains("\"source-name\" : \"source\""));
129         assertTrue(res.contains("\"domain\" : \"PROD\""));
130         assertTrue(res.contains("\"sequence-number\" : \"23\""));
131         assertTrue(res.contains("\"severity\" : \"ALERT\""));
132         assertTrue(res.contains("\"event-type\" : \"surprise\""));
133         assertTrue(res.contains("\"version\" : \"v12\""));
134         assertTrue(res.contains("\"action\" : \"action!\""));
135         assertTrue(res.contains("\"entity-link\" : \"entity-link\""));
136         assertTrue(res.contains("\"entity\""));
137         assertTrue(res.contains("\"hello\""));
138         assertTrue(res.contains("\"chars\" : \"world\""));
139         assertTrue(res.contains("\"string\" : \"world\""));
140         assertTrue(res.contains("\"valueType\" : \"STRING\""));
141     }
142
143     @Test(expected = AAIException.class)
144     public void testStoreDynamicEventNullObj() throws AAIException {
145         DynamicEntity eventHeader = Mockito.mock(DynamicEntity.class);
146         DynamicJAXBContext notificationJaxbContext =
147                 nodeIngestor.getContextForVersion(schemaVersions.getEdgeLabelVersion());
148         sne.storeDynamicEvent(notificationJaxbContext, "v12", eventHeader, null);
149     }
150
151     @Test(expected = Exception.class)
152     public void testStoreDynamicEventAAIException() throws Exception {
153
154         DynamicJAXBContext notificationJaxbContext =
155                 nodeIngestor.getContextForVersion(schemaVersions.getEdgeLabelVersion());
156         DynamicEntity obj = Mockito.mock(DynamicEntity.class);
157         DynamicEntity eventHeader = Mockito.mock(DynamicEntity.class);
158         sne.storeDynamicEvent(notificationJaxbContext, "v12", eventHeader, obj);
159     }
160
161     @Test(expected = AAIException.class)
162     public void testStoreEventIntrospectorNullObj() throws Exception {
163         Loader loader = Mockito.mock(Loader.class);
164         sne.storeEventAndSendToJms(loader, null, null);
165     }
166
167     @Ignore("Stopped working since the model driven story")
168     @Test
169     public void testStoreEvent1Introspector() throws Exception {
170         Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getEdgeLabelVersion());
171         Introspector eventHeader = loader.introspectorFromName("notification-event-header");
172         eventHeader.setValue("id", "123");
173         eventHeader.setValue("timestamp", "current-time");
174         eventHeader.setValue("entity-link", "entity-link");
175         eventHeader.setValue("action", "action!");
176         eventHeader.setValue("event-type", "surprise");
177         eventHeader.setValue("domain", "PROD");
178         eventHeader.setValue("source-name", "source");
179         eventHeader.setValue("sequence-number", "23");
180         eventHeader.setValue("severity", "ALERT");
181         eventHeader.setValue("version", "v12");
182         Introspector obj = loader.introspectorFromName("notification-event");
183         String res = sne.storeEventAndSendToJms(loader, eventHeader, obj);
184
185         assertNotNull(res);
186         assertTrue(res.contains("\"cambria.partition\":\"" + AAIConstants.UEB_PUB_PARTITION_AAI + "\""));
187         assertTrue(res.contains("\"event-header\""));
188         assertTrue(res.contains("\"id\":\"123\""));
189         assertTrue(res.contains("\"timestamp\":\"current-time\""));
190         assertTrue(res.contains("\"source-name\":\"source\""));
191         assertTrue(res.contains("\"domain\":\"PROD\""));
192         assertTrue(res.contains("\"sequence-number\":\"23\""));
193         assertTrue(res.contains("\"severity\":\"ALERT\""));
194         assertTrue(res.contains("\"event-type\":\"surprise\""));
195         assertTrue(res.contains("\"version\":\"v12\""));
196         assertTrue(res.contains("\"action\":\"action!\""));
197         assertTrue(res.contains("\"entity-link\":\"entity-link\""));
198         assertTrue(res.contains("\"notification-event\""));
199     }
200
201     @Ignore("Stopped working since the model driven story")
202     @Test
203     public void testStoreEventIntrospectorEmptyEventHeader() throws Exception {
204         Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getEdgeLabelVersion());
205         Introspector eventHeader = loader.introspectorFromName("notification-event-header");
206         Introspector obj = loader.introspectorFromName("notification-event");
207
208         String res = sne.storeEventAndSendToJms(loader, eventHeader, obj);
209
210         assertNotNull(res);
211         assertTrue(res.contains("\"cambria.partition\":\"" + AAIConstants.UEB_PUB_PARTITION_AAI + "\""));
212         assertTrue(res.contains("\"event-header\""));
213         assertTrue(res.contains("\"id\""));
214         assertTrue(res.contains("\"timestamp\""));
215         assertTrue(
216                 res.contains("\"source-name\":\"" + AAIConfig.get("aai.notificationEvent.default.sourceName") + "\""));
217         assertTrue(res.contains("\"domain\":\"" + AAIConfig.get("aai.notificationEvent.default.domain") + "\""));
218         assertTrue(res.contains(
219                 "\"sequence-number\":\"" + AAIConfig.get("aai.notificationEvent.default.sequenceNumber") + "\""));
220         assertTrue(res.contains("\"severity\":\"" + AAIConfig.get("aai.notificationEvent.default.severity") + "\""));
221         assertTrue(res.contains("\"event-type\":\"" + AAIConfig.get("aai.notificationEvent.default.eventType") + "\""));
222         assertTrue(res.contains("\"version\":\"" + AAIConfig.get("aai.notificationEvent.default.version") + "\""));
223         assertTrue(res.contains("\"action\":\"UNK\""));
224         assertTrue(res.contains("\"entity-link\":\"UNK\""));
225         assertTrue(res.contains("\"notification-event\""));
226     }
227 }