BuildOutputRecordUdf implements ScalarUdf { @SamzaSqlUdfMethod(disableArgumentCheck = true) public SamzaSqlRelRecord execute(Object... args) { int numOfArgs = args.length; Validate.isTrue(numOfArgs % 2 == 0, "numOfArgs should be an even number"); List<String> fieldNames = new ArrayList<>(); List<Object> fieldValues = new ArrayList<>(); for (int i = 0; i < numOfArgs - 1; i += 2) { fieldNames.add((String) args[i]); fieldValues.add(args[i + 1]); } return new SamzaSqlRelRecord(fieldNames, fieldValues); }  @Override void init(Config udfConfig, Context context); @SamzaSqlUdfMethod(disableArgumentCheck = true) SamzaSqlRelRecord execute(Object... args);  }
@Test public void testNoArgs() { BuildOutputRecordUdf buildOutputRecordUdf = new BuildOutputRecordUdf(); SamzaSqlRelRecord actualRecord = buildOutputRecordUdf.execute(); SamzaSqlRelRecord expectedRecord = new SamzaSqlRelRecord(new ArrayList<>(), new ArrayList<>()); Assert.assertEquals(actualRecord.getFieldNames(), expectedRecord.getFieldNames()); Assert.assertEquals(actualRecord.getFieldValues(), expectedRecord.getFieldValues()); }
@Test public void testSinglePair() { BuildOutputRecordUdf buildOutputRecordUdf = new BuildOutputRecordUdf(); SamzaSqlRelRecord actualRecord = buildOutputRecordUdf.execute("key", "value"); SamzaSqlRelRecord expectedRecord = new SamzaSqlRelRecord(Arrays.asList("key"), Arrays.asList("value")); Assert.assertEquals(actualRecord.getFieldNames(), expectedRecord.getFieldNames()); Assert.assertEquals(actualRecord.getFieldValues(), expectedRecord.getFieldValues()); }
@Test public void testMultiPairs() { BuildOutputRecordUdf buildOutputRecordUdf = new BuildOutputRecordUdf(); SamzaSqlRelRecord actualRecord = buildOutputRecordUdf.execute("k1", "v1", "k2", "v2"); SamzaSqlRelRecord expectedRecord = new SamzaSqlRelRecord(Arrays.asList("k1", "k2"), Arrays.asList("v1", "v2")); Assert.assertEquals(actualRecord.getFieldNames(), expectedRecord.getFieldNames()); Assert.assertEquals(actualRecord.getFieldValues(), expectedRecord.getFieldValues()); }
@Test public void testNestedRecord() { BuildOutputRecordUdf buildOutputRecordUdf = new BuildOutputRecordUdf(); SamzaSqlRelRecord nestedSamzaSqlRelRecord = new SamzaSqlRelRecord(Arrays.asList("k3"), Arrays.asList("v3")); SamzaSqlRelRecord actualRecord = buildOutputRecordUdf.execute("k1", "v1", "k2", nestedSamzaSqlRelRecord); SamzaSqlRelRecord expectedRecord = new SamzaSqlRelRecord(Arrays.asList("k1", "k2"), Arrays.asList("v1", nestedSamzaSqlRelRecord)); Assert.assertEquals(actualRecord.getFieldNames(), expectedRecord.getFieldNames()); Assert.assertEquals(actualRecord.getFieldValues(), expectedRecord.getFieldValues()); }
@Test(expected = NullPointerException.class) public void testNullArgs() { BuildOutputRecordUdf buildOutputRecordUdf = new BuildOutputRecordUdf(); buildOutputRecordUdf.execute(null); }
@Test(expected = IllegalArgumentException.class) public void testOddNumOfArgs() { BuildOutputRecordUdf buildOutputRecordUdf = new BuildOutputRecordUdf(); buildOutputRecordUdf.execute("k1"); }