Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 1 | /* |
andrewsbrown | 7e6b9e8 | 2015-03-03 16:11:11 -0800 | [diff] [blame] | 2 | * jndn-management |
| 3 | * Copyright (c) 2015, Intel Corporation. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU Lesser General Public License, |
| 7 | * version 3, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope it will be useful, but WITHOUT ANY |
| 10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 11 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for |
| 12 | * more details. |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 13 | */ |
| 14 | package com.intel.jndn.management; |
| 15 | |
| 16 | import com.intel.jndn.management.types.StatusDataset; |
| 17 | import com.intel.jndn.management.types.RibEntry; |
| 18 | import com.intel.jndn.management.types.Route; |
Andrew Brown | baf21d5 | 2015-07-13 10:32:46 -0700 | [diff] [blame^] | 19 | import com.intel.jndn.utils.client.SimpleClient; |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 20 | import java.util.List; |
| 21 | import junit.framework.Assert; |
| 22 | import net.named_data.jndn.Data; |
| 23 | import net.named_data.jndn.Face; |
| 24 | import net.named_data.jndn.Interest; |
| 25 | import net.named_data.jndn.Name; |
| 26 | import net.named_data.jndn.encoding.EncodingException; |
| 27 | import net.named_data.jndn.util.Blob; |
| 28 | import static org.junit.Assert.assertEquals; |
| 29 | import static org.junit.Assert.assertTrue; |
| 30 | import org.junit.Test; |
| 31 | |
| 32 | /** |
| 33 | * Test encoding/decoding for RibEntry. |
| 34 | * |
| 35 | * @author Andrew Brown <andrew.brown@intel.com> |
| 36 | */ |
| 37 | public class RibEntryTest { |
| 38 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 39 | /** |
| 40 | * Test encoding/decoding |
| 41 | * |
| 42 | * @throws java.lang.Exception |
| 43 | */ |
| 44 | @Test |
| 45 | public void testEncodeDecode() throws Exception { |
| 46 | Route route = new Route(); |
| 47 | route.setFaceId(42); |
| 48 | route.setCost(100); |
| 49 | route.setOrigin(0); |
| 50 | RibEntry entry = new RibEntry(); |
| 51 | entry.setName(new Name("/rib/entry/test")); |
| 52 | entry.getRoutes().add(route); |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 53 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 54 | // encode |
| 55 | Blob encoded = entry.wireEncode(); |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 56 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 57 | // decode |
| 58 | RibEntry decoded = new RibEntry(); |
| 59 | decoded.wireDecode(encoded.buf()); |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 60 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 61 | // test |
| 62 | Assert.assertEquals(entry.getName().toUri(), decoded.getName().toUri()); |
| 63 | Assert.assertEquals(entry.getRoutes().get(0).getFaceId(), decoded.getRoutes().get(0).getFaceId()); |
| 64 | Assert.assertEquals(entry.getRoutes().get(0).getCost(), decoded.getRoutes().get(0).getCost()); |
| 65 | Assert.assertEquals(entry.getRoutes().get(0).getOrigin(), decoded.getRoutes().get(0).getOrigin()); |
| 66 | } |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 67 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 68 | /** |
| 69 | * Integration test to run on actual system |
| 70 | * |
| 71 | * @param args |
| 72 | * @throws EncodingException |
| 73 | */ |
| 74 | public static void main(String[] args) throws Exception { |
| 75 | Face forwarder = new Face("localhost"); |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 76 | |
andrewsbrown | 9a6d7ba | 2015-03-24 09:53:29 -0700 | [diff] [blame] | 77 | // build management Interest packet; see <a href="http://redmine.named-data.net/projects/nfd/wiki/StatusDataset">http://redmine.named-data.net/projects/nfd/wiki/StatusDataset</a> |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 78 | Interest interest = new Interest(new Name("/localhost/nfd/rib/list")); |
| 79 | interest.setMustBeFresh(true); |
| 80 | interest.setChildSelector(Interest.CHILD_SELECTOR_RIGHT); |
| 81 | interest.setInterestLifetimeMilliseconds(2000.0); |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 82 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 83 | // send packet |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 84 | Data data = SimpleClient.getDefault().getSync(forwarder, interest); |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 85 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 86 | // decode results |
| 87 | List<RibEntry> results = StatusDataset.wireDecode(data.getContent(), RibEntry.class); |
| 88 | assertTrue(results.size() > 0); |
andrewsbrown | 6251c37 | 2015-03-09 14:52:04 -0700 | [diff] [blame] | 89 | assertEquals("/localhost/nfd/rib", results.get(0).getName().toUri()); |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 90 | } |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 91 | } |