RuntimeModelParserImpl implements RuntimeModelParser { void extractComponentFile(String modelId, InputStream zis, String name) throws IOException { String externalComponentsDir = externalComponentLoader.getExternalComponentsDir(); if (externalComponentsDir != null) { externalComponentsDir = externalComponentsDir.endsWith(File.separator) ? externalComponentsDir : externalComponentsDir + File.separator; String newFileName = null; if (options.isComponentPartition()) { newFileName = externalComponentsDir + modelId + File.separator + name.replaceAll(COMPONENTS_EXPORT_PATH, ""); } else { newFileName = externalComponentsDir + name.replaceAll(COMPONENTS_EXPORT_PATH, ""); } File target = new File(newFileName); target.getParentFile().mkdirs(); final int BUFFER_SIZE = 1024; byte[] buffer = new byte[BUFFER_SIZE]; int read = 0; try (FileOutputStream fos = new FileOutputStream(target)) { while ((read = zis.read(buffer, 0, BUFFER_SIZE)) >= 0) { fos.write(buffer, 0, read); } } } }  @Override RuntimeModel parse(String modelId, InputStream is);  }
@Test public void testUnzipComponentFile() throws IOException, URISyntaxException { Path componentPath = Paths.get(this.getClass().getResource("/").toURI()); when(externalComponentLoader.getExternalComponentsDir()).thenReturn(componentPath.toString()); String componentFileContent1 = "This is a component file."; String fileName = "component1/someFile.txt"; InputStream zis = new ByteArrayInputStream(componentFileContent1.getBytes()); parser.extractComponentFile("test", zis, fileName); Path componentFilePath = componentPath.resolve(fileName); assertTrue(Files.exists(componentFilePath)); assertEquals(componentFileContent1, Files.readAllLines(componentFilePath).get(0)); Files.delete(componentFilePath); }
@Test public void testUnzipComponentFileWithPartition() throws IOException, URISyntaxException { Path componentPath = Paths.get(this.getClass().getResource("/").toURI()); when(externalComponentLoader.getExternalComponentsDir()).thenReturn(componentPath.toString()); when(runtimeOptions.isComponentPartition()).thenReturn(true); String modelId = "testModel"; String componentFileContent1 = "This is a component file."; String fileName = "component1/someFile.txt"; InputStream zis = new ByteArrayInputStream(componentFileContent1.getBytes()); parser.extractComponentFile(modelId, zis, fileName); Path componentFilePath = Paths.get(componentPath.toString(), modelId, fileName); assertTrue(Files.exists(componentFilePath)); assertEquals(componentFileContent1, Files.readAllLines(componentFilePath).get(0)); Files.delete(componentFilePath); }