ArpPacketHandler 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; } packetDispatcher.dispatchPacket(packetReceived.getPayload(), rawPacket.getIngress(), ethernetPacket.getSourceMac(), ethernetPacket.getDestinationMac()); } ArpPacketHandler(PacketDispatcher packetDispatcher); @Override void onArpPacketReceived(ArpPacketReceived packetReceived);  }
@Test public void onArpPacketReceivedTest_NoEthernetPacket() throws Exception { ArrayList<PacketChain> packetChainList = new ArrayList<PacketChain>(); packetChainList.add(new PacketChainBuilder().setPacket(new RawPacketBuilder().build()).build()); packetChainList.add(new PacketChainBuilder().setPacket(new ArpPacketBuilder().build()).build()); ArpPacketReceived arpReceived = new ArpPacketReceivedBuilder().setPacketChain(packetChainList).build(); arpPacketHandler.onArpPacketReceived(arpReceived); verify(packetDispatcher, times(0)).dispatchPacket(any(byte[].class), any(NodeConnectorRef.class), any(MacAddress.class), any(MacAddress.class)); }
@Test public void onArpPacketReceivedTest_NoArpPacket() throws Exception { ArrayList<PacketChain> packetChainList = new ArrayList<PacketChain>(); packetChainList.add(new PacketChainBuilder().setPacket(new RawPacketBuilder().build()).build()); packetChainList.add(new PacketChainBuilder().setPacket(new EthernetPacketBuilder().build()).build()); ArpPacketReceived arpReceived = new ArpPacketReceivedBuilder().setPacketChain(packetChainList).build(); arpPacketHandler.onArpPacketReceived(arpReceived); verify(packetDispatcher, times(0)).dispatchPacket(any(byte[].class), any(NodeConnectorRef.class), any(MacAddress.class), any(MacAddress.class)); }
@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().build()).build()); packetChainList.add(new PacketChainBuilder().setPacket(new ArpPacketBuilder().build()).build()); ArpPacketReceived arpReceived = new ArpPacketReceivedBuilder().setPacketChain(packetChainList).build(); arpPacketHandler.onArpPacketReceived(arpReceived); verify(packetDispatcher, times(1)).dispatchPacket(any(byte[].class), any(NodeConnectorRef.class), any(MacAddress.class), any(MacAddress.class)); }
@Test public void onArpPacketReceivedTest_NullInput() throws Exception { arpPacketHandler.onArpPacketReceived(null); verify(packetDispatcher, times(0)).dispatchPacket(any(byte[].class), any(NodeConnectorRef.class), any(MacAddress.class), any(MacAddress.class)); }
@Test public void onArpPacketReceivedTest_NullPacketChain() throws Exception { ArpPacketReceived arpReceived = new ArpPacketReceivedBuilder().build(); arpPacketHandler.onArpPacketReceived(arpReceived); verify(packetDispatcher, times(0)).dispatchPacket(any(byte[].class), any(NodeConnectorRef.class), any(MacAddress.class), any(MacAddress.class)); }
@Test public void onArpPacketReceivedTest_EmptyPacketChain() throws Exception { ArrayList<PacketChain> packetChainList = new ArrayList<PacketChain>(); ArpPacketReceived arpReceived = new ArpPacketReceivedBuilder().setPacketChain(packetChainList).build(); arpPacketHandler.onArpPacketReceived(arpReceived); verify(packetDispatcher, times(0)).dispatchPacket(any(byte[].class), any(NodeConnectorRef.class), any(MacAddress.class), any(MacAddress.class)); }
@Test public void onArpPacketReceivedTest_NoRawPacket() throws Exception { ArrayList<PacketChain> packetChainList = new ArrayList<PacketChain>(); packetChainList.add(new PacketChainBuilder().setPacket(new EthernetPacketBuilder().build()).build()); packetChainList.add(new PacketChainBuilder().setPacket(new ArpPacketBuilder().build()).build()); ArpPacketReceived arpReceived = new ArpPacketReceivedBuilder().setPacketChain(packetChainList).build(); arpPacketHandler.onArpPacketReceived(arpReceived); verify(packetDispatcher, times(0)).dispatchPacket(any(byte[].class), any(NodeConnectorRef.class), any(MacAddress.class), any(MacAddress.class)); }