blob: 11f17a9156ec9f313c67f7693d5472e145ab5584 [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;
andrewsbrown69d53292015-03-17 19:37:34 +010017import com.intel.jndn.mock.MockTransport;
andrewsbrownb005ee62015-03-31 14:45:54 -070018import com.intel.jndn.utils.client.SegmentedFutureData;
Andrew Browndb457052015-02-21 15:41:58 -080019import java.io.IOException;
andrewsbrown69d53292015-03-17 19:37:34 +010020import java.util.List;
21import java.util.concurrent.ExecutionException;
22import java.util.concurrent.Future;
23import java.util.logging.Logger;
Andrew Browndb457052015-02-21 15:41:58 -080024import net.named_data.jndn.Data;
andrewsbrown69d53292015-03-17 19:37:34 +010025import net.named_data.jndn.Face;
Andrew Browndb457052015-02-21 15:41:58 -080026import net.named_data.jndn.Interest;
27import net.named_data.jndn.Name;
28import net.named_data.jndn.Name.Component;
29import net.named_data.jndn.OnInterest;
30import net.named_data.jndn.transport.Transport;
31import net.named_data.jndn.util.Blob;
32import org.junit.Test;
33import static org.junit.Assert.*;
andrewsbrown629816c2015-04-07 09:04:21 -070034import org.junit.Before;
Andrew Browndb457052015-02-21 15:41:58 -080035
36/**
37 * Test SegmentedClient functionality.
38 *
39 * @author Andrew Brown <andrew.brown@intel.com>
40 */
41public class SegmentedClientTest {
42
andrewsbrown69d53292015-03-17 19:37:34 +010043 private static final Logger logger = Logger.getLogger(SimpleClient.class.getName());
andrewsbrown629816c2015-04-07 09:04:21 -070044 private MockFace face;
45
46 @Before
47 public void beforeTest(){
48 face = new MockFace();
49 }
andrewsbrown69d53292015-03-17 19:37:34 +010050
Andrew Browndb457052015-02-21 15:41:58 -080051 /**
52 * Test of getSync method, of class SegmentedClient.
andrewsbrown69d53292015-03-17 19:37:34 +010053 *
54 * @throws java.lang.Exception
Andrew Browndb457052015-02-21 15:41:58 -080055 */
56 @Test
57 public void testGetSync() throws Exception {
Andrew Browndb457052015-02-21 15:41:58 -080058 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 }
andrewsbrown69d53292015-03-17 19:37:34 +010081
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 {
andrewsbrown69d53292015-03-17 19:37:34 +010089 // 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"));
andrewsbrownb005ee62015-03-31 14:45:54 -070092
andrewsbrown69d53292015-03-17 19:37:34 +010093 // 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 }
andrewsbrownb005ee62015-03-31 14:45:54 -0700100
101 /**
andrewsbrown4dddd472015-04-01 14:28:46 -0700102 * Test that a sync failed request fails with an exception.
103 */
104 @Test(expected = IOException.class)
105 public void testSyncFailureToRetrieve() throws IOException {
andrewsbrown629816c2015-04-07 09:04:21 -0700106 SegmentedClient.getDefault().getSync(face, new Name("/test/no-data"));
andrewsbrown4dddd472015-04-01 14:28:46 -0700107 }
108
109 /**
andrewsbrownb005ee62015-03-31 14:45:54 -0700110 * 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 {
andrewsbrown629816c2015-04-07 09:04:21 -0700118 Name name = new Name("/test/123").appendSegment(15);
119 Data data = buildSegmentedData(name);
andrewsbrownb005ee62015-03-31 14:45:54 -0700120 data.setContent(new Blob("...."));
121 face.addResponse(name, data);
122
123 SegmentedFutureData future = (SegmentedFutureData) SegmentedClient.getDefault().getAsync(face, name);
andrewsbrown629816c2015-04-07 09:04:21 -0700124 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;
andrewsbrownb005ee62015-03-31 14:45:54 -0700173 }
andrewsbrown4dddd472015-04-01 14:28:46 -0700174
Andrew Browndb457052015-02-21 15:41:58 -0800175}