Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MikhailSuendukov committed Aug 16, 2023
1 parent 3bc6119 commit bafad1f
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,6 @@ public void run() {
eventLog.setTag(aTransmissionTarget);
if (aTransmissionTarget == mDefaultTransmissionTarget) {
eventLog.setUserId(userId);
eventLog.setDataResidencyRegion(AppCenter.getDataResidencyRegion());
}
} else {
AppCenterLog.error(LOG_TAG, "This transmission target is disabled.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public abstract class AbstractLog implements Log {
/**
* timestamp property.
*/
private static final String TIMESTAMP = "timestamp";
@VisibleForTesting
static final String TIMESTAMP = "timestamp";

/**
* Session identifier property.
Expand All @@ -58,7 +59,8 @@ public abstract class AbstractLog implements Log {
/**
* Data residency region property.
*/
private static final String DATA_RESIDENCY_REGION = "dataResidencyRegion";
@VisibleForTesting
static final String DATA_RESIDENCY_REGION = "dataResidencyRegion";

/**
* Collection of transmissionTargetTokens that this log should be sent to.
Expand Down Expand Up @@ -194,7 +196,7 @@ public void write(JSONStringer writer) throws JSONException {
writer.endObject();
}
if (getDataResidencyRegion() != null) {
JSONUtils.write(writer, DATA_RESIDENCY_REGION, getUserId());
JSONUtils.write(writer, DATA_RESIDENCY_REGION, getDataResidencyRegion());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,16 @@ public void setCountryCode() {
DeviceInfoHelper.setCountryCode(eq(expectedCountryCode));
}

@Test
public void setDataResidencyRegion() {

/* Set country code. */
String expectedDataResidencyRegion = "rg";
AppCenter.setDataResidencyRegion(expectedDataResidencyRegion);

assertEquals(AppCenter.getDataResidencyRegion(), expectedDataResidencyRegion);
}

@Test
public void setDefaultLogLevelRelease() {
mApplicationInfo.flags = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@

package com.microsoft.appcenter.ingestion.models;

import com.microsoft.appcenter.AppCenter;
import com.microsoft.appcenter.ingestion.models.json.JSONDateUtils;
import com.microsoft.appcenter.ingestion.models.json.JSONUtils;
import com.microsoft.appcenter.test.TestUtils;
import com.microsoft.appcenter.utils.AppCenterLog;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONStringer;
import org.junit.Test;
import org.powermock.core.classloader.annotations.PrepareForTest;

import java.util.Date;
import java.util.UUID;

import static com.microsoft.appcenter.ingestion.models.AbstractLog.DATA_RESIDENCY_REGION;
import static com.microsoft.appcenter.ingestion.models.AbstractLog.TIMESTAMP;
import static com.microsoft.appcenter.test.TestUtils.checkEquals;
import static com.microsoft.appcenter.test.TestUtils.checkNotEquals;
import static org.junit.Assert.assertEquals;
Expand All @@ -24,10 +31,15 @@
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.verifyStatic;

@SuppressWarnings("unused")
public class AbstractLogTest {

@PrepareForTest({
JSONUtils.class
})

@SuppressWarnings("InstantiationOfUtilityClass")
@Test
public void utilsCoverage() {
Expand Down Expand Up @@ -93,6 +105,16 @@ public void compare() {
b.setUserId(userId1);
checkEquals(a, b);

/* Data residency region. */
String dataResidencyRegion1 = "rg1";
String dataResidencyRegion2 = "rg2";
a.setDataResidencyRegion(dataResidencyRegion1);
checkNotEquals(a, b);
b.setDataResidencyRegion(dataResidencyRegion2);
checkNotEquals(a, b);
b.setDataResidencyRegion(dataResidencyRegion1);
checkEquals(a, b);

/* Device. */
Device d1 = new Device();
d1.setLocale("a");
Expand Down Expand Up @@ -149,6 +171,36 @@ public void writeNullDeviceTest() throws JSONException {
verify(mockJsonStringer, never()).key(AbstractLog.DEVICE);
}

@Test
public void readNotNullDataResidencyRegionTest() throws JSONException {
JSONObject mockJsonObject = mock(JSONObject.class);
when(mockJsonObject.has(DATA_RESIDENCY_REGION)).thenReturn(true);
when(mockJsonObject.optString(DATA_RESIDENCY_REGION)).thenReturn("RG");
when(mockJsonObject.getString(CommonProperties.TYPE)).thenReturn("mockType");
when(mockJsonObject.getString(TIMESTAMP)).thenReturn(JSONDateUtils.toString(new Date()));

AbstractLog mockLog = new MockLogWithType();
mockLog.read(mockJsonObject);

verify(mockJsonObject).optString(DATA_RESIDENCY_REGION, null);
}

@Test
public void writeNotNullDataResidencyRegionTest() throws JSONException {
JSONStringer mockJsonStringer = mock(JSONStringer.class);
when(mockJsonStringer.key(anyString())).thenReturn(mockJsonStringer);
when(mockJsonStringer.value(anyString())).thenReturn(mockJsonStringer);

AbstractLog mockLog = new MockLog();
mockLog.setTimestamp(new Date());
mockLog.setDataResidencyRegion("RG");
mockLog.write(mockJsonStringer);


verifyStatic(JSONUtils.class);
JSONUtils.write(mockJsonStringer, DATA_RESIDENCY_REGION, anyString());
}

private static class MockLog extends AbstractLog {

@Override
Expand Down

0 comments on commit bafad1f

Please sign in to comment.