Integrate aai-schema-ingest library into aai-core
[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 package org.onap.aai.util;
21
22 import com.fasterxml.jackson.core.JsonGenerationException;
23 import com.fasterxml.jackson.databind.JsonMappingException;
24 import org.eclipse.persistence.dynamic.DynamicEntity;
25 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
26 import org.junit.Before;
27 import org.junit.BeforeClass;
28 import org.junit.Ignore;
29 import org.junit.Test;
30 import org.mockito.Mockito;
31 import org.onap.aai.AAISetup;
32 import org.onap.aai.dmaap.AAIDmaapEventJMSProducer;
33 import org.onap.aai.domain.notificationEvent.NotificationEvent.EventHeader;
34 import org.onap.aai.exceptions.AAIException;
35 import org.onap.aai.introspection.Introspector;
36 import org.onap.aai.introspection.Loader;
37 import org.onap.aai.introspection.ModelType;
38
39 import javax.json.Json;
40 import javax.json.JsonObject;
41 import java.io.IOException;
42
43 import static org.junit.Assert.assertNotNull;
44 import static org.junit.Assert.assertTrue;
45
46 public class StoreNotificationEventTest extends AAISetup {
47
48         private static AAIDmaapEventJMSProducer producer;
49         private static StoreNotificationEvent sne;
50         
51         
52         @BeforeClass
53         public static void setUp() {
54                 producer = Mockito.mock(AAIDmaapEventJMSProducer.class);
55         //      sne = new StoreNotificationEvent(producer, "transiationId", "sourceOfTruth");
56         }
57         
58         @Before
59         public void setUpBefore() {
60                 producer = Mockito.mock(AAIDmaapEventJMSProducer.class);
61                 sne = new StoreNotificationEvent(producer, "transiationId", "sourceOfTruth");
62                 
63         }
64
65         @Test(expected = AAIException.class)
66         public void testStoreEventNullObj() throws AAIException {
67                 sne.storeEvent(new EventHeader(), null);
68         }
69
70         @Test(expected = AAIException.class)
71         public void testStoreEventInvalidObjForPojoUtils() throws AAIException {
72                 sne.storeEvent(new EventHeader(), new Object());
73         }
74
75         @Test
76         public void testStoreEventEmptyEventHeader() throws AAIException, JsonGenerationException, JsonMappingException, IOException {
77                 JsonObject object = Json.createObjectBuilder().add("hello", "world").build();
78                 String res = sne.storeEvent(new EventHeader(), object);
79
80                 assertNotNull(res);
81                 assertTrue(res.contains("\"cambria.partition\" : \"" + AAIConstants.UEB_PUB_PARTITION_AAI + "\""));
82                 assertTrue(res.contains("\"event-header\""));
83                 assertTrue(res.contains("\"id\""));
84                 assertTrue(res.contains("\"timestamp\""));
85                 assertTrue(res.contains("\"source-name\" : \"" + AAIConfig.get("aai.notificationEvent.default.sourceName") + "\""));
86                 assertTrue(res.contains("\"domain\" : \"" + AAIConfig.get("aai.notificationEvent.default.domain") + "\""));
87                 assertTrue(res.contains("\"sequence-number\" : \"" + AAIConfig.get("aai.notificationEvent.default.sequenceNumber") + "\""));
88                 assertTrue(res.contains("\"severity\" : \"" + AAIConfig.get("aai.notificationEvent.default.severity") + "\""));
89                 assertTrue(res.contains("\"event-type\" : \"" + AAIConfig.get("aai.notificationEvent.default.eventType") + "\""));
90                 assertTrue(res.contains("\"version\" : \"" + AAIConfig.get("aai.notificationEvent.default.version") + "\""));
91                 assertTrue(res.contains("\"action\" : \"UNK\""));
92                 assertTrue(res.contains("\"entity-link\" : \"UNK\""));
93                 assertTrue(res.contains("\"entity\""));
94                 assertTrue(res.contains("\"hello\""));
95                 assertTrue(res.contains("\"chars\" : \"world\""));
96                 assertTrue(res.contains("\"string\" : \"world\""));
97                 assertTrue(res.contains("\"valueType\" : \"STRING\""));
98         }
99
100         @Test
101         public void testStoreEvent() throws AAIException, JsonGenerationException, JsonMappingException, IOException {
102                 JsonObject object = Json.createObjectBuilder().add("hello", "world").build();
103                 EventHeader eh = new EventHeader();
104                 eh.setId("123");
105                 eh.setTimestamp("current-time");
106                 eh.setEntityLink("entity-link");
107                 eh.setAction("action!");
108                 eh.setEventType("surprise");
109                 eh.setDomain("PROD");
110                 eh.setSourceName("source");
111                 eh.setSequenceNumber("23");
112                 eh.setSeverity("ALERT");
113                 eh.setVersion("v12");
114
115                 String res = sne.storeEvent(eh, object);
116
117                 assertNotNull(res);
118                 assertTrue(res.contains("\"cambria.partition\" : \"" + AAIConstants.UEB_PUB_PARTITION_AAI + "\""));
119                 assertTrue(res.contains("\"event-header\""));
120                 assertTrue(res.contains("\"id\" : \"123\""));
121                 assertTrue(res.contains("\"timestamp\" : \"current-time\""));
122                 assertTrue(res.contains("\"source-name\" : \"source\""));
123                 assertTrue(res.contains("\"domain\" : \"PROD\""));
124                 assertTrue(res.contains("\"sequence-number\" : \"23\""));
125                 assertTrue(res.contains("\"severity\" : \"ALERT\""));
126                 assertTrue(res.contains("\"event-type\" : \"surprise\""));
127                 assertTrue(res.contains("\"version\" : \"v12\""));
128                 assertTrue(res.contains("\"action\" : \"action!\""));
129                 assertTrue(res.contains("\"entity-link\" : \"entity-link\""));
130                 assertTrue(res.contains("\"entity\""));
131                 assertTrue(res.contains("\"hello\""));
132                 assertTrue(res.contains("\"chars\" : \"world\""));
133                 assertTrue(res.contains("\"string\" : \"world\""));
134                 assertTrue(res.contains("\"valueType\" : \"STRING\""));
135         }
136
137         @Test(expected=AAIException.class)
138         public void testStoreDynamicEventNullObj() throws AAIException {
139                 DynamicEntity eventHeader = Mockito.mock(DynamicEntity.class);
140                 DynamicJAXBContext notificationJaxbContext = nodeIngestor.getContextForVersion(schemaVersions.getEdgeLabelVersion());
141                 sne.storeDynamicEvent(notificationJaxbContext, "v12", eventHeader, null);
142         }
143
144         @Test(expected = Exception.class)
145         public void testStoreDynamicEventAAIException() throws Exception {
146
147             DynamicJAXBContext notificationJaxbContext = nodeIngestor.getContextForVersion(schemaVersions.getEdgeLabelVersion());
148             DynamicEntity obj = Mockito.mock(DynamicEntity.class);
149                 DynamicEntity eventHeader = Mockito.mock(DynamicEntity.class);
150                 sne.storeDynamicEvent(notificationJaxbContext, "v12", eventHeader, obj);
151         }
152         
153         @Test(expected = AAIException.class)
154         public void testStoreEventIntrospectorNullObj() throws Exception {
155                 Loader loader = Mockito.mock(Loader.class);
156                 sne.storeEvent(loader, null, null);
157         }
158         
159         @Ignore("Stopped working since the model driven story")
160         @Test
161         public void testStoreEvent1Introspector() throws Exception {
162                 Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getEdgeLabelVersion());
163                 Introspector eventHeader = loader.introspectorFromName("notification-event-header");
164                 eventHeader.setValue("id", "123");
165                 eventHeader.setValue("timestamp", "current-time");
166                 eventHeader.setValue("entity-link", "entity-link");
167                 eventHeader.setValue("action", "action!");
168                 eventHeader.setValue("event-type", "surprise");
169                 eventHeader.setValue("domain", "PROD");
170                 eventHeader.setValue("source-name", "source");
171                 eventHeader.setValue("sequence-number", "23");
172                 eventHeader.setValue("severity", "ALERT");
173                 eventHeader.setValue("version", "v12");
174                 Introspector obj = loader.introspectorFromName("notification-event");
175                 String res = sne.storeEvent(loader, eventHeader, obj);
176                 
177                 assertNotNull(res);
178                 assertTrue(res.contains("\"cambria.partition\":\"" + AAIConstants.UEB_PUB_PARTITION_AAI + "\""));
179                 assertTrue(res.contains("\"event-header\""));
180                 assertTrue(res.contains("\"id\":\"123\""));
181                 assertTrue(res.contains("\"timestamp\":\"current-time\""));
182                 assertTrue(res.contains("\"source-name\":\"source\""));
183                 assertTrue(res.contains("\"domain\":\"PROD\""));
184                 assertTrue(res.contains("\"sequence-number\":\"23\""));
185                 assertTrue(res.contains("\"severity\":\"ALERT\""));
186                 assertTrue(res.contains("\"event-type\":\"surprise\""));
187                 assertTrue(res.contains("\"version\":\"v12\""));
188                 assertTrue(res.contains("\"action\":\"action!\""));
189                 assertTrue(res.contains("\"entity-link\":\"entity-link\""));
190                 assertTrue(res.contains("\"notification-event\""));
191         }
192         
193         @Ignore("Stopped working since the model driven story")
194         @Test
195         public void testStoreEventIntrospectorEmptyEventHeader() throws Exception {
196                 Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getEdgeLabelVersion());
197                 Introspector eventHeader = loader.introspectorFromName("notification-event-header");
198                 Introspector obj = loader.introspectorFromName("notification-event");
199                 
200                 String res = sne.storeEvent(loader, eventHeader, obj);
201                 
202                 assertNotNull(res);
203                 assertTrue(res.contains("\"cambria.partition\":\"" + AAIConstants.UEB_PUB_PARTITION_AAI + "\""));
204                 assertTrue(res.contains("\"event-header\""));
205                 assertTrue(res.contains("\"id\""));
206                 assertTrue(res.contains("\"timestamp\""));
207                 assertTrue(res.contains("\"source-name\":\"" + AAIConfig.get("aai.notificationEvent.default.sourceName") + "\""));
208                 assertTrue(res.contains("\"domain\":\"" + AAIConfig.get("aai.notificationEvent.default.domain") + "\""));
209                 assertTrue(res.contains("\"sequence-number\":\"" + AAIConfig.get("aai.notificationEvent.default.sequenceNumber") + "\""));
210                 assertTrue(res.contains("\"severity\":\"" + AAIConfig.get("aai.notificationEvent.default.severity") + "\""));
211                 assertTrue(res.contains("\"event-type\":\"" + AAIConfig.get("aai.notificationEvent.default.eventType") + "\""));
212                 assertTrue(res.contains("\"version\":\"" + AAIConfig.get("aai.notificationEvent.default.version") + "\""));
213                 assertTrue(res.contains("\"action\":\"UNK\""));
214                 assertTrue(res.contains("\"entity-link\":\"UNK\""));
215                 assertTrue(res.contains("\"notification-event\""));
216         }
217 }