Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Session payload versions #1911

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.io.OutputStreamWriter;
import java.io.Writer;

public class SessionV1PayloadTest {
public class SessionLegacyPayloadTest {

private File file;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.bugsnag.android

import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import java.io.BufferedWriter
import java.io.File
import java.io.FileOutputStream
import java.io.OutputStreamWriter
import java.io.Writer
import java.util.Date

class SessionPayloadTest {
lateinit var session: Session

@Before
fun setUp() {
session = BugsnagTestUtils.generateSession()
session.app = BugsnagTestUtils.generateApp()
session.device = BugsnagTestUtils.generateDevice()
}

/**
* Serialises sessions from a file instead
*/

@Test
fun testSessionV2PayloadFromFile() {
// write file to disk

val v2File = File.createTempFile(
"150450000000053a27e4e-967c-4e5c-91be-2e86f2eb7cdc",
"_v2.json"
)
val fos = FileOutputStream(v2File.absolutePath)
val out: Writer = BufferedWriter(OutputStreamWriter(fos, "UTF-8"))
val stream = JsonStream(out)
session.toStream(stream)
out.flush()
val v2Payload = Session(v2File, Notifier(), NoopLogger, "_my-uuid-uuuuuuuuuuuuuuuuuuuuuuuuuuuu")
val v2Obj = BugsnagTestUtils.streamableToJson(v2Payload)
assertNotNull(v2Obj.getJSONObject("app"))
assertNotNull(v2Obj.getJSONObject("device"))
assertNotNull(v2Obj.getJSONObject("notifier"))
val sessions = v2Obj.getJSONArray("sessions")
assertNotNull(sessions)
assertEquals(1, sessions.length().toLong())
val session = sessions.getJSONObject(0)
assertEquals("test", session.getString("id"))
assertFalse(session.has("notifier"))
}

@Test
fun testSessionV3PayloadFromFile() {
// write file to disk

val v3File = File.createTempFile(
"150450000000053a27e4e-967c-4e5c-91be-2e86f2eb7cdc",
"_v3.json"
)
val fos = FileOutputStream(v3File.absolutePath)
val out: Writer = BufferedWriter(OutputStreamWriter(fos, "UTF-8"))
val stream = JsonStream(out)
session.toStream(stream)
out.flush()
val v3Payload = Session(v3File, Notifier(), NoopLogger, "_my-uuid-uuuuuuuuuuuuuuuuuuuuuuuuuuuu")
val v3Object = BugsnagTestUtils.streamableToJson(v3Payload)
assertNotNull(v3Object)
assertNotNull(v3Object.getJSONObject("app"))
assertNotNull(v3Object.getJSONObject("device"))
assertNotNull(v3Object.getJSONObject("notifier"))
val sessions = v3Object.getJSONArray("sessions")
assertNotNull(sessions)
assertEquals(1, sessions.length().toLong())
val session = sessions.getJSONObject(0)
assertEquals("test", session.getString("id"))
assertFalse(session.has("notifier"))
}

@Test
fun testAutoCapturedOverride() {
session = Session(
"id",
Date(),
null,
false,
Notifier(),
NoopLogger,
"TEST APIKEY"
)
assertFalse(session.isAutoCaptured)
session.isAutoCaptured = true
assertTrue(session.isAutoCaptured)
val obj = BugsnagTestUtils.streamableToJson(session)
val sessionNode = obj.getJSONArray("sessions").getJSONObject(0)
assertFalse(sessionNode.has("user"))
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,10 @@ void setAutoCaptured(boolean autoCaptured) {
*
* @return whether the payload is v2
*/
boolean isV2Payload() {
return file != null && file.getName().endsWith("_v2.json");

boolean isLegacyPayload() {
return !(file != null
&& (file.getName().endsWith("_v2.json") || file.getName().endsWith("_v3.json")));
}

Notifier getNotifier() {
Expand All @@ -227,10 +229,10 @@ Notifier getNotifier() {
@Override
public void toStream(@NonNull JsonStream writer) throws IOException {
if (file != null) {
if (isV2Payload()) {
serializeV2Payload(writer);
if (!isLegacyPayload()) {
serializePayload(writer);
} else {
serializeV1Payload(writer);
serializeLegacyPayload(writer);
}
} else {
writer.beginObject();
Expand All @@ -244,11 +246,11 @@ public void toStream(@NonNull JsonStream writer) throws IOException {
}
}

private void serializeV2Payload(@NonNull JsonStream writer) throws IOException {
private void serializePayload(@NonNull JsonStream writer) throws IOException {
writer.value(file);
}

private void serializeV1Payload(@NonNull JsonStream writer) throws IOException {
private void serializeLegacyPayload(@NonNull JsonStream writer) throws IOException {
writer.beginObject();
writer.name("notifier").value(notifier);
writer.name("app").value(app);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ void flushStoredSession(File storedFile) {
storedFile, client.getNotifier(), logger, configuration.getApiKey()
);

if (!payload.isV2Payload()) { // collect data here
if (payload.isLegacyPayload()) { // collect data here
payload.setApp(client.getAppDataCollector().generateApp());
payload.setDevice(client.getDeviceDataCollector().generateDevice());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,17 @@ class SessionTest {
}

@Test
fun isV2() {
assertFalse(session.isV2Payload)
fun isLegacyPayload() {
assertTrue(session.isLegacyPayload)
val file = File("150450000000053a27e4e-967c-4e5c-91be-2e86f2eb7cdc.json")
assertFalse(Session(file, Notifier(), NoopLogger, apiKey).isV2Payload)
assertTrue(
assertTrue(Session(file, Notifier(), NoopLogger, apiKey).isLegacyPayload)
assertFalse(
Session(
File("150450000000053a27e4e-967c-4e5c-91be-2e86f2eb7cdc_v2.json"),
Notifier(),
NoopLogger,
apiKey
).isV2Payload
).isLegacyPayload
)
}

Expand Down