AddressObserverUsingArp implements ArpPacketListener { @Override public void onArpPacketReceived(ArpPacketReceived packetReceived) { if (packetReceived == null || packetReceived.getPacketChain() == null) { return; } RawPacket rawPacket = null; EthernetPacket ethernetPacket = null; ArpPacket arpPacket = null; for (PacketChain packetChain : packetReceived.getPacketChain()) { if (packetChain.getPacket() instanceof RawPacket) { rawPacket = (RawPacket) packetChain.getPacket(); } else if (packetChain.getPacket() instanceof EthernetPacket) { ethernetPacket = (EthernetPacket) packetChain.getPacket(); } else if (packetChain.getPacket() instanceof ArpPacket) { arpPacket = (ArpPacket) packetChain.getPacket(); } } if (rawPacket == null || ethernetPacket == null || arpPacket == null) { return; } addressObservationWriter.addAddress(ethernetPacket.getSourceMac(), new IpAddress(arpPacket.getSourceProtocolAddress().toCharArray()), rawPacket.getIngress()); } AddressObserverUsingArp(AddressObservationWriter addressObservationWriter); @Override void onArpPacketReceived(ArpPacketReceived packetReceived);  }
@Test public void onArpPacketReceivedTest() throws Exception { ArrayList<PacketChain> packetChainList = new ArrayList<PacketChain>(); packetChainList.add(new PacketChainBuilder().setPacket(new RawPacketBuilder().build()).build()); packetChainList.add(new PacketChainBuilder() .setPacket(new EthernetPacketBuilder().setSourceMac(new MacAddress("aa:bb:cc:dd:ee:ff")).build()) .build()); packetChainList.add(new PacketChainBuilder() .setPacket(new ArpPacketBuilder().setSourceProtocolAddress("1.2.3.4").build()).build()); ArpPacketReceived arpReceived = new ArpPacketReceivedBuilder().setPacketChain(packetChainList).build(); AddressObserverUsingArp addressOberserverArp = new AddressObserverUsingArp(addressObservationWriterMock); addressOberserverArp.onArpPacketReceived(arpReceived); verify(addressObservationWriterMock, times(1)).addAddress(any(MacAddress.class), any(IpAddress.class), any(NodeConnectorRef.class)); }
@Test public void onArpPacketReceivedNullInputTest1() throws Exception { ArpPacketReceived arpReceived = new ArpPacketReceivedBuilder().setPacketChain(null).build(); AddressObserverUsingArp addressOberserverArp = new AddressObserverUsingArp(addressObservationWriterMock); addressOberserverArp.onArpPacketReceived(arpReceived); verify(addressObservationWriterMock, times(0)).addAddress(any(MacAddress.class), any(IpAddress.class), any(NodeConnectorRef.class)); }
@Test public void onArpPacketReceivedNullInputTest2() throws Exception { ArrayList<PacketChain> packetChainList = new ArrayList<PacketChain>(); packetChainList.add(new PacketChainBuilder().setPacket(new RawPacketBuilder().build()).build()); packetChainList.add(new PacketChainBuilder() .setPacket(new EthernetPacketBuilder().setSourceMac(new MacAddress("aa:bb:cc:dd:ee:ff")).build()) .build()); ArpPacketReceived arpReceived = new ArpPacketReceivedBuilder().setPacketChain(packetChainList).build(); AddressObserverUsingArp addressOberserverArp = new AddressObserverUsingArp(addressObservationWriterMock); addressOberserverArp.onArpPacketReceived(arpReceived); verify(addressObservationWriterMock, times(0)).addAddress(any(MacAddress.class), any(IpAddress.class), any(NodeConnectorRef.class)); }