blob: 319ee6a05941cd15af35ded447079bd6e48de1a4 [file] [log] [blame]
Andrew Browndb457052015-02-21 15:41:58 -08001/*
andrewsbrown4feb2da2015-03-03 16:05:29 -08002 * 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 Browndb457052015-02-21 15:41:58 -080013 */
14package com.intel.jndn.utils;
15
16import com.intel.jndn.mock.MockFace;
andrewsbrownb005ee62015-03-31 14:45:54 -070017import com.intel.jndn.utils.client.SegmentedFutureData;
Andrew Browndb457052015-02-21 15:41:58 -080018import java.io.IOException;
andrewsbrown69d53292015-03-17 19:37:34 +010019import java.util.List;
20import java.util.concurrent.ExecutionException;
21import java.util.concurrent.Future;
22import java.util.logging.Logger;
Andrew Browndb457052015-02-21 15:41:58 -080023import net.named_data.jndn.Data;
24import net.named_data.jndn.Interest;
25import net.named_data.jndn.Name;
26import net.named_data.jndn.Name.Component;
27import net.named_data.jndn.OnInterest;
28import net.named_data.jndn.transport.Transport;
29import net.named_data.jndn.util.Blob;
30import org.junit.Test;
31import static org.junit.Assert.*;
andrewsbrown629816c2015-04-07 09:04:21 -070032import org.junit.Before;
Andrew Browndb457052015-02-21 15:41:58 -080033
34/**
35 * Test SegmentedClient functionality.
36 *
37 * @author Andrew Brown <andrew.brown@intel.com>
38 */
39public class SegmentedClientTest {
40
andrewsbrown69d53292015-03-17 19:37:34 +010041 private static final Logger logger = Logger.getLogger(SimpleClient.class.getName());
andrewsbrown629816c2015-04-07 09:04:21 -070042 private MockFace face;
andrewsbrown2d7ee8d2015-04-15 12:40:58 -070043
andrewsbrown629816c2015-04-07 09:04:21 -070044 @Before
andrewsbrown2d7ee8d2015-04-15 12:40:58 -070045 public void beforeTest() {
andrewsbrown629816c2015-04-07 09:04:21 -070046 face = new MockFace();
47 }
andrewsbrown69d53292015-03-17 19:37:34 +010048
Andrew Browndb457052015-02-21 15:41:58 -080049 /**
50 * Test of getSync method, of class SegmentedClient.
andrewsbrown69d53292015-03-17 19:37:34 +010051 *
52 * @throws java.lang.Exception
Andrew Browndb457052015-02-21 15:41:58 -080053 */
54 @Test
55 public void testGetSync() throws Exception {
Andrew Browndb457052015-02-21 15:41:58 -080056 face.registerPrefix(new Name("/segmented/data"), new OnInterest() {
57 private int count = 0;
58 private int max = 9;
59
60 @Override
61 public void onInterest(Name prefix, Interest interest, Transport transport, long registeredPrefixId) {
62 Data data = new Data(interest.getName());
63 if (!SegmentedClient.hasSegment(data.getName())) {
64 data.getName().appendSegment(0);
65 }
66 data.getMetaInfo().setFinalBlockId(Component.fromNumberWithMarker(max, 0x00));
67 data.setContent(new Blob("."));
68 try {
69 transport.send(data.wireEncode().buf());
70 } catch (IOException e) {
71 fail(e.getMessage());
72 }
73 }
74 }, null);
75
76 Data data = SegmentedClient.getDefault().getSync(face, new Name("/segmented/data").appendSegment(0));
77 assertEquals(10, data.getContent().size());
78 }
andrewsbrown69d53292015-03-17 19:37:34 +010079
80 /**
81 * Test that a failed request fails with an exception.
82 *
83 * @throws java.lang.Exception
84 */
85 @Test(expected = ExecutionException.class)
86 public void testFailureToRetrieve() throws Exception {
andrewsbrown69d53292015-03-17 19:37:34 +010087 // 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"));
andrewsbrownb005ee62015-03-31 14:45:54 -070090
andrewsbrown69d53292015-03-17 19:37:34 +010091 // 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 }
andrewsbrownb005ee62015-03-31 14:45:54 -070098
99 /**
andrewsbrown4dddd472015-04-01 14:28:46 -0700100 * Test that a sync failed request fails with an exception.
101 */
102 @Test(expected = IOException.class)
103 public void testSyncFailureToRetrieve() throws IOException {
andrewsbrown629816c2015-04-07 09:04:21 -0700104 SegmentedClient.getDefault().getSync(face, new Name("/test/no-data"));
andrewsbrown4dddd472015-04-01 14:28:46 -0700105 }
106
107 /**
andrewsbrownb005ee62015-03-31 14:45:54 -0700108 * 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 {
andrewsbrown629816c2015-04-07 09:04:21 -0700116 Name name = new Name("/test/123").appendSegment(15);
117 Data data = buildSegmentedData(name);
andrewsbrownb005ee62015-03-31 14:45:54 -0700118 data.setContent(new Blob("...."));
119 face.addResponse(name, data);
120
121 SegmentedFutureData future = (SegmentedFutureData) SegmentedClient.getDefault().getAsync(face, name);
andrewsbrown629816c2015-04-07 09:04:21 -0700122 assertEquals(name.getPrefix(-1).toUri(), future.getName().toUri());
123 assertEquals(name.getPrefix(-1).toUri(), future.get().getName().toUri());
124 }
125
126 /**
127 * Verify that Data packets with no content do not cause errors; identifies
128 * bug.
129 *
130 * @throws Exception
131 */
132 @Test
133 public void testNoContent() throws Exception {
134 Name name = new Name("/test/no-content").appendSegment(0);
135 Data data = buildSegmentedData(name);
136 face.addResponse(name, data);
137
138 Future<Data> result = SegmentedClient.getDefault().getAsync(face, name);
139 assertEquals("/test/no-content", result.get().getName().toUri());
140 assertEquals("", result.get().getContent().toString());
141 }
142
143 /**
andrewsbrown2d7ee8d2015-04-15 12:40:58 -0700144 * Verify that segmented content is the correct length when retrieved by the
145 * client.
146 *
147 * @throws Exception
148 */
149 @Test
150 public void testContentLength() throws Exception {
151 Data data1 = new Data(new Name("/test/content-length").appendSegment(0));
152 data1.setContent(new Blob("0123456789"));
153 data1.getMetaInfo().setFinalBlockId(Name.Component.fromNumberWithMarker(1, 0x00));
154 face.addResponse(data1.getName(), data1);
155
156 Data data2 = new Data(new Name("/test/content-length").appendSegment(1));
157 data2.setContent(new Blob("0123456789"));
158 data1.getMetaInfo().setFinalBlockId(Name.Component.fromNumberWithMarker(1, 0x00));
159 face.addResponse(data2.getName(), data2);
160
161 Future<Data> result = SegmentedClient.getDefault().getAsync(face, new Name("/test/content-length").appendSegment(0));
162 assertEquals(20, result.get().getContent().size());
163 }
164
165 /**
andrewsbrown629816c2015-04-07 09:04:21 -0700166 * If a Data packet does not have a FinalBlockId, the SegmentedClient should
167 * just return the packet.
168 *
169 * @throws Exception
170 */
171 @Test
172 public void testNoFinalBlockId() throws Exception {
173 Name name = new Name("/test/no-final-block-id");
174 Data data = new Data(name);
175 data.setContent(new Blob("1"));
176 face.addResponse(name, data);
177
178 Future<Data> result = SegmentedClient.getDefault().getAsync(face, name);
179 assertEquals("/test/no-final-block-id", result.get().getName().toUri());
180 assertEquals("1", result.get().getContent().toString());
181 }
182
183 /**
184 * Helper method, sets FinalBlockId from last Name component
185 *
186 * @param name
187 * @return
188 */
189 private Data buildSegmentedData(Name name) {
190 Data data = new Data(name);
191 data.getMetaInfo().setFinalBlockId(name.get(-1));
192 return data;
andrewsbrownb005ee62015-03-31 14:45:54 -0700193 }
andrewsbrown4dddd472015-04-01 14:28:46 -0700194
Andrew Browndb457052015-02-21 15:41:58 -0800195}