Skip to content

Commit

Permalink
Fix tests and codacy issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ekigamba committed Mar 28, 2019
1 parent 392dba8 commit 1d27218
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,9 @@ public void sendAuthorizationDetails(@NonNull Map<String, Object> 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)) {
Expand Down
16 changes: 11 additions & 5 deletions p2p-sync/src/main/java/org/smartregister/p2p/util/Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,32 +59,38 @@ 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<NetworkInterface> 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) {
res1.deleteCharAt(res1.length() - 1);
}

macAddress = res1.toString();
break;
}

} catch (SocketException ex) {
Timber.e(ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))
Expand All @@ -52,22 +55,41 @@ 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);
}

@Nullable
@Override
public DiscoveredDevice getCurrentPeerDevice() {
return Mockito.mock(DiscoveredDevice.class);
return currentDevice;
}

@Override
public void setCurrentDevice(@Nullable DiscoveredDevice discoveredDevice) {
currentDevice = discoveredDevice;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand All @@ -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))
Expand Down

0 comments on commit 1d27218

Please sign in to comment.