DDMRESTDataProvider implements DDMDataProvider { protected DDMDataProviderResponse createDDMDataProviderResponse( DocumentContext documentContext, DDMDataProviderRequest ddmDataProviderRequest, DDMRESTDataProviderSettings ddmRESTDataProviderSettings) { DDMDataProviderOutputParametersSettings[] outputParameterSettingsArray = ddmRESTDataProviderSettings.outputParameters(); DDMDataProviderResponse.Builder builder = DDMDataProviderResponse.Builder.newBuilder(); if ((outputParameterSettingsArray == null) || (outputParameterSettingsArray.length == 0)) { return builder.build(); } for (DDMDataProviderOutputParametersSettings outputParameterSettings : outputParameterSettingsArray) { String id = outputParameterSettings.outputParameterId(); String type = outputParameterSettings.outputParameterType(); String path = outputParameterSettings.outputParameterPath(); if (Objects.equals(type, "text")) { String value = documentContext.read( normalizePath(path), String.class); builder = builder.withOutput(id, value); } else if (Objects.equals(type, "number")) { Number value = documentContext.read( normalizePath(path), Number.class); builder = builder.withOutput(id, value); } else if (Objects.equals(type, "list")) { String[] paths = StringUtil.split(path, CharPool.SEMICOLON); String normalizedValuePath = normalizePath(paths[0]); String normalizedKeyPath = normalizedValuePath; List<String> values = documentContext.read( normalizedValuePath, List.class); if (values == null) { continue; } List<String> keys = new ArrayList<>(values); if (paths.length >= 2) { normalizedKeyPath = normalizePath(paths[1]); keys = documentContext.read(normalizedKeyPath); } List<KeyValuePair> keyValuePairs = new ArrayList<>(); for (int i = 0; i < values.size(); i++) { keyValuePairs.add( new KeyValuePair(keys.get(i), values.get(i))); } if (ddmRESTDataProviderSettings.pagination()) { Optional<String> paginationStartOptional = ddmDataProviderRequest.getParameterOptional( "paginationStart", String.class); int start = Integer.valueOf( paginationStartOptional.orElse("1")); Optional<String> paginationEndOptional = ddmDataProviderRequest.getParameterOptional( "paginationEnd", String.class); int end = Integer.valueOf( paginationEndOptional.orElse("10")); if (keyValuePairs.size() > (end - start)) { List<KeyValuePair> sublist = ListUtil.subList( keyValuePairs, start, end); builder = builder.withOutput(id, sublist); } } else { builder = builder.withOutput(id, keyValuePairs); } } } return builder.build(); }  @Override DDMDataProviderResponse getData(
			DDMDataProviderRequest ddmDataProviderRequest); @Override Class<?> getSettings();  }
