Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 1 | /* |
andrewsbrown | 4feb2da | 2015-03-03 16:05:29 -0800 | [diff] [blame] | 2 | * jndn-utils |
| 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 | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 13 | */ |
| 14 | package com.intel.jndn.utils; |
| 15 | |
| 16 | import com.intel.jndn.mock.MockFace; |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 17 | import com.intel.jndn.mock.MockTransport; |
andrewsbrown | b005ee6 | 2015-03-31 14:45:54 -0700 | [diff] [blame] | 18 | import com.intel.jndn.utils.client.SegmentedFutureData; |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 19 | import java.io.IOException; |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 20 | import java.util.List; |
| 21 | import java.util.concurrent.ExecutionException; |
| 22 | import java.util.concurrent.Future; |
| 23 | import java.util.logging.Logger; |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 24 | import net.named_data.jndn.Data; |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 25 | import net.named_data.jndn.Face; |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 26 | import net.named_data.jndn.Interest; |
| 27 | import net.named_data.jndn.Name; |
| 28 | import net.named_data.jndn.Name.Component; |
| 29 | import net.named_data.jndn.OnInterest; |
| 30 | import net.named_data.jndn.transport.Transport; |
| 31 | import net.named_data.jndn.util.Blob; |
| 32 | import org.junit.Test; |
| 33 | import static org.junit.Assert.*; |
andrewsbrown | 629816c | 2015-04-07 09:04:21 -0700 | [diff] [blame^] | 34 | import org.junit.Before; |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Test SegmentedClient functionality. |
| 38 | * |
| 39 | * @author Andrew Brown <andrew.brown@intel.com> |
| 40 | */ |
| 41 | public class SegmentedClientTest { |
| 42 | |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 43 | private static final Logger logger = Logger.getLogger(SimpleClient.class.getName()); |
andrewsbrown | 629816c | 2015-04-07 09:04:21 -0700 | [diff] [blame^] | 44 | private MockFace face; |
| 45 | |
| 46 | @Before |
| 47 | public void beforeTest(){ |
| 48 | face = new MockFace(); |
| 49 | } |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 50 | |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 51 | /** |
| 52 | * Test of getSync method, of class SegmentedClient. |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 53 | * |
| 54 | * @throws java.lang.Exception |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 55 | */ |
| 56 | @Test |
| 57 | public void testGetSync() throws Exception { |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 58 | face.registerPrefix(new Name("/segmented/data"), new OnInterest() { |
| 59 | private int count = 0; |
| 60 | private int max = 9; |
| 61 | |
| 62 | @Override |
| 63 | public void onInterest(Name prefix, Interest interest, Transport transport, long registeredPrefixId) { |
| 64 | Data data = new Data(interest.getName()); |
| 65 | if (!SegmentedClient.hasSegment(data.getName())) { |
| 66 | data.getName().appendSegment(0); |
| 67 | } |
| 68 | data.getMetaInfo().setFinalBlockId(Component.fromNumberWithMarker(max, 0x00)); |
| 69 | data.setContent(new Blob(".")); |
| 70 | try { |
| 71 | transport.send(data.wireEncode().buf()); |
| 72 | } catch (IOException e) { |
| 73 | fail(e.getMessage()); |
| 74 | } |
| 75 | } |
| 76 | }, null); |
| 77 | |
| 78 | Data data = SegmentedClient.getDefault().getSync(face, new Name("/segmented/data").appendSegment(0)); |
| 79 | assertEquals(10, data.getContent().size()); |
| 80 | } |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * Test that a failed request fails with an exception. |
| 84 | * |
| 85 | * @throws java.lang.Exception |
| 86 | */ |
| 87 | @Test(expected = ExecutionException.class) |
| 88 | public void testFailureToRetrieve() throws Exception { |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 89 | // retrieve non-existent data, should timeout |
| 90 | logger.info("Client expressing interest asynchronously: /test/no-data"); |
| 91 | List<Future<Data>> futureSegments = SegmentedClient.getDefault().getAsyncList(face, new Name("/test/no-data")); |
andrewsbrown | b005ee6 | 2015-03-31 14:45:54 -0700 | [diff] [blame] | 92 | |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 93 | // the list of future packets should be initialized |
| 94 | assertEquals(1, futureSegments.size()); |
| 95 | assertTrue(futureSegments.get(0).isDone()); |
| 96 | |
| 97 | // should throw error |
| 98 | futureSegments.get(0).get(); |
| 99 | } |
andrewsbrown | b005ee6 | 2015-03-31 14:45:54 -0700 | [diff] [blame] | 100 | |
| 101 | /** |
andrewsbrown | 4dddd47 | 2015-04-01 14:28:46 -0700 | [diff] [blame] | 102 | * Test that a sync failed request fails with an exception. |
| 103 | */ |
| 104 | @Test(expected = IOException.class) |
| 105 | public void testSyncFailureToRetrieve() throws IOException { |
andrewsbrown | 629816c | 2015-04-07 09:04:21 -0700 | [diff] [blame^] | 106 | SegmentedClient.getDefault().getSync(face, new Name("/test/no-data")); |
andrewsbrown | 4dddd47 | 2015-04-01 14:28:46 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /** |
andrewsbrown | b005ee6 | 2015-03-31 14:45:54 -0700 | [diff] [blame] | 110 | * Ensure Name of the returned Data is the same as was requested; identifies |
| 111 | * bug where the last Name.Component was always cut off. |
| 112 | * |
| 113 | * @throws InterruptedException |
| 114 | * @throws ExecutionException |
| 115 | */ |
| 116 | @Test |
| 117 | public void testNameShorteningLogic() throws InterruptedException, ExecutionException { |
andrewsbrown | 629816c | 2015-04-07 09:04:21 -0700 | [diff] [blame^] | 118 | Name name = new Name("/test/123").appendSegment(15); |
| 119 | Data data = buildSegmentedData(name); |
andrewsbrown | b005ee6 | 2015-03-31 14:45:54 -0700 | [diff] [blame] | 120 | data.setContent(new Blob("....")); |
| 121 | face.addResponse(name, data); |
| 122 | |
| 123 | SegmentedFutureData future = (SegmentedFutureData) SegmentedClient.getDefault().getAsync(face, name); |
andrewsbrown | 629816c | 2015-04-07 09:04:21 -0700 | [diff] [blame^] | 124 | assertEquals(name.getPrefix(-1).toUri(), future.getName().toUri()); |
| 125 | assertEquals(name.getPrefix(-1).toUri(), future.get().getName().toUri()); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Verify that Data packets with no content do not cause errors; identifies |
| 130 | * bug. |
| 131 | * |
| 132 | * @throws Exception |
| 133 | */ |
| 134 | @Test |
| 135 | public void testNoContent() throws Exception { |
| 136 | Name name = new Name("/test/no-content").appendSegment(0); |
| 137 | Data data = buildSegmentedData(name); |
| 138 | face.addResponse(name, data); |
| 139 | |
| 140 | Future<Data> result = SegmentedClient.getDefault().getAsync(face, name); |
| 141 | assertEquals("/test/no-content", result.get().getName().toUri()); |
| 142 | assertEquals("", result.get().getContent().toString()); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * If a Data packet does not have a FinalBlockId, the SegmentedClient should |
| 147 | * just return the packet. |
| 148 | * |
| 149 | * @throws Exception |
| 150 | */ |
| 151 | @Test |
| 152 | public void testNoFinalBlockId() throws Exception { |
| 153 | Name name = new Name("/test/no-final-block-id"); |
| 154 | Data data = new Data(name); |
| 155 | data.setContent(new Blob("1")); |
| 156 | face.addResponse(name, data); |
| 157 | |
| 158 | Future<Data> result = SegmentedClient.getDefault().getAsync(face, name); |
| 159 | assertEquals("/test/no-final-block-id", result.get().getName().toUri()); |
| 160 | assertEquals("1", result.get().getContent().toString()); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Helper method, sets FinalBlockId from last Name component |
| 165 | * |
| 166 | * @param name |
| 167 | * @return |
| 168 | */ |
| 169 | private Data buildSegmentedData(Name name) { |
| 170 | Data data = new Data(name); |
| 171 | data.getMetaInfo().setFinalBlockId(name.get(-1)); |
| 172 | return data; |
andrewsbrown | b005ee6 | 2015-03-31 14:45:54 -0700 | [diff] [blame] | 173 | } |
andrewsbrown | 4dddd47 | 2015-04-01 14:28:46 -0700 | [diff] [blame] | 174 | |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 175 | } |