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 / utils / CDAPPluginUtilsTest.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.utils;\r
22 \r
23 import co.cask.cdap.api.data.format.StructuredRecord;\r
24 import co.cask.cdap.api.data.schema.Schema;\r
25 import org.junit.Test;\r
26 import org.openecomp.dcae.apod.analytics.cdap.plugins.BaseAnalyticsCDAPPluginsUnitTest;\r
27 import org.openecomp.dcae.apod.analytics.common.exception.DCAEAnalyticsRuntimeException;\r
28 \r
29 import static org.hamcrest.CoreMatchers.is;\r
30 import static org.junit.Assert.assertNotNull;\r
31 import static org.junit.Assert.assertNull;\r
32 import static org.junit.Assert.assertThat;\r
33 \r
34 /**\r
35  * @author Rajiv Singla . Creation Date: 1/30/2017.\r
36  */\r
37 public class CDAPPluginUtilsTest extends BaseAnalyticsCDAPPluginsUnitTest {\r
38 \r
39 \r
40     @Test\r
41     public void testValidateSchemaContainsFieldsWhenSchemaIsNotNull() throws Exception {\r
42         final Schema dMaaPMRSinkTestSchema = getDMaaPMRSinkTestSchema();\r
43         CDAPPluginUtils.validateSchemaContainsFields(dMaaPMRSinkTestSchema, "message");\r
44     }\r
45 \r
46     @Test\r
47     public void testValidateSchemaContainsFieldsWhenInputSchemaIsNull() throws Exception {\r
48         CDAPPluginUtils.validateSchemaContainsFields(null, "message");\r
49     }\r
50 \r
51     @Test\r
52     public void testCreateStructuredRecord() throws Exception {\r
53         final StructuredRecord testMessage = CDAPPluginUtils.createDMaaPMRResponseStructuredRecord("testMessage");\r
54         assertNotNull(testMessage);\r
55     }\r
56 \r
57 \r
58     @Test\r
59     public void testCreateOutputStructuredRecordBuilder() throws Exception {\r
60 \r
61         final String messageFieldName = "message";\r
62         final String firstInputFieldName = "inputField1";\r
63         final String secondInputFieldName = "inputField2";\r
64 \r
65 \r
66         final Schema inputSchema = Schema.recordOf(\r
67                 "inputSchema",\r
68                 Schema.Field.of(messageFieldName, Schema.of(Schema.Type.STRING)),\r
69                 Schema.Field.of(firstInputFieldName, Schema.nullableOf(Schema.of(Schema.Type.STRING))),\r
70                 Schema.Field.of(secondInputFieldName, Schema.nullableOf(Schema.of(Schema.Type.STRING)))\r
71         );\r
72 \r
73         final String addedFieldName = "addedField";\r
74         final Schema outputSchema = Schema.recordOf(\r
75                 "outputSchema",\r
76                 Schema.Field.of(messageFieldName, Schema.of(Schema.Type.STRING)),\r
77                 Schema.Field.of(firstInputFieldName, Schema.nullableOf(Schema.of(Schema.Type.STRING))),\r
78                 Schema.Field.of(addedFieldName, Schema.nullableOf(Schema.of(Schema.Type.STRING))) // added field\r
79                 // missing second Input Field\r
80         );\r
81 \r
82         // input structured record\r
83         final String messageFieldValue = "Message String";\r
84         final String firstFieldValue = "Input Field 1";\r
85         final String secondFieldValue = "Input Field 2";\r
86         final StructuredRecord inputSR = StructuredRecord.builder(inputSchema)\r
87                 .set(messageFieldName, messageFieldValue)\r
88                 .set(firstInputFieldName, firstFieldValue)\r
89                 .set(secondInputFieldName, secondFieldValue)\r
90                 .build();\r
91 \r
92         final StructuredRecord.Builder outputStructuredRecordBuilder =\r
93                 CDAPPluginUtils.createOutputStructuredRecordBuilder(outputSchema, inputSR);\r
94 \r
95         final String addedFieldValue = "Added Field Value";\r
96         final StructuredRecord outputSR = outputStructuredRecordBuilder\r
97                 .set(addedFieldName, addedFieldValue)\r
98                 .build();\r
99 \r
100         assertThat("Added Field field value copied correctly",\r
101                 outputSR.get(addedFieldName).toString(), is(addedFieldValue));\r
102 \r
103         assertThat("Output SR has message field copied correctly",\r
104                 outputSR.get(messageFieldName).toString(), is(messageFieldValue));\r
105 \r
106         assertThat("First Field value copied correctly",\r
107                 outputSR.get(firstInputFieldName).toString(), is(firstFieldValue));\r
108 \r
109         assertNull("Second Field value is null as output schema does not have the field",\r
110                 outputSR.get(secondInputFieldName));\r
111 \r
112     }\r
113 \r
114 \r
115     @Test\r
116     public void testAddFieldValueToStructuredRecordBuilder() throws Exception {\r
117 \r
118         final String messageFieldName = "message";\r
119         final String firstInputFieldName = "inputField1";\r
120         final String addedFieldName = "addedField";\r
121         final String firstFieldValue = "Input Field 1";\r
122         final String addedFieldValue = "Added Field Value";\r
123         final Schema outputSchema = Schema.recordOf(\r
124                 "outputSchema",\r
125                 Schema.Field.of(messageFieldName, Schema.of(Schema.Type.STRING)),\r
126                 Schema.Field.of(firstInputFieldName, Schema.nullableOf(Schema.of(Schema.Type.STRING))),\r
127                 Schema.Field.of(addedFieldName, Schema.nullableOf(Schema.of(Schema.Type.STRING))) // added field\r
128         );\r
129 \r
130         final StructuredRecord.Builder outputSRBuilder = StructuredRecord.builder(outputSchema)\r
131                 .set(messageFieldName, "Some message")\r
132                 .set(firstInputFieldName, firstFieldValue);\r
133 \r
134         final StructuredRecord.Builder addedFieldSRBuilder = CDAPPluginUtils.addFieldValueToStructuredRecordBuilder(\r
135                 outputSRBuilder, outputSchema, addedFieldName, addedFieldValue);\r
136 \r
137         // Try adding field to output Structured record that is not in output schema\r
138         final String nonExistentFieldName = "fieldNotInOutputSchema";\r
139         final String nonExistentFieldValue = "Some Value";\r
140         final StructuredRecord outputSR = CDAPPluginUtils.addFieldValueToStructuredRecordBuilder(\r
141                 addedFieldSRBuilder, outputSchema, nonExistentFieldName, nonExistentFieldValue).build();\r
142 \r
143         assertThat("Output SR must contain added Field which is in output schema",\r
144                 outputSR.get(addedFieldName).toString(), is(addedFieldValue));\r
145         assertNull("Output SR must not contain field that is not in output schema",\r
146                 outputSR.get(nonExistentFieldName));\r
147 \r
148     }\r
149 \r
150     @Test(expected = DCAEAnalyticsRuntimeException.class)\r
151     public void testValidateSchemaFieldTypeWhenInputSchemaIsNotValidJson() throws Exception {\r
152         CDAPPluginUtils.validateSchemaFieldType("Invalid Schema", "field1", Schema.Type.STRING);\r
153     }\r
154 \r
155     @Test(expected = DCAEAnalyticsRuntimeException.class)\r
156     public void testSetOutputSchemaWhenOutputSchemaIsNotValidJson() throws Exception {\r
157         CDAPPluginUtils.setOutputSchema(null, "Invalid output Schema");\r
158     }\r
159 \r
160     @Test(expected = DCAEAnalyticsRuntimeException.class)\r
161     public void testExtractFieldMappingsWhenFieldMappingValueIsEmpty() throws Exception {\r
162         CDAPPluginUtils.extractFieldMappings("path1:,path2:value2");\r
163     }\r
164 \r
165     @Test(expected = DCAEAnalyticsRuntimeException.class)\r
166     public void testExtractFieldMappingsWhenFieldMappingAreBlank() throws Exception {\r
167         CDAPPluginUtils.extractFieldMappings("path1: ,path2:value2");\r
168     }\r
169 \r
170 \r
171 }\r