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