DDMRESTDataProvider implements DDMDataProvider { protected DDMDataProviderResponse doGetData( DDMDataProviderRequest ddmDataProviderRequest) throws Exception { Optional<DDMDataProviderInstance> ddmDataProviderInstance = fetchDDMDataProviderInstance( ddmDataProviderRequest.getDDMDataProviderId()); if (!ddmDataProviderInstance.isPresent()) { DDMDataProviderResponse.Builder builder = DDMDataProviderResponse.Builder.newBuilder(); return builder.withStatus( DDMDataProviderResponseStatus.SERVICE_UNAVAILABLE ).build(); } DDMRESTDataProviderSettings ddmRESTDataProviderSettings = ddmDataProviderInstanceSettings.getSettings( ddmDataProviderInstance.get(), DDMRESTDataProviderSettings.class); HttpRequest httpRequest = HttpRequest.get( buildURL(ddmDataProviderRequest, ddmRESTDataProviderSettings)); if (StringUtil.startsWith( ddmRESTDataProviderSettings.url(), Http.HTTPS)) { httpRequest.trustAllCerts(true); } if (Validator.isNotNull(ddmRESTDataProviderSettings.username())) { httpRequest.basicAuthentication( ddmRESTDataProviderSettings.username(), ddmRESTDataProviderSettings.password()); } setRequestParameters( ddmDataProviderRequest, ddmRESTDataProviderSettings, httpRequest); String cacheKey = getCacheKey(httpRequest); DDMDataProviderResponse ddmDataProviderResponse = _portalCache.get( cacheKey); if ((ddmDataProviderResponse != null) && ddmRESTDataProviderSettings.cacheable()) { return ddmDataProviderResponse; } HttpResponse httpResponse = null; Map<String, Object> proxySettings = getProxySettings(); if (isNonproxiedHttpRequest(httpRequest, proxySettings)) { httpResponse = httpRequest.send(); } else { SocketHttpConnectionProvider socketHttpConnectionProvider = new SocketHttpConnectionProvider(); String proxyAddress = GetterUtil.getString( proxySettings.get("proxyAddress")); int proxyPort = GetterUtil.getInteger( proxySettings.get("proxyPort")); socketHttpConnectionProvider.useProxy( ProxyInfo.httpProxy(proxyAddress, proxyPort, null, null)); HttpRequest proxiedHttpRequest = httpRequest.withConnectionProvider( socketHttpConnectionProvider); httpResponse = proxiedHttpRequest.send(); } httpResponse.charset("UTF-8"); String responseBodyText = _removeUTFBOM(httpResponse.bodyText()); DocumentContext documentContext = JsonPath.parse(responseBodyText); ddmDataProviderResponse = createDDMDataProviderResponse( documentContext, ddmDataProviderRequest, ddmRESTDataProviderSettings); if (ddmRESTDataProviderSettings.cacheable()) { _portalCache.put(cacheKey, ddmDataProviderResponse); } return ddmDataProviderResponse; }  @Override DDMDataProviderResponse getData(
			DDMDataProviderRequest ddmDataProviderRequest); @Override Class<?> getSettings();  }
@Test public void testDoGetData() throws Exception { DDMDataProviderInstanceService ddmDataProviderInstanceService = mock( DDMDataProviderInstanceService.class); DDMDataProviderInstance ddmDataProviderInstance = mock( DDMDataProviderInstance.class); when( ddmDataProviderInstanceService.fetchDataProviderInstance(1L) ).thenReturn( ddmDataProviderInstance ); DDMDataProviderInstanceSettings ddmDataProviderInstanceSettings = mock( DDMDataProviderInstanceSettings.class); DDMRESTDataProviderSettings ddmRESTDataProviderSettings = _createDDMRESTDataProviderSettings(); when( ddmDataProviderInstanceSettings.getSettings( Matchers.any(DDMDataProviderInstance.class), Matchers.any()) ).thenReturn( ddmRESTDataProviderSettings ); mockStatic(HttpRequest.class); HttpRequest httpRequest = mock(HttpRequest.class); HttpRequest spyHttpRequest = spy(httpRequest); when( HttpRequest.get(Matchers.anyString()) ).thenReturn( spyHttpRequest ); HttpResponse httpResponse = mock(HttpResponse.class); HttpResponse spyHttpResponse = spy(httpResponse); when( spyHttpRequest.send() ).thenReturn( spyHttpResponse ); when( spyHttpResponse.bodyText() ).thenReturn( "{}" ); DDMDataProviderRequest.Builder builder = DDMDataProviderRequest.Builder.newBuilder(); DDMDataProviderRequest ddmDataProviderRequest = builder.withDDMDataProviderId( "1" ).build(); _ddmRESTDataProvider.ddmDataProviderInstanceService = ddmDataProviderInstanceService; _ddmRESTDataProvider.ddmDataProviderInstanceSettings = ddmDataProviderInstanceSettings; MultiVMPool multiVMPool = mock(MultiVMPool.class); PortalCache portalCache = mock(PortalCache.class); PortalCache spyPortalCache = spy(portalCache); when( multiVMPool.getPortalCache(DDMRESTDataProvider.class.getName()) ).thenReturn( spyPortalCache ); _ddmRESTDataProvider.setMultiVMPool(multiVMPool); _ddmRESTDataProvider.doGetData(ddmDataProviderRequest); ArgumentCaptor<String> userNameArgumentCaptor = ArgumentCaptor.forClass( String.class); ArgumentCaptor<String> passwordArgumentCaptor = ArgumentCaptor.forClass( String.class); Mockito.verify( spyHttpRequest, Mockito.times(1) ).basicAuthentication( userNameArgumentCaptor.capture(), passwordArgumentCaptor.capture() ); Assert.assertEquals( ddmRESTDataProviderSettings.username(), userNameArgumentCaptor.getValue()); Assert.assertEquals( ddmRESTDataProviderSettings.password(), passwordArgumentCaptor.getValue()); Mockito.verify( spyHttpRequest, Mockito.times(1) ).send(); Mockito.verify( spyHttpResponse, Mockito.times(1) ).bodyText(); Mockito.verify( spyPortalCache, Mockito.times(1) ).put( Matchers.any(Serializable.class), Matchers.any() ); }
@Test public void testDoGetDataServiceUnavailable() throws Exception { DDMDataProviderInstanceService ddmDataProviderInstanceService = mock( DDMDataProviderInstanceService.class); _ddmRESTDataProvider.ddmDataProviderInstanceService = ddmDataProviderInstanceService; when( ddmDataProviderInstanceService.fetchDataProviderInstanceByUuid("id") ).thenReturn( null ); DDMDataProviderRequest.Builder builder = DDMDataProviderRequest.Builder.newBuilder(); DDMDataProviderRequest ddmDataProviderRequest = builder.withDDMDataProviderId( "id" ).build(); DDMDataProviderResponse ddmDataProviderResponse = _ddmRESTDataProvider.doGetData(ddmDataProviderRequest); Assert.assertEquals( DDMDataProviderResponseStatus.SERVICE_UNAVAILABLE, ddmDataProviderResponse.getStatus()); }