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.*; |
| 34 | |
| 35 | /** |
| 36 | * Test SegmentedClient functionality. |
| 37 | * |
| 38 | * @author Andrew Brown <andrew.brown@intel.com> |
| 39 | */ |
| 40 | public class SegmentedClientTest { |
| 41 | |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 42 | private static final Logger logger = Logger.getLogger(SimpleClient.class.getName()); |
| 43 | |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 44 | /** |
| 45 | * Test of getSync method, of class SegmentedClient. |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 46 | * |
| 47 | * @throws java.lang.Exception |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 48 | */ |
| 49 | @Test |
| 50 | public void testGetSync() throws Exception { |
| 51 | MockFace face = new MockFace(); |
| 52 | face.registerPrefix(new Name("/segmented/data"), new OnInterest() { |
| 53 | private int count = 0; |
| 54 | private int max = 9; |
| 55 | |
| 56 | @Override |
| 57 | public void onInterest(Name prefix, Interest interest, Transport transport, long registeredPrefixId) { |
| 58 | Data data = new Data(interest.getName()); |
| 59 | if (!SegmentedClient.hasSegment(data.getName())) { |
| 60 | data.getName().appendSegment(0); |
| 61 | } |
| 62 | data.getMetaInfo().setFinalBlockId(Component.fromNumberWithMarker(max, 0x00)); |
| 63 | data.setContent(new Blob(".")); |
| 64 | try { |
| 65 | transport.send(data.wireEncode().buf()); |
| 66 | } catch (IOException e) { |
| 67 | fail(e.getMessage()); |
| 68 | } |
| 69 | } |
| 70 | }, null); |
| 71 | |
| 72 | Data data = SegmentedClient.getDefault().getSync(face, new Name("/segmented/data").appendSegment(0)); |
| 73 | assertEquals(10, data.getContent().size()); |
| 74 | } |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * Test that a failed request fails with an exception. |
| 78 | * |
| 79 | * @throws java.lang.Exception |
| 80 | */ |
| 81 | @Test(expected = ExecutionException.class) |
| 82 | public void testFailureToRetrieve() throws Exception { |
| 83 | // setup face |
| 84 | MockTransport transport = new MockTransport(); |
| 85 | Face face = new Face(transport, null); |
| 86 | |
| 87 | // retrieve non-existent data, should timeout |
| 88 | logger.info("Client expressing interest asynchronously: /test/no-data"); |
| 89 | List<Future<Data>> futureSegments = SegmentedClient.getDefault().getAsyncList(face, new Name("/test/no-data")); |
andrewsbrown | b005ee6 | 2015-03-31 14:45:54 -0700 | [diff] [blame] | 90 | |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 91 | // the list of future packets should be initialized |
| 92 | assertEquals(1, futureSegments.size()); |
| 93 | assertTrue(futureSegments.get(0).isDone()); |
| 94 | |
| 95 | // should throw error |
| 96 | futureSegments.get(0).get(); |
| 97 | } |
andrewsbrown | b005ee6 | 2015-03-31 14:45:54 -0700 | [diff] [blame] | 98 | |
| 99 | /** |
andrewsbrown | 4dddd47 | 2015-04-01 14:28:46 -0700 | [diff] [blame^] | 100 | * Test that a sync failed request fails with an exception. |
| 101 | */ |
| 102 | @Test(expected = IOException.class) |
| 103 | public void testSyncFailureToRetrieve() throws IOException { |
| 104 | SegmentedClient.getDefault().getSync(new MockFace(), new Name("/test/no-data")); |
| 105 | } |
| 106 | |
| 107 | /** |
andrewsbrown | b005ee6 | 2015-03-31 14:45:54 -0700 | [diff] [blame] | 108 | * Ensure Name of the returned Data is the same as was requested; identifies |
| 109 | * bug where the last Name.Component was always cut off. |
| 110 | * |
| 111 | * @throws InterruptedException |
| 112 | * @throws ExecutionException |
| 113 | */ |
| 114 | @Test |
| 115 | public void testNameShorteningLogic() throws InterruptedException, ExecutionException { |
| 116 | MockFace face = new MockFace(); |
| 117 | Name name = new Name("/test/123"); |
| 118 | Data data = new Data(name); |
| 119 | data.setContent(new Blob("....")); |
| 120 | face.addResponse(name, data); |
| 121 | |
| 122 | SegmentedFutureData future = (SegmentedFutureData) SegmentedClient.getDefault().getAsync(face, name); |
| 123 | assertEquals(name.toUri(), future.getName().toUri()); |
| 124 | assertEquals(name.toUri(), future.get().getName().toUri()); |
| 125 | } |
andrewsbrown | 4dddd47 | 2015-04-01 14:28:46 -0700 | [diff] [blame^] | 126 | |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 127 | } |