blob: 56231db5ee2eb1e8c646ea1da2ae13994e13e0cc [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.*;
34
35/**
36 * Test SegmentedClient functionality.
37 *
38 * @author Andrew Brown <andrew.brown@intel.com>
39 */
40public class SegmentedClientTest {
41
andrewsbrown69d53292015-03-17 19:37:34 +010042 private static final Logger logger = Logger.getLogger(SimpleClient.class.getName());
43
Andrew Browndb457052015-02-21 15:41:58 -080044 /**
45 * Test of getSync method, of class SegmentedClient.
andrewsbrown69d53292015-03-17 19:37:34 +010046 *
47 * @throws java.lang.Exception
Andrew Browndb457052015-02-21 15:41:58 -080048 */
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 }
andrewsbrown69d53292015-03-17 19:37:34 +010075
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"));
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 {
104 SegmentedClient.getDefault().getSync(new MockFace(), new Name("/test/no-data"));
105 }
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 {
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 }
andrewsbrown4dddd472015-04-01 14:28:46 -0700126
Andrew Browndb457052015-02-21 15:41:58 -0800127}