Initial TCA commit into DCAEGEN2
[dcaegen2/analytics/tca.git] / dcae-analytics-cdap-plugins / src / test / java / org / openecomp / dcae / apod / analytics / cdap / plugins / transform / filter / JsonPathFilterTest.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.cdap.plugins.transform.filter;
22
23 import co.cask.cdap.api.data.format.StructuredRecord;
24 import co.cask.cdap.api.data.schema.Schema;
25 import co.cask.cdap.etl.api.Emitter;
26 import co.cask.cdap.etl.api.PipelineConfigurer;
27 import co.cask.cdap.etl.api.StageConfigurer;
28 import org.junit.Test;
29 import org.mockito.ArgumentMatchers;
30 import org.mockito.Mockito;
31 import org.openecomp.dcae.apod.analytics.cdap.plugins.BaseAnalyticsCDAPPluginsUnitTest;
32
33 import java.util.Date;
34
35 import static org.mockito.ArgumentMatchers.any;
36 import static org.mockito.Mockito.doNothing;
37 import static org.mockito.Mockito.mock;
38 import static org.mockito.Mockito.times;
39 import static org.mockito.Mockito.verify;
40 import static org.mockito.Mockito.when;
41
42 /**
43  * @author Rajiv Singla . Creation Date: 3/3/2017.
44  */
45 public class JsonPathFilterTest extends BaseAnalyticsCDAPPluginsUnitTest {
46
47
48     @Test
49     public void testInitializeWhenFilterMappingIsValid() throws Exception {
50         final JsonPathFilter jsonPathFilter = new JsonPathFilter(getJsonPathFilterPluginConfig());
51         jsonPathFilter.initialize(null);
52     }
53
54
55     @Test
56     public void configurePipeline() throws Exception {
57         final JsonPathFilter jsonPathFilter = new JsonPathFilter(getJsonPathFilterPluginConfig());
58         final PipelineConfigurer pipelineConfigurer = mock(PipelineConfigurer.class);
59         final StageConfigurer stageConfigurer = mock(StageConfigurer.class);
60         when(pipelineConfigurer.getStageConfigurer()).thenReturn(stageConfigurer);
61         when(stageConfigurer.getInputSchema()).thenReturn(getSimpleTCAPluginInputSchema());
62         doNothing().when(stageConfigurer).setOutputSchema(any(Schema.class));
63         jsonPathFilter.configurePipeline(pipelineConfigurer);
64         verify(stageConfigurer, times(1)).setOutputSchema(any(Schema.class));
65     }
66
67     @Test
68     public void testTransform() throws Exception {
69         final JsonPathFilter jsonPathFilter = new JsonPathFilter(getJsonPathFilterPluginConfig());
70         jsonPathFilter.initialize(null);
71         final StructuredRecord inputSR = StructuredRecord.builder(getJsonFilterPluginInputSchema())
72                 .set("ts", new Date().getTime())
73                 .set("responseCode", 200)
74                 .set("responseMessage", "OK")
75                 .set("message", fromStream(CEF_MESSAGE_JSON_FILE_LOCATION))
76                 .build();
77
78         final Emitter emitter = Mockito.mock(Emitter.class);
79         doNothing().when(emitter).emit(ArgumentMatchers.any(StructuredRecord.class));
80         jsonPathFilter.transform(inputSR, emitter);
81         verify(emitter, times(1)).emit(any(StructuredRecord.class));
82     }
83
84 }