/*
* Copyright 2017 StreamSets Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.streamsets.pipeline.stage.processor.fieldtypeconverter;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.streamsets.pipeline.api.Field;
import com.streamsets.pipeline.api.FileRef;
import com.streamsets.pipeline.api.OnRecordError;
import com.streamsets.pipeline.api.ProtoConfigurableEntity;
import com.streamsets.pipeline.api.Record;
import com.streamsets.pipeline.api.StageException;
import com.streamsets.pipeline.api.base.OnRecordErrorException;
import com.streamsets.pipeline.config.DateFormat;
import com.streamsets.pipeline.config.DecimalScaleRoundingStrategy;
import com.streamsets.pipeline.config.ZonedDateTimeFormat;
import com.streamsets.pipeline.sdk.ProcessorRunner;
import com.streamsets.pipeline.sdk.RecordCreator;
import com.streamsets.pipeline.sdk.StageRunner;
import com.streamsets.pipeline.stage.common.HeaderAttributeConstants;
import org.junit.Assert;
import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
import static com.streamsets.pipeline.stage.processor.fieldtypeconverter.Errors.CONVERTER_04;
import static com.streamsets.testing.Matchers.mapFieldWithEntry;
import static org.junit.Assert.assertThat;
public class TestFieldTypeConverterProcessorFields {
@Test
public void testStringToNonExistentField() throws StageException {
FieldTypeConverterConfig fieldTypeConverterConfig =
new FieldTypeConverterConfig();
fieldTypeConverterConfig.fields = ImmutableList.of("/nonExistent", "/beginner", "/expert", "/skilled");
fieldTypeConverterConfig.targetType = Field.Type.BOOLEAN;
fieldTypeConverterConfig.dataLocale = "en";
ProcessorRunner runner = new ProcessorRunner.Builder(FieldTypeConverterDProcessor.class)
.addConfiguration("convertBy", ConvertBy.BY_FIELD)
.addConfiguration("fieldTypeConverterConfigs", ImmutableList.of(fieldTypeConverterConfig))
.addOutputLane("a").build();
runner.runInit();
try {
Map<String, Field> map = new LinkedHashMap<>();
map.put("beginner", Field.create("false"));
map.put("intermediate", Field.create("yes"));
map.put("advanced", Field.create("no"));
map.put("expert", Field.create("true"));
map.put("skilled", Field.create("122345566"));
map.put("null", Field.create(Field.Type.STRING, null));
Record record = RecordCreator.create("s", "s:1");
record.set(Field.create(map));
StageRunner.Output output = runner.runProcess(ImmutableList.of(record));
Assert.assertEquals(1, output.getRecords().get("a").size());
Field field = output.getRecords().get("a").get(0).get();
Assert.assertTrue(field.getValue() instanceof Map);
Map<String, Field> result = field.getValueAsMap();
Assert.assertTrue(result.size() == 6);
Assert.assertTrue(result.containsKey("beginner"));
Assert.assertEquals(false, result.get("beginner").getValue());
Assert.assertTrue(result.containsKey("intermediate"));
Assert.assertEquals("yes", result.get("intermediate").getValue());
Assert.assertTrue(result.containsKey("advanced"));
Assert.assertEquals("no", result.get("advanced").getValue());
Assert.assertTrue(result.containsKey("expert"));
Assert.assertEquals(true, result.get("expert").getValue());
Assert.assertTrue(result.containsKey("skilled"));
Assert.assertEquals(false, result.get("skilled").getValue());
Assert.assertTrue(result.containsKey("null"));
Assert.assertEquals(null, result.get("null").getValue());
} finally {
runner.runDestroy();
}
}
@Test
public void testStringToBoolean() throws StageException {
FieldTypeConverterConfig fieldTypeConverterConfig =
new FieldTypeConverterConfig();
fieldTypeConverterConfig.fields = ImmutableList.of("/beginner", "/intermediate", "/skilled", "/advanced", "/expert"
, "/null");
fieldTypeConverterConfig.targetType = Field.Type.BOOLEAN;
fieldTypeConverterConfig.dataLocale = "en";
ProcessorRunner runner = new ProcessorRunner.Builder(FieldTypeConverterDProcessor.class)
.addConfiguration("convertBy", ConvertBy.BY_FIELD)
.addConfiguration("fieldTypeConverterConfigs", ImmutableList.of(fieldTypeConverterConfig))
.addOutputLane("a").build();
runner.runInit();
try {
Map<String, Field> map = new LinkedHashMap<>();
map.put("beginner", Field.create("false"));
map.put("intermediate", Field.create("yes"));
map.put("advanced", Field.create("no"));
map.put("expert", Field.create("true"));
map.put("skilled", Field.create("122345566"));
map.put("null", Field.create(Field.Type.STRING, null));
Record record = RecordCreator.create("s", "s:1");
record.set(Field.create(map));
StageRunner.Output output = runner.runProcess(ImmutableList.of(record));
Assert.assertEquals(1, output.getRecords().get("a").size());
Field field = output.getRecords().get("a").get(0).get();
Assert.assertTrue(field.getValue() instanceof Map);
Map<String, Field> result = field.getValueAsMap();
Assert.assertTrue(result.size() == 6);
Assert.assertTrue(result.containsKey("beginner"));
Assert.assertEquals(false, result.get("beginner").getValue());
Assert.assertTrue(result.containsKey("intermediate"));
Assert.assertEquals(false, result.get("intermediate").getValue());
Assert.assertTrue(result.containsKey("advanced"));
Assert.assertEquals(false, result.get("advanced").getValue());
Assert.assertTrue(result.containsKey("expert"));
Assert.assertEquals(true, result.get("expert").getValue());
Assert.assertTrue(result.containsKey("skilled"));
Assert.assertEquals(false, result.get("skilled").getValue());
Assert.assertTrue(result.containsKey("null"));
Assert.assertEquals(null, result.get("null").getValue());
} finally {
runner.runDestroy();
}
}
@Test
public void testBooleanToInt() throws StageException {
FieldTypeConverterConfig fieldTypeConverterConfig = new FieldTypeConverterConfig();
fieldTypeConverterConfig.fields = ImmutableList.of("/t", "/f");
fieldTypeConverterConfig.targetType = Field.Type.INTEGER;
ProcessorRunner runner = new ProcessorRunner.Builder(FieldTypeConverterDProcessor.class)
.addConfiguration("convertBy", ConvertBy.BY_FIELD)
.addConfiguration("fieldTypeConverterConfigs", ImmutableList.of(fieldTypeConverterConfig))
.addOutputLane("a").build();
runner.runInit();
try {
Map<String, Field> map = new LinkedHashMap<>();
map.put("t", Field.create(true));
map.put("f", Field.create(false));
Record record = RecordCreator.create
逸格草草
- 粉丝: 36
- 资源: 4592
最新资源
- 最新算法北方苍鹰(NGO)与其他算法进行对比 2、NGO算法是2022年新出的算法 3、用几种算法跑测试函数进行对比 4、十分详细的 5、NGO算法主要与ssa、woa、pso、gwo等算法对比 ma
- 三相电压源型逆变器闭环控制仿真模型,孤岛运行 采用电压外环,电流内环的双PI控制,LCL滤波器 在对称负载和不对称负载的情况下,三相输出电压均可保持稳定 运行环境为matlab simulink
- 直流电机双闭环控制,有关直流电机控制系统仿真均
- 五相永磁同步电机矢量控制,滞环控制,弱磁控制,五相永磁同步电机Svpwm双闭环控制
- fpga实现双线性插值缩放代码及资料
- 基于matlab医学图像处理
- 非隔离双向DC DC变器 buck-boost变器仿真 输入侧为直流电压源,输出侧接蓄电池 模型采用电压外环电流内环的双闭环控制方式 正向运行时电压源给电池恒流恒压充电,反向运行时电池放电维持直流侧电
- fpga图像缩放代码及相关资料
- HX711称重,stm32c8t6内核 esp8266阿里云服务器,app上显示重量 OLED 屏幕显示 (只代码)
- 单相全桥逆变器SPWM控制模型 双极性SPWM和单极性SPWM都有 运行环境为matlab simulink
- 二极管中点钳位型三电平整流器(NPC型整流器)MATLAB Simulink仿真 电压电流双闭环控制
- FPGA实现VGA转HDMI功能的IP,配详细的接口和使用说明
- -输电线路故障行波仿真举例, -仿真由3电源和4段分布参数构成环网作为输电线路故障行波仿真平台
- 西门子S7-1200与Factory IO联合仿真程序,6x9立体仓库、双立体仓库,可实现对物的: 自动连续存功能,自动连续取功能,指定位置存功能,指定位置取功能,满仓,空仓,指定仓库有无物报警等功能
- comsol光子晶体光纤有效折射率,模式色散,有效模式面积计算
- 云计算、边缘计算-云边协同系统模型 线形搜索算法寻找最优路径 多线程并行提升系统性能 Matlab实现
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0