@Test public void testListOutputWithoutPagination() { DocumentContext documentContext = mock(DocumentContext.class); DDMDataProviderRequest.Builder builder = DDMDataProviderRequest.Builder.newBuilder(); DDMDataProviderRequest ddmDataProviderRequest = builder.build(); String outputParameterId = StringUtil.randomString(); DDMRESTDataProviderSettings ddmRESTDataProviderSettings = _createSettingsWithOutputParameter( outputParameterId, "list output", false, "value;key", "list"); when( documentContext.read(".value", List.class) ).thenReturn( new ArrayList() { { add("Rio de Janeiro"); add("São Paulo"); add("Sergipe"); add("Alagoas"); add("Amazonas"); } } ); when( documentContext.read(".key") ).thenReturn( new ArrayList() { { add("5"); add("6"); add("7"); add("8"); add("9"); } } ); DDMDataProviderResponse ddmDataProviderResponse = _ddmRESTDataProvider.createDDMDataProviderResponse( documentContext, ddmDataProviderRequest, ddmRESTDataProviderSettings); Optional<List<KeyValuePair>> optional = ddmDataProviderResponse.getOutputOptional( outputParameterId, List.class); List<KeyValuePair> keyValuePairs = new ArrayList<KeyValuePair>() { { add(new KeyValuePair("5", "Rio de Janeiro")); add(new KeyValuePair("6", "São Paulo")); add(new KeyValuePair("7", "Sergipe")); add(new KeyValuePair("8", "Alagoas")); add(new KeyValuePair("9", "Amazonas")); } }; Assert.assertEquals( keyValuePairs.toString(), keyValuePairs, optional.get()); }
@Test public void testListOutputWithPagination() { DocumentContext documentContext = mock(DocumentContext.class); DDMDataProviderRequest.Builder builder = DDMDataProviderRequest.Builder.newBuilder(); DDMDataProviderRequest ddmDataProviderRequest = builder.withParameter( "paginationStart", "0" ).withParameter( "paginationEnd", "3" ).build(); String outputParameterId = StringUtil.randomString(); DDMRESTDataProviderSettings ddmRESTDataProviderSettings = _createSettingsWithOutputParameter( outputParameterId, "list output", true, "value;key", "list"); when( documentContext.read(".value", List.class) ).thenReturn( new ArrayList() { { add("Pernambuco"); add("Paraiba"); add("Ceara"); add("Rio Grande do Norte"); } } ); when( documentContext.read(".key") ).thenReturn( new ArrayList() { { add("1"); add("2"); add("3"); add("4"); } } ); DDMDataProviderResponse ddmDataProviderResponse = _ddmRESTDataProvider.createDDMDataProviderResponse( documentContext, ddmDataProviderRequest, ddmRESTDataProviderSettings); Optional<List<KeyValuePair>> optional = ddmDataProviderResponse.getOutputOptional( outputParameterId, List.class); List<KeyValuePair> keyValuePairs = new ArrayList<KeyValuePair>() { { add(new KeyValuePair("1", "Pernambuco")); add(new KeyValuePair("2", "Paraiba")); add(new KeyValuePair("3", "Ceara")); } }; Assert.assertEquals( keyValuePairs.toString(), keyValuePairs, optional.get()); }
@Test public void testNumberOutput() { DocumentContext documentContext = mock(DocumentContext.class); DDMDataProviderRequest.Builder builder = DDMDataProviderRequest.Builder.newBuilder(); DDMDataProviderRequest ddmDataProviderRequest = builder.build(); String outputParameterId = StringUtil.randomString(); DDMRESTDataProviderSettings ddmRESTDataProviderSettings = _createSettingsWithOutputParameter( outputParameterId, "number output", false, "numberProp", "number"); when( documentContext.read(".numberProp", Number.class) ).thenReturn( 1 ); DDMDataProviderResponse ddmDataProviderResponse = _ddmRESTDataProvider.createDDMDataProviderResponse( documentContext, ddmDataProviderRequest, ddmRESTDataProviderSettings); Optional<Number> optional = ddmDataProviderResponse.getOutputOptional( outputParameterId, Number.class); Assert.assertEquals(1, optional.get()); }
@Test public void testTextOutput() { DocumentContext documentContext = mock(DocumentContext.class); DDMDataProviderRequest.Builder builder = DDMDataProviderRequest.Builder.newBuilder(); DDMDataProviderRequest ddmDataProviderRequest = builder.build(); String outputParameterId = StringUtil.randomString(); DDMRESTDataProviderSettings ddmRESTDataProviderSettings = _createSettingsWithOutputParameter( outputParameterId, "text output", false, "textProp", "text"); when( documentContext.read(".textProp", String.class) ).thenReturn( "brazil" ); DDMDataProviderResponse ddmDataProviderResponse = _ddmRESTDataProvider.createDDMDataProviderResponse( documentContext, ddmDataProviderRequest, ddmRESTDataProviderSettings); Optional<String> optional = ddmDataProviderResponse.getOutputOptional( outputParameterId, String.class); Assert.assertEquals("brazil", optional.get()); }