TemplateDownloadTracker implements CommandCallback { @Override public void onResponse( final Response response, final CommandResult commandResult ) { if ( response != null && response.getStdOut() != null ) { StringTokenizer stringTokenizer = new StringTokenizer( commandResult.getStdOut(), "\n\r\f" ); while ( stringTokenizer.hasMoreTokens() ) { String line = stringTokenizer.nextToken(); Matcher downloadingMatcher = templateIsBeingDownloadedPattern.matcher( line ); if ( downloadingMatcher.groupCount() > 0 && downloadingMatcher.find() ) { currentTemplate = downloadingMatcher.group( 1 ); } if ( currentTemplate != null ) { Matcher downloadPercentMatcher = templateDownloadPercentPattern.matcher( line ); if ( downloadPercentMatcher.groupCount() > 0 && downloadPercentMatcher.find() ) { resourceHostEntity.updateTemplateDownloadProgress( environmentId, currentTemplate, Double.valueOf( downloadPercentMatcher.group( 1 ) ).intValue() ); } } } Matcher templateExistsMatcher = templateExistsPattern.matcher( response.getStdOut() ); if ( templateExistsMatcher.groupCount() > 0 ) { while ( templateExistsMatcher.find() ) { resourceHostEntity .updateTemplateDownloadProgress( environmentId, templateExistsMatcher.group( 1 ), 100 ); } } Matcher installingMatcher = templateIsBeingInstalledPattern.matcher( response.getStdOut() ); if ( installingMatcher.groupCount() > 0 ) { while ( installingMatcher.find() ) { resourceHostEntity .updateTemplateDownloadProgress( environmentId, installingMatcher.group( 1 ), 100 ); } } Matcher unpackingMatcher = templateIsBeingUnpackedPattern.matcher( response.getStdOut() ); if ( unpackingMatcher.groupCount() > 0 ) { while ( unpackingMatcher.find() ) { resourceHostEntity .updateTemplateDownloadProgress( environmentId, unpackingMatcher.group( 1 ), 100 ); } } } } TemplateDownloadTracker( final ResourceHostEntity resourceHostEntity, final String environmentId ); @Override void onResponse( final Response response, final CommandResult commandResult );  }
@Test public void testInstanceExist() throws Exception { doReturn( "time=\"2017-03-22 12:10:08\" level=info msg=\"Importing apache\" \n" + "time=\"2017-03-22 12:10:08\" level=info msg=\"apache instance exist\" " ).when( response ) .getStdOut(); tracker.onResponse( response, commandResult ); verify( resourceHostEntity ).updateTemplateDownloadProgress( ENV_ID, "apache", 100 ); }
@Test public void testInstallingTemplate() throws Exception { doReturn( "time=\"2017-03-22 12:10:08\" level=info msg=\"Installing template cassandra\"" ).when( response ) .getStdOut(); tracker.onResponse( response, commandResult ); verify( resourceHostEntity ).updateTemplateDownloadProgress( ENV_ID, "cassandra", 100 ); }
@Test public void testDownloadingTemplate() throws Exception { doReturn( "time=\"2017-03-22 12:10:08\" level=info msg=\"Downloading cassandra\" \n" + " 162.94 MiB / 209.08 MiB 77.93% 30s\n" + " 163.28 MiB / 209.08 MiB 78.10% 30s\n" + " 163.58 MiB / 209.08 MiB 78.24% 30s\n" + " 163.84 MiB / 209.08 MiB 78.36% 30s\n" + " 164.19 MiB / 209.08 MiB 78.53% 29s\n" + " 164.52 MiB / 209.08 MiB 78.69% 29s\n" + " 164.84 MiB / 209.08 MiB 78.84% 29s\n" + " 165.14 MiB / 209.08 MiB 78.99% 29s\n" + " 165.47 MiB / 209.08 MiB 79.14% 29s\n" + " 165.77 MiB / 209.08 MiB 79.28% 28s\n" + " 166.08 MiB / 209.08 MiB 79.43% 28s\n" + " 166.41 MiB / 209.08 MiB 79.59% 28s" ) .when( commandResult ).getStdOut(); tracker.onResponse( response, commandResult ); verify( resourceHostEntity, atLeastOnce() ).updateTemplateDownloadProgress( ENV_ID, "cassandra", 78 ); verify( resourceHostEntity, atLeastOnce() ).updateTemplateDownloadProgress( ENV_ID, "cassandra", 79 ); }