Initial TCA commit into DCAEGEN2
[dcaegen2/analytics/tca.git] / dcae-analytics-cdap-plugins / src / test / java / org / openecomp / dcae / apod / analytics / cdap / plugins / validator / JsonPathFilterPluginConfigValidatorTest.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.validator;
22
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.openecomp.dcae.apod.analytics.cdap.plugins.BaseAnalyticsCDAPPluginsUnitTest;
26 import org.openecomp.dcae.apod.analytics.cdap.plugins.domain.config.filter.JsonPathFilterPluginConfig;
27 import org.openecomp.dcae.apod.analytics.cdap.plugins.domain.config.filter.TestJsonPathFilterPluginConfig;
28 import org.openecomp.dcae.apod.analytics.common.validation.GenericValidationResponse;
29
30 import static org.junit.Assert.assertFalse;
31 import static org.junit.Assert.assertTrue;
32
33 /**
34  * @author Rajiv Singla . Creation Date: 3/3/2017.
35  */
36 public class JsonPathFilterPluginConfigValidatorTest extends BaseAnalyticsCDAPPluginsUnitTest {
37
38     private TestJsonPathFilterPluginConfig jsonPathFilterPluginConfig;
39     private JsonPathFilterPluginConfigValidator jsonPathFilterPluginConfigValidator;
40
41     @Before
42     public void before() {
43         jsonPathFilterPluginConfig = getJsonPathFilterPluginConfig();
44         jsonPathFilterPluginConfigValidator = new JsonPathFilterPluginConfigValidator();
45     }
46
47
48     @Test
49     public void testValidateAppSettingsWhenNoValidationErrors() throws Exception {
50         final GenericValidationResponse<JsonPathFilterPluginConfig> validationResponse =
51                 jsonPathFilterPluginConfigValidator.validateAppSettings(jsonPathFilterPluginConfig);
52         assertFalse(validationResponse.hasErrors());
53     }
54
55     @Test
56     public void testValidateAppSettingsWhenFilterMappingsAreEmpty() throws Exception {
57         jsonPathFilterPluginConfig.setJsonFilterMappings("");
58         assertResponseHasErrors(jsonPathFilterPluginConfig, jsonPathFilterPluginConfigValidator);
59     }
60
61     @Test
62     public void testValidateAppSettingsWhenOutputSchemaIsNotPresent() throws Exception {
63         jsonPathFilterPluginConfig.setSchema(null);
64         assertResponseHasErrors(jsonPathFilterPluginConfig, jsonPathFilterPluginConfigValidator);
65     }
66
67     @Test
68     public void testValidateAppSettingsWhenOutputSchemaFilterMatchedFieldIsNotBoolean() throws Exception {
69         final String outputSchemaWithMatchedFieldNotBoolean =
70                 "{\"type\":\"record\"," +
71                         "\"name\":\"etlSchemaBody\",\"fields\":" +
72                         "[" +
73                         "{\"name\":\"ts\",\"type\":\"long\"}," +
74                         "{\"name\":\"filterMatched\",\"type\":[\"string\",\"null\"]}," +
75                         "{\"name\":\"responseCode\",\"type\":\"int\"}," +
76                         "{\"name\":\"responseMessage\",\"type\":\"string\"}," +
77                         "{\"name\":\"message\",\"type\":\"string\"}" +
78                         "]" +
79                         "}";
80         jsonPathFilterPluginConfig.setSchema(outputSchemaWithMatchedFieldNotBoolean);
81         assertResponseHasErrors(jsonPathFilterPluginConfig, jsonPathFilterPluginConfigValidator);
82     }
83
84     @Test
85     public void testValidateAppSettingsWhenOutputSchemaFilterMatchedFieldIsNotNullable() throws Exception {
86         final String outputSchemaWithMatchedFieldNotNullable =
87                 "{\"type\":\"record\"," +
88                         "\"name\":\"etlSchemaBody\",\"fields\":" +
89                         "[" +
90                         "{\"name\":\"ts\",\"type\":\"long\"}," +
91                         "{\"name\":\"filterMatched\",\"type\":\"boolean\"}," +
92                         "{\"name\":\"responseCode\",\"type\":\"int\"}," +
93                         "{\"name\":\"responseMessage\",\"type\":\"string\"}," +
94                         "{\"name\":\"message\",\"type\":\"string\"}" +
95                         "]" +
96                         "}";
97         jsonPathFilterPluginConfig.setSchema(outputSchemaWithMatchedFieldNotNullable);
98         assertResponseHasErrors(jsonPathFilterPluginConfig, jsonPathFilterPluginConfigValidator);
99     }
100
101     private static void assertResponseHasErrors(final TestJsonPathFilterPluginConfig jsonPluginConfig,
102                                                 final JsonPathFilterPluginConfigValidator validator) {
103         final GenericValidationResponse validationResponse = validator.validateAppSettings(jsonPluginConfig);
104         assertTrue(validationResponse.hasErrors());
105     }
106
107 }