TeruU | 3c049c4 | 2014-04-15 10:13:25 -0700 | [diff] [blame^] | 1 | package net.onrc.onos.core.datastore; |
| 2 | |
| 3 | import static org.junit.Assert.*; |
| 4 | |
| 5 | import java.net.InetAddress; |
| 6 | import java.net.UnknownHostException; |
| 7 | import java.util.HashMap; |
| 8 | import java.util.Map; |
| 9 | |
| 10 | import net.floodlightcontroller.util.MACAddress; |
| 11 | import net.onrc.onos.core.datastore.IKVTable.IKVEntry; |
| 12 | |
| 13 | import org.junit.After; |
| 14 | import org.junit.Before; |
| 15 | import org.junit.Test; |
| 16 | |
| 17 | public class KVArpCacheTest { |
| 18 | |
| 19 | KVArpCache arpCache = null; |
| 20 | InetAddress ip = null; |
| 21 | MACAddress mac = null; |
| 22 | |
| 23 | @Before |
| 24 | public void setUp() throws Exception { |
| 25 | arpCache = new KVArpCache(); |
| 26 | try { |
| 27 | mac = MACAddress.valueOf("00:01:02:03:04:05"); |
| 28 | ip = InetAddress.getLocalHost(); |
| 29 | } catch (UnknownHostException e) { |
| 30 | fail(); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | @After |
| 35 | public void tearDown() throws Exception { |
| 36 | arpCache.dropArpCache(); |
| 37 | arpCache = null; |
| 38 | } |
| 39 | |
| 40 | @Test |
| 41 | public void testKVArpCache() { |
| 42 | assertNotNull(arpCache); |
| 43 | } |
| 44 | |
| 45 | @Test |
| 46 | public void testCreate() { |
| 47 | byte[] byteMac = mac.toBytes(); |
| 48 | try { |
| 49 | long verison = arpCache.create(ip, byteMac); |
| 50 | assertNotEquals(arpCache.getVersionNonexistant(), verison); |
| 51 | } catch (ObjectExistsException e) { |
| 52 | fail(); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | @Test |
| 57 | public void testForceCreate() { |
| 58 | byte[] byteMac = mac.toBytes(); |
| 59 | long version = arpCache.forceCreate(ip, byteMac); |
| 60 | assertNotEquals(arpCache.getVersionNonexistant(), version); |
| 61 | } |
| 62 | |
| 63 | @Test |
| 64 | public void testRead() { |
| 65 | byte[] byteMac = mac.toBytes(); |
| 66 | try { |
| 67 | arpCache.create(ip, byteMac); |
| 68 | byte[] entry = arpCache.read(ip).getValue(); |
| 69 | assertEquals(MACAddress.valueOf(byteMac), MACAddress.valueOf(entry)); |
| 70 | } catch (ObjectDoesntExistException | ObjectExistsException e) { |
| 71 | fail(); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | @Test |
| 76 | public void testUpdateInetAddressByteArray() { |
| 77 | byte[] byteMac = mac.toBytes(); |
| 78 | byte[] byteMac2 = MACAddress.valueOf("00:01:02:03:04:06").toBytes(); |
| 79 | try { |
| 80 | arpCache.create(ip, byteMac); |
| 81 | arpCache.update(ip, byteMac2); |
| 82 | byte[] entry = arpCache.read(ip).getValue(); |
| 83 | assertEquals(MACAddress.valueOf(byteMac2), MACAddress.valueOf(entry)); |
| 84 | } catch (ObjectDoesntExistException | ObjectExistsException e) { |
| 85 | fail(); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | @Test |
| 90 | public void testForceDelete() { |
| 91 | byte[] byteMac = mac.toBytes(); |
| 92 | long ver = arpCache.forceCreate(ip, byteMac); |
| 93 | long deletedVer= arpCache.forceDelete(ip); |
| 94 | assertEquals(ver, deletedVer); |
| 95 | } |
| 96 | |
| 97 | @Test |
| 98 | public void testGetAllEntries() { |
| 99 | byte[] ipAddr = new byte[]{10, 0, 0, 1}; |
| 100 | InetAddress ip2 = null; |
| 101 | try { |
| 102 | ip2 = InetAddress.getByAddress(ipAddr); |
| 103 | } catch (UnknownHostException e) { |
| 104 | fail(); |
| 105 | } |
| 106 | |
| 107 | byte[] byteMac = mac.toBytes(); |
| 108 | byte[] byteMac2 = MACAddress.valueOf("00:01:02:03:04:06").toBytes(); |
| 109 | Map<InetAddress, byte[]> map = new HashMap<InetAddress, byte[]>(); |
| 110 | try { |
| 111 | arpCache.create(ip, byteMac); |
| 112 | map.put(ip, byteMac); |
| 113 | arpCache.create(ip2, byteMac2); |
| 114 | map.put(ip2, byteMac2); |
| 115 | for(IKVEntry entry : arpCache.getAllEntries()) { |
| 116 | try { |
| 117 | assertTrue(map.containsKey(InetAddress.getByAddress(entry.getKey()))); |
| 118 | MACAddress mac1 = MACAddress.valueOf(map.get(InetAddress.getByAddress(entry.getKey()))); |
| 119 | MACAddress mac2 = MACAddress.valueOf(entry.getValue()); |
| 120 | assertEquals(mac1, mac2); |
| 121 | } catch (UnknownHostException e) { |
| 122 | fail(); |
| 123 | } |
| 124 | } |
| 125 | } catch (ObjectExistsException e) { |
| 126 | fail(); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | } |