@Override public void storeCompletionMetadata( CompletionMetadata completionMetadata, NetworkId networkId, SnapshotId snapshotId) throws IOException { Path completionMetadataPath = getSnapshotCompletionMetadataPath(networkId, snapshotId); mkdirs(completionMetadataPath.getParent()); FileUtils.write( completionMetadataPath.toFile(), BatfishObjectMapper.writeString(completionMetadata), UTF_8); }
@Test public void testSerialization() throws IOException { String serialized = BatfishObjectMapper.writeString(IntegerSpace.PORTS); assertThat( BatfishObjectMapper.mapper().readValue(serialized, IntegerSpace.class), equalTo(IntegerSpace.PORTS)); }
private static Row toSearchFiltersRow( @Nullable AclLineMatchExpr description, Row row, boolean generateExplanations) { RowBuilder rowBuilder = Row.builder().putAll(row, row.getColumnNames()); if (generateExplanations) { /* * Sending the explanation to the client as a JSON blob. TODO: do something better. */ String jsonDescription; try { jsonDescription = BatfishObjectMapper.writeString(description); } catch (JsonProcessingException e) { e.printStackTrace(); jsonDescription = e.getMessage(); } rowBuilder.put(COL_HEADER_SPACE, jsonDescription); } return rowBuilder.build(); }
@Test public void testPutQuestionAdHocMissingNetwork() throws JsonProcessingException { String network = "network1"; String question = "question1"; String questionJson = BatfishObjectMapper.writeString(new TestQuestion()); Response response = getQuestionTargetAdHoc(network, question) .put(Entity.entity(questionJson, MediaType.APPLICATION_JSON)); assertThat(response.getStatus(), equalTo(NOT_FOUND.getStatusCode())); }
@Test public void testPutQuestionAnalysisMissingNetwork() throws JsonProcessingException { String network = "network1"; String question = "question1"; String questionJson = BatfishObjectMapper.writeString(new TestQuestion()); Response response = getQuestionTargetAdHoc(network, question) .put(Entity.entity(questionJson, MediaType.APPLICATION_JSON)); assertThat(response.getStatus(), equalTo(NOT_FOUND.getStatusCode())); }
@Test public void testSerialization() throws JsonProcessingException { PrefixRange pr = PrefixRange.fromString("1.2.3.4/8:17-19"); assertThat(BatfishObjectMapper.writeString(pr), equalTo("\"1.0.0.0/8:17-19\"")); }
@Test public void testPutQuestionAnalysisMissingAnalysis() throws JsonProcessingException { String network = "network1"; String question = "question1"; String analysis = "analysis1"; String questionJson = BatfishObjectMapper.writeString(new TestQuestion()); Response response = getQuestionTargetAnalysis(network, question, analysis) .put(Entity.entity(questionJson, MediaType.APPLICATION_JSON)); assertThat(response.getStatus(), equalTo(NOT_FOUND.getStatusCode())); }
@Test public void testGetQuestionAdHocSuccess() throws JsonProcessingException { String network = "network1"; String question = "question1"; String questionJson = BatfishObjectMapper.writeString(new TestQuestion()); Main.getWorkMgr().initNetwork(network, null); Main.getWorkMgr().uploadQuestion(network, question, questionJson); Response response = getQuestionTargetAdHoc(network, question).get(); assertThat(response.getStatus(), equalTo(OK.getStatusCode())); assertThat(response.readEntity(String.class), equalTo(questionJson)); }
@Test public void testListQuestionsAdHocSuccess() throws IOException { String network = "network1"; String question = "question1"; Main.getWorkMgr().initNetwork(network, null); Main.getWorkMgr() .uploadQuestion(network, question, BatfishObjectMapper.writeString(new TestQuestion())); Response response = getTargetAdHoc(network).get(); assertThat(response.getStatus(), equalTo(OK.getStatusCode())); assertThat(response.readEntity(SortedSet.class), equalTo(ImmutableSortedSet.of(question))); }
@Test public void testGetQuestionAnalysisSuccess() throws JsonProcessingException { String network = "network1"; String question = "question1"; String analysis = "analysis1"; String questionJson = BatfishObjectMapper.writeString(new TestQuestion()); Main.getWorkMgr().initNetwork(network, null); Main.getWorkMgr() .configureAnalysis( network, true, analysis, ImmutableMap.of(question, questionJson), ImmutableList.of(), false); Response response = getQuestionTargetAnalysis(network, question, analysis).get(); assertThat(response.getStatus(), equalTo(OK.getStatusCode())); assertThat(response.readEntity(String.class), equalTo(questionJson)); }
boolean queueWork(WorkItem wItem) { try { WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_QUEUE_WORK); MultiPart multiPart = new MultiPart(); multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE); addTextMultiPart( multiPart, CoordConsts.SVC_KEY_WORKITEM, BatfishObjectMapper.writeString(wItem)); addTextMultiPart(multiPart, CoordConsts.SVC_KEY_API_KEY, _settings.getApiKey()); JSONObject jObj = postData(webTarget, multiPart); return jObj != null; } catch (Exception e) { _logger.errorf("exception: "); _logger.error(Throwables.getStackTraceAsString(e) + "\n"); return false; } }
@Test public void testGetQuestionAnalysisMissingQuestion() throws JsonProcessingException { String network = "network1"; String question = "question1"; String analysis = "analysis1"; String questionJson = BatfishObjectMapper.writeString(new TestQuestion()); Main.getWorkMgr().initNetwork(network, null); Main.getWorkMgr() .configureAnalysis( network, true, analysis, ImmutableMap.of(question, questionJson), ImmutableList.of(), false); Response response = getQuestionTargetAnalysis(network, "someOtherQuestion", analysis).get(); assertThat(response.getStatus(), equalTo(NOT_FOUND.getStatusCode())); }
@Test public void testListQuestionsAnalysisSuccess() throws IOException { String network = "network1"; String analysis = "analysis1"; String question = "question1"; Main.getWorkMgr().initNetwork(network, null); Main.getWorkMgr() .configureAnalysis( network, true, analysis, ImmutableMap.of(question, BatfishObjectMapper.writeString(new TestQuestion())), ImmutableList.of(), false); Response response = getTargetAnalysis(network, analysis).get(); assertThat(response.getStatus(), equalTo(OK.getStatusCode())); assertThat(response.readEntity(SortedSet.class), equalTo(ImmutableSortedSet.of(question))); }
@Test public void testPutQuestionAnalysisSuccess() throws JsonProcessingException { String network = "network1"; String question = "question1"; String analysis = "analysis1"; String questionJson = BatfishObjectMapper.writeString(new TestQuestion()); Main.getWorkMgr().initNetwork(network, null); Main.getWorkMgr() .configureAnalysis( network, true, analysis, ImmutableMap.of( "someOtherQuestion", BatfishObjectMapper.writeString(new TestQuestion())), ImmutableList.of(), false); Response response = getQuestionTargetAnalysis(network, question, analysis) .put(Entity.entity(questionJson, MediaType.APPLICATION_JSON)); assertThat(response.getStatus(), equalTo(OK.getStatusCode())); } }
@Test public void testSerializationFields() throws JsonParseException, JsonMappingException, JsonProcessingException, IOException { Variable variable = new Variable(); String fieldName = "f1"; Field field = new Field(); field.setOptional(true); variable.setFields(ImmutableMap.of(fieldName, field)); // fields survive serialization cyle intact assertThat( BatfishObjectMapper.mapper() .readValue(BatfishObjectMapper.writeString(variable), Variable.class) .getFields() .get(fieldName) .getOptional(), equalTo(true)); }
@Test public void testPutQuestionAdHocSuccess() throws JsonProcessingException { String network = "network1"; String question = "question1"; String questionJson = BatfishObjectMapper.writeString(new TestQuestion()); Main.getWorkMgr().initNetwork(network, null); Main.getWorkMgr().uploadQuestion(network, question, questionJson); Response response = getQuestionTargetAdHoc(network, question) .put(Entity.entity(questionJson, MediaType.APPLICATION_JSON)); assertThat(response.getStatus(), equalTo(OK.getStatusCode())); }
@Test public void testDeleteQuestionAnalysisMissingQuestion() throws JsonProcessingException { String network = "network1"; String question = "question1"; String analysis = "analysis1"; Main.getWorkMgr().initNetwork(network, null); Main.getWorkMgr() .configureAnalysis( network, true, analysis, ImmutableMap.of(question, BatfishObjectMapper.writeString(new TestQuestion())), ImmutableList.of(), false); Response response = getQuestionTargetAnalysis(network, "someOtherQuestion", analysis).delete(); assertThat(response.getStatus(), equalTo(NOT_FOUND.getStatusCode())); }
@Test public void testDeleteQuestionAdHocSuccess() throws JsonProcessingException { String network = "network1"; String question = "question1"; Main.getWorkMgr().initNetwork(network, null); Main.getWorkMgr() .uploadQuestion(network, question, BatfishObjectMapper.writeString(new TestQuestion())); Response response = getQuestionTargetAdHoc(network, question).delete(); // should succed the first time assertThat(response.getStatus(), equalTo(OK.getStatusCode())); response = getQuestionTargetAdHoc(network, question).delete(); // should fail the second time assertThat(response.getStatus(), equalTo(NOT_FOUND.getStatusCode())); }
@Test public void testComputeWorkDetailsAdHocQuestion() throws IOException { String network = "network1"; String snapshot = "snapshot1"; String question = "question1"; _manager.initNetwork(network, null); uploadTestSnapshot(network, snapshot); _manager.uploadQuestion(network, question, BatfishObjectMapper.writeString(new TestQuestion())); WorkItem workItem = new WorkItem( UUID.randomUUID(), network, snapshot, ImmutableMap.of(BfConsts.COMMAND_ANSWER, "", BfConsts.ARG_QUESTION_NAME, question)); WorkDetails workDetails = _manager.computeWorkDetails(workItem); assertThat(workDetails.baseTestrig, equalTo(snapshot)); assertThat(workDetails.workType, equalTo(WorkType.PARSING_DEPENDENT_ANSWERING)); }
@Test public void testNullableSeveritySerialization() throws JsonParseException, JsonMappingException, JsonProcessingException, IOException { IssueConfigBean bean0 = new IssueConfigBean("major1", new MinorIssueConfig("minor1", 0, null)); IssueConfigBean beanNull = new IssueConfigBean("major1", new MinorIssueConfig("minor1", null, null)); IssueConfigBean bean0Deserialized = BatfishObjectMapper.mapper() .readValue(BatfishObjectMapper.writeString(bean0), IssueConfigBean.class); IssueConfigBean beanNullDeserialized = BatfishObjectMapper.mapper() .readValue(BatfishObjectMapper.writeString(beanNull), IssueConfigBean.class); new EqualsTester() .addEqualityGroup(bean0, bean0Deserialized) .addEqualityGroup(beanNull, beanNullDeserialized) .testEquals(); } }