FieldHelper { public static void setField(Object current, String path, List value) { String[] splitPath = path.split("\\."); Object target = findTargetForField(current, splitPath); setFieldOnTarget(target, splitPath[splitPath.length - 1], value); } private  FieldHelper(); static void setField(Object current, String path, List value); static Map<String, Field> fieldMapByName(Collection<Field> fields); static void addOrUpdateMetadataForCombobox(Field field); static void addMetadataForRelationship(String typeClass, Field field); static void createMetadataForManyToManyRelationship(Field field, String relatedClass, String collectionType,
                                                               String relatedField, boolean isOwningSide); static void setMetadataForManyToManyRelationship(Field field, boolean isOwningSide);  }
@Test public void shouldSetCorrectValuesForFields() { FieldDto field = fieldDto(); FieldHelper.setField(field, "basic.displayName", asList("newDispName")); assertEquals("newDispName", field.getBasic().getDisplayName()); FieldHelper.setField(field, "basic.tooltip", asList("newTooltip")); assertEquals("newTooltip", field.getBasic().getTooltip()); FieldHelper.setField(field, "basic.placeholder", asList("newPlaceholder")); assertEquals("newPlaceholder", field.getBasic().getPlaceholder()); FieldHelper.setField(field, "metadata.0.key", asList("newKey")); assertEquals("newKey", field.getMetadata().get(0).getKey()); }
@Test public void shouldAddAndRemoveLookups() { AdvancedSettingsDto advancedSettings = advancedSettingsDto(); FieldHelper.setField(advancedSettings, "$addNewIndex", asList("Lookup 1")); assertEquals(3, advancedSettings.getIndexes().size()); FieldHelper.setField(advancedSettings, "$removeIndex", asList(1)); assertEquals(2, advancedSettings.getIndexes().size()); }
@Test public void shouldEditLookups() { AdvancedSettingsDto advancedSettingsDto = advancedSettingsDto(); FieldHelper.setField(advancedSettingsDto, "indexes.0.lookupName", asList("newVal")); assertEquals("newVal", advancedSettingsDto.getIndexes().get(0).getLookupName()); FieldHelper.setField(advancedSettingsDto, "indexes.1.singleObjectReturn", asList(true)); assertTrue(advancedSettingsDto.getIndexes().get(1).isSingleObjectReturn()); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowIllegalArgumentExceptionForWrongPaths() { FieldHelper.setField(fieldDto(), "wrong.wrong.wrong", asList("val")); }