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