7708dcdf8c1a39326d57850f03cdf1c7446617e4
[dcaegen2/analytics/tca.git] / dcae-analytics-cdap-plugins / src / test / java / org / openecomp / dcae / apod / analytics / cdap / plugins / validator / JsonPathFilterPluginConfigValidatorTest.java
1 /*\r
2  * ===============================LICENSE_START======================================\r
3  *  dcae-analytics\r
4  * ================================================================================\r
5  *    Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  *  Licensed under the Apache License, Version 2.0 (the "License");\r
8  *  you may not use this file except in compliance with the License.\r
9  *   You may obtain a copy of the License at\r
10  *\r
11  *          http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  *  Unless required by applicable law or agreed to in writing, software\r
14  *  distributed under the License is distributed on an "AS IS" BASIS,\r
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  *  See the License for the specific language governing permissions and\r
17  *  limitations under the License.\r
18  *  ============================LICENSE_END===========================================\r
19  */\r
20 \r
21 package org.openecomp.dcae.apod.analytics.cdap.plugins.validator;\r
22 \r
23 import org.junit.Before;\r
24 import org.junit.Test;\r
25 import org.openecomp.dcae.apod.analytics.cdap.plugins.BaseAnalyticsCDAPPluginsUnitTest;\r
26 import org.openecomp.dcae.apod.analytics.cdap.plugins.domain.config.filter.JsonPathFilterPluginConfig;\r
27 import org.openecomp.dcae.apod.analytics.cdap.plugins.domain.config.filter.TestJsonPathFilterPluginConfig;\r
28 import org.openecomp.dcae.apod.analytics.common.validation.GenericValidationResponse;\r
29 \r
30 import static org.junit.Assert.assertFalse;\r
31 import static org.junit.Assert.assertTrue;\r
32 \r
33 /**\r
34  * @author Rajiv Singla . Creation Date: 3/3/2017.\r
35  */\r
36 public class JsonPathFilterPluginConfigValidatorTest extends BaseAnalyticsCDAPPluginsUnitTest {\r
37 \r
38     private TestJsonPathFilterPluginConfig jsonPathFilterPluginConfig;\r
39     private JsonPathFilterPluginConfigValidator jsonPathFilterPluginConfigValidator;\r
40 \r
41     @Before\r
42     public void before() {\r
43         jsonPathFilterPluginConfig = getJsonPathFilterPluginConfig();\r
44         jsonPathFilterPluginConfigValidator = new JsonPathFilterPluginConfigValidator();\r
45     }\r
46 \r
47 \r
48     @Test\r
49     public void testValidateAppSettingsWhenNoValidationErrors() throws Exception {\r
50         final GenericValidationResponse<JsonPathFilterPluginConfig> validationResponse =\r
51                 jsonPathFilterPluginConfigValidator.validateAppSettings(jsonPathFilterPluginConfig);\r
52         assertFalse(validationResponse.hasErrors());\r
53     }\r
54 \r
55     @Test\r
56     public void testValidateAppSettingsWhenFilterMappingsAreEmpty() throws Exception {\r
57         jsonPathFilterPluginConfig.setJsonFilterMappings("");\r
58         assertResponseHasErrors(jsonPathFilterPluginConfig, jsonPathFilterPluginConfigValidator);\r
59     }\r
60 \r
61     @Test\r
62     public void testValidateAppSettingsWhenOutputSchemaIsNotPresent() throws Exception {\r
63         jsonPathFilterPluginConfig.setSchema(null);\r
64         assertResponseHasErrors(jsonPathFilterPluginConfig, jsonPathFilterPluginConfigValidator);\r
65     }\r
66 \r
67     @Test\r
68     public void testValidateAppSettingsWhenOutputSchemaFilterMatchedFieldIsNotBoolean() throws Exception {\r
69         final String outputSchemaWithMatchedFieldNotBoolean =\r
70                 "{\"type\":\"record\"," +\r
71                         "\"name\":\"etlSchemaBody\",\"fields\":" +\r
72                         "[" +\r
73                         "{\"name\":\"ts\",\"type\":\"long\"}," +\r
74                         "{\"name\":\"filterMatched\",\"type\":[\"string\",\"null\"]}," +\r
75                         "{\"name\":\"responseCode\",\"type\":\"int\"}," +\r
76                         "{\"name\":\"responseMessage\",\"type\":\"string\"}," +\r
77                         "{\"name\":\"message\",\"type\":\"string\"}" +\r
78                         "]" +\r
79                         "}";\r
80         jsonPathFilterPluginConfig.setSchema(outputSchemaWithMatchedFieldNotBoolean);\r
81         assertResponseHasErrors(jsonPathFilterPluginConfig, jsonPathFilterPluginConfigValidator);\r
82     }\r
83 \r
84     @Test\r
85     public void testValidateAppSettingsWhenOutputSchemaFilterMatchedFieldIsNotNullable() throws Exception {\r
86         final String outputSchemaWithMatchedFieldNotNullable =\r
87                 "{\"type\":\"record\"," +\r
88                         "\"name\":\"etlSchemaBody\",\"fields\":" +\r
89                         "[" +\r
90                         "{\"name\":\"ts\",\"type\":\"long\"}," +\r
91                         "{\"name\":\"filterMatched\",\"type\":\"boolean\"}," +\r
92                         "{\"name\":\"responseCode\",\"type\":\"int\"}," +\r
93                         "{\"name\":\"responseMessage\",\"type\":\"string\"}," +\r
94                         "{\"name\":\"message\",\"type\":\"string\"}" +\r
95                         "]" +\r
96                         "}";\r
97         jsonPathFilterPluginConfig.setSchema(outputSchemaWithMatchedFieldNotNullable);\r
98         assertResponseHasErrors(jsonPathFilterPluginConfig, jsonPathFilterPluginConfigValidator);\r
99     }\r
100 \r
101     private static void assertResponseHasErrors(final TestJsonPathFilterPluginConfig jsonPluginConfig,\r
102                                                 final JsonPathFilterPluginConfigValidator validator) {\r
103         final GenericValidationResponse validationResponse = validator.validateAppSettings(jsonPluginConfig);\r
104         assertTrue(validationResponse.hasErrors());\r
105     }\r
106 \r
107 }\r