DDMDataProviderInstancesDataProvider implements DDMDataProvider { @Override public DDMDataProviderResponse getData( DDMDataProviderRequest ddmDataProviderRequest) { List<KeyValuePair> keyValuePairs = new ArrayList<>(); try { long[] groupIds = portal.getCurrentAndAncestorSiteGroupIds( ddmDataProviderRequest.getGroupId()); List<DDMDataProviderInstance> ddmDataProviderInstances = ddmDataProviderInstanceLocalService.getDataProviderInstances( groupIds); for (DDMDataProviderInstance ddmDataProviderInstance : ddmDataProviderInstances) { long value = ddmDataProviderInstance.getDataProviderInstanceId(); String label = ddmDataProviderInstance.getName( LocaleThreadLocal.getThemeDisplayLocale()); keyValuePairs.add( new KeyValuePair(String.valueOf(value), label)); } } catch (Exception exception) { if (_log.isDebugEnabled()) { _log.debug(exception, exception); } } DDMDataProviderResponse.Builder builder = DDMDataProviderResponse.Builder.newBuilder(); return builder.withOutput( "Default-Output", 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.withGroupId( 1 ).build(); long[] groups = {1, 2}; when( _portal.getCurrentAndAncestorSiteGroupIds(1) ).thenReturn( groups ); DDMDataProviderInstance ddmDataProviderInstance1 = _createDDMDataProviderInstanceMock(1, "Data Provider Instance 1"); DDMDataProviderInstance ddmDataProviderInstance2 = _createDDMDataProviderInstanceMock(2, "Data Provider Instance 2"); when( _ddmDataProviderInstanceLocalService.getDataProviderInstances( groups) ).thenReturn( Arrays.asList(ddmDataProviderInstance1, ddmDataProviderInstance2) ); DDMDataProviderResponse ddmDataProviderResponse = _ddmDataProviderInstancesDataProvider.getData( ddmDataProviderRequest); Assert.assertTrue(ddmDataProviderResponse.hasOutput("Default-Output")); Optional<List<KeyValuePair>> optional = ddmDataProviderResponse.getOutputOptional( "Default-Output", List.class); Assert.assertTrue(optional.isPresent()); List<KeyValuePair> keyValuePairs = new ArrayList<KeyValuePair>() { { add(new KeyValuePair("1", "Data Provider Instance 1")); add(new KeyValuePair("2", "Data Provider Instance 2")); } }; Assert.assertEquals(keyValuePairs, optional.get()); }
@Test public void testThrowException() throws Exception { DDMDataProviderRequest.Builder builder = DDMDataProviderRequest.Builder.newBuilder(); DDMDataProviderRequest ddmDataProviderRequest = builder.withGroupId( 1 ).build(); when( _portal.getCurrentAndAncestorSiteGroupIds(1) ).thenThrow( Exception.class ); DDMDataProviderResponse ddmDataProviderResponse = _ddmDataProviderInstancesDataProvider.getData( ddmDataProviderRequest); Assert.assertTrue(ddmDataProviderResponse.hasOutput("Default-Output")); Optional<List<KeyValuePair>> optional = ddmDataProviderResponse.getOutputOptional( "Default-Output", List.class); Assert.assertTrue(optional.isPresent()); List<KeyValuePair> keyValuePairs = optional.get(); Assert.assertEquals(keyValuePairs.toString(), 0, keyValuePairs.size()); }