0050c101f82e6e8841228df22ef2ca2f3ce95c9a
[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 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.util;
23
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.StringWriter;
30 import java.util.HashMap;
31 import java.util.Map;
32
33 import javax.json.Json;
34 import javax.json.JsonObject;
35
36 import org.eclipse.persistence.dynamic.DynamicEntity;
37 import org.eclipse.persistence.dynamic.DynamicType;
38 import org.eclipse.persistence.jaxb.JAXBContextProperties;
39 import org.eclipse.persistence.jaxb.JAXBMarshaller;
40 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
41 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory;
42 import org.junit.BeforeClass;
43 import org.junit.Test;
44 import org.mockito.Mockito;
45 import org.onap.aai.AAISetup;
46 import org.onap.aai.dmaap.AAIDmaapEventJMSProducer;
47 import org.onap.aai.domain.notificationEvent.NotificationEvent.EventHeader;
48 import org.onap.aai.exceptions.AAIException;
49 import org.onap.aai.introspection.Introspector;
50 import org.onap.aai.introspection.Loader;
51 import org.onap.aai.introspection.LoaderFactory;
52 import org.onap.aai.introspection.ModelType;
53 import org.onap.aai.introspection.Version;
54
55 import com.fasterxml.jackson.core.JsonGenerationException;
56 import com.fasterxml.jackson.databind.JsonMappingException;
57
58 public class StoreNotificationEventTest extends AAISetup {
59
60         private static AAIDmaapEventJMSProducer producer;
61         private static StoreNotificationEvent sne;
62
63         @BeforeClass
64         public static void setUp() throws Exception {
65                 producer = Mockito.mock(AAIDmaapEventJMSProducer.class);
66                 sne = new StoreNotificationEvent(producer, "transiationId", "sourceOfTruth");
67         }
68
69         @Test(expected = AAIException.class)
70         public void testStoreEventNullObj() throws AAIException {
71                 sne.storeEvent(new EventHeader(), null);
72         }
73
74         @Test(expected = AAIException.class)
75         public void testStoreEventInvalidObjForPojoUtils() throws AAIException {
76                 sne.storeEvent(new EventHeader(), new Object());
77         }
78
79         @Test
80         public void testStoreEventEmptyEventHeader() throws AAIException, JsonGenerationException, JsonMappingException, IOException {
81                 JsonObject object = Json.createObjectBuilder().add("hello", "world").build();
82                 String res = sne.storeEvent(new EventHeader(), object);
83
84                 assertNotNull(res);
85                 assertTrue(res.contains("\"cambria.partition\" : \"" + AAIConstants.UEB_PUB_PARTITION_AAI + "\""));
86                 assertTrue(res.contains("\"event-header\""));
87                 assertTrue(res.contains("\"id\""));
88                 assertTrue(res.contains("\"timestamp\""));
89                 assertTrue(res.contains("\"source-name\" : \"" + AAIConfig.get("aai.notificationEvent.default.sourceName") + "\""));
90                 assertTrue(res.contains("\"domain\" : \"" + AAIConfig.get("aai.notificationEvent.default.domain") + "\""));
91                 assertTrue(res.contains("\"sequence-number\" : \"" + AAIConfig.get("aai.notificationEvent.default.sequenceNumber") + "\""));
92                 assertTrue(res.contains("\"severity\" : \"" + AAIConfig.get("aai.notificationEvent.default.severity") + "\""));
93                 assertTrue(res.contains("\"event-type\" : \"" + AAIConfig.get("aai.notificationEvent.default.eventType") + "\""));
94                 assertTrue(res.contains("\"version\" : \"" + AAIConfig.get("aai.notificationEvent.default.version") + "\""));
95                 assertTrue(res.contains("\"action\" : \"UNK\""));
96                 assertTrue(res.contains("\"entity-link\" : \"UNK\""));
97                 assertTrue(res.contains("\"entity\""));
98                 assertTrue(res.contains("\"hello\""));
99                 assertTrue(res.contains("\"chars\" : \"world\""));
100                 assertTrue(res.contains("\"string\" : \"world\""));
101                 assertTrue(res.contains("\"valueType\" : \"STRING\""));
102         }
103
104         @Test
105         public void testStoreEvent() throws AAIException, JsonGenerationException, JsonMappingException, IOException {
106                 JsonObject object = Json.createObjectBuilder().add("hello", "world").build();
107                 EventHeader eh = new EventHeader();
108                 eh.setId("123");
109                 eh.setTimestamp("current-time");
110                 eh.setEntityLink("entity-link");
111                 eh.setAction("action!");
112                 eh.setEventType("surprise");
113                 eh.setDomain("PROD");
114                 eh.setSourceName("source");
115                 eh.setSequenceNumber("23");
116                 eh.setSeverity("ALERT");
117                 eh.setVersion("v11");
118
119                 String res = sne.storeEvent(eh, object);
120
121                 assertNotNull(res);
122                 assertTrue(res.contains("\"cambria.partition\" : \"" + AAIConstants.UEB_PUB_PARTITION_AAI + "\""));
123                 assertTrue(res.contains("\"event-header\""));
124                 assertTrue(res.contains("\"id\" : \"123\""));
125                 assertTrue(res.contains("\"timestamp\" : \"current-time\""));
126                 assertTrue(res.contains("\"source-name\" : \"source\""));
127                 assertTrue(res.contains("\"domain\" : \"PROD\""));
128                 assertTrue(res.contains("\"sequence-number\" : \"23\""));
129                 assertTrue(res.contains("\"severity\" : \"ALERT\""));
130                 assertTrue(res.contains("\"event-type\" : \"surprise\""));
131                 assertTrue(res.contains("\"version\" : \"v11\""));
132                 assertTrue(res.contains("\"action\" : \"action!\""));
133                 assertTrue(res.contains("\"entity-link\" : \"entity-link\""));
134                 assertTrue(res.contains("\"entity\""));
135                 assertTrue(res.contains("\"hello\""));
136                 assertTrue(res.contains("\"chars\" : \"world\""));
137                 assertTrue(res.contains("\"string\" : \"world\""));
138                 assertTrue(res.contains("\"valueType\" : \"STRING\""));
139         }
140
141         @Test(expected=AAIException.class)
142         public void testStoreDynamicEventNullObj() throws AAIException {
143                 DynamicEntity eventHeader = Mockito.mock(DynamicEntity.class);
144                 DynamicJAXBContext notificationJaxbContext = Mockito.mock(DynamicJAXBContext.class);
145                 sne.storeDynamicEvent(notificationJaxbContext, "v11", eventHeader, null);
146         }
147
148         @Test(expected = AAIException.class)
149         public void testStoreDynamicEventAAIException() throws Exception {
150                 DynamicEntity eventHeader = Mockito.mock(DynamicEntity.class);
151                 DynamicEntity obj = Mockito.mock(DynamicEntity.class);
152                 DynamicJAXBContext notificationJaxbContext = Mockito.mock(DynamicJAXBContext.class);
153                 ClassLoader cl = getClass().getClassLoader();
154                 InputStream is = cl.getResourceAsStream("oxm/aai_oxm_v11.xml");
155                 Map<String, Object> properties = new HashMap<String, Object>();
156                 properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, is);
157                 DynamicJAXBContext notificationJaxbContextReal = DynamicJAXBContextFactory.createContextFromOXM(cl, properties);
158                 DynamicType dtReal = notificationJaxbContextReal.getDynamicType("inventory.aai.onap.org.v11.NotificationEvent");
159                 DynamicType dt = Mockito.mock(DynamicType.class);
160                 DynamicEntity notificationEventReal = dtReal.newDynamicEntity();
161                 JAXBMarshaller marshaller = Mockito.mock(JAXBMarshaller.class);
162                 
163                 Mockito.when(notificationJaxbContext.getDynamicType(Mockito.anyString())).thenReturn(dt);
164                 Mockito.when(dt.newDynamicEntity()).thenReturn(notificationEventReal);
165                 Mockito.when(notificationJaxbContext.createMarshaller()).thenReturn(marshaller);
166                 Mockito.doNothing().when(marshaller).marshal(Mockito.any(DynamicJAXBContext.class), Mockito.any(StringWriter.class));
167
168                 sne.storeDynamicEvent(notificationJaxbContext, "v11", eventHeader, obj);
169         }
170         
171         @Test(expected = AAIException.class)
172         public void testStoreEventIntrospectorNullObj() throws Exception {
173                 Loader loader = Mockito.mock(Loader.class);
174                 sne.storeEvent(loader, null, null);
175         }
176         
177         @Test
178         public void testStoreEventIntrospector() throws Exception {
179                 Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.v11);
180                 Introspector eventHeader = loader.introspectorFromName("notification-event-header");
181                 eventHeader.setValue("id", "123");
182                 eventHeader.setValue("timestamp", "current-time");
183                 eventHeader.setValue("entity-link", "entity-link");
184                 eventHeader.setValue("action", "action!");
185                 eventHeader.setValue("event-type", "surprise");
186                 eventHeader.setValue("domain", "PROD");
187                 eventHeader.setValue("source-name", "source");
188                 eventHeader.setValue("sequence-number", "23");
189                 eventHeader.setValue("severity", "ALERT");
190                 eventHeader.setValue("version", "v11");
191                 
192                 Introspector obj = loader.introspectorFromName("notification-event");
193                 
194                 String res = sne.storeEvent(loader, eventHeader, obj);
195                 
196                 assertNotNull(res);
197                 assertTrue(res.contains("\"cambria.partition\":\"" + AAIConstants.UEB_PUB_PARTITION_AAI + "\""));
198                 assertTrue(res.contains("\"event-header\""));
199                 assertTrue(res.contains("\"id\":\"123\""));
200                 assertTrue(res.contains("\"timestamp\":\"current-time\""));
201                 assertTrue(res.contains("\"source-name\":\"source\""));
202                 assertTrue(res.contains("\"domain\":\"PROD\""));
203                 assertTrue(res.contains("\"sequence-number\":\"23\""));
204                 assertTrue(res.contains("\"severity\":\"ALERT\""));
205                 assertTrue(res.contains("\"event-type\":\"surprise\""));
206                 assertTrue(res.contains("\"version\":\"v11\""));
207                 assertTrue(res.contains("\"action\":\"action!\""));
208                 assertTrue(res.contains("\"entity-link\":\"entity-link\""));
209                 assertTrue(res.contains("\"notification-event\""));
210         }
211         
212         @Test
213         public void testStoreEventIntrospectorEmptyEventHeader() throws Exception {
214                 Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.v11);
215                 Introspector eventHeader = loader.introspectorFromName("notification-event-header");
216                 Introspector obj = loader.introspectorFromName("notification-event");
217                 
218                 String res = sne.storeEvent(loader, eventHeader, obj);
219                 
220                 assertNotNull(res);
221                 assertTrue(res.contains("\"cambria.partition\":\"" + AAIConstants.UEB_PUB_PARTITION_AAI + "\""));
222                 assertTrue(res.contains("\"event-header\""));
223                 assertTrue(res.contains("\"id\""));
224                 assertTrue(res.contains("\"timestamp\""));
225                 assertTrue(res.contains("\"source-name\":\"" + AAIConfig.get("aai.notificationEvent.default.sourceName") + "\""));
226                 assertTrue(res.contains("\"domain\":\"" + AAIConfig.get("aai.notificationEvent.default.domain") + "\""));
227                 assertTrue(res.contains("\"sequence-number\":\"" + AAIConfig.get("aai.notificationEvent.default.sequenceNumber") + "\""));
228                 assertTrue(res.contains("\"severity\":\"" + AAIConfig.get("aai.notificationEvent.default.severity") + "\""));
229                 assertTrue(res.contains("\"event-type\":\"" + AAIConfig.get("aai.notificationEvent.default.eventType") + "\""));
230                 assertTrue(res.contains("\"version\":\"" + AAIConfig.get("aai.notificationEvent.default.version") + "\""));
231                 assertTrue(res.contains("\"action\":\"UNK\""));
232                 assertTrue(res.contains("\"entity-link\":\"UNK\""));
233                 assertTrue(res.contains("\"notification-event\""));
234         }
235 }