6f83653c8d312d8fdf000740245c76576fb5890a
[dcaegen2/analytics/tca.git] / dcae-analytics-tca / src / test / java / org / openecomp / dcae / apod / analytics / tca / processor / TCACEFJsonProcessorTest.java
1 /*
2  * ===============================LICENSE_START======================================
3  *  dcae-analytics
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
21 package org.openecomp.dcae.apod.analytics.tca.processor;
22
23 import org.junit.Test;
24 import org.openecomp.dcae.apod.analytics.common.exception.MessageProcessingException;
25 import org.openecomp.dcae.apod.analytics.model.domain.cef.EventListener;
26 import org.openecomp.dcae.apod.analytics.tca.BaseAnalyticsTCAUnitTest;
27
28 import static org.junit.Assert.assertFalse;
29 import static org.junit.Assert.assertNotNull;
30
31 /**
32  *
33  * @author Rajiv Singla . Creation Date: 11/9/2016.
34  */
35 public class TCACEFJsonProcessorTest extends BaseAnalyticsTCAUnitTest {
36
37
38     // A valid CEF Message
39     @Test
40     public void testCEFJsonProcessorWithValidCEFMessage() throws Exception {
41
42         final String cefMessageString = fromStream(CEF_MESSAGE_JSON_FILE_LOCATION);
43         final TCACEFProcessorContext tcacefProcessorContext =
44                 new TCACEFProcessorContext(cefMessageString, getSampleTCAPolicy());
45
46         TCACEFJsonProcessor tcacefJsonProcessor = new TCACEFJsonProcessor();
47         final TCACEFProcessorContext finalProcessorContext = tcacefJsonProcessor.apply(tcacefProcessorContext);
48
49         final EventListener cefEventListener = finalProcessorContext.getCEFEventListener();
50
51         assertNotNull("CEF Event Listener must be present", cefEventListener);
52
53     }
54
55     // Even if message is not a valid CEF format but still a Json - Json Processor will parse it
56     @Test
57     public void testCEFJsonProcessorWithValidJson() throws Exception {
58
59         final TCACEFProcessorContext tcacefProcessorContext = new TCACEFProcessorContext(
60                 " { \"key\" : \"value\" } ", getSampleTCAPolicy());
61
62         TCACEFJsonProcessor tcacefJsonProcessor = new TCACEFJsonProcessor();
63         final TCACEFProcessorContext finalProcessorContext = tcacefJsonProcessor.apply(tcacefProcessorContext);
64         final EventListener cefEventListener = finalProcessorContext.getCEFEventListener();
65
66         assertNotNull("Even if message is not a valid CEF format but a valid Json.Json Processor must be able to " +
67                         "parse it",
68                 cefEventListener);
69     }
70
71     @Test(expected = MessageProcessingException.class)
72     public void testCEFJsonProcessorWithCEFMessageAsNull() throws Exception {
73
74         final TCACEFProcessorContext tcacefProcessorContext = new TCACEFProcessorContext(null, getSampleTCAPolicy());
75
76         TCACEFJsonProcessor tcacefJsonProcessor = new TCACEFJsonProcessor();
77         tcacefJsonProcessor.apply(tcacefProcessorContext);
78
79     }
80
81     @Test
82     public void testCEFJsonProcessorWithCEFMessageIsBlank() throws Exception {
83
84         final TCACEFProcessorContext tcacefProcessorContext = new TCACEFProcessorContext("   ", getSampleTCAPolicy());
85
86         TCACEFJsonProcessor tcacefJsonProcessor = new TCACEFJsonProcessor();
87         final TCACEFProcessorContext finalProcessorContext = tcacefJsonProcessor.apply(tcacefProcessorContext);
88         assertFalse("Blank message must terminate processing of message chain", finalProcessorContext
89                 .canProcessingContinue());
90     }
91
92
93     @Test
94     public void testCEFJsonProcessorWithCEFMessageWhichIsNotValidMessage() throws Exception {
95
96         final TCACEFProcessorContext tcacefProcessorContext = new TCACEFProcessorContext(" Invalid Message ",
97                 getSampleTCAPolicy());
98
99         TCACEFJsonProcessor tcacefJsonProcessor = new TCACEFJsonProcessor();
100         final TCACEFProcessorContext finalProcessorContext = tcacefJsonProcessor.apply(tcacefProcessorContext);
101         assertFalse("Invalid message must terminate processing of message chain", finalProcessorContext
102                 .canProcessingContinue());
103     }
104
105
106     @Test(expected = MessageProcessingException.class)
107     public void testCEFJsonProcessorWithCEFMessageWhichIsNotValidJson() throws Exception {
108
109         final TCACEFProcessorContext tcacefProcessorContext = new TCACEFProcessorContext(
110                 " { \"Invalid Event Listener Json\" } ", getSampleTCAPolicy());
111
112         TCACEFJsonProcessor tcacefJsonProcessor = new TCACEFJsonProcessor();
113         tcacefJsonProcessor.apply(tcacefProcessorContext);
114     }
115
116
117 }