Skip to content

Commit

Permalink
Corrected on Mr.Cognolato's suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
EnricoCestaro committed Mar 1, 2020
1 parent 3f464a5 commit c523e3b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class IdFinderHandler {
private static final String SEARCHER_NULL = "The searcher cannot be null";
private static final String NODES_LIST_NULL = "The nodeList cannot be null";
private static final String NODES_LIST_EMPTY = "The nodeList cannot be empty";
private static final String INSUFFICIENT_ATTEMPTS = "The attemptsNumber must be higher then zero";
private static final String INSUFFICIENT_ATTEMPTS = "The attemptsNumber must be higher then zero";

/**
* Searches for a specific Id, which needs to be closer to a given resource Id.
Expand All @@ -61,7 +61,12 @@ public static void searchId(@NonNull KademliaId idToFind, @NonNull SMSPeer searc
SMSKademliaRoutingTable table = KademliaJoinableNetwork.getInstance().getLocalRoutingTable();
//Create a list containing the closest nodes (five is more than enough)
List<SMSKademliaNode> nodeList = table.findClosest(idToFind, maxAttempts);

//Local node, inserted in the list if not already present
SMSKademliaNode local = KademliaJoinableNetwork.getInstance().getLocalNode();
if (!nodeList.contains(local)) {
//inserted in the last position, must be the last node to be checked
nodeList.add(local);
}
searchIdList(idToFind, searcher, nodeList);
}

Expand All @@ -77,13 +82,6 @@ public static void searchIdList(@NonNull KademliaId idToFind, @NonNull SMSPeer s
@NonNull List<SMSKademliaNode> nodeList) {
if (nodeList.isEmpty()) throw new IllegalArgumentException(NODES_LIST_EMPTY);
if (nodeList == null) throw new NullPointerException(NODES_LIST_NULL);

//Local node, inserted in the list if not already present
SMSKademliaNode local = KademliaJoinableNetwork.getInstance().getLocalNode();
if (!nodeList.contains(local)) {
//inserted in the last position, must be the last node to be checked
nodeList.add(local);
}
//Get the node with the node ID closest to the idToFind
//The first node in the nodeList is the one with the node Id closest to the idToFind
SMSKademliaNode closestNode = nodeList.get(0);
Expand Down Expand Up @@ -177,8 +175,8 @@ private static void retryIfDead(KademliaId idToFind, SMSPeer searcherNode, SMSPe
* @param attemptsNumber The maximum number of attempts before saving the resource in the local
* dictionary
*/
private static void setAttemptsNumber(int attemptsNumber) {
if(attemptsNumber < 1) throw new IllegalArgumentException(INSUFFICIENT_ATTEMPTS);
public static void setAttemptsNumber(int attemptsNumber) {
if (attemptsNumber < 1) throw new IllegalArgumentException(INSUFFICIENT_ATTEMPTS);
maxAttempts = attemptsNumber;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@
@RunWith(PowerMockRunner.class)
@PrepareForTest({KademliaNetwork.class, SMSManager.class, KademliaJoinableNetwork.class})
public class IdFinderHandlerTest {
//SEARCHER
private SMSPeer SEARCHER_PEER;
private KademliaId SEARCHER_ID;
private SMSKademliaNode SEARCHER;
//searcher
private SMSPeer searcherPeer;
private KademliaId searcherId;
private SMSKademliaNode searcher;
//NODE1
private SMSPeer VALID_PEER1;
private KademliaId VALID_NODE1_ID;
private SMSKademliaNode VALID_NODE1;
private SMSPeer validPeer1;
private KademliaId validNodeId1;
private SMSKademliaNode validNode1;
//NODE2
private SMSPeer VALID_PEER2;
private KademliaId VALID_NODE2_ID;
private SMSKademliaNode VALID_NODE2;
private SMSPeer validPeer2;
private KademliaId validNodeId2;
private SMSKademliaNode validNode2;

private KademliaJoinableNetwork networkMock;
private SMSManager smsManagerMock;
Expand All @@ -51,19 +51,19 @@ public class IdFinderHandlerTest {
@Before
public void setup() {
//Searcher
SEARCHER_PEER = new SMSPeer("+393423541601");
SEARCHER_ID = new KademliaId(SEARCHER_PEER);
SEARCHER = new SMSKademliaNode(SEARCHER_PEER);
searcherPeer = new SMSPeer("+393423541601");
searcherId = new KademliaId(searcherPeer);
searcher = new SMSKademliaNode(searcherPeer);
//Node1
VALID_PEER1 = new SMSPeer("+393335552121");
VALID_NODE1_ID = new KademliaId(VALID_PEER1);
VALID_NODE1 = new SMSKademliaNode(VALID_PEER1);
validPeer1 = new SMSPeer("+393335552121");
validNodeId1 = new KademliaId(validPeer1);
validNode1 = new SMSKademliaNode(validPeer1);
//Node2
VALID_PEER2 = new SMSPeer("+393423541602");
VALID_NODE2_ID = new KademliaId(VALID_PEER2);
VALID_NODE2 = new SMSKademliaNode(VALID_PEER2);
validPeer2 = new SMSPeer("+393423541602");
validNodeId2 = new KademliaId(validPeer2);
validNode2 = new SMSKademliaNode(validPeer2);
//RoutingTable
routingTable = new SMSKademliaRoutingTable(SEARCHER, new DefaultConfiguration());
routingTable = new SMSKademliaRoutingTable(searcher, new DefaultConfiguration());

//MOCK
networkMock = mock(KademliaJoinableNetwork.class);
Expand All @@ -75,28 +75,25 @@ public void setup() {
PowerMockito.mockStatic(KademliaJoinableNetwork.class);
PowerMockito.when(KademliaJoinableNetwork.getInstance()).thenReturn(networkMock);

when(networkMock.getLocalNode()).thenReturn(SEARCHER);
when(networkMock.getLocalNode()).thenReturn(searcher);
when(networkMock.getLocalRoutingTable()).thenReturn(routingTable);

when(networkMock.isAlive(SEARCHER_PEER)).thenReturn(true);
when(networkMock.isAlive(VALID_PEER1)).thenReturn(true);
when(networkMock.isAlive(VALID_PEER2)).thenReturn(false);

when(networkMock.isAlive(searcherPeer)).thenReturn(true);
when(networkMock.isAlive(validPeer1)).thenReturn(true);
when(networkMock.isAlive(validPeer2)).thenReturn(false);

when(networkMock.getRequestsHandler()).thenReturn(requestsHandler);

//when(networkMock.isNodeInNetwork(VALID_NODE2)).thenReturn(true);

}

@Test(expected = NullPointerException.class)
public void nullId_throws() {
IdFinderHandler.searchId(null, SEARCHER_PEER);
IdFinderHandler.searchId(null, searcherPeer);
}

@Test(expected = NullPointerException.class)
public void nullSearcher_throws() {
IdFinderHandler.searchId(VALID_NODE1_ID, null);
IdFinderHandler.searchId(validNodeId1, null);
}

/**
Expand All @@ -106,17 +103,17 @@ public void nullSearcher_throws() {
*/
@Test
public void nullRequest_throws() {
IdFinderHandler.searchId(VALID_NODE1_ID, SEARCHER_PEER);
IdFinderHandler.searchId(validNodeId1, searcherPeer);
}

/**
* In case the searched Id corresponds to my Id, there is no research made by SMS messages
*/
@Test
public void findMyId() {
IdFinderHandler.searchId(SEARCHER_ID, SEARCHER_PEER);
String expectedTextMessage = RequestTypes.Ping.ordinal() + " " + SEARCHER_ID + " / / /";
SMSMessage expectedMessage = new SMSMessage(SEARCHER_PEER, expectedTextMessage);
IdFinderHandler.searchId(searcherId, searcherPeer);
String expectedTextMessage = RequestTypes.Ping.ordinal() + " " + searcherId + " / / /";
SMSMessage expectedMessage = new SMSMessage(searcherPeer, expectedTextMessage);
verify(smsManagerMock, times(0)).sendMessage(expectedMessage);
}

Expand All @@ -127,8 +124,8 @@ public void findMyId() {
*/
@Test
public void findNotMyId_inserted() {
routingTable.insert(VALID_NODE1);
IdFinderHandler.searchId(VALID_NODE1_ID, SEARCHER_PEER);
routingTable.insert(validNode1);
IdFinderHandler.searchId(validNodeId1, searcherPeer);
verify(smsManagerMock, times(1)).sendMessage(any(SMSMessage.class));
}

Expand All @@ -139,8 +136,8 @@ public void findNotMyId_inserted() {
*/
@Test
public void findNotMyId_inserted_DEAD() {
routingTable.insert(VALID_NODE2);
IdFinderHandler.searchId(VALID_NODE2_ID, SEARCHER_PEER);
routingTable.insert(validNode2);
IdFinderHandler.searchId(validNodeId2, searcherPeer);
verify(smsManagerMock, times(1)).sendMessage(any(SMSMessage.class));
}

Expand All @@ -150,16 +147,16 @@ public void findNotMyId_inserted_DEAD() {
*/
@Test
public void findNotMyId_notInserted() {
IdFinderHandler.searchId(VALID_NODE1_ID, SEARCHER_PEER);
IdFinderHandler.searchId(validNodeId1, searcherPeer);
verify(smsManagerMock, times(0)).sendMessage(any(SMSMessage.class));
}


@Test
public void findIdInTable_notSearchedByMe() {
IdFinderHandler.searchId(SEARCHER_ID, VALID_PEER1);
String expectedTextMessage = RequestTypes.FindIdSearchResult.ordinal() + " " + SEARCHER + " / / /";
SMSMessage expectedMessage = new SMSMessage(VALID_PEER1, expectedTextMessage);
IdFinderHandler.searchId(searcherId, validPeer1);
String expectedTextMessage = RequestTypes.FindIdSearchResult.ordinal() + " " + searcher + " / / /";
SMSMessage expectedMessage = new SMSMessage(validPeer1, expectedTextMessage);
verify(smsManagerMock, times(1)).sendMessage(expectedMessage);
}

Expand All @@ -168,20 +165,20 @@ public void findIdInTable_notSearchedByMe() {
*/
@Test
public void findIdInTable_notSearchedByMe_DEAD() {
IdFinderHandler.searchId(SEARCHER_ID, VALID_PEER2);
String expectedTextMessage = RequestTypes.FindIdSearchResult.ordinal() + " " + SEARCHER + " / / /";
SMSMessage expectedMessage = new SMSMessage(VALID_PEER2, expectedTextMessage);
IdFinderHandler.searchId(searcherId, validPeer2);
String expectedTextMessage = RequestTypes.FindIdSearchResult.ordinal() + " " + searcher + " / / /";
SMSMessage expectedMessage = new SMSMessage(validPeer2, expectedTextMessage);
verify(smsManagerMock, times(1)).sendMessage(expectedMessage);
}

@Test
public void forwardRequest() {
routingTable.insert(VALID_NODE1);
IdFinderHandler.searchId(VALID_NODE1_ID, SEARCHER_PEER);
routingTable.insert(validNode1);
IdFinderHandler.searchId(validNodeId1, searcherPeer);
String expectedTextMessage =
RequestTypes.FindId.ordinal() + " " +
VALID_NODE1_ID + " " + SEARCHER_PEER + " / /";
SMSMessage expectedMessage = new SMSMessage(VALID_PEER1, expectedTextMessage);
validNodeId1 + " " + searcherPeer + " / /";
SMSMessage expectedMessage = new SMSMessage(validPeer1, expectedTextMessage);
verify(smsManagerMock, times(1)).sendMessage(expectedMessage);
}

Expand All @@ -196,9 +193,9 @@ public void forwardRequest() {
@Test
public void noLocalNode_localNodeSearched() {
List<SMSKademliaNode> nodes = routingTable.getAllNodes();
nodes.add(VALID_NODE1);
nodes.remove(SEARCHER);
IdFinderHandler.searchIdList(SEARCHER_ID, SEARCHER_PEER, nodes);
nodes.add(validNode1);
nodes.remove(searcher);
IdFinderHandler.searchIdList(searcherId, searcherPeer, nodes);
verify(smsManagerMock, times(1)).sendMessage(any(SMSMessage.class));
}

Expand All @@ -208,10 +205,10 @@ public void noLocalNode_localNodeSearched() {
@Test
public void noLocalNode_notLocalNodeSearched() {
List<SMSKademliaNode> nodes = routingTable.getAllNodes();
nodes.add(VALID_NODE1);
nodes.remove(SEARCHER);
IdFinderHandler.searchIdList(VALID_NODE1_ID, SEARCHER_PEER, nodes);
//A message is sent to the VALID_NODE1
nodes.add(validNode1);
nodes.remove(searcher);
IdFinderHandler.searchIdList(validNodeId1, searcherPeer, nodes);
//A message is sent to the validNode1
verify(smsManagerMock, times(1)).sendMessage(any(SMSMessage.class));
}
}

0 comments on commit c523e3b

Please sign in to comment.