Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 1 | /* |
| 2 | * File name: FibEntryTest.java |
| 3 | * |
| 4 | * Purpose: Test encode/decode of FibEntry and NextHopRecord |
| 5 | * |
| 6 | * © Copyright Intel Corporation. All rights reserved. |
| 7 | * Intel Corporation, 2200 Mission College Boulevard, |
| 8 | * Santa Clara, CA 95052-8119, USA |
| 9 | */ |
| 10 | package com.intel.jndn.management; |
| 11 | |
| 12 | import com.intel.jndn.management.types.StatusDataset; |
| 13 | import com.intel.jndn.management.types.NextHopRecord; |
| 14 | import com.intel.jndn.management.types.FibEntry; |
| 15 | import com.intel.jndn.utils.Client; |
| 16 | import java.util.List; |
| 17 | import junit.framework.Assert; |
| 18 | import net.named_data.jndn.Data; |
| 19 | import net.named_data.jndn.Face; |
| 20 | import net.named_data.jndn.Interest; |
| 21 | import net.named_data.jndn.Name; |
| 22 | import net.named_data.jndn.encoding.EncodingException; |
| 23 | import net.named_data.jndn.util.Blob; |
| 24 | import static org.junit.Assert.assertEquals; |
| 25 | import static org.junit.Assert.assertTrue; |
| 26 | import org.junit.Test; |
| 27 | |
| 28 | /** |
| 29 | * Test encode/decode of FibEntry and NextHopRecord |
| 30 | * |
| 31 | * @author Andrew Brown <andrew.brown@intel.com> |
| 32 | */ |
| 33 | public class FibEntryTest { |
| 34 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame^] | 35 | /** |
| 36 | * Test encoding/decoding |
| 37 | * |
| 38 | * @throws java.lang.Exception |
| 39 | */ |
| 40 | @Test |
| 41 | public void testEncodeDecode() throws Exception { |
| 42 | NextHopRecord nextHopRecord = new NextHopRecord(); |
| 43 | nextHopRecord.setFaceId(42); |
| 44 | nextHopRecord.setCost(100); |
| 45 | FibEntry entry = new FibEntry(); |
| 46 | entry.setName(new Name("/fib/entry/test")); |
| 47 | entry.getRecords().add(nextHopRecord); |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 48 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame^] | 49 | // encode |
| 50 | Blob encoded = entry.wireEncode(); |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 51 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame^] | 52 | // decode |
| 53 | FibEntry decoded = new FibEntry(); |
| 54 | decoded.wireDecode(encoded.buf()); |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 55 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame^] | 56 | // test |
| 57 | Assert.assertEquals(entry.getName().toUri(), decoded.getName().toUri()); |
| 58 | Assert.assertEquals(entry.getRecords().get(0).getFaceId(), decoded.getRecords().get(0).getFaceId()); |
| 59 | Assert.assertEquals(entry.getRecords().get(0).getCost(), decoded.getRecords().get(0).getCost()); |
| 60 | } |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 61 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame^] | 62 | /** |
| 63 | * Integration test to run on actual system |
| 64 | * |
| 65 | * @param args |
| 66 | * @throws EncodingException |
| 67 | */ |
| 68 | public static void main(String[] args) throws Exception { |
| 69 | Face forwarder = new Face("localhost"); |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 70 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame^] | 71 | // build management Interest packet; see http://redmine.named-data.net/projects/nfd/wiki/StatusDataset |
| 72 | Interest interest = new Interest(new Name("/localhost/nfd/fib/list")); |
| 73 | interest.setMustBeFresh(true); |
| 74 | interest.setChildSelector(Interest.CHILD_SELECTOR_RIGHT); |
| 75 | interest.setInterestLifetimeMilliseconds(2000.0); |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 76 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame^] | 77 | // send packet |
| 78 | Data data = Client.getDefault().getSync(forwarder, interest); |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 79 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame^] | 80 | // decode results |
| 81 | List<FibEntry> results = StatusDataset.wireDecode(data.getContent(), FibEntry.class); |
| 82 | assertTrue(results.size() > 0); |
| 83 | assertEquals("/localhost/nfd", results.get(0).getName().toUri()); |
| 84 | } |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 85 | } |