/** * Deserialize a {@link ProfileConfig}. * * @param bytes Raw bytes containing a UTF-8 JSON String. * @return The Profile definition. * @throws IOException */ public static ProfileConfig fromBytes(byte[] bytes) throws IOException { return JSONUtils.INSTANCE.load(new String(bytes), ProfileConfig.class); }
/** * Deserialize a {@link ProfilerConfig}. * * @param bytes Raw bytes containing a UTF-8 JSON String. * @return The Profiler configuration. * @throws IOException */ public static ProfilerConfig fromBytes(byte[] bytes) throws IOException { return JSONUtils.INSTANCE.load(new String(bytes), ProfilerConfig.class); }
private Map<String, Object> getGlobalConfig(Optional<CuratorFramework> zkClient) { if (zkClient.isPresent()) { try { return JSONUtils.INSTANCE.load( new ByteArrayInputStream(ConfigurationsUtils.readGlobalConfigBytesFromZookeeper(zkClient.get())), JSONUtils.MAP_SUPPLIER); } catch (Exception e) { LOG.warn("Exception thrown while attempting to get global config from Zookeeper.", e); } } return new LinkedHashMap<>(); }
public static void main(String... argv) throws IOException { String templateFile = argv[0]; String schemaFile = argv[1]; Map<String, Object> template = JSONUtils.INSTANCE.load(new File(templateFile), JSONUtils.MAP_SUPPLIER); try(PrintWriter pw = new PrintWriter(new File(schemaFile))) { translate(pw, template); } } }
public static SensorParserConfig fromBytes(byte[] config) throws IOException { SensorParserConfig ret = JSONUtils.INSTANCE.load(new String(config), SensorParserConfig.class); ret.init(); return ret; }
public static Map<String, Object> parse(String doc) { try { return JSONUtils.INSTANCE.load(doc, JSONUtils.MAP_SUPPLIER); } catch (IOException e) { throw new IllegalStateException(e.getMessage(), e); } }
@Test public void applyPatch_modifies_complex_source_json_doc() throws IOException { String actual = new String(JSONUtils.INSTANCE.applyPatch(patchComplexJson, complexJson)); assertThat(JSONUtils.INSTANCE.load(actual, JSONUtils.MAP_SUPPLIER), equalTo(JSONUtils.INSTANCE.load(expectedComplexJson, JSONUtils.MAP_SUPPLIER))); }
@Test public void testGetSubgroups_grouped() throws IOException { for(String c : GROUPED_CONFIGS) { EnrichmentConfig enrichmentConfig = JSONUtils.INSTANCE.load(c, EnrichmentConfig.class); Assert.assertNotNull(enrichmentConfig.getEnrichmentConfigs().get("stellar")); ConfigHandler handler = enrichmentConfig.getEnrichmentConfigs().get("stellar"); List<String> subgroups = Configs.STELLAR.getSubgroups(handler); Assert.assertEquals("group1", subgroups.get(0)); Assert.assertEquals("group2", subgroups.get(1)); Assert.assertEquals(2, subgroups.size()); } }
@Test public void loads_file_with_map_class() throws Exception { Map<String, Object> expected = new HashMap<String, Object>() {{ put("a", "hello"); put("b", "world"); }}; Map<String, Object> actual = JSONUtils.INSTANCE.load(configFile, Map.class); assertThat("config not equal", actual, equalTo(expected)); }
@Test public void loads_file_with_typeref() throws Exception { Map<String, Object> expected = new HashMap<String, Object>() {{ put("a", "hello"); put("b", "world"); }}; Map<String, Object> actual = JSONUtils.INSTANCE .load(configFile, JSONUtils.MAP_SUPPLIER); assertThat("config not equal", actual, equalTo(expected)); }
@Test public void testGetSubgroups_default() throws IOException { for(String c : DEFAULT_CONFIGS) { EnrichmentConfig enrichmentConfig = JSONUtils.INSTANCE.load(c, EnrichmentConfig.class); Assert.assertNotNull(enrichmentConfig.getEnrichmentConfigs().get("stellar")); ConfigHandler handler = enrichmentConfig.getEnrichmentConfigs().get("stellar"); List<String> subgroups = Configs.STELLAR.getSubgroups(handler); Assert.assertEquals("", subgroups.get(0)); Assert.assertEquals(1, subgroups.size()); } }
@Test public void testAllVariableUsage() throws Exception { JSONObject message = getMessage(); EnrichmentConfig enrichmentConfig = JSONUtils.INSTANCE.load(allVariableConfig, EnrichmentConfig.class); Assert.assertNotNull(enrichmentConfig.getEnrichmentConfigs().get("stellar")); ConfigHandler handler = enrichmentConfig.getEnrichmentConfigs().get("stellar"); JSONObject enriched = enrich(message, "", handler); Assert.assertEquals("stellar_test", enriched.get("stmt1")); }
@Test public void different_type_filter_query() throws Exception { SearchRequest request = JSONUtils.INSTANCE.load(differentTypeFilterQuery, SearchRequest.class); SearchResponse response = dao.search(request); Assert.assertEquals(1, response.getTotal()); List<SearchResult> results = response.getResults(); Assert.assertEquals("bro", results.get(0).getSource().get("source:type")); Assert.assertEquals("data 1", results.get(0).getSource().get("ttl")); }
@Test public void testSerialization() throws Exception { EnrichmentConfig config = JSONUtils.INSTANCE.load(sourceConfigStr, EnrichmentConfig.class); Assert.assertTrue(config.getFieldMap().get("stellar") instanceof Map); Assert.assertTrue(config.getEnrichmentConfigs().get("stellar") instanceof ConfigHandler); Assert.assertEquals(Configs.STELLAR, ((ConfigHandler)config.getEnrichmentConfigs().get("stellar")).getType()); }
@Test public void throws_exception_on_aggregation_queries_on_non_string_non_numeric_fields() throws Exception { thrown.expect(InvalidSearchException.class); thrown.expectMessage("Failed to execute search"); GroupRequest request = JSONUtils.INSTANCE.load(badGroupQuery, GroupRequest.class); dao.group(request); }
@Test public void sort_by_guid() throws Exception { SearchRequest request = JSONUtils.INSTANCE.load(sortByGuidQuery, SearchRequest.class); SearchResponse response = getIndexDao().search(request); Assert.assertEquals(5, response.getTotal()); List<SearchResult> results = response.getResults(); for (int i = 0; i < 5; ++i) { Map<String, Object> source = results.get(i).getSource(); Assert.assertEquals(1, source.size()); Assert.assertEquals(source.get("guid"), "bro_" + (i + 1)); } }
public ParserDriver(String sensorType, String parserConfig, String globalConfig) throws IOException { SensorParserConfig sensorParserConfig = SensorParserConfig.fromBytes(parserConfig.getBytes()); this.sensorType = sensorType == null ? sensorParserConfig.getSensorTopic() : sensorType; config = new ParserConfigurations(); config.updateSensorParserConfig(this.sensorType, SensorParserConfig.fromBytes(parserConfig.getBytes())); config.updateGlobalConfig(JSONUtils.INSTANCE.load(globalConfig, JSONUtils.MAP_SUPPLIER)); parserRunner = new ParserRunnerImpl(new HashSet<String>() {{ add(sensorType); }}); }
@Test public void no_results_returned_when_query_does_not_match() throws Exception { SearchRequest request = JSONUtils.INSTANCE.load(noResultsFieldsQuery, SearchRequest.class); SearchResponse response = getIndexDao().search(request); Assert.assertEquals(0, response.getTotal()); }
@Test public void testEnrich() throws Exception { ThreatIntelAdapter tia = new ThreatIntelAdapter(); tia.lookup = lookup; SensorEnrichmentConfig broSc = JSONUtils.INSTANCE.load(sourceConfigStr, SensorEnrichmentConfig.class); JSONObject actualMessage = tia.enrich(new CacheKey("ip_dst_addr", "10.0.2.3", broSc)); Assert.assertNotNull(actualMessage); Assert.assertEquals(expectedMessage, actualMessage); }
@Test public void testEnrichNonString() throws Exception { ThreatIntelAdapter tia = new ThreatIntelAdapter(); tia.lookup = lookup; SensorEnrichmentConfig broSc = JSONUtils.INSTANCE.load(sourceConfigStr, SensorEnrichmentConfig.class); JSONObject actualMessage = tia.enrich(new CacheKey("ip_dst_addr", "10.0.2.3", broSc)); Assert.assertNotNull(actualMessage); Assert.assertEquals(expectedMessage, actualMessage); actualMessage = tia.enrich(new CacheKey("ip_dst_addr", 10L, broSc)); Assert.assertEquals(actualMessage,new JSONObject()); }