2 * ===============================LICENSE_START======================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===========================================
21 package org.openecomp.dcae.apod.analytics.model.util;
23 import com.fasterxml.jackson.core.type.TypeReference;
24 import org.junit.Test;
25 import org.openecomp.dcae.apod.analytics.model.BaseAnalyticsModelUnitTest;
26 import org.openecomp.dcae.apod.analytics.model.domain.cef.EventListener;
28 import java.io.InputStream;
29 import java.util.List;
31 import static org.hamcrest.CoreMatchers.is;
32 import static org.junit.Assert.assertNotNull;
33 import static org.junit.Assert.assertThat;
36 * @author Rajiv Singla . Creation Date: 12/16/2016.
38 public class AnalyticsModelJsonUtilsTest extends BaseAnalyticsModelUnitTest {
40 private static final TypeReference<List<EventListener>> EVENT_LISTENER_TYPE_REFERENCE =
41 new TypeReference<List<EventListener>>() {
44 final String cefMessagesJsonFileLocation = "data/json/cef/cef_messages.json";
45 final String eventListenerJsonFileLocation = "data/json/cef/event_listener.json";
48 public void testReadValueWithTypeReference() throws Exception {
49 final InputStream resourceAsStream =
50 Thread.currentThread().getContextClassLoader().getResourceAsStream(cefMessagesJsonFileLocation);
51 List<EventListener> eventListeners = AnalyticsModelJsonUtils.readValue(resourceAsStream,
52 EVENT_LISTENER_TYPE_REFERENCE);
53 assertThat("Event Listeners size must be 350", eventListeners.size(), is(350));
57 public void testReadValueWithClassType() throws Exception {
58 final InputStream resourceAsStream =
59 Thread.currentThread().getContextClassLoader().getResourceAsStream(eventListenerJsonFileLocation);
60 final EventListener eventListener = AnalyticsModelJsonUtils.readValue(resourceAsStream, EventListener.class);
61 assertNotNull("Event Listener event is not null", eventListener.getEvent());
65 public void testWriteValueAsString() throws Exception {
66 final InputStream resourceAsStream =
67 Thread.currentThread().getContextClassLoader().getResourceAsStream(eventListenerJsonFileLocation);
68 final EventListener eventListener = AnalyticsModelJsonUtils.readValue(resourceAsStream, EventListener.class);
69 final String eventListenerString = AnalyticsModelJsonUtils.writeValueAsString(eventListener);
70 assertJson(fromStream(eventListenerJsonFileLocation), eventListenerString);
74 public void testReadValueWithJsonString() throws Exception {
75 final EventListener eventListener =
76 AnalyticsModelJsonUtils.readValue(fromStream(eventListenerJsonFileLocation), EventListener.class);
77 assertNotNull("Event Listener event is not null", eventListener.getEvent());