DDMDataProviderInstanceOutputParametersDataProvider implements DDMDataProvider { @Override public DDMDataProviderResponse getData( DDMDataProviderRequest ddmDataProviderRequest) { Optional<Long> dataProviderInstanceIdOptional = ddmDataProviderRequest.getParameterOptional( "dataProviderInstanceId", String.class); long dataProviderInstanceId = 0; if (dataProviderInstanceIdOptional.isPresent()) { dataProviderInstanceId = GetterUtil.getLong( dataProviderInstanceIdOptional.get()); } DDMDataProviderResponse.Builder builder = DDMDataProviderResponse.Builder.newBuilder(); List<KeyValuePair> keyValuePairs = new ArrayList<>(); if (dataProviderInstanceId == 0) { return builder.withOutput( "outputParameterNames", keyValuePairs ).build(); } try { DDMDataProviderOutputParametersSettings[] ddmDataProviderOutputParametersSettings = getDDMDataProviderOutputParametersSettings( dataProviderInstanceId); for (DDMDataProviderOutputParametersSettings ddmDataProviderOutputParametersSetting : ddmDataProviderOutputParametersSettings) { keyValuePairs.add( new KeyValuePair( ddmDataProviderOutputParametersSetting. outputParameterId(), ddmDataProviderOutputParametersSetting. outputParameterName())); } } catch (Exception exception) { _log.error( String.format( "Unable to get the output parameters for data provider " + "instance with id '%d'", dataProviderInstanceId), exception); } return builder.withOutput( "outputParameterNames", keyValuePairs ).build(); }  @Override DDMDataProviderResponse getData(
		DDMDataProviderRequest ddmDataProviderRequest); @Override Class<?> getSettings();  }
@Test public void testGetData() throws Exception { DDMDataProviderRequest.Builder builder = DDMDataProviderRequest.Builder.newBuilder(); DDMDataProviderRequest ddmDataProviderRequest = builder.withParameter( "dataProviderInstanceId", "1" ).build(); when( _ddmDataProviderInstanceService.getDataProviderInstance(1) ).thenReturn( _ddmDataProviderInstance ); when( _ddmDataProviderInstance.getType() ).thenReturn( "rest" ); when( _ddmDataProviderTracker.getDDMDataProvider("rest") ).thenReturn( _ddmDataProvider ); when( _ddmDataProvider.getSettings() ).thenReturn( (Class)TestDDMDataProviderParameterSettings.class ); DDMForm ddmForm = DDMFormFactory.create(_ddmDataProvider.getSettings()); DDMFormValues ddmFormValues = DDMFormValuesTestUtil.createDDMFormValues( ddmForm); ddmFormValues.addDDMFormFieldValue( DDMFormValuesTestUtil.createUnlocalizedDDMFormFieldValue( "url", "http: DDMFormFieldValue outputParamaters = DDMFormValuesTestUtil.createUnlocalizedDDMFormFieldValue( "outputParameters", StringPool.BLANK); ddmFormValues.addDDMFormFieldValue(outputParamaters); outputParamaters.addNestedDDMFormFieldValue( DDMFormValuesTestUtil.createUnlocalizedDDMFormFieldValue( "outputParameterName", "Country Id")); outputParamaters.addNestedDDMFormFieldValue( DDMFormValuesTestUtil.createUnlocalizedDDMFormFieldValue( "outputParameterPath", "countryId")); outputParamaters.addNestedDDMFormFieldValue( DDMFormValuesTestUtil.createUnlocalizedDDMFormFieldValue( "outputParameterType", "[\"number\"]")); String countryIdOutputParameterId = StringUtil.randomString(); outputParamaters.addNestedDDMFormFieldValue( DDMFormValuesTestUtil.createUnlocalizedDDMFormFieldValue( "outputParameterId", countryIdOutputParameterId)); outputParamaters = DDMFormValuesTestUtil.createUnlocalizedDDMFormFieldValue( "outputParameters", StringPool.BLANK); ddmFormValues.addDDMFormFieldValue(outputParamaters); outputParamaters.addNestedDDMFormFieldValue( DDMFormValuesTestUtil.createUnlocalizedDDMFormFieldValue( "outputParameterName", "Country Name")); outputParamaters.addNestedDDMFormFieldValue( DDMFormValuesTestUtil.createUnlocalizedDDMFormFieldValue( "outputParameterPath", "countryName")); outputParamaters.addNestedDDMFormFieldValue( DDMFormValuesTestUtil.createUnlocalizedDDMFormFieldValue( "outputParameterType", "[\"string\"]")); String countryNameOutputParameterId = StringUtil.randomString(); outputParamaters.addNestedDDMFormFieldValue( DDMFormValuesTestUtil.createUnlocalizedDDMFormFieldValue( "outputParameterId", countryNameOutputParameterId)); DDMFormValuesDeserializerDeserializeResponse ddmFormValuesDeserializerDeserializeResponse = DDMFormValuesDeserializerDeserializeResponse.Builder.newBuilder( ddmFormValues ).build(); when( _ddmFormValuesDeserializer.deserialize( Matchers.any(DDMFormValuesDeserializerDeserializeRequest.class)) ).thenReturn( ddmFormValuesDeserializerDeserializeResponse ); DDMDataProviderResponse ddmDataProviderResponse = _ddmDataProviderInstanceOutputParametersDataProvider.getData( ddmDataProviderRequest); Optional<List<KeyValuePair>> outputParameterNamesOptional = ddmDataProviderResponse.getOutputOptional( "outputParameterNames", List.class); Assert.assertTrue(outputParameterNamesOptional.isPresent()); List<KeyValuePair> keyValuePairs = new ArrayList<KeyValuePair>() { { add(new KeyValuePair(countryIdOutputParameterId, "Country Id")); add( new KeyValuePair( countryNameOutputParameterId, "Country Name")); } }; Assert.assertEquals( keyValuePairs.toString(), keyValuePairs, outputParameterNamesOptional.get()); }
@Test public void testThrowException() throws Exception { DDMDataProviderRequest.Builder builder = DDMDataProviderRequest.Builder.newBuilder(); DDMDataProviderRequest ddmDataProviderRequest = builder.withParameter( "dataProviderInstanceId", "1" ).build(); when( _ddmDataProviderInstanceService.getDataProviderInstance(1) ).thenThrow( Exception.class ); DDMDataProviderResponse ddmDataProviderResponse = _ddmDataProviderInstanceOutputParametersDataProvider.getData( ddmDataProviderRequest); Optional<List<KeyValuePair>> optional = ddmDataProviderResponse.getOutputOptional( "outputParameterNames", List.class); Assert.assertTrue(optional.isPresent()); List<KeyValuePair> keyValuePairs = optional.get(); Assert.assertEquals(keyValuePairs.toString(), 0, keyValuePairs.size()); }
@Test public void testWithoutDataProviderInstanceIdParameter() throws Exception { DDMDataProviderRequest.Builder builder = DDMDataProviderRequest.Builder.newBuilder(); DDMDataProviderRequest ddmDataProviderRequest = builder.build(); DDMDataProviderResponse ddmDataProviderResponse = _ddmDataProviderInstanceOutputParametersDataProvider.getData( ddmDataProviderRequest); Assert.assertTrue( ddmDataProviderResponse.hasOutput("outputParameterNames")); Optional<List<KeyValuePair>> outputParameterNamesOptional = ddmDataProviderResponse.getOutputOptional( "outputParameterNames", List.class); Assert.assertTrue(outputParameterNamesOptional.isPresent()); List<KeyValuePair> keyValuePairs = new ArrayList<>(); Assert.assertEquals( keyValuePairs.toString(), keyValuePairs, outputParameterNamesOptional.get()); }