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