SetOptionsFunction implements DDMExpressionFunction.Function2<String, String, Boolean>,
			   DDMExpressionObserverAware, DDMExpressionParameterAccessorAware { @Override public Boolean apply(String field, String json) { if ((_ddmExpressionObserver == null) || (_ddmExpressionParameterAccessor == null)) { return false; } UpdateFieldPropertyRequest.Builder builder = UpdateFieldPropertyRequest.Builder.newBuilder( field, "options", createKeyValuePairList( json, _ddmExpressionParameterAccessor.getLocale())); _ddmExpressionObserver.updateFieldProperty(builder.build()); return true; } SetOptionsFunction(JSONFactory jsonFactory); @Override Boolean apply(String field, String json); @Override String getName(); @Override void setDDMExpressionObserver(
		DDMExpressionObserver ddmExpressionObserver); @Override void setDDMExpressionParameterAccessor(
		DDMExpressionParameterAccessor ddmExpressionParameterAccessor); static final String NAME; }
@Test public void testApply() { when( _language.getLanguageId(new Locale("pt", "BR")) ).thenReturn( "pt_BR" ); JSONObject jsonObject = _jsonFactory.createJSONObject(); JSONArray jsonArray = _jsonFactory.createJSONArray(); jsonArray.put( _createJSONObject("label1", "value1") ).put( _createJSONObject("label2", "value2") ).put( _createJSONObject("label3", "value3") ); jsonObject.put("pt_BR", jsonArray); String json = jsonObject.toJSONString(); DefaultDDMExpressionObserver defaultDDMExpressionObserver = new DefaultDDMExpressionObserver(); DefaultDDMExpressionObserver spy = spy(defaultDDMExpressionObserver); _setOptionsFunction.setDDMExpressionObserver(spy); _setOptionsFunction.setDDMExpressionParameterAccessor( new DefaultDDMExpressionParameterAccessor()); Boolean result = _setOptionsFunction.apply("optionList", json); ArgumentCaptor<UpdateFieldPropertyRequest> argumentCaptor = ArgumentCaptor.forClass(UpdateFieldPropertyRequest.class); Mockito.verify( spy, Mockito.times(1) ).updateFieldProperty( argumentCaptor.capture() ); UpdateFieldPropertyRequest updateFieldPropertyRequest = argumentCaptor.getValue(); Assert.assertEquals( "optionList", updateFieldPropertyRequest.getField()); Map<String, Object> properties = updateFieldPropertyRequest.getProperties(); Assert.assertTrue(properties.containsKey("options")); List<KeyValuePair> keyValuePairs = new ArrayList<KeyValuePair>() { { add(new KeyValuePair("value1", "label1")); add(new KeyValuePair("value2", "label2")); add(new KeyValuePair("value3", "label3")); } }; Assert.assertEquals(keyValuePairs, properties.get("options")); Assert.assertTrue(result); }
@Test public void testNullObserver() { Assert.assertFalse(_setOptionsFunction.apply("field", "json")); }