2 * ===============================LICENSE_START======================================
\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
11 * http://www.apache.org/licenses/LICENSE-2.0
\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
21 package org.openecomp.dcae.apod.analytics.cdap.plugins.utils;
\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
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
35 * @author Rajiv Singla . Creation Date: 1/30/2017.
\r
37 public class CDAPPluginUtilsTest extends BaseAnalyticsCDAPPluginsUnitTest {
\r
41 public void testValidateSchemaContainsFieldsWhenSchemaIsNotNull() throws Exception {
\r
42 final Schema dMaaPMRSinkTestSchema = getDMaaPMRSinkTestSchema();
\r
43 CDAPPluginUtils.validateSchemaContainsFields(dMaaPMRSinkTestSchema, "message");
\r
47 public void testValidateSchemaContainsFieldsWhenInputSchemaIsNull() throws Exception {
\r
48 CDAPPluginUtils.validateSchemaContainsFields(null, "message");
\r
52 public void testCreateStructuredRecord() throws Exception {
\r
53 final StructuredRecord testMessage = CDAPPluginUtils.createDMaaPMRResponseStructuredRecord("testMessage");
\r
54 assertNotNull(testMessage);
\r
59 public void testCreateOutputStructuredRecordBuilder() throws Exception {
\r
61 final String messageFieldName = "message";
\r
62 final String firstInputFieldName = "inputField1";
\r
63 final String secondInputFieldName = "inputField2";
\r
66 final Schema inputSchema = Schema.recordOf(
\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
73 final String addedFieldName = "addedField";
\r
74 final Schema outputSchema = Schema.recordOf(
\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
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
92 final StructuredRecord.Builder outputStructuredRecordBuilder =
\r
93 CDAPPluginUtils.createOutputStructuredRecordBuilder(outputSchema, inputSR);
\r
95 final String addedFieldValue = "Added Field Value";
\r
96 final StructuredRecord outputSR = outputStructuredRecordBuilder
\r
97 .set(addedFieldName, addedFieldValue)
\r
100 assertThat("Added Field field value copied correctly",
\r
101 outputSR.get(addedFieldName).toString(), is(addedFieldValue));
\r
103 assertThat("Output SR has message field copied correctly",
\r
104 outputSR.get(messageFieldName).toString(), is(messageFieldValue));
\r
106 assertThat("First Field value copied correctly",
\r
107 outputSR.get(firstInputFieldName).toString(), is(firstFieldValue));
\r
109 assertNull("Second Field value is null as output schema does not have the field",
\r
110 outputSR.get(secondInputFieldName));
\r
116 public void testAddFieldValueToStructuredRecordBuilder() throws Exception {
\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
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
130 final StructuredRecord.Builder outputSRBuilder = StructuredRecord.builder(outputSchema)
\r
131 .set(messageFieldName, "Some message")
\r
132 .set(firstInputFieldName, firstFieldValue);
\r
134 final StructuredRecord.Builder addedFieldSRBuilder = CDAPPluginUtils.addFieldValueToStructuredRecordBuilder(
\r
135 outputSRBuilder, outputSchema, addedFieldName, addedFieldValue);
\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
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
150 @Test(expected = DCAEAnalyticsRuntimeException.class)
\r
151 public void testValidateSchemaFieldTypeWhenInputSchemaIsNotValidJson() throws Exception {
\r
152 CDAPPluginUtils.validateSchemaFieldType("Invalid Schema", "field1", Schema.Type.STRING);
\r
155 @Test(expected = DCAEAnalyticsRuntimeException.class)
\r
156 public void testSetOutputSchemaWhenOutputSchemaIsNotValidJson() throws Exception {
\r
157 CDAPPluginUtils.setOutputSchema(null, "Invalid output Schema");
\r
160 @Test(expected = DCAEAnalyticsRuntimeException.class)
\r
161 public void testExtractFieldMappingsWhenFieldMappingValueIsEmpty() throws Exception {
\r
162 CDAPPluginUtils.extractFieldMappings("path1:,path2:value2");
\r
165 @Test(expected = DCAEAnalyticsRuntimeException.class)
\r
166 public void testExtractFieldMappingsWhenFieldMappingAreBlank() throws Exception {
\r
167 CDAPPluginUtils.extractFieldMappings("path1: ,path2:value2");
\r