public static synchronized void write(ReferenceLibrary data, Path dataPath) throws JsonProcessingException { CommonUtil.writeFile(dataPath, BatfishObjectMapper.writePrettyString(data)); } }
@Override public void storeMajorIssueConfig( NetworkId network, IssueSettingsId majorIssueType, MajorIssueConfig majorIssueConfig) throws IOException { Path path = _d.getMajorIssueConfigDir(network, majorIssueType); if (Files.notExists(path)) { Files.createDirectories(path.getParent()); } CommonUtil.writeFile(path, BatfishObjectMapper.mapper().writeValueAsString(majorIssueConfig)); }
@Override public void assignNetworkNodeRolesId(NetworkId networkId, NodeRolesId networkNodeRolesId) { Path idFile = getNetworkNodeRolesIdPath(networkId); idFile.getParent().toFile().mkdirs(); CommonUtil.writeFile(idFile, networkNodeRolesId.getId()); }
@Override public void assignQuestionSettingsId( String questionClassId, NetworkId networkId, QuestionSettingsId questionSettingsId) { Path idFile = getQuestionSettingsIdPath(questionClassId, networkId); idFile.getParent().toFile().mkdirs(); CommonUtil.writeFile(idFile, questionSettingsId.getId()); }
@Override public void assignAnalysis(String analysis, NetworkId networkId, AnalysisId analysisId) { Path idFile = getAnalysisIdPath(analysis, networkId); idFile.getParent().toFile().mkdirs(); CommonUtil.writeFile(idFile, analysisId.getId()); }
@Override public void assignIssueSettingsId( String majorIssueType, NetworkId networkId, IssueSettingsId issueSettingsId) { Path idFile = getIssueSettingsIdPath(majorIssueType, networkId); idFile.getParent().toFile().mkdirs(); CommonUtil.writeFile(idFile, issueSettingsId.getId()); }
@Override public void assignNetwork(String network, NetworkId networkId) { Path idFile = getNetworkIdPath(network); idFile.getParent().toFile().mkdirs(); CommonUtil.writeFile(idFile, networkId.getId()); }
@Override public void assignQuestion( String question, NetworkId networkId, QuestionId questionId, AnalysisId analysisId) { Path idFile = getQuestionIdPath(question, networkId, analysisId); idFile.getParent().toFile().mkdirs(); CommonUtil.writeFile(idFile, questionId.getId()); }
@Override public void assignSnapshot(String snapshot, NetworkId networkId, SnapshotId snapshotId) { Path idFile = getSnapshotIdPath(snapshot, networkId); idFile.getParent().toFile().mkdirs(); CommonUtil.writeFile(idFile, snapshotId.getId()); }
@Override public void storeQuestion( String questionStr, NetworkId network, QuestionId question, @Nullable AnalysisId analysis) { Path questionPath = getQuestionPath(network, question, analysis); mkdirs(questionPath.getParent()); CommonUtil.writeFile(questionPath, questionStr); }
@Override public void storeAnswer(String answerStr, AnswerId answerId) { Path answerPath = getAnswerPath(answerId); mkdirs(answerPath.getParent()); CommonUtil.writeFile(answerPath, answerStr); }
@Override public void storeAnswerMetadata(AnswerMetadata answerMetadata, AnswerId answerId) { String metricsStr; try { metricsStr = BatfishObjectMapper.writePrettyString(answerMetadata); } catch (JsonProcessingException e) { throw new BatfishException("Could not write answer metrics", e); } Path answerMetadataPath = getAnswerMetadataPath(answerId); mkdirs(answerMetadataPath.getParent()); CommonUtil.writeFile(answerMetadataPath, metricsStr); }
/** Creates and returns path to a snapshot dir with the specified config */ public static Path createSnapshot( String snapshot, String fileName, String content, TemporaryFolder folder) { Path tmpSnapshotSrcDir = folder.getRoot().toPath().resolve(snapshot); // intentional duplication of snapshot to provide subdir Path tmpSnapshotConfig = tmpSnapshotSrcDir .resolve(snapshot) .resolve(BfConsts.RELPATH_CONFIGURATIONS_DIR) .resolve(fileName); tmpSnapshotConfig.getParent().toFile().mkdirs(); CommonUtil.writeFile(tmpSnapshotConfig, content); return tmpSnapshotSrcDir; }
@Test public void testGetSnapshotSubdirValid() { Path root = _folder.getRoot().toPath(); Path s1Path = root.resolve("s1"); s1Path.toFile().mkdirs(); Path s2Path = root.resolve("__MACOSX"); // ignored dir s2Path.toFile().mkdirs(); CommonUtil.writeFile(s1Path.resolve("file1"), "content"); CommonUtil.writeFile(s2Path.resolve("file2"), "content"); // extra dir should be ignored, and s1Path should be considered the snapshot subdir assertThat(WorkMgr.getSnapshotSubdir(root), equalTo(s1Path)); }
@Test public void testGetSnapshotSubdirInvalid() { Path root = _folder.getRoot().toPath(); Path s1Path = root.resolve("s1"); s1Path.toFile().mkdirs(); Path s2Path = root.resolve("s2"); s2Path.toFile().mkdirs(); CommonUtil.writeFile(s1Path.resolve("file1"), "content"); CommonUtil.writeFile(s2Path.resolve("file2"), "content"); // invalid because there are two top-level dirs _thrown.expect(BatfishException.class); WorkMgr.getSnapshotSubdir(root); }
private @Nonnull String writeTemplateFile(String templateName) { Path questionTemplateDir = _folder.getRoot().toPath().resolve("templates"); String templateText = String.format( "{\"class\":\"%s\",\"%s\":{\"%s\":\"%s\"}}", TestQuestion.class, BfConsts.PROP_INSTANCE, BfConsts.PROP_INSTANCE_NAME, templateName); Path questionTemplateFile = questionTemplateDir.resolve(templateName + ".json"); questionTemplateDir.toFile().mkdirs(); CommonUtil.writeFile(questionTemplateFile, templateText); Main.getSettings().setQuestionTemplateDirs(ImmutableList.of(questionTemplateDir)); return templateText; }
private @Nonnull String writeTemplateFile(String templateName) { Path questionTemplateDir = _questionsTemplatesFolder.getRoot().toPath().resolve("templates"); String templateText = String.format( "{\"class\":\"%s\",\"%s\":{\"%s\":\"%s\"}}", TestQuestion.class, BfConsts.PROP_INSTANCE, BfConsts.PROP_INSTANCE_NAME, templateName); Path questionTemplateFile = questionTemplateDir.resolve(templateName + ".json"); questionTemplateDir.toFile().mkdirs(); CommonUtil.writeFile(questionTemplateFile, templateText); Main.getSettings().setQuestionTemplateDirs(ImmutableList.of(questionTemplateDir)); return templateText; } }
@Test public void testLoadWorkLog() throws IOException { // setup: pretend a worker logger has written a file NetworkId network = new NetworkId("network"); SnapshotId snapshot = new SnapshotId("snapshot"); Path dir = _storage.getDirectoryProvider().getSnapshotOutputDir(network, snapshot); final boolean mkdirs = dir.toFile().mkdirs(); assertThat(mkdirs, equalTo(true)); CommonUtil.writeFile(dir.resolve("workid.log"), "testoutput"); // Test: read log using storage API assertThat(_storage.loadWorkLog(network, snapshot, "workid"), equalTo("testoutput")); }
@Test public void getNodes() throws IOException { _manager.initNetwork("container", null); // create a testrig and write a topology object for it createSnapshotWithMetadata("container", "testrig1"); Topology topology = new Topology("testrig1"); topology.setNodes(ImmutableSet.of(new Node("a1"), new Node("b1"))); CommonUtil.writeFile( _manager .getdirSnapshot("container", "testrig1") .resolve( Paths.get(BfConsts.RELPATH_OUTPUT, BfConsts.RELPATH_TESTRIG_POJO_TOPOLOGY_PATH)), BatfishObjectMapper.mapper().writeValueAsString(topology)); // should get the nodes of the topology when we ask for it assertThat(_manager.getNodes("container", "testrig1"), equalTo(ImmutableSet.of("a1", "b1"))); }
@Test public void testLoadQuestionsFromDir() throws Exception { JSONObject testQuestion = new JSONObject(); testQuestion.put( "instance", new JSONObject() .put("instanceName", "testQuestionName") .put("description", "test question description")); Path questionJsonPath = _folder.newFile("testquestion.json").toPath(); CommonUtil.writeFile(questionJsonPath, testQuestion.toString()); Multimap<String, String> loadedQuestions = Client.loadQuestionsFromDir(questionJsonPath.toString()); Multimap<String, String> expectedMap = HashMultimap.create(); expectedMap.put("testQuestionName", testQuestion.toString()); // checking if questions are loaded from disk correctly assertEquals(expectedMap, loadedQuestions); }