TCA: Support for VES/A&AI enrichment
[dcaegen2/analytics/tca.git] / dcae-analytics-cdap-plugins / src / test / java / org / openecomp / dcae / apod / analytics / cdap / plugins / validator / SimpleTCAPluginConfigValidatorTest.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.tca.SimpleTCAPluginConfig;\r
27 import org.openecomp.dcae.apod.analytics.cdap.plugins.domain.config.tca.TestSimpleTCAPluginConfig;\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: 2/21/2017.\r
35  */\r
36 public class SimpleTCAPluginConfigValidatorTest extends BaseAnalyticsCDAPPluginsUnitTest {\r
37 \r
38     private TestSimpleTCAPluginConfig testSimpleTCAPluginConfig;\r
39     private SimpleTCAPluginConfigValidator simpleTCAPluginConfigValidator;\r
40 \r
41     @Before\r
42     public void before() {\r
43         testSimpleTCAPluginConfig = getTestSimpleTCAPluginConfig();\r
44         simpleTCAPluginConfigValidator = new SimpleTCAPluginConfigValidator();\r
45     }\r
46 \r
47     @Test\r
48     public void testValidateAppSettingsWhenAllSettingsAreValid() throws Exception {\r
49         final GenericValidationResponse<SimpleTCAPluginConfig> validationResponse =\r
50                 simpleTCAPluginConfigValidator.validateAppSettings(testSimpleTCAPluginConfig);\r
51         assertFalse(validationResponse.hasErrors());\r
52     }\r
53 \r
54     @Test\r
55     public void testValidateAppSettingsWhenVESMessageFieldNameIsMissing() throws Exception {\r
56         testSimpleTCAPluginConfig.setVesMessageFieldName(null);\r
57         assertResponseHasErrors(testSimpleTCAPluginConfig, simpleTCAPluginConfigValidator);\r
58     }\r
59 \r
60     @Test\r
61     public void testValidateAppSettingsWhenPolicyJsonIsMissing() throws Exception {\r
62         testSimpleTCAPluginConfig.setPolicyJson(null);\r
63         assertResponseHasErrors(testSimpleTCAPluginConfig, simpleTCAPluginConfigValidator);\r
64     }\r
65 \r
66     @Test\r
67     public void testValidateAppSettingsWhenAlertFieldNameIsMissing() throws Exception {\r
68         testSimpleTCAPluginConfig.setAlertFieldName(null);\r
69         assertResponseHasErrors(testSimpleTCAPluginConfig, simpleTCAPluginConfigValidator);\r
70     }\r
71 \r
72     @Test\r
73     public void testValidateAppSettingsWhenOutputSchemaIsNull() throws Exception {\r
74         testSimpleTCAPluginConfig.setSchema(null);\r
75         assertResponseHasErrors(testSimpleTCAPluginConfig, simpleTCAPluginConfigValidator);\r
76     }\r
77 \r
78     @Test\r
79     public void testValidateAppSettingsWhenMessageTypeFieldNameIsMissing() throws Exception {\r
80         testSimpleTCAPluginConfig.setMessageTypeFieldName(null);\r
81         assertResponseHasErrors(testSimpleTCAPluginConfig, simpleTCAPluginConfigValidator);\r
82     }\r
83 \r
84     @Test\r
85     public void testValidateAppSettingsWhenAlertFieldIsNullableInOutputSchema() throws Exception {\r
86         testSimpleTCAPluginConfig.setSchema(\r
87                 "{\"type\":\"record\"," +\r
88                         "\"name\":\"etlSchemaBody\"," +\r
89                         "\"fields\":[" +\r
90                             "{\"name\":\"ts\",\"type\":\"long\"}," +\r
91                             "{\"name\":\"responseCode\",\"type\":\"int\"}," +\r
92                             "{\"name\":\"responseMessage\",\"type\":\"string\"}," +\r
93                             "{\"name\":\"message\",\"type\":\"string\"}," +\r
94                             "{\"name\":\"alert\",\"type\":[\"string\",\"null\"]}," +\r
95                             "{\"name\":\"tcaMessageType\",\"type\":\"string\"}]}");\r
96         final GenericValidationResponse<SimpleTCAPluginConfig> validationResponse =\r
97                 simpleTCAPluginConfigValidator.validateAppSettings(testSimpleTCAPluginConfig);\r
98         assertFalse(validationResponse.hasErrors());\r
99 \r
100     }\r
101 \r
102     @Test\r
103     public void testValidateAppSettingsWhenAlertFieldIsNotPresentInOutputSchema() throws Exception {\r
104         testSimpleTCAPluginConfig.setSchema(\r
105                 "{\"type\":\"record\"," +\r
106                         "\"name\":\"etlSchemaBody\"," +\r
107                         "\"fields\":[" +\r
108                         "{\"name\":\"ts\",\"type\":\"long\"}," +\r
109                         "{\"name\":\"responseCode\",\"type\":\"int\"}," +\r
110                         "{\"name\":\"responseMessage\",\"type\":\"string\"}," +\r
111                         "{\"name\":\"message\",\"type\":\"string\"}," +\r
112                         "{\"name\":\"tcaMessageType\",\"type\":\"string\"}]}");\r
113         final GenericValidationResponse<SimpleTCAPluginConfig> validationResponse =\r
114                 simpleTCAPluginConfigValidator.validateAppSettings(testSimpleTCAPluginConfig);\r
115         assertFalse(validationResponse.hasErrors());\r
116 \r
117     }\r
118 \r
119     @Test\r
120     public void testValidateAppSettingsWhenAlertFieldIsNullableButNotStringTypeInOutputSchema() throws Exception {\r
121         testSimpleTCAPluginConfig.setSchema(\r
122                 "{\"type\":\"record\"," +\r
123                         "\"name\":\"etlSchemaBody\"," +\r
124                         "\"fields\":[" +\r
125                         "{\"name\":\"ts\",\"type\":\"long\"}," +\r
126                         "{\"name\":\"responseCode\",\"type\":\"int\"}," +\r
127                         "{\"name\":\"responseMessage\",\"type\":\"string\"}," +\r
128                         "{\"name\":\"message\",\"type\":\"string\"}," +\r
129                         "{\"name\":\"alert\",\"type\":[\"int\",\"null\"]}," +\r
130                         "{\"name\":\"tcaMessageType\",\"type\":\"string\"}]}");\r
131         assertResponseHasErrors(testSimpleTCAPluginConfig, simpleTCAPluginConfigValidator);\r
132     }\r
133 \r
134     @Test\r
135     public void testValidateAppSettingsWhenAlertFieldNameIsNotNullableInOutputSchema() throws Exception {\r
136         testSimpleTCAPluginConfig.setSchema(\r
137                 "{\"type\":\"record\"," +\r
138                         "\"name\":\"etlSchemaBody\"," +\r
139                         "\"fields\":[" +\r
140                         "{\"name\":\"ts\",\"type\":\"long\"}," +\r
141                         "{\"name\":\"responseCode\",\"type\":\"int\"}," +\r
142                         "{\"name\":\"responseMessage\",\"type\":\"string\"}," +\r
143                         "{\"name\":\"message\",\"type\":\"string\"}," +\r
144                         "{\"name\":\"alert\",\"type\":\"string\"}," +\r
145                         "{\"name\":\"tcaMessageType\",\"type\":\"string\"}]}");\r
146         assertResponseHasErrors(testSimpleTCAPluginConfig, simpleTCAPluginConfigValidator);\r
147     }\r
148 \r
149 \r
150 \r
151     private static void assertResponseHasErrors(final TestSimpleTCAPluginConfig pluginConfig,\r
152                                                 final SimpleTCAPluginConfigValidator validator) {\r
153         final GenericValidationResponse validationResponse = validator.validateAppSettings(pluginConfig);\r
154         assertTrue(validationResponse.hasErrors());\r
155         LOG.debug("Validation Error Message: {}", validationResponse.getAllErrorMessage());\r
156     }\r
157 }\r