private <T extends JsonSerializable> String createJsonFor(T value) throws IOException { StringWriter stringWriter = new StringWriter(); JsonTelemetryDataSerializer temp = new JsonTelemetryDataSerializer(new BufferedWriter(stringWriter)); value.serialize(temp); temp.close(); String jsonStringToAppend = stringWriter.toString(); if (Strings.isNullOrEmpty(jsonStringToAppend) || JSON_EMPTY_OBJECT.equals(jsonStringToAppend)) { return ""; } return jsonStringToAppend; }
/** * THIS IS FOR DEBUGGING AND TESTING ONLY! * DON'T USE THIS IN HAPPY-PATH, PRODUCTION CODE. * * @return Json representation of this telemetry item. */ @Override public String toString() { StringWriter sw = new StringWriter(); try { JsonTelemetryDataSerializer jtds = new JsonTelemetryDataSerializer(sw); this.serialize(jtds); jtds.close(); return sw.toString(); } catch (IOException e) { // shouldn't happen with a string writer throw new RuntimeException("Error serializing "+this.getClass().getSimpleName()+" toString", e); } }
private static ArrayList<String> toJson(List<Telemetry> telemetries) throws IOException { StringWriter writer = new StringWriter(); JsonTelemetryDataSerializer jsonWriter = new JsonTelemetryDataSerializer(writer); ArrayList<String> asJsons = new ArrayList<String>(); for (Telemetry telemetry : telemetries) { telemetry.serialize(jsonWriter); jsonWriter.close(); String asJson = writer.toString(); asJsons.add(asJson); writer.getBuffer().setLength(0); jsonWriter.reset(writer); } return asJsons; } }
@Override protected boolean doSend(Telemetry telemetry) { StringWriter writer = new StringWriter(); JsonTelemetryDataSerializer jsonWriter = null; try { jsonWriter = new JsonTelemetryDataSerializer(writer); telemetry.serialize(jsonWriter); jsonWriter.close(); String asJson = writer.toString(); telemetryBuffer.add(asJson); telemetry.reset(); } catch (IOException e) { InternalLogger.INSTANCE.error("Failed to serialize Telemetry"); InternalLogger.INSTANCE.trace("Stack trace is %s", ExceptionUtils.getStackTrace(e)); return false; } return true; }
private static void verifyJson( PerformanceCounterTelemetry telemetry, String expectedCategory, String expectedCounter, String expectedInstance, double expectedValue) throws IOException { telemetry.setTimestamp(new Date()); StringWriter writer = new StringWriter(); JsonTelemetryDataSerializer jsonWriter = null; jsonWriter = new JsonTelemetryDataSerializer(writer); telemetry.serialize(jsonWriter); jsonWriter.close(); String asJson = writer.toString(); String expectedPerformanceDataPartFormat = "\"baseData\":{\"ver\":2,\"categoryName\":\"%s\",\"counterName\":\"%s\",\"instanceName\":\"%s\",\"value\":%.1f,\"properties\":null}}}"; String expected = String.format(expectedPerformanceDataPartFormat, expectedCategory, expectedCounter, expectedInstance, expectedValue); assertTrue(asJson.indexOf(expected) != -1); } }
@Test public void testTelemetryNameWithIkey_Empty() throws IOException{ StubTelemetry telemetry = new StubTelemetry("Test Base Telemetry"); telemetry.setTimestamp(new Date()); StringWriter writer = new StringWriter(); JsonTelemetryDataSerializer jsonWriter = new JsonTelemetryDataSerializer(writer); telemetry.serialize(jsonWriter); jsonWriter.close(); String asJson = writer.toString(); int index = asJson.indexOf("\"name\":\"Microsoft.ApplicationInsights.Stub\""); assertTrue(index != -1); }
@Test public void testEmptyAndDefaultSanitization() throws IOException { TestClassWithStrings testClassWithStrings = new TestClassWithStrings(); testClassWithStrings.setS1(""); StringWriter stringWriter = new StringWriter(); JsonTelemetryDataSerializer tested = new JsonTelemetryDataSerializer(stringWriter); testClassWithStrings.serialize(tested); tested.close(); String str = stringWriter.toString(); Map<String, String> recoveryMap = new Gson().fromJson(str, new TypeToken<HashMap<String, String>>() {}.getType()); assertEquals("DEFAULT s1", recoveryMap.get("s1")); assertEquals(null, recoveryMap.get("s2")); }
@Test public void testWriteNotRequiredMethodWithEmptyValue() throws IOException { TestClassWithStrings testClassWithStrings = new TestClassWithStrings(); testClassWithStrings.setS2(""); StringWriter stringWriter = new StringWriter(); JsonTelemetryDataSerializer tested = new JsonTelemetryDataSerializer(stringWriter); testClassWithStrings.serialize(tested); tested.close(); String str = stringWriter.toString(); Map<String, String> recoveryMap = new Gson().fromJson(str, new TypeToken<HashMap<String, String>>() {}.getType()); assertEquals("DEFAULT s1", recoveryMap.get("s1")); assertNull(recoveryMap.get("s2")); }
@Test public void testLengthOfStrings() throws IOException { TestClassWithStrings testClassWithStrings = new TestClassWithStrings(); String s1 = TelemetryTestsUtils.createString(110); testClassWithStrings.setS1(s1); testClassWithStrings.setS2("abc"); StringWriter stringWriter = new StringWriter(); JsonTelemetryDataSerializer tested = new JsonTelemetryDataSerializer(stringWriter); testClassWithStrings.serialize(tested); tested.close(); String str = stringWriter.toString(); TestClassWithStrings bac = new Gson().fromJson(str, TestClassWithStrings.class); Map<String, String> recoveryMap = new Gson().fromJson(str, new TypeToken<HashMap<String, String>>() {}.getType()); assertNotEquals((recoveryMap.get("s1")).length(), s1.length()); assertNotEquals(bac, testClassWithStrings); }
@Test public void testStrings() throws IOException { TestClassWithStrings testClassWithStrings = new TestClassWithStrings(); testClassWithStrings.setS1("s1"); testClassWithStrings.setS2("s2"); StringWriter stringWriter = new StringWriter(); JsonTelemetryDataSerializer tested = new JsonTelemetryDataSerializer(stringWriter); testClassWithStrings.serialize(tested); tested.close(); String str = stringWriter.toString(); TestClassWithStrings bac = new Gson().fromJson(str, TestClassWithStrings.class); assertEquals(bac, testClassWithStrings); }
@Test public void testTelemetryNameWithIkey() throws IOException{ StubTelemetry telemetry = new StubTelemetry("Test Base Telemetry"); telemetry.getContext().setInstrumentationKey("AIF-00000000-1111-2222-3333-000000000000"); telemetry.setTimestamp(new Date()); StringWriter writer = new StringWriter(); JsonTelemetryDataSerializer jsonWriter = new JsonTelemetryDataSerializer(writer); telemetry.serialize(jsonWriter); jsonWriter.close(); String asJson = writer.toString(); int index = asJson.indexOf("\"name\":\"Microsoft.ApplicationInsights.aif00000000111122223333000000000000.Stub\""); assertTrue(index != -1); }
@Test public void testTelemetryNameWithIkey_SpecialChar() throws IOException{ StubTelemetry telemetry = new StubTelemetry("Test Base Telemetry"); telemetry.getContext().setInstrumentationKey("--. .--"); telemetry.setTimestamp(new Date()); StringWriter writer = new StringWriter(); JsonTelemetryDataSerializer jsonWriter = new JsonTelemetryDataSerializer(writer); telemetry.serialize(jsonWriter); jsonWriter.close(); String asJson = writer.toString(); int index = asJson.indexOf("\"name\":\"Microsoft.ApplicationInsights.Stub\""); assertTrue(index != -1); }
@Test public void testSanitization() throws IOException { TestClassWithStrings testClassWithStrings = new TestClassWithStrings(); String s1 = "\\'\\f\\b\\f\\n\\r\\t/\\"; String s2 = "0x0021\t"; testClassWithStrings.setS1(s1); testClassWithStrings.setS2(s2); StringWriter stringWriter = new StringWriter(); JsonTelemetryDataSerializer tested = new JsonTelemetryDataSerializer(stringWriter); testClassWithStrings.serialize(tested); tested.close(); String str = stringWriter.toString(); Map<String, String> recoveryMap = new Gson().fromJson(str, new TypeToken<HashMap<String, String>>() {}.getType()); assertEquals("\\'\\f\\b\\f\\n\\r\\t/\\", recoveryMap.get("s1")); assertEquals("0x0021\t", recoveryMap.get("s2")); }
jsonWriter.close(); String asJson = writer.toString();
@Test public void testFloatingPointInfinity() throws IOException { StubClass stubClass = new StubClass(); stubClass.setF1(Float.POSITIVE_INFINITY); stubClass.setF2(Float.POSITIVE_INFINITY); stubClass.setD1(Double.POSITIVE_INFINITY); stubClass.setD2(Double.POSITIVE_INFINITY); StringWriter stringWriter = new StringWriter(); JsonTelemetryDataSerializer tested = new JsonTelemetryDataSerializer(stringWriter); stubClass.serialize(tested); tested.close(); String str = stringWriter.toString(); System.out.println("[testFloatingPointInfinity] Serialized StubClass: " + str); Gson gson = new Gson(); StubClass bac = gson.fromJson(str, StubClass.class); final double epsilon = Math.ulp(0.0); assertEquals(0, bac.f1, epsilon); assertEquals(0, bac.f2, epsilon); assertEquals(0, bac.d1, epsilon); assertEquals(0, bac.d2, epsilon); } }
@Test public void testFloatingPointNaNs() throws IOException { StubClass stubClass = new StubClass(); stubClass.setF1(Float.NaN); stubClass.setF2(Float.NaN); stubClass.setD1(Double.NaN); stubClass.setD2(Double.NaN); StringWriter stringWriter = new StringWriter(); JsonTelemetryDataSerializer tested = new JsonTelemetryDataSerializer(stringWriter); stubClass.serialize(tested); tested.close(); String str = stringWriter.toString(); System.out.println("[testFloatingPointNaNs] Serialized StubClass: " + str); Gson gson = new Gson(); StubClass bac = gson.fromJson(str, StubClass.class); final double epsilon = Math.ulp(0.0); assertEquals(0, bac.f1, epsilon); assertEquals(0, bac.f2, epsilon); assertEquals(0, bac.d1, epsilon); assertEquals(0, bac.d2, epsilon); }
private static void verifyEnvelope(SessionStateData sessionStateData, SessionState expectedState) throws IOException { Envelope envelope = new Envelope(); envelope.setName((new SessionStateTelemetry()).getEnvelopName()); Data<SessionStateData> tmp = new Data<SessionStateData>(); tmp.setBaseData(sessionStateData); tmp.setBaseType((new SessionStateTelemetry()).getBaseTypeName()); envelope.setData(tmp); StringWriter writer = new StringWriter(); JsonTelemetryDataSerializer jsonWriter = new JsonTelemetryDataSerializer(writer); envelope.serialize(jsonWriter); jsonWriter.close(); String asJson = writer.toString(); String expectedDataAsString = String.format("\"data\":{\"baseType\":\"SessionStateData\",\"baseData\":{\"ver\":2,\"state\":\"%s\"}}", expectedState.toString()); int index = asJson.indexOf(expectedDataAsString); assertTrue(index != -1); index = asJson.indexOf("\"name\":\"SessionState\""); assertTrue(index != -1); } }
JsonTelemetryDataSerializer tested = new JsonTelemetryDataSerializer(stringWriter); stubClass.serialize(tested); tested.close(); String str = stringWriter.toString();