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