From d237fc1a62aa1726f94d9a799af011ce8beabea1 Mon Sep 17 00:00:00 2001 From: jason plumb <75337021+breedx-splk@users.noreply.github.com> Date: Mon, 26 Sep 2022 11:06:07 -0700 Subject: [PATCH] Add carrier information to spans (#358) * overload constructor for null * Add CarrierFinder and add Carrier to CurrentNetwork. * spotless * relax Q to P * add builder for CurrentNetwork * change type and eliminate double null check * rename class to match version 28 * set NET_HOST_CONNECTION_TYPE in common method * change optional to null * regen tostring * address review suggestions * fix test NPE * remove unused constructor --- .../src/main/java/com/splunk/rum/Carrier.java | 123 ++++++++++++++ .../java/com/splunk/rum/CarrierFinder.java | 56 +++++++ .../java/com/splunk/rum/ConnectionUtil.java | 4 +- .../java/com/splunk/rum/CurrentNetwork.java | 95 +++++++++-- .../java/com/splunk/rum/NetworkDetector.java | 4 +- .../java/com/splunk/rum/NetworkMonitor.java | 18 +- ...tor.java => PostApi28NetworkDetector.java} | 19 ++- .../com/splunk/rum/RumAttributeAppender.java | 26 ++- .../com/splunk/rum/SimpleNetworkDetector.java | 15 +- .../com/splunk/rum/CarrierFinderTest.java | 65 ++++++++ .../com/splunk/rum/ConnectionUtilTest.java | 40 +++-- .../rum/MemoryBufferingExporterTest.java | 6 +- .../com/splunk/rum/NetworkDetectorTest.java | 2 +- .../com/splunk/rum/NetworkMonitorTest.java | 19 ++- .../rum/PostApi28NetworkDetectorTest.java | 150 +++++++++++++++++ .../rum/PostApi29NetworkDetectorTest.java | 155 ------------------ .../splunk/rum/RumAttributeAppenderTest.java | 5 +- .../com/splunk/rum/RumInitializerTest.java | 4 +- .../splunk/rum/SimpleNetworkDetectorTest.java | 14 +- 19 files changed, 585 insertions(+), 235 deletions(-) create mode 100644 splunk-otel-android/src/main/java/com/splunk/rum/Carrier.java create mode 100644 splunk-otel-android/src/main/java/com/splunk/rum/CarrierFinder.java rename splunk-otel-android/src/main/java/com/splunk/rum/{PostApi29NetworkDetector.java => PostApi28NetworkDetector.java} (86%) create mode 100644 splunk-otel-android/src/test/java/com/splunk/rum/CarrierFinderTest.java create mode 100644 splunk-otel-android/src/test/java/com/splunk/rum/PostApi28NetworkDetectorTest.java delete mode 100644 splunk-otel-android/src/test/java/com/splunk/rum/PostApi29NetworkDetectorTest.java diff --git a/splunk-otel-android/src/main/java/com/splunk/rum/Carrier.java b/splunk-otel-android/src/main/java/com/splunk/rum/Carrier.java new file mode 100644 index 000000000..e402c34c9 --- /dev/null +++ b/splunk-otel-android/src/main/java/com/splunk/rum/Carrier.java @@ -0,0 +1,123 @@ +/* + * Copyright Splunk Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.splunk.rum; + +import android.os.Build; +import android.telephony.TelephonyManager; +import androidx.annotation.Nullable; +import androidx.annotation.RequiresApi; +import java.util.Objects; + +@RequiresApi(api = Build.VERSION_CODES.P) +class Carrier { + + private final int id; + private final @Nullable String name; + private final @Nullable String mobileCountryCode; // 3 digits + private final @Nullable String mobileNetworkCode; // 2 or 3 digits + private final @Nullable String isoCountryCode; + + static Builder builder() { + return new Builder(); + } + + Carrier(Builder builder) { + this.id = builder.id; + this.name = builder.name; + this.mobileCountryCode = builder.mobileCountryCode; + this.mobileNetworkCode = builder.mobileNetworkCode; + this.isoCountryCode = builder.isoCountryCode; + } + + int getId() { + return id; + } + + @Nullable + String getName() { + return name; + } + + @Nullable + String getMobileCountryCode() { + return mobileCountryCode; + } + + @Nullable + String getMobileNetworkCode() { + return mobileNetworkCode; + } + + @Nullable + String getIsoCountryCode() { + return isoCountryCode; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Carrier carrier = (Carrier) o; + return id == carrier.id + && Objects.equals(name, carrier.name) + && Objects.equals(mobileCountryCode, carrier.mobileCountryCode) + && Objects.equals(mobileNetworkCode, carrier.mobileNetworkCode) + && Objects.equals(isoCountryCode, carrier.isoCountryCode); + } + + @Override + public int hashCode() { + return Objects.hash(id, name, mobileCountryCode, mobileNetworkCode, isoCountryCode); + } + + static class Builder { + private int id = TelephonyManager.UNKNOWN_CARRIER_ID; + private @Nullable String name = null; + private @Nullable String mobileCountryCode = null; + private @Nullable String mobileNetworkCode = null; + private @Nullable String isoCountryCode = null; + + Carrier build() { + return new Carrier(this); + } + + Builder id(int id) { + this.id = id; + return this; + } + + Builder name(String name) { + this.name = name; + return this; + } + + Builder mobileCountryCode(String countryCode) { + this.mobileCountryCode = countryCode; + return this; + } + + Builder mobileNetworkCode(String networkCode) { + this.mobileNetworkCode = networkCode; + return this; + } + + Builder isoCountryCode(String isoCountryCode) { + this.isoCountryCode = isoCountryCode; + return this; + } + } +} diff --git a/splunk-otel-android/src/main/java/com/splunk/rum/CarrierFinder.java b/splunk-otel-android/src/main/java/com/splunk/rum/CarrierFinder.java new file mode 100644 index 000000000..9215bf902 --- /dev/null +++ b/splunk-otel-android/src/main/java/com/splunk/rum/CarrierFinder.java @@ -0,0 +1,56 @@ +/* + * Copyright Splunk Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.splunk.rum; + +import android.os.Build; +import android.telephony.TelephonyManager; +import androidx.annotation.RequiresApi; + +@RequiresApi(api = Build.VERSION_CODES.P) +class CarrierFinder { + + private final TelephonyManager telephonyManager; + + CarrierFinder(TelephonyManager telephonyManager) { + this.telephonyManager = telephonyManager; + } + + Carrier get() { + Carrier.Builder builder = Carrier.builder(); + int id = telephonyManager.getSimCarrierId(); + builder.id(id); + CharSequence name = telephonyManager.getSimCarrierIdName(); + if (validString(name)) { + builder.name(name.toString()); + } + String simOperator = telephonyManager.getSimOperator(); + if (validString(simOperator) && simOperator.length() >= 5) { + String countryCode = simOperator.substring(0, 3); + String networkCode = simOperator.substring(3); + builder.mobileCountryCode(countryCode).mobileNetworkCode(networkCode); + } + String isoCountryCode = telephonyManager.getSimCountryIso(); + if (validString(isoCountryCode)) { + builder.isoCountryCode(isoCountryCode); + } + return builder.build(); + } + + private boolean validString(CharSequence str) { + return !(str == null || str.length() == 0); + } +} diff --git a/splunk-otel-android/src/main/java/com/splunk/rum/ConnectionUtil.java b/splunk-otel-android/src/main/java/com/splunk/rum/ConnectionUtil.java index 9267e281d..03e3ee918 100644 --- a/splunk-otel-android/src/main/java/com/splunk/rum/ConnectionUtil.java +++ b/splunk-otel-android/src/main/java/com/splunk/rum/ConnectionUtil.java @@ -33,9 +33,9 @@ class ConnectionUtil { static final CurrentNetwork NO_NETWORK = - new CurrentNetwork(NetworkState.NO_NETWORK_AVAILABLE, null); + CurrentNetwork.builder(NetworkState.NO_NETWORK_AVAILABLE).build(); static final CurrentNetwork UNKNOWN_NETWORK = - new CurrentNetwork(NetworkState.TRANSPORT_UNKNOWN, null); + CurrentNetwork.builder(NetworkState.TRANSPORT_UNKNOWN).build(); private final NetworkDetector networkDetector; diff --git a/splunk-otel-android/src/main/java/com/splunk/rum/CurrentNetwork.java b/splunk-otel-android/src/main/java/com/splunk/rum/CurrentNetwork.java index 023a63adc..86e095839 100644 --- a/splunk-otel-android/src/main/java/com/splunk/rum/CurrentNetwork.java +++ b/splunk-otel-android/src/main/java/com/splunk/rum/CurrentNetwork.java @@ -16,17 +16,19 @@ package com.splunk.rum; +import android.os.Build; import androidx.annotation.Nullable; import java.util.Objects; -import java.util.Optional; final class CurrentNetwork { + @Nullable private final Carrier carrier; private final NetworkState state; @Nullable private final String subType; - CurrentNetwork(NetworkState state, @Nullable String subType) { - this.state = state; - this.subType = subType; + private CurrentNetwork(Builder builder) { + this.carrier = builder.carrier; + this.state = builder.state; + this.subType = builder.subType; } boolean isOnline() { @@ -37,29 +39,92 @@ NetworkState getState() { return state; } - Optional getSubType() { - return Optional.ofNullable(subType); + @Nullable + String getSubType() { + return subType; } @Override public String toString() { - return "CurrentNetwork{" + "state=" + state + ", subType='" + subType + '\'' + '}'; + return "CurrentNetwork{" + + "carrier=" + + carrier + + ", state=" + + state + + ", subType='" + + subType + + '\'' + + '}'; } @Override public boolean equals(Object o) { - if (this == o) { - return true; - } - if (!(o instanceof CurrentNetwork)) { - return false; - } + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; CurrentNetwork that = (CurrentNetwork) o; - return state == that.state && Objects.equals(subType, that.subType); + return Objects.equals(carrier, that.carrier) + && state == that.state + && Objects.equals(subType, that.subType); } @Override public int hashCode() { - return Objects.hash(state, subType); + return Objects.hash(carrier, state, subType); + } + + @SuppressWarnings("NullAway") + @Nullable + public String getCarrierCountryCode() { + return haveCarrier() ? carrier.getMobileCountryCode() : null; + } + + @SuppressWarnings("NullAway") + @Nullable + public String getCarrierIsoCountryCode() { + return haveCarrier() ? carrier.getIsoCountryCode() : null; + } + + @SuppressWarnings("NullAway") + @Nullable + public String getCarrierNetworkCode() { + return haveCarrier() ? carrier.getMobileNetworkCode() : null; + } + + @SuppressWarnings("NullAway") + @Nullable + public String getCarrierName() { + return haveCarrier() ? carrier.getName() : null; + } + + private boolean haveCarrier() { + return (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) && (carrier != null); + } + + static Builder builder(NetworkState state) { + return new Builder(state); + } + + static class Builder { + @Nullable private Carrier carrier; + private final NetworkState state; + @Nullable private String subType; + + public Builder(NetworkState state) { + this.state = state; + } + + CurrentNetwork build() { + return new CurrentNetwork(this); + } + + public Builder carrier(@Nullable Carrier carrier) { + this.carrier = carrier; + return this; + } + + public Builder subType(@Nullable String subType) { + this.subType = subType; + return this; + } } } diff --git a/splunk-otel-android/src/main/java/com/splunk/rum/NetworkDetector.java b/splunk-otel-android/src/main/java/com/splunk/rum/NetworkDetector.java index 9c26cb184..4e1bdd66b 100644 --- a/splunk-otel-android/src/main/java/com/splunk/rum/NetworkDetector.java +++ b/splunk-otel-android/src/main/java/com/splunk/rum/NetworkDetector.java @@ -30,7 +30,9 @@ static NetworkDetector create(Context context) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); - return new PostApi29NetworkDetector(connectivityManager, telephonyManager, context); + CarrierFinder carrierFinder = new CarrierFinder(telephonyManager); + return new PostApi28NetworkDetector( + connectivityManager, telephonyManager, carrierFinder, context); } return new SimpleNetworkDetector(connectivityManager); } diff --git a/splunk-otel-android/src/main/java/com/splunk/rum/NetworkMonitor.java b/splunk-otel-android/src/main/java/com/splunk/rum/NetworkMonitor.java index 4e66e8c3c..04fb526f5 100644 --- a/splunk-otel-android/src/main/java/com/splunk/rum/NetworkMonitor.java +++ b/splunk-otel-android/src/main/java/com/splunk/rum/NetworkMonitor.java @@ -22,7 +22,6 @@ import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.trace.Span; import io.opentelemetry.api.trace.Tracer; -import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; import java.util.concurrent.atomic.AtomicBoolean; class NetworkMonitor implements AppStateListener { @@ -78,19 +77,10 @@ public void onAvailable(boolean deviceIsOnline, CurrentNetwork activeNetwork) { Span available = tracer.spanBuilder("network.change") .setAttribute(NETWORK_STATUS_KEY, "available") - .startSpan() - // put these after span start to override what might be set in the - // RumAttributeAppender. - .setAttribute( - NET_HOST_CONNECTION_TYPE, - activeNetwork.getState().getHumanName()); - activeNetwork - .getSubType() - .ifPresent( - subType -> - available.setAttribute( - SemanticAttributes.NET_HOST_CONNECTION_SUBTYPE, - subType)); + .startSpan(); + // put these after span start to override what might be set in the + // RumAttributeAppender. + RumAttributeAppender.appendNetworkAttributes(available, activeNetwork); available.end(); } } diff --git a/splunk-otel-android/src/main/java/com/splunk/rum/PostApi29NetworkDetector.java b/splunk-otel-android/src/main/java/com/splunk/rum/PostApi28NetworkDetector.java similarity index 86% rename from splunk-otel-android/src/main/java/com/splunk/rum/PostApi29NetworkDetector.java rename to splunk-otel-android/src/main/java/com/splunk/rum/PostApi28NetworkDetector.java index 60c76cc4a..25c78dfa2 100644 --- a/splunk-otel-android/src/main/java/com/splunk/rum/PostApi29NetworkDetector.java +++ b/splunk-otel-android/src/main/java/com/splunk/rum/PostApi28NetworkDetector.java @@ -30,18 +30,21 @@ import androidx.annotation.RequiresApi; import androidx.core.app.ActivityCompat; -@RequiresApi(api = Build.VERSION_CODES.Q) -class PostApi29NetworkDetector implements NetworkDetector { +@RequiresApi(api = Build.VERSION_CODES.P) +class PostApi28NetworkDetector implements NetworkDetector { private final ConnectivityManager connectivityManager; private final TelephonyManager telephonyManager; + private final CarrierFinder carrierFinder; private final Context context; - PostApi29NetworkDetector( + PostApi28NetworkDetector( ConnectivityManager connectivityManager, TelephonyManager telephonyManager, + CarrierFinder carrierFinder, Context context) { this.connectivityManager = connectivityManager; this.telephonyManager = telephonyManager; + this.carrierFinder = carrierFinder; this.context = context; } @@ -54,16 +57,20 @@ public CurrentNetwork detectCurrentNetwork() { return NO_NETWORK; } String subType = null; + Carrier carrier = carrierFinder.get(); if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) { // If the app has the permission, use it to get a subtype. if (hasPermission(Manifest.permission.READ_PHONE_STATE)) { subType = getDataNetworkTypeName(telephonyManager.getDataNetworkType()); } - return new CurrentNetwork(NetworkState.TRANSPORT_CELLULAR, subType); + return CurrentNetwork.builder(NetworkState.TRANSPORT_CELLULAR) + .carrier(carrier) + .subType(subType) + .build(); } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { - return new CurrentNetwork(NetworkState.TRANSPORT_WIFI, null); + return CurrentNetwork.builder(NetworkState.TRANSPORT_WIFI).carrier(carrier).build(); } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) { - return new CurrentNetwork(NetworkState.TRANSPORT_VPN, null); + return CurrentNetwork.builder(NetworkState.TRANSPORT_VPN).carrier(carrier).build(); } // there is an active network, but it doesn't fall into the neat buckets above return UNKNOWN_NETWORK; diff --git a/splunk-otel-android/src/main/java/com/splunk/rum/RumAttributeAppender.java b/splunk-otel-android/src/main/java/com/splunk/rum/RumAttributeAppender.java index 207be3dd3..a2176f240 100644 --- a/splunk-otel-android/src/main/java/com/splunk/rum/RumAttributeAppender.java +++ b/splunk-otel-android/src/main/java/com/splunk/rum/RumAttributeAppender.java @@ -22,12 +22,18 @@ import static io.opentelemetry.semconv.resource.attributes.ResourceAttributes.OS_NAME; import static io.opentelemetry.semconv.resource.attributes.ResourceAttributes.OS_TYPE; import static io.opentelemetry.semconv.resource.attributes.ResourceAttributes.OS_VERSION; +import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.NET_HOST_CARRIER_ICC; +import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.NET_HOST_CARRIER_MCC; +import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.NET_HOST_CARRIER_MNC; +import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.NET_HOST_CARRIER_NAME; import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.NET_HOST_CONNECTION_SUBTYPE; import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.NET_HOST_CONNECTION_TYPE; import android.os.Build; +import androidx.annotation.Nullable; import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; +import io.opentelemetry.api.trace.Span; import io.opentelemetry.context.Context; import io.opentelemetry.sdk.trace.ReadWriteSpan; import io.opentelemetry.sdk.trace.ReadableSpan; @@ -83,10 +89,22 @@ public void onStart(Context parentContext, ReadWriteSpan span) { String currentScreen = visibleScreenTracker.getCurrentlyVisibleScreen(); span.setAttribute(SplunkRum.SCREEN_NAME_KEY, currentScreen); CurrentNetwork currentNetwork = connectionUtil.getActiveNetwork(); - span.setAttribute(NET_HOST_CONNECTION_TYPE, currentNetwork.getState().getHumanName()); - currentNetwork - .getSubType() - .ifPresent(subtype -> span.setAttribute(NET_HOST_CONNECTION_SUBTYPE, subtype)); + appendNetworkAttributes(span, currentNetwork); + } + + static void appendNetworkAttributes(Span span, CurrentNetwork currentNetwork) { + setIfNotNull(span, NET_HOST_CONNECTION_TYPE, currentNetwork.getState().getHumanName()); + setIfNotNull(span, NET_HOST_CONNECTION_SUBTYPE, currentNetwork.getSubType()); + setIfNotNull(span, NET_HOST_CARRIER_NAME, currentNetwork.getCarrierName()); + setIfNotNull(span, NET_HOST_CARRIER_MCC, currentNetwork.getCarrierCountryCode()); + setIfNotNull(span, NET_HOST_CARRIER_MNC, currentNetwork.getCarrierNetworkCode()); + setIfNotNull(span, NET_HOST_CARRIER_ICC, currentNetwork.getCarrierIsoCountryCode()); + } + + private static void setIfNotNull(Span span, AttributeKey key, @Nullable String value) { + if (value != null) { + span.setAttribute(key, value); + } } @Override diff --git a/splunk-otel-android/src/main/java/com/splunk/rum/SimpleNetworkDetector.java b/splunk-otel-android/src/main/java/com/splunk/rum/SimpleNetworkDetector.java index 44b74a018..edcdf32e9 100644 --- a/splunk-otel-android/src/main/java/com/splunk/rum/SimpleNetworkDetector.java +++ b/splunk-otel-android/src/main/java/com/splunk/rum/SimpleNetworkDetector.java @@ -38,14 +38,17 @@ public CurrentNetwork detectCurrentNetwork() { } switch (activeNetwork.getType()) { case ConnectivityManager.TYPE_MOBILE: // Deprecated in API 28 - return new CurrentNetwork( - NetworkState.TRANSPORT_CELLULAR, activeNetwork.getSubtypeName()); + return CurrentNetwork.builder(NetworkState.TRANSPORT_CELLULAR) + .subType(activeNetwork.getSubtypeName()) + .build(); case ConnectivityManager.TYPE_WIFI: // Deprecated in API 28 - return new CurrentNetwork( - NetworkState.TRANSPORT_WIFI, activeNetwork.getSubtypeName()); + return CurrentNetwork.builder(NetworkState.TRANSPORT_WIFI) + .subType(activeNetwork.getSubtypeName()) + .build(); case ConnectivityManager.TYPE_VPN: - return new CurrentNetwork( - NetworkState.TRANSPORT_VPN, activeNetwork.getSubtypeName()); + return CurrentNetwork.builder(NetworkState.TRANSPORT_VPN) + .subType(activeNetwork.getSubtypeName()) + .build(); } // there is an active network, but it doesn't fall into the neat buckets above return UNKNOWN_NETWORK; diff --git a/splunk-otel-android/src/test/java/com/splunk/rum/CarrierFinderTest.java b/splunk-otel-android/src/test/java/com/splunk/rum/CarrierFinderTest.java new file mode 100644 index 000000000..99a96bb75 --- /dev/null +++ b/splunk-otel-android/src/test/java/com/splunk/rum/CarrierFinderTest.java @@ -0,0 +1,65 @@ +/* + * Copyright Splunk Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.splunk.rum; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import android.telephony.TelephonyManager; +import org.junit.Test; + +public class CarrierFinderTest { + + @Test + public void testSimpleGet() { + Carrier expected = + Carrier.builder() + .id(206) + .name("ShadyTel") + .isoCountryCode("US") + .mobileCountryCode("usa") + .mobileNetworkCode("omg") + .build(); + + TelephonyManager manager = mock(TelephonyManager.class); + when(manager.getSimCarrierId()).thenReturn(expected.getId()); + when(manager.getSimCarrierIdName()).thenReturn(expected.getName()); + when(manager.getSimCountryIso()).thenReturn(expected.getIsoCountryCode()); + when(manager.getSimOperator()) + .thenReturn(expected.getMobileCountryCode() + expected.getMobileNetworkCode()); + + CarrierFinder finder = new CarrierFinder(manager); + Carrier carrier = finder.get(); + assertThat(carrier).isEqualTo(expected); + } + + @Test + public void testMostlyInvalid() { + Carrier expected = Carrier.builder().build(); + + TelephonyManager manager = mock(TelephonyManager.class); + when(manager.getSimCarrierId()).thenReturn(-1); + when(manager.getSimCarrierIdName()).thenReturn(null); + when(manager.getSimCountryIso()).thenReturn(null); + when(manager.getSimOperator()).thenReturn(null); + + CarrierFinder finder = new CarrierFinder(manager); + Carrier carrier = finder.get(); + assertThat(carrier).isEqualTo(expected); + } +} diff --git a/splunk-otel-android/src/test/java/com/splunk/rum/ConnectionUtilTest.java b/splunk-otel-android/src/test/java/com/splunk/rum/ConnectionUtilTest.java index 51a3b1db3..040eed343 100644 --- a/splunk-otel-android/src/test/java/com/splunk/rum/ConnectionUtilTest.java +++ b/splunk-otel-android/src/test/java/com/splunk/rum/ConnectionUtilTest.java @@ -53,15 +53,20 @@ public void lollipop() { ConnectivityManager connectivityManager = mock(ConnectivityManager.class); when(networkDetector.detectCurrentNetwork()) - .thenReturn(new CurrentNetwork(NetworkState.TRANSPORT_WIFI, null)) // called on init - .thenReturn(new CurrentNetwork(NetworkState.TRANSPORT_CELLULAR, "LTE")); + .thenReturn( + CurrentNetwork.builder(NetworkState.TRANSPORT_WIFI) + .build()) // called on init + .thenReturn( + CurrentNetwork.builder(NetworkState.TRANSPORT_CELLULAR) + .subType("LTE") + .build()); ConnectionUtil connectionUtil = new ConnectionUtil(networkDetector); connectionUtil.startMonitoring(() -> networkRequest, connectivityManager); assertTrue(connectionUtil.isOnline()); assertEquals( - new CurrentNetwork(NetworkState.TRANSPORT_WIFI, null), + CurrentNetwork.builder(NetworkState.TRANSPORT_WIFI).build(), connectionUtil.getActiveNetwork()); ArgumentCaptor monitorCaptor = @@ -76,12 +81,14 @@ public void lollipop() { if (timesCalled == 1) { assertTrue(deviceIsOnline); assertEquals( - new CurrentNetwork(NetworkState.TRANSPORT_CELLULAR, "LTE"), + CurrentNetwork.builder(NetworkState.TRANSPORT_CELLULAR) + .subType("LTE") + .build(), currentNetwork); } else { assertFalse(deviceIsOnline); assertEquals( - new CurrentNetwork(NetworkState.NO_NETWORK_AVAILABLE, null), + CurrentNetwork.builder(NetworkState.NO_NETWORK_AVAILABLE).build(), currentNetwork); } }); @@ -101,15 +108,18 @@ public void modernSdks() { ConnectivityManager connectivityManager = mock(ConnectivityManager.class); when(networkDetector.detectCurrentNetwork()) - .thenReturn(new CurrentNetwork(NetworkState.TRANSPORT_WIFI, null)) - .thenReturn(new CurrentNetwork(NetworkState.TRANSPORT_CELLULAR, "LTE")); + .thenReturn(CurrentNetwork.builder(NetworkState.TRANSPORT_WIFI).build()) + .thenReturn( + CurrentNetwork.builder(NetworkState.TRANSPORT_CELLULAR) + .subType("LTE") + .build()); ConnectionUtil connectionUtil = new ConnectionUtil(networkDetector); connectionUtil.startMonitoring(() -> networkRequest, connectivityManager); assertTrue(connectionUtil.isOnline()); assertEquals( - new CurrentNetwork(NetworkState.TRANSPORT_WIFI, null), + CurrentNetwork.builder(NetworkState.TRANSPORT_WIFI).build(), connectionUtil.getActiveNetwork()); verify(connectivityManager, never()) .registerNetworkCallback(eq(networkRequest), isA(NetworkCallback.class)); @@ -125,12 +135,14 @@ public void modernSdks() { if (timesCalled == 1) { assertTrue(deviceIsOnline); assertEquals( - new CurrentNetwork(NetworkState.TRANSPORT_CELLULAR, "LTE"), + CurrentNetwork.builder(NetworkState.TRANSPORT_CELLULAR) + .subType("LTE") + .build(), currentNetwork); } else { assertFalse(deviceIsOnline); assertEquals( - new CurrentNetwork(NetworkState.NO_NETWORK_AVAILABLE, null), + CurrentNetwork.builder(NetworkState.NO_NETWORK_AVAILABLE).build(), currentNetwork); } }); @@ -158,7 +170,7 @@ public void networkDetectorExceptionOnCallbackRegistration() { ConnectivityManager connectivityManager = mock(ConnectivityManager.class); when(networkDetector.detectCurrentNetwork()) - .thenReturn(new CurrentNetwork(NetworkState.TRANSPORT_WIFI, null)); + .thenReturn(CurrentNetwork.builder(NetworkState.TRANSPORT_WIFI).build()); doThrow(new SecurityException("bug")) .when(connectivityManager) .registerDefaultNetworkCallback(isA(NetworkCallback.class)); @@ -166,7 +178,7 @@ public void networkDetectorExceptionOnCallbackRegistration() { ConnectionUtil connectionUtil = new ConnectionUtil(networkDetector); connectionUtil.startMonitoring(() -> mock(NetworkRequest.class), connectivityManager); assertEquals( - new CurrentNetwork(NetworkState.TRANSPORT_WIFI, null), + CurrentNetwork.builder(NetworkState.TRANSPORT_WIFI).build(), connectionUtil.refreshNetworkStatus()); } @@ -178,7 +190,7 @@ public void networkDetectorExceptionOnCallbackRegistration_lollipop() { NetworkRequest networkRequest = mock(NetworkRequest.class); when(networkDetector.detectCurrentNetwork()) - .thenReturn(new CurrentNetwork(NetworkState.TRANSPORT_WIFI, null)); + .thenReturn(CurrentNetwork.builder(NetworkState.TRANSPORT_WIFI).build()); doThrow(new SecurityException("bug")) .when(connectivityManager) .registerNetworkCallback(eq(networkRequest), isA(NetworkCallback.class)); @@ -186,7 +198,7 @@ public void networkDetectorExceptionOnCallbackRegistration_lollipop() { ConnectionUtil connectionUtil = new ConnectionUtil(networkDetector); connectionUtil.startMonitoring(() -> networkRequest, connectivityManager); assertEquals( - new CurrentNetwork(NetworkState.TRANSPORT_WIFI, null), + CurrentNetwork.builder(NetworkState.TRANSPORT_WIFI).build(), connectionUtil.refreshNetworkStatus()); } diff --git a/splunk-otel-android/src/test/java/com/splunk/rum/MemoryBufferingExporterTest.java b/splunk-otel-android/src/test/java/com/splunk/rum/MemoryBufferingExporterTest.java index 8f1c78466..94837b954 100644 --- a/splunk-otel-android/src/test/java/com/splunk/rum/MemoryBufferingExporterTest.java +++ b/splunk-otel-android/src/test/java/com/splunk/rum/MemoryBufferingExporterTest.java @@ -44,7 +44,7 @@ public class MemoryBufferingExporterTest { @Before public void setUp() { when(connectionUtil.refreshNetworkStatus()) - .thenReturn(new CurrentNetwork(NetworkState.TRANSPORT_CELLULAR, null)); + .thenReturn(CurrentNetwork.builder(NetworkState.TRANSPORT_CELLULAR).build()); } @Test @@ -63,8 +63,8 @@ public void happyPath() { @Test public void offlinePath() { when(connectionUtil.refreshNetworkStatus()) - .thenReturn(new CurrentNetwork(NetworkState.NO_NETWORK_AVAILABLE, null)) - .thenReturn(new CurrentNetwork(NetworkState.TRANSPORT_UNKNOWN, null)); + .thenReturn(CurrentNetwork.builder(NetworkState.NO_NETWORK_AVAILABLE).build()) + .thenReturn(CurrentNetwork.builder(NetworkState.TRANSPORT_UNKNOWN).build()); SpanExporter delegate = mock(SpanExporter.class); MemoryBufferingExporter bufferingExporter = diff --git a/splunk-otel-android/src/test/java/com/splunk/rum/NetworkDetectorTest.java b/splunk-otel-android/src/test/java/com/splunk/rum/NetworkDetectorTest.java index d2b6caa96..f1e9be8fa 100644 --- a/splunk-otel-android/src/test/java/com/splunk/rum/NetworkDetectorTest.java +++ b/splunk-otel-android/src/test/java/com/splunk/rum/NetworkDetectorTest.java @@ -35,7 +35,7 @@ public void quiznos() { Context context = ApplicationProvider.getApplicationContext(); NetworkDetector networkDetector = NetworkDetector.create(context); - assertTrue(networkDetector instanceof PostApi29NetworkDetector); + assertTrue(networkDetector instanceof PostApi28NetworkDetector); } @Test diff --git a/splunk-otel-android/src/test/java/com/splunk/rum/NetworkMonitorTest.java b/splunk-otel-android/src/test/java/com/splunk/rum/NetworkMonitorTest.java index b702d21fa..3f3290fea 100644 --- a/splunk-otel-android/src/test/java/com/splunk/rum/NetworkMonitorTest.java +++ b/splunk-otel-android/src/test/java/com/splunk/rum/NetworkMonitorTest.java @@ -46,7 +46,7 @@ public void networkAvailable_wifi() { NetworkMonitor.TracingConnectionStateListener listener = new NetworkMonitor.TracingConnectionStateListener(tracer, new AtomicBoolean(true)); - listener.onAvailable(true, new CurrentNetwork(NetworkState.TRANSPORT_WIFI, null)); + listener.onAvailable(true, CurrentNetwork.builder(NetworkState.TRANSPORT_WIFI).build()); List spans = otelTesting.getSpans(); assertEquals(1, spans.size()); @@ -63,7 +63,9 @@ public void networkAvailable_cellular() { NetworkMonitor.TracingConnectionStateListener listener = new NetworkMonitor.TracingConnectionStateListener(tracer, new AtomicBoolean(true)); - listener.onAvailable(true, new CurrentNetwork(NetworkState.TRANSPORT_CELLULAR, "LTE")); + listener.onAvailable( + true, + CurrentNetwork.builder(NetworkState.TRANSPORT_CELLULAR).subType("LTE").build()); List spans = otelTesting.getSpans(); assertEquals(1, spans.size()); @@ -80,7 +82,8 @@ public void networkLost() { NetworkMonitor.TracingConnectionStateListener listener = new NetworkMonitor.TracingConnectionStateListener(tracer, new AtomicBoolean(true)); - listener.onAvailable(false, new CurrentNetwork(NetworkState.NO_NETWORK_AVAILABLE, null)); + listener.onAvailable( + false, CurrentNetwork.builder(NetworkState.NO_NETWORK_AVAILABLE).build()); List spans = otelTesting.getSpans(); assertEquals(1, spans.size()); @@ -99,13 +102,17 @@ public void noEventsPlease() { NetworkMonitor.TracingConnectionStateListener listener = new NetworkMonitor.TracingConnectionStateListener(tracer, shouldEmitChangeEvents); - listener.onAvailable(false, new CurrentNetwork(NetworkState.NO_NETWORK_AVAILABLE, null)); + listener.onAvailable( + false, CurrentNetwork.builder(NetworkState.NO_NETWORK_AVAILABLE).build()); assertTrue(otelTesting.getSpans().isEmpty()); - listener.onAvailable(true, new CurrentNetwork(NetworkState.TRANSPORT_CELLULAR, "LTE")); + listener.onAvailable( + true, + CurrentNetwork.builder(NetworkState.TRANSPORT_CELLULAR).subType("LTE").build()); assertTrue(otelTesting.getSpans().isEmpty()); shouldEmitChangeEvents.set(true); - listener.onAvailable(false, new CurrentNetwork(NetworkState.NO_NETWORK_AVAILABLE, null)); + listener.onAvailable( + false, CurrentNetwork.builder(NetworkState.NO_NETWORK_AVAILABLE).build()); assertEquals(1, otelTesting.getSpans().size()); } } diff --git a/splunk-otel-android/src/test/java/com/splunk/rum/PostApi28NetworkDetectorTest.java b/splunk-otel-android/src/test/java/com/splunk/rum/PostApi28NetworkDetectorTest.java new file mode 100644 index 000000000..e33cb756d --- /dev/null +++ b/splunk-otel-android/src/test/java/com/splunk/rum/PostApi28NetworkDetectorTest.java @@ -0,0 +1,150 @@ +/* + * Copyright Splunk Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.splunk.rum; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import android.content.Context; +import android.net.ConnectivityManager; +import android.net.Network; +import android.net.NetworkCapabilities; +import android.os.Build; +import android.telephony.TelephonyManager; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; + +@RunWith(RobolectricTestRunner.class) +@Config(sdk = Build.VERSION_CODES.P) +public class PostApi28NetworkDetectorTest { + + ConnectivityManager connectivityManager; + TelephonyManager telephonyManager; + Context context; + Network network; + NetworkCapabilities networkCapabilities; + CarrierFinder carrierFinder; + + @Before + public void setup() { + connectivityManager = mock(ConnectivityManager.class); + telephonyManager = mock(TelephonyManager.class); + context = mock(Context.class); + network = mock(Network.class); + carrierFinder = mock(CarrierFinder.class); + networkCapabilities = mock(NetworkCapabilities.class); + + when(connectivityManager.getActiveNetwork()).thenReturn(network); + when(connectivityManager.getNetworkCapabilities(network)).thenReturn(networkCapabilities); + } + + @Test + public void none() { + when(connectivityManager.getNetworkCapabilities(network)).thenReturn(null); + + PostApi28NetworkDetector networkDetector = + new PostApi28NetworkDetector( + connectivityManager, telephonyManager, carrierFinder, context); + CurrentNetwork currentNetwork = networkDetector.detectCurrentNetwork(); + assertEquals( + CurrentNetwork.builder(NetworkState.NO_NETWORK_AVAILABLE).build(), currentNetwork); + } + + @Test + public void wifi() { + when(networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)).thenReturn(true); + + PostApi28NetworkDetector networkDetector = + new PostApi28NetworkDetector( + connectivityManager, telephonyManager, carrierFinder, context); + CurrentNetwork currentNetwork = networkDetector.detectCurrentNetwork(); + assertEquals(CurrentNetwork.builder(NetworkState.TRANSPORT_WIFI).build(), currentNetwork); + } + + @Test + public void cellular() { + when(telephonyManager.getDataNetworkType()).thenReturn(TelephonyManager.NETWORK_TYPE_LTE); + when(networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) + .thenReturn(true); + + PostApi28NetworkDetector networkDetector = + new PostApi28NetworkDetector( + connectivityManager, telephonyManager, carrierFinder, context); + CurrentNetwork currentNetwork = networkDetector.detectCurrentNetwork(); + assertEquals( + CurrentNetwork.builder(NetworkState.TRANSPORT_CELLULAR).subType("LTE").build(), + currentNetwork); + } + + @Test + public void cellular_noTelephonyPermissions() { + when(telephonyManager.getDataNetworkType()).thenReturn(TelephonyManager.NETWORK_TYPE_LTE); + when(networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) + .thenReturn(true); + + PostApi28NetworkDetector networkDetector = + new PostApi28NetworkDetector( + connectivityManager, telephonyManager, carrierFinder, context) { + @Override + boolean hasPermission(String permission) { + return false; + } + }; + CurrentNetwork currentNetwork = networkDetector.detectCurrentNetwork(); + assertEquals( + CurrentNetwork.builder(NetworkState.TRANSPORT_CELLULAR).build(), currentNetwork); + } + + @Test + public void other() { + PostApi28NetworkDetector networkDetector = + new PostApi28NetworkDetector( + connectivityManager, telephonyManager, carrierFinder, context); + CurrentNetwork currentNetwork = networkDetector.detectCurrentNetwork(); + assertEquals( + CurrentNetwork.builder(NetworkState.TRANSPORT_UNKNOWN).build(), currentNetwork); + } + + @Test + public void vpn() { + when(networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)).thenReturn(true); + + PostApi28NetworkDetector networkDetector = + new PostApi28NetworkDetector( + connectivityManager, telephonyManager, carrierFinder, context); + CurrentNetwork currentNetwork = networkDetector.detectCurrentNetwork(); + assertEquals(CurrentNetwork.builder(NetworkState.TRANSPORT_VPN).build(), currentNetwork); + } + + @Test + public void carrierIsSet() { + Carrier carrier = Carrier.builder().name("flib").build(); + when(networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) + .thenReturn(true); + when(carrierFinder.get()).thenReturn(carrier); + PostApi28NetworkDetector networkDetector = + new PostApi28NetworkDetector( + connectivityManager, telephonyManager, carrierFinder, context); + CurrentNetwork currentNetwork = networkDetector.detectCurrentNetwork(); + assertThat(currentNetwork.getCarrierName()).isEqualTo("flib"); + } +} diff --git a/splunk-otel-android/src/test/java/com/splunk/rum/PostApi29NetworkDetectorTest.java b/splunk-otel-android/src/test/java/com/splunk/rum/PostApi29NetworkDetectorTest.java deleted file mode 100644 index be6b10063..000000000 --- a/splunk-otel-android/src/test/java/com/splunk/rum/PostApi29NetworkDetectorTest.java +++ /dev/null @@ -1,155 +0,0 @@ -/* - * Copyright Splunk Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.splunk.rum; - -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import android.content.Context; -import android.net.ConnectivityManager; -import android.net.Network; -import android.net.NetworkCapabilities; -import android.os.Build; -import android.telephony.TelephonyManager; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.RobolectricTestRunner; -import org.robolectric.annotation.Config; - -@RunWith(RobolectricTestRunner.class) -@Config(sdk = Build.VERSION_CODES.Q) -public class PostApi29NetworkDetectorTest { - - @Test - public void none() { - ConnectivityManager connectivityManager = mock(ConnectivityManager.class); - TelephonyManager telephonyManager = mock(TelephonyManager.class); - Context context = mock(Context.class); - - Network network = mock(Network.class); - when(connectivityManager.getActiveNetwork()).thenReturn(network); - when(connectivityManager.getNetworkCapabilities(network)).thenReturn(null); - - PostApi29NetworkDetector networkDetector = - new PostApi29NetworkDetector(connectivityManager, telephonyManager, context); - CurrentNetwork currentNetwork = networkDetector.detectCurrentNetwork(); - assertEquals(new CurrentNetwork(NetworkState.NO_NETWORK_AVAILABLE, null), currentNetwork); - } - - @Test - public void wifi() { - ConnectivityManager connectivityManager = mock(ConnectivityManager.class); - TelephonyManager telephonyManager = mock(TelephonyManager.class); - Context context = mock(Context.class); - - Network network = mock(Network.class); - when(connectivityManager.getActiveNetwork()).thenReturn(network); - NetworkCapabilities networkCapabilities = mock(NetworkCapabilities.class); - when(connectivityManager.getNetworkCapabilities(network)).thenReturn(networkCapabilities); - - when(networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)).thenReturn(true); - - PostApi29NetworkDetector networkDetector = - new PostApi29NetworkDetector(connectivityManager, telephonyManager, context); - CurrentNetwork currentNetwork = networkDetector.detectCurrentNetwork(); - assertEquals(new CurrentNetwork(NetworkState.TRANSPORT_WIFI, null), currentNetwork); - } - - @Test - public void cellular() { - ConnectivityManager connectivityManager = mock(ConnectivityManager.class); - TelephonyManager telephonyManager = mock(TelephonyManager.class); - Context context = mock(Context.class); - - Network network = mock(Network.class); - when(connectivityManager.getActiveNetwork()).thenReturn(network); - NetworkCapabilities networkCapabilities = mock(NetworkCapabilities.class); - when(connectivityManager.getNetworkCapabilities(network)).thenReturn(networkCapabilities); - when(telephonyManager.getDataNetworkType()).thenReturn(TelephonyManager.NETWORK_TYPE_LTE); - - when(networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) - .thenReturn(true); - - PostApi29NetworkDetector networkDetector = - new PostApi29NetworkDetector(connectivityManager, telephonyManager, context); - CurrentNetwork currentNetwork = networkDetector.detectCurrentNetwork(); - assertEquals(new CurrentNetwork(NetworkState.TRANSPORT_CELLULAR, "LTE"), currentNetwork); - } - - @Test - public void cellular_noTelephonyPermissions() { - ConnectivityManager connectivityManager = mock(ConnectivityManager.class); - TelephonyManager telephonyManager = mock(TelephonyManager.class); - Context context = mock(Context.class); - - Network network = mock(Network.class); - when(connectivityManager.getActiveNetwork()).thenReturn(network); - NetworkCapabilities networkCapabilities = mock(NetworkCapabilities.class); - when(connectivityManager.getNetworkCapabilities(network)).thenReturn(networkCapabilities); - when(telephonyManager.getDataNetworkType()).thenReturn(TelephonyManager.NETWORK_TYPE_LTE); - - when(networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) - .thenReturn(true); - - PostApi29NetworkDetector networkDetector = - new PostApi29NetworkDetector(connectivityManager, telephonyManager, context) { - @Override - boolean hasPermission(String permission) { - return false; - } - }; - CurrentNetwork currentNetwork = networkDetector.detectCurrentNetwork(); - assertEquals(new CurrentNetwork(NetworkState.TRANSPORT_CELLULAR, null), currentNetwork); - } - - @Test - public void other() { - ConnectivityManager connectivityManager = mock(ConnectivityManager.class); - TelephonyManager telephonyManager = mock(TelephonyManager.class); - Context context = mock(Context.class); - - Network network = mock(Network.class); - when(connectivityManager.getActiveNetwork()).thenReturn(network); - NetworkCapabilities networkCapabilities = mock(NetworkCapabilities.class); - when(connectivityManager.getNetworkCapabilities(network)).thenReturn(networkCapabilities); - - PostApi29NetworkDetector networkDetector = - new PostApi29NetworkDetector(connectivityManager, telephonyManager, context); - CurrentNetwork currentNetwork = networkDetector.detectCurrentNetwork(); - assertEquals(new CurrentNetwork(NetworkState.TRANSPORT_UNKNOWN, null), currentNetwork); - } - - @Test - public void vpn() { - ConnectivityManager connectivityManager = mock(ConnectivityManager.class); - TelephonyManager telephonyManager = mock(TelephonyManager.class); - Context context = mock(Context.class); - - Network network = mock(Network.class); - when(connectivityManager.getActiveNetwork()).thenReturn(network); - NetworkCapabilities networkCapabilities = mock(NetworkCapabilities.class); - when(connectivityManager.getNetworkCapabilities(network)).thenReturn(networkCapabilities); - - when(networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)).thenReturn(true); - - PostApi29NetworkDetector networkDetector = - new PostApi29NetworkDetector(connectivityManager, telephonyManager, context); - CurrentNetwork currentNetwork = networkDetector.detectCurrentNetwork(); - assertEquals(new CurrentNetwork(NetworkState.TRANSPORT_VPN, null), currentNetwork); - } -} diff --git a/splunk-otel-android/src/test/java/com/splunk/rum/RumAttributeAppenderTest.java b/splunk-otel-android/src/test/java/com/splunk/rum/RumAttributeAppenderTest.java index 8f5fb119a..4dd4d1ceb 100644 --- a/splunk-otel-android/src/test/java/com/splunk/rum/RumAttributeAppenderTest.java +++ b/splunk-otel-android/src/test/java/com/splunk/rum/RumAttributeAppenderTest.java @@ -46,7 +46,10 @@ public class RumAttributeAppenderTest { public void setUp() { visibleScreenTracker = mock(VisibleScreenTracker.class); when(connectionUtil.getActiveNetwork()) - .thenReturn(new CurrentNetwork(NetworkState.TRANSPORT_CELLULAR, "LTE")); + .thenReturn( + CurrentNetwork.builder(NetworkState.TRANSPORT_CELLULAR) + .subType("LTE") + .build()); } @Test diff --git a/splunk-otel-android/src/test/java/com/splunk/rum/RumInitializerTest.java b/splunk-otel-android/src/test/java/com/splunk/rum/RumInitializerTest.java index 79b4e1c22..1d9e09a1f 100644 --- a/splunk-otel-android/src/test/java/com/splunk/rum/RumInitializerTest.java +++ b/splunk-otel-android/src/test/java/com/splunk/rum/RumInitializerTest.java @@ -163,8 +163,8 @@ SpanExporter getCoreSpanExporter(String endpoint) { ConnectionUtil connectionUtil = mock(ConnectionUtil.class); - CurrentNetwork offline = new CurrentNetwork(NetworkState.NO_NETWORK_AVAILABLE, null); - CurrentNetwork online = new CurrentNetwork(NetworkState.TRANSPORT_WIFI, null); + CurrentNetwork offline = CurrentNetwork.builder(NetworkState.NO_NETWORK_AVAILABLE).build(); + CurrentNetwork online = CurrentNetwork.builder(NetworkState.TRANSPORT_WIFI).build(); when(connectionUtil.refreshNetworkStatus()).thenReturn(offline, online); long currentTimeNanos = MILLISECONDS.toNanos(System.currentTimeMillis()); diff --git a/splunk-otel-android/src/test/java/com/splunk/rum/SimpleNetworkDetectorTest.java b/splunk-otel-android/src/test/java/com/splunk/rum/SimpleNetworkDetectorTest.java index 92d2dd3b0..e9f39b636 100644 --- a/splunk-otel-android/src/test/java/com/splunk/rum/SimpleNetworkDetectorTest.java +++ b/splunk-otel-android/src/test/java/com/splunk/rum/SimpleNetworkDetectorTest.java @@ -47,7 +47,8 @@ public void none() { CurrentNetwork currentNetwork = networkDetector.detectCurrentNetwork(); - assertEquals(new CurrentNetwork(NetworkState.NO_NETWORK_AVAILABLE, null), currentNetwork); + assertEquals( + CurrentNetwork.builder(NetworkState.NO_NETWORK_AVAILABLE).build(), currentNetwork); } @Test @@ -70,7 +71,8 @@ public void other() { CurrentNetwork currentNetwork = networkDetector.detectCurrentNetwork(); - assertEquals(new CurrentNetwork(NetworkState.TRANSPORT_UNKNOWN, null), currentNetwork); + assertEquals( + CurrentNetwork.builder(NetworkState.TRANSPORT_UNKNOWN).build(), currentNetwork); } @Test @@ -93,7 +95,7 @@ public void wifi() { CurrentNetwork currentNetwork = networkDetector.detectCurrentNetwork(); - assertEquals(new CurrentNetwork(NetworkState.TRANSPORT_WIFI, null), currentNetwork); + assertEquals(CurrentNetwork.builder(NetworkState.TRANSPORT_WIFI).build(), currentNetwork); } @Test @@ -116,7 +118,7 @@ public void vpn() { CurrentNetwork currentNetwork = networkDetector.detectCurrentNetwork(); - assertEquals(new CurrentNetwork(NetworkState.TRANSPORT_VPN, null), currentNetwork); + assertEquals(CurrentNetwork.builder(NetworkState.TRANSPORT_VPN).build(), currentNetwork); } @Test @@ -137,6 +139,8 @@ public void cellularWithSubtype() { CurrentNetwork currentNetwork = networkDetector.detectCurrentNetwork(); - assertEquals(new CurrentNetwork(NetworkState.TRANSPORT_CELLULAR, "LTE"), currentNetwork); + assertEquals( + CurrentNetwork.builder(NetworkState.TRANSPORT_CELLULAR).subType("LTE").build(), + currentNetwork); } }