private boolean compareArraysByStrictOrder(JSONArray expected, JSONArray actual) { if (expected.size() != actual.size()) { return false; } for (int index = 0; index < expected.size(); index++) { Object expectedElt = expected.get(index); Object actualElt = actual.get(index); if (!compare(expectedElt, actualElt)) { return false; } } return true; }
private static PerfCounterPack extractPerfCounterPack(JSONArray perfJson, String objName) { PerfCounterPack perfPack = new PerfCounterPack(); perfPack.time = System.currentTimeMillis(); perfPack.timetype = TimeTypeEnum.REALTIME; perfPack.objName = objName; for (int i = 0; i < perfJson.size(); i++) { JSONObject perf = (JSONObject) perfJson.get(i); String name = (String) perf.get("name"); Number value = (Number) perf.get("value"); perfPack.data.put(name, new FloatValue(value.floatValue())); } return perfPack; }
String uuid = (String) ((JSONObject) json.get(0)).get("id"); return UuidUtils.fromFlatString(uuid);
public static void process(Reader in) { JSONParser parser = new JSONParser(); try { Object o = parser.parse(in); if (o instanceof JSONArray) { JSONArray jsonArray = (JSONArray) o; for (int i = 0; i <jsonArray.size(); i++) { Object element = jsonArray.get(i); if (element instanceof JSONObject) { process((JSONObject) element); } } } else if (o instanceof JSONObject) { process((JSONObject) o); } else { scouter.server.Logger.println("SC-8001", 30, "Incorrect body"); } } catch(Throwable th) { scouter.server.Logger.println("SC-8000", 30, "Http body parsing error", th); } }
/** * Reads a LootItem from its JSON form. * * @param object a LootItem in JSON form */ public LootItem(JSONObject object) { defaultItem = new DefaultLootItem((JSONObject) object.get("default")); if (object.containsKey("conditions")) { JSONArray array = (JSONArray) object.get("conditions"); conditionalItems = new ConditionalLootItem[array.size()]; for (int i = 0; i < array.size(); i++) { JSONObject json = (JSONObject) array.get(i); conditionalItems[i] = new ConditionalLootItem(json); } } else { conditionalItems = NO_ITEMS; } } }
/** * Parses a loot table from JSON. * * @param object a loot table in JSON form */ public EntityLootTable(JSONObject object) { if (object.containsKey("experience")) { // NON-NLS this.experience = new LootRandomValues((JSONObject) object.get("experience")); // NON-NLS } else { this.experience = null; } if (object.containsKey("items")) { // NON-NLS JSONArray array = (JSONArray) object.get("items"); // NON-NLS this.items = new LootItem[array.size()]; for (int i = 0; i < array.size(); i++) { JSONObject json = (JSONObject) array.get(i); this.items[i] = new LootItem(json); } } else { this.items = NO_ITEMS; } } }
JSONObject counterInfo = (JSONObject)countersArray.get(i); String name = (String) counterInfo.get("name"); String unit = (String) counterInfo.get("unit"); JSONObject counterInfo = (JSONObject)countersArray.get(i); String name = (String) counterInfo.get("name"); if (objectType.getCounter(name) != null) continue;
private Object extractValue(Object payload, String key) { if (payload instanceof JSONObject) { return ((JSONObject) payload).get(key); } else if (payload instanceof JSONArray) { return ((JSONArray) payload).get(Integer.parseInt(key)); } else { return null; } } }
/** * Loads the class to * * @param stream label index stream * @return Map of integer -> label name * @throws IOException when the stream breaks unexpectedly * @throws ParseException when the input doesn't contain a valid JSON map */ public Map<Integer, String> loadClassIndex(InputStream stream) throws IOException, ParseException { String content = IOUtils.toString(stream); JSONObject jIndex = (JSONObject) new JSONParser().parse(content); Map<Integer, String> classMap = new HashMap<>(); for (Object key : jIndex.keySet()) { JSONArray names = (JSONArray) jIndex.get(key); classMap.put(Integer.parseInt(key.toString()), names.get(names.size() - 1).toString()); } return classMap; }
private boolean replaceKeyArray(JSONObject payload, String toKey, String[] fromKeys) { for (String fromKey : fromKeys) { if (payload.containsKey(fromKey)) { JSONArray value = (JSONArray) payload.remove(fromKey); if (value != null && !value.isEmpty()) { payload.put(toKey, value.get(0)); _LOG.trace("[Metron] Added {} to {}", toKey, payload); return true; } } } return false; }
@SuppressWarnings("rawtypes") @Test public void testFilesBroMessage() throws ParseException { Map rawMessageMap = (Map) jsonParser.parse(filesBroMessage); JSONObject rawJson = (JSONObject) rawMessageMap.get(rawMessageMap.keySet().iterator().next()); JSONObject broJson = broParser.parse(filesBroMessage.getBytes()).get(0); String expectedBroTimestamp = "1425845251.334"; Assert.assertEquals(broJson.get("bro_timestamp"), expectedBroTimestamp); String expectedTimestamp = "1425845251334"; Assert.assertEquals(broJson.get("timestamp").toString(), expectedTimestamp); Assert.assertEquals(broJson.get("ip_src_addr").toString(), ((JSONArray)rawJson.get("tx_hosts")).get(0).toString()); Assert.assertEquals(broJson.get("ip_dst_addr").toString(), ((JSONArray)rawJson.get("rx_hosts")).get(0).toString()); Assert.assertTrue(broJson.get("original_string").toString().startsWith(rawMessageMap.keySet().iterator().next().toString().toUpperCase())); Assert.assertEquals(broJson.get("fuid").toString(), rawJson.get("fuid").toString()); Assert.assertEquals(broJson.get("md5").toString(), rawJson.get("md5").toString()); Assert.assertEquals(broJson.get("analyzers").toString(), rawJson.get("analyzers").toString()); Assert.assertTrue(broJson.get("original_string").toString().startsWith("FILES")); }
@Test @SuppressWarnings("unchecked") public void testPatchNotAllowedAlert() throws ParseException { PatchRequest pr = new PatchRequest(); Map<String, Object> patch = (JSONObject) new JSONParser().parse(alertPatchRequest); pr.setPatch(Collections.singletonList((JSONObject) ((JSONArray) patch.get("patch")).get(0))); assertFalse(dao.isPatchAllowed(pr)); }
@Test @SuppressWarnings("unchecked") public void testPatchNotAllowedStatus() throws ParseException { PatchRequest pr = new PatchRequest(); Map<String, Object> patch = (JSONObject) new JSONParser().parse(statusPatchRequest); pr.setPatch(Collections.singletonList((JSONObject) ((JSONArray) patch.get("patch")).get(0))); assertFalse(dao.isPatchAllowed(pr)); }
@Test @SuppressWarnings("unchecked") public void testPatchAllowedName() throws ParseException { PatchRequest pr = new PatchRequest(); Map<String, Object> patch = (JSONObject) new JSONParser().parse(namePatchRequest); pr.setPatch(Collections.singletonList((JSONObject) ((JSONArray) patch.get("patch")).get(0))); assertTrue(dao.isPatchAllowed(pr)); }
@Test public void testOutput() throws ParseException { List<MetricName> inputMetricNames = new ArrayList<MetricName>() {{ add(new MetricName("foo", false)); add(new MetricName("bar", false)); }}; String output = handler.getSerializedJSON(inputMetricNames); JSONParser jsonParser = new JSONParser(); JSONArray tokenInfos = (JSONArray) jsonParser.parse(output); Assert.assertEquals("Unexpected result size", 2, tokenInfos.size()); Set<String> expectedOutputSet = new HashSet<String>(); for (MetricName metricName : inputMetricNames) { expectedOutputSet.add(metricName.getName() + "|" + metricName.isCompleteName()); } Set<String> outputSet = new HashSet<String>(); for (int i = 0; i< inputMetricNames.size(); i++) { JSONObject object = (JSONObject) tokenInfos.get(i); Iterator it = object.entrySet().iterator(); while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); outputSet.add(entry.getKey() + "|" + entry.getValue()); } } Assert.assertEquals("Unexpected size", expectedOutputSet.size(), outputSet.size()); Assert.assertTrue("Output contains no more elements than expected", expectedOutputSet.containsAll(outputSet)); Assert.assertTrue("Output contains no less elements than expected", outputSet.containsAll(expectedOutputSet)); }
@Test public void testSets() throws Exception { final JSONBasicRollupsOutputSerializer serializer = new JSONBasicRollupsOutputSerializer(); final MetricData metricData = new MetricData( FakeMetricDataGenerator.generateFakeSetRollupPoints(), "unknown"); JSONObject metricDataJSON = serializer.transformRollupData(metricData, PlotRequestParser.DEFAULT_SET); final JSONArray data = (JSONArray)metricDataJSON.get("values"); Assert.assertEquals(5, data.size()); for (int i = 0; i < data.size(); i++) { final JSONObject dataJSON = (JSONObject)data.get(i); Assert.assertNotNull(dataJSON.get("numPoints")); Assert.assertEquals(Sets.newHashSet(i, i % 2, i / 2).size(), dataJSON.get("numPoints")); } }
@Test public void testCounters() throws Exception { final JSONBasicRollupsOutputSerializer serializer = new JSONBasicRollupsOutputSerializer(); final MetricData metricData = new MetricData( FakeMetricDataGenerator.generateFakeCounterRollupPoints(), "unknown"); JSONObject metricDataJSON = serializer.transformRollupData(metricData, PlotRequestParser.DEFAULT_COUNTER); final JSONArray data = (JSONArray)metricDataJSON.get("values"); Assert.assertEquals(5, data.size()); for (int i = 0; i < data.size(); i++) { final JSONObject dataJSON = (JSONObject)data.get(i); Assert.assertNotNull(dataJSON.get("numPoints")); Assert.assertEquals(i+1, dataJSON.get("numPoints")); Assert.assertNotNull(dataJSON.get("sum")); Assert.assertEquals((long) (i + 1000), dataJSON.get("sum")); Assert.assertNull(dataJSON.get("rate")); } }
@Test public void setTimers() throws Exception { final JSONBasicRollupsOutputSerializer serializer = new JSONBasicRollupsOutputSerializer(); final MetricData metricData = new MetricData( FakeMetricDataGenerator.generateFakeTimerRollups(), "unknown"); JSONObject metricDataJSON = serializer.transformRollupData(metricData, PlotRequestParser.DEFAULT_TIMER); final JSONArray data = (JSONArray)metricDataJSON.get("values"); Assert.assertEquals(5, data.size()); for (int i = 0; i < data.size(); i++) { final JSONObject dataJSON = (JSONObject)data.get(i); Assert.assertNotNull(dataJSON.get("numPoints")); Assert.assertNotNull(dataJSON.get("average")); Assert.assertNotNull(dataJSON.get("rate")); // bah. I'm too lazy to check equals. } }
@Test public void testTransformRollupDataAtFullRes() throws Exception { final JSONBasicRollupsOutputSerializer serializer = new JSONBasicRollupsOutputSerializer(); final MetricData metricData = new MetricData(FakeMetricDataGenerator.generateFakeFullResPoints(), "unknown"); JSONObject metricDataJSON = serializer.transformRollupData(metricData, filterStats); final JSONArray data = (JSONArray) metricDataJSON.get("values"); // Assert that we have some data to test Assert.assertTrue(data.size() > 0); for (int i = 0; i < data.size(); i++) { final JSONObject dataJSON = (JSONObject) data.get(i); final Points.Point<SimpleNumber> point = (Points.Point<SimpleNumber>) metricData.getData().getPoints().get(dataJSON.get("timestamp")); Assert.assertEquals(point.getData().getValue(), dataJSON.get("average")); Assert.assertEquals(point.getData().getValue(), dataJSON.get("min")); Assert.assertEquals(point.getData().getValue(), dataJSON.get("max")); // Assert that variance isn't present Assert.assertNull(dataJSON.get("variance")); // Assert numPoints isn't present Assert.assertNull(dataJSON.get("numPoints")); // Assert sum isn't present Assert.assertNull(dataJSON.get("sum")); } }
@Test public void testGauges() throws Exception { final JSONBasicRollupsOutputSerializer serializer = new JSONBasicRollupsOutputSerializer(); final MetricData metricData = new MetricData( FakeMetricDataGenerator.generateFakeGaugeRollups(), "unknown"); JSONObject metricDataJSON = serializer.transformRollupData(metricData, PlotRequestParser.DEFAULT_GAUGE); final JSONArray data = (JSONArray)metricDataJSON.get("values"); Assert.assertEquals(5, data.size()); for (int i = 0; i < data.size(); i++) { final JSONObject dataJSON = (JSONObject)data.get(i); Assert.assertNotNull(dataJSON.get("numPoints")); Assert.assertEquals(1L, dataJSON.get("numPoints")); Assert.assertNotNull("latest"); Assert.assertEquals(i, dataJSON.get("latest")); // other fields were filtered out. Assert.assertNull(dataJSON.get(MetricStat.AVERAGE.toString())); Assert.assertNull(dataJSON.get(MetricStat.VARIANCE.toString())); Assert.assertNull(dataJSON.get(MetricStat.MIN.toString())); Assert.assertNull(dataJSON.get(MetricStat.MAX.toString())); } }