CalvalusStaging { List<String> getProductResultUrls(String jobId, Map<String, String> calvalusDefaultConfig) throws WpsResultProductException { Production production; try { ProductionService productionService = CalvalusProductionService.getServiceContainerSingleton().getProductionService(); production = productionService.getProduction(jobId); } catch (ProductionException | IOException exception) { throw new WpsResultProductException(exception); } String stagingDirectoryPath = calvalusDefaultConfig.get("calvalus.wps.staging.path") + "/" + production.getStagingPath(); File stagingDirectory = new File(System.getProperty("catalina.base") + CALWPS_ROOT_PATH + "/" + stagingDirectoryPath); CalvalusLogger.getLogger().info("looking up results in staging dir " + stagingDirectory); Optional<File[]> productResultFiles = Optional.ofNullable(stagingDirectory.listFiles()); if (productResultFiles.isPresent()) { return doGetProductResultUrls(production, stagingDirectoryPath, productResultFiles.get()); } else { return new ArrayList<>(); } } CalvalusStaging(WpsServerContext wpsServerContext);   }
@Test public void canGetProductResultUrls() throws Exception { mockProductionService = mock(ProductionService.class); when(mockProduction.getName()).thenReturn(DUMMY_JOB_ID); when(mockProduction.getStagingPath()).thenReturn(DUMMY_PATH); when(mockProductionService.getProduction(anyString())).thenReturn(mockProduction); ServiceContainer mockServiceContainer = mock(ServiceContainer.class); PowerMockito.when(mockServiceContainer.getProductionService()).thenReturn(mockProductionService); PowerMockito.when(CalvalusProductionService.getServiceContainerSingleton()).thenReturn(mockServiceContainer); Map<String, String> mockCalvalusDefaultConfig = getMockDefaultConfig(); File mockFile = mock(File.class); File[] productResultFiles = getProductResultFiles(); when(mockFile.listFiles()).thenReturn(productResultFiles); PowerMockito.whenNew(File.class).withArguments(anyString()).thenReturn(mockFile); calvalusStaging = new CalvalusStaging(mockServerContext); List<String> productResultUrls = calvalusStaging.getProductResultUrls("jobId", mockCalvalusDefaultConfig); assertThat(productResultUrls.size(), equalTo(2)); assertThat(productResultUrls, hasItems("http: assertThat(productResultUrls, hasItems("http: }
@Test public void canGetEmptyProductResultUrlsWhenNoProducts() throws Exception { mockProductionService = mock(ProductionService.class); when(mockProduction.getName()).thenReturn(DUMMY_JOB_ID); when(mockProduction.getStagingPath()).thenReturn(DUMMY_PATH); when(mockProductionService.getProduction(anyString())).thenReturn(mockProduction); ServiceContainer mockServiceContainer = mock(ServiceContainer.class); PowerMockito.when(mockServiceContainer.getProductionService()).thenReturn(mockProductionService); PowerMockito.when(CalvalusProductionService.getServiceContainerSingleton()).thenReturn(mockServiceContainer); Map<String, String> mockCalvalusDefaultConfig = getMockDefaultConfig(); File mockFile = mock(File.class); when(mockFile.listFiles()).thenReturn(new File[0]); PowerMockito.whenNew(File.class).withArguments(anyString()).thenReturn(mockFile); calvalusStaging = new CalvalusStaging(mockServerContext); List<String> productResultUrls = calvalusStaging.getProductResultUrls("jobId", mockCalvalusDefaultConfig); assertThat(productResultUrls.size(), equalTo(0)); }
@Test public void canGetEmptyProductResultUrlsWhenDirectoryNotExist() throws Exception { mockProductionService = mock(ProductionService.class); when(mockProduction.getName()).thenReturn(DUMMY_JOB_ID); when(mockProduction.getStagingPath()).thenReturn(DUMMY_PATH); when(mockProductionService.getProduction(anyString())).thenReturn(mockProduction); ServiceContainer mockServiceContainer = mock(ServiceContainer.class); PowerMockito.when(mockServiceContainer.getProductionService()).thenReturn(mockProductionService); PowerMockito.when(CalvalusProductionService.getServiceContainerSingleton()).thenReturn(mockServiceContainer); Map<String, String> mockCalvalusDefaultConfig = getMockDefaultConfig(); File mockFile = mock(File.class); when(mockFile.listFiles()).thenReturn(null); PowerMockito.whenNew(File.class).withArguments(anyString()).thenReturn(mockFile); calvalusStaging = new CalvalusStaging(mockServerContext); List<String> productResultUrls = calvalusStaging.getProductResultUrls("jobId", mockCalvalusDefaultConfig); assertThat(productResultUrls.size(), equalTo(0)); }
@Test(expected = WpsResultProductException.class) public void canCatchExceptionWhenGetResultUrls() throws Exception { mockProductionService = mock(ProductionService.class); when(mockProductionService.getProduction(anyString())).thenThrow(new ProductionException("error in getting production")); ServiceContainer mockServiceContainer = mock(ServiceContainer.class); PowerMockito.when(mockServiceContainer.getProductionService()).thenReturn(mockProductionService); PowerMockito.when(CalvalusProductionService.getServiceContainerSingleton()).thenReturn(mockServiceContainer); Map<String, String> mockCalvalusDefaultConfig = getMockDefaultConfig(); calvalusStaging = new CalvalusStaging(mockServerContext); calvalusStaging.getProductResultUrls("jobId", mockCalvalusDefaultConfig); }