CouchDbInput extends BaseStep implements StepInterface { public boolean init( StepMetaInterface stepMetaInterface, StepDataInterface stepDataInterface ) { if ( super.init( stepMetaInterface, stepDataInterface ) ) { meta = (CouchDbInputMeta) stepMetaInterface; data = (CouchDbInputData) stepDataInterface; String hostname = environmentSubstitute( meta.getHostname() ); int port = Const.toInt( environmentSubstitute( meta.getPort() ), 5984 ); String db = environmentSubstitute( meta.getDbName() ); String design = environmentSubstitute( meta.getDesignDocument() ); String view = environmentSubstitute( meta.getViewName() ); if ( StringUtils.isEmpty( design ) ) { log.logError( "Please provide a design document to use" ); return false; } if ( StringUtils.isEmpty( view ) ) { log.logError( "Please provide a view name to look at" ); return false; } String realUser = environmentSubstitute( meta.getAuthenticationUser() ); String realPass = Encr.decryptPasswordOptionallyEncrypted( environmentSubstitute( meta.getAuthenticationPassword() ) ); String url = buildUrl( hostname, port, db, design, view ); logBasic( "Querying CouchDB view on URL: " + url ); try { HttpClient client = createHttpClient( realUser, realPass ); HttpGet method = getMethodFactory.create( url ); data.inputStream = null; data.bufferedInputStream = null; HttpClientContext context = null; if ( StringUtils.isNotBlank( hostname ) ) { context = getHttpClientContext( hostname, port ); } HttpResponse httpResponse = context != null ? client.execute( method, context ) : client.execute( method ); int result = httpResponse.getStatusLine().getStatusCode(); data.inputStream = httpResponse.getEntity().getContent(); data.bufferedInputStream = new BufferedInputStream( data.inputStream, 1000 ); if ( result < 200 || result >= 300 ) { StringBuilder err = new StringBuilder(); int c; while ( ( c = data.bufferedInputStream.read() ) >= 0 ) { err.append( (char) c ); } logError( "Web request returned code " + result + " : " + err.toString() ); return false; } data.counter = 0; return true; } catch ( Exception e ) { logError( BaseMessages.getString( PKG, "CouchDbInput.ErrorConnectingToCouchDb.Exception", hostname, "" + port, db, view ), e ); return false; } } return false; } CouchDbInput( StepMeta stepMeta, StepDataInterface stepDataInterface, int copyNr, TransMeta transMeta,
                       Trans trans ); @Deprecated  CouchDbInput( StepMeta stepMeta, StepDataInterface stepDataInterface, int copyNr, TransMeta transMeta,
                       Trans trans, HttpClientFactory httpClientFactory,
                       GetMethodFactory getMethodFactory ); static String buildUrl( String hostname, int port, String db, String design, String view ); boolean processRow( StepMetaInterface smi, StepDataInterface sdi ); boolean init( StepMetaInterface stepMetaInterface, StepDataInterface stepDataInterface ); @Override void dispose( StepMetaInterface smi, StepDataInterface sdi );  }
@Test public void testInit() throws IOException { CouchDbInputMeta couchDbInputMeta = (CouchDbInputMeta) stepMockHelper.initStepMetaInterface; CouchDbInputData couchDbInputData = (CouchDbInputData) stepMockHelper.initStepDataInterface; final String testHostname = "testHostname"; final String testPort = "9999"; final String testDbName = "testDbName"; final String testDoc = "testDoc"; final String testView = "testView"; final String testUser = "testUser"; final String testPassword = "testPassword"; when( couchDbInputMeta.getHostname() ).thenReturn( testHostname ); when( couchDbInputMeta.getPort() ).thenReturn( testPort ); when( couchDbInputMeta.getDbName() ).thenReturn( testDbName ); when( couchDbInputMeta.getDesignDocument() ).thenReturn( testDoc ); when( couchDbInputMeta.getViewName() ).thenReturn( testView ); when( couchDbInputMeta.getAuthenticationUser() ).thenReturn( testUser ); when( couchDbInputMeta.getAuthenticationPassword() ).thenReturn( testPassword ); HttpGet getMethod = mock( HttpGet.class ); when( getMethodFactory.create( CouchDbInput .buildUrl( testHostname, Const.toInt( testPort, 5984 ), testDbName, testDoc, testView ) ) ).thenReturn( getMethod ); HttpClient httpClient = mock( HttpClient.class ); doReturn( httpClient ).when( couchDbInput ).createHttpClient( anyString(), anyString() ); HttpResponse httpResponseMock = mock(HttpResponse.class); HttpEntity httpEntity = mock(HttpEntity.class); doReturn( httpEntity ).when( httpResponseMock ).getEntity(); StatusLine statusLineMock = mock(StatusLine.class); doReturn( httpResponseMock ).when( httpClient ).execute( anyObject() ); doReturn( httpResponseMock ).when( httpClient ).execute( any( HttpUriRequest.class ), any( HttpContext.class ) ); doReturn( statusLineMock ).when( httpResponseMock ).getStatusLine(); doReturn( 200 ).when( statusLineMock ).getStatusCode(); assertTrue( couchDbInput.init( couchDbInputMeta, couchDbInputData ) ); verify( couchDbInput ).createHttpClient( "testUser", "testPassword" ); verify( couchDbInput ).getHttpClientContext( "testHostname", Integer.parseInt( testPort ) ); }