diff --git a/p2p-sync/src/main/java/org/smartregister/p2p/presenter/P2PSenderPresenter.java b/p2p-sync/src/main/java/org/smartregister/p2p/presenter/P2PSenderPresenter.java index 4902364..ffc842b 100644 --- a/p2p-sync/src/main/java/org/smartregister/p2p/presenter/P2PSenderPresenter.java +++ b/p2p-sync/src/main/java/org/smartregister/p2p/presenter/P2PSenderPresenter.java @@ -349,9 +349,9 @@ public void sendAuthorizationDetails(@NonNull Map authorizationD public void onPayloadReceived(@NonNull String endpointId, @NonNull Payload payload) { Timber.i(view.getString(R.string.log_received_payload_from_endpoint), endpointId); if (connectionLevel != null) { - if (connectionLevel.equals(ConnectionLevel.AUTHORIZED)) { - // Do nothing until the hash_key has been received on the other side - } else if (connectionLevel.equals(ConnectionLevel.AUTHENTICATED)) { + // We ignore the authorized state since we should not process anything at this point but more + // at #onConnectionAuthorized + if (connectionLevel.equals(ConnectionLevel.AUTHENTICATED)) { // Should get the details to authorize performAuthorization(payload); } else if (connectionLevel.equals(ConnectionLevel.SENT_HASH_KEY)) { diff --git a/p2p-sync/src/main/java/org/smartregister/p2p/util/Device.java b/p2p-sync/src/main/java/org/smartregister/p2p/util/Device.java index f718a38..e0b33fa 100644 --- a/p2p-sync/src/main/java/org/smartregister/p2p/util/Device.java +++ b/p2p-sync/src/main/java/org/smartregister/p2p/util/Device.java @@ -59,23 +59,29 @@ public static final String generateUniqueDeviceId(Context context) { * * @return WLAN0 MAC address or NULL if unable to get the mac address */ + @Nullable public static String getMacAddress() { String macAddress = null; try { List all = Collections.list(NetworkInterface.getNetworkInterfaces()); + NetworkInterface wifiInterface = null; for (NetworkInterface nif : all) { - if (!nif.getName().equalsIgnoreCase("wlan0")) { - continue; + if (nif.getName().equalsIgnoreCase("wlan0")) { + wifiInterface = nif; + break; } + } - byte[] macBytes = nif.getHardwareAddress(); + if (wifiInterface != null) { + byte[] macBytes = wifiInterface.getHardwareAddress(); if (macBytes == null) { return null; } StringBuilder res1 = new StringBuilder(); for (byte b : macBytes) { - res1.append(Integer.toHexString(b & 0xFF) + ":"); + res1.append(Integer.toHexString(b & 0xFF)); + res1.append(":"); } if (res1.length() > 0) { @@ -83,8 +89,8 @@ public static String getMacAddress() { } macAddress = res1.toString(); - break; } + } catch (SocketException ex) { Timber.e(ex); } diff --git a/p2p-sync/src/test/java/org/smartregister/p2p/presenter/BaseP2pModeSelectPresenterTest.java b/p2p-sync/src/test/java/org/smartregister/p2p/presenter/BaseP2pModeSelectPresenterTest.java index 7035dec..7706ef3 100644 --- a/p2p-sync/src/test/java/org/smartregister/p2p/presenter/BaseP2pModeSelectPresenterTest.java +++ b/p2p-sync/src/test/java/org/smartregister/p2p/presenter/BaseP2pModeSelectPresenterTest.java @@ -3,6 +3,8 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import com.google.android.gms.nearby.connection.DiscoveredEndpointInfo; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -34,6 +36,7 @@ public void setUp() throws Exception { @Test public void sendTextMessageShouldCallInteractorSendMessage() { String message = "Hello world"; + p2PModeSelectPresenter.setCurrentDevice(new DiscoveredDevice("endpointid", Mockito.mock(DiscoveredEndpointInfo.class))); p2PModeSelectPresenter.sendTextMessage(message); Mockito.verify(interactor, Mockito.times(1)) @@ -52,14 +55,28 @@ public void onStopShouldDismissDialogsAndCallInteractorStoppingAdvertisingAndDis .stopAdvertising(); Mockito.verify(interactor, Mockito.times(1)) .stopDiscovering(); - Mockito.verify(interactor, Mockito.times(1)) - .closeAllEndpoints(); + Mockito.verify(interactor, Mockito.times(0)) + .disconnectFromEndpoint(Mockito.anyString()); Mockito.verify(interactor, Mockito.times(1)) .cleanupResources(); } + @Test + public void onStopShouldDisconnectFromConnectedEndpoint() { + String endpointId = "endpointId"; + DiscoveredDevice discoveredDevice = new DiscoveredDevice(endpointId, Mockito.mock(DiscoveredEndpointInfo.class)); + + p2PModeSelectPresenter.setCurrentDevice(discoveredDevice); + p2PModeSelectPresenter.onStop(); + + Mockito.verify(interactor, Mockito.times(1)) + .disconnectFromEndpoint(ArgumentMatchers.eq(endpointId)); + } + private class P2pModeSelectPresenter extends BaseP2pModeSelectPresenter { + private DiscoveredDevice currentDevice; + protected P2pModeSelectPresenter(@NonNull P2pModeSelectContract.View view, @NonNull P2pModeSelectContract.Interactor p2pModeSelectInteractor) { super(view, p2pModeSelectInteractor); } @@ -67,7 +84,12 @@ protected P2pModeSelectPresenter(@NonNull P2pModeSelectContract.View view, @NonN @Nullable @Override public DiscoveredDevice getCurrentPeerDevice() { - return Mockito.mock(DiscoveredDevice.class); + return currentDevice; + } + + @Override + public void setCurrentDevice(@Nullable DiscoveredDevice discoveredDevice) { + currentDevice = discoveredDevice; } } } \ No newline at end of file diff --git a/p2p-sync/src/test/java/org/smartregister/p2p/presenter/P2PReceiverPresenterTest.java b/p2p-sync/src/test/java/org/smartregister/p2p/presenter/P2PReceiverPresenterTest.java index 4a5899b..89abe84 100644 --- a/p2p-sync/src/test/java/org/smartregister/p2p/presenter/P2PReceiverPresenterTest.java +++ b/p2p-sync/src/test/java/org/smartregister/p2p/presenter/P2PReceiverPresenterTest.java @@ -391,11 +391,8 @@ public void onConnectionRejectedShouldRestartAdvertisingModeAndResetState() { public void onConnectionUnknownErrorShouldRestartAdvertisingAndResetState() { String endpointId = "id"; - DiscoveredDevice discoveredDevice = Mockito.mock(DiscoveredDevice.class); - - ReflectionHelpers.setField(p2PReceiverPresenter, "currentSender", discoveredDevice); - assertNotNull(ReflectionHelpers.getField(p2PReceiverPresenter, "currentSender")); - + DiscoveredDevice discoveredDevice = new DiscoveredDevice(endpointId, Mockito.mock(DiscoveredEndpointInfo.class)); + p2PReceiverPresenter.setCurrentDevice(discoveredDevice); p2PReceiverPresenter.onConnectionUnknownError(endpointId, Mockito.mock(ConnectionResolution.class)); Mockito.verify(p2PReceiverPresenter, Mockito.times(1)) @@ -406,12 +403,8 @@ public void onConnectionUnknownErrorShouldRestartAdvertisingAndResetState() { @Test public void onConnectionBrokenShouldRestartAdvertisingAndResetState() { String endpointId = "id"; - - DiscoveredDevice discoveredDevice = Mockito.mock(DiscoveredDevice.class); - - ReflectionHelpers.setField(p2PReceiverPresenter, "currentSender", discoveredDevice); - assertNotNull(ReflectionHelpers.getField(p2PReceiverPresenter, "currentSender")); - + DiscoveredDevice discoveredDevice = new DiscoveredDevice(endpointId, Mockito.mock(DiscoveredEndpointInfo.class)); + p2PReceiverPresenter.setCurrentDevice(discoveredDevice); p2PReceiverPresenter.onConnectionBroken(endpointId); Mockito.verify(p2PReceiverPresenter, Mockito.times(1)) @@ -423,11 +416,8 @@ public void onConnectionBrokenShouldRestartAdvertisingAndResetState() { public void onDisconnectedShouldRestartAdvertisingAndResetState() { String endpointId = "id"; - DiscoveredDevice discoveredDevice = Mockito.mock(DiscoveredDevice.class); - - ReflectionHelpers.setField(p2PReceiverPresenter, "currentSender", discoveredDevice); - assertNotNull(ReflectionHelpers.getField(p2PReceiverPresenter, "currentSender")); - + DiscoveredDevice discoveredDevice = new DiscoveredDevice(endpointId, Mockito.mock(DiscoveredEndpointInfo.class)); + p2PReceiverPresenter.setCurrentDevice(discoveredDevice); p2PReceiverPresenter.onDisconnected(endpointId); Mockito.verify(p2PReceiverPresenter, Mockito.times(1